我已經創建了一個DSAPI篩選器來使用客戶端證書對用戶進行身份驗證。用戶通過代理連接,代理將用戶的證書添加到請求標頭。PEM_read_bio_X509(有時)失敗(OpenSSL 1.0.1p)
#define HDR_SSL_CLIENT_CERT "SSL_CLIENT_CERT"
我使用Apache作爲HTTPS代理;客戶使用NGINX。我已經發現,NGINX增加了,而不是空間TABS,我也確信,該證書數據的格式是否正確,然後再交由我的代碼
#define BUFFER_SIZE 4096
char certData[BUFFER_SIZE+1] = {0,};
certData解析包含證書的Base64表示(TABS和空間被\ n替換)
-----BEGIN CERTIFICATE-----
MIIDOTCCAiGgAwIBAgIBAjANBgkqhkiG9w0BAQsFADBGMQswCQYDVQQGEwJERTEO
MAwGA1UECgwFRE9TWVMxETAPBgNVBAsMCFRSQVZFTEVSMRQwEgYDVQQDDAtUUkFW
RUxFUiBDQTAeFw0xNjA0MTgxMzA2MjdaFw0yNjA0MTgxMzA2MjdaMBUxEzARBgNV
BAMMCkdlb3JnIER1bWEwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCG
ctEsl++/4LgK8RJId2NUPJgjFKDl76jp38GNMtcCqt+ADUPvR+suoy/zeuRXs7hw
25YAx49U/FYFlu3Xlmb57ACyPtbhLPpV2Y8fJ0EXD2pY1G3oEWlKWWk6ErT2vg7V
ppOajckkx3EmkVrALhQgdOqQDHJ6Y2xQSgpKWGORmoEtYQepJ/LGWBfE4muZjUJk
euUf0fmHFehMw8X0ErPDFxDuAH+d7kjjUl+EqSQCqLqqrg50GMrM0vKIqyqqbUQF
wLQYyFllYkj0h1VQ+KhyxwVkq2snR+Z2EJe1A7xsUwY5D/9dVK5ih6xeIrpgvgCd
6Amx2KF9lh8yEZi1NMPPAgMBAAGjYzBhMB8GA1UdEQQYMBaBFEdlb3JnRHVtYUBz
dGFkdGRvLmRlMB0GA1UdDgQWBBRIX2fz2ahSFgOCf03W4pn9t/BomjAfBgNVHSME
GDAWgBR7RJ1HsYOVlc4TOAzeqIqETopeCTANBgkqhkiG9w0BAQsFAAOCAQEALWre
gJYsSD6i3e4MhJOhR0FFincqdnVEeEoVMr4GDSZRMUPSTjNMTdGLLMFHpU9p/cGZ
4b30k7dQWhIao7aLIgDOXaATr14fLXrZqRM/MXusd27nFKQRZf1ktrxr0vIZqnw4
SuniS3NP7SuVEbUeTWU8nVub17aUWX8T4C8yAHKmancSSgMXwFhXTNq0aIvwRzIv
TzyK0SDXSc68kQkf3evTRvKfvlmQGWXL6BukTGJS1870x3IrDK19Phi5PUYXQtZV
uwaRg1fRUyPno0GCIZiMxCY4rWy+AaM3CO7Ua5+KEiAdWKrBP6Jd24hZuH8ZhuZ/
9u5SSvUA1bGAT02eqQ==
-----END CERTIFICATE-----
然後我用下面的代碼從一個certData X509:
BIO * bio = BIO_new(BIO_s_mem());
X509 * clientCert = X509_new();
bio = BIO_new_mem_buf(certData, -1);
PEM_read_bio_X509(bio, &clientCert, 0, NULL);
if (clientCert == NULL) {
debugOut("PEM_read_bio_X509 failed...\n");
if(bio) {
BIO_free(bio);
}
return false;
}
我們還沒有看到當u任何問題用Apache唱DSAPI; NGINX也有效。但有時,PEM_read_bio_X509失敗,並且沒有創建clientCert。
我的代碼有什麼明顯的錯誤嗎?
PEM_read_bio_X509和NGINX是否存在已知問題?
我目前使用openSSL 1.0.1p。
更新:這裏是取代了製表和空格
char szHeaderAuthToken[MAX_BUF_LEN+1] = {0,};
包含數據提交代理
size_t last = certLen - lastblank;
while (szHeaderClientCert[j] != '\0') {
c = szHeaderClientCert[j];
// skip first and last 'space' char
if (j == 10 || j == last) {
c = ' ';
} else {
if (isspace(c) || ('\t' == c)) c = '\n';
}
certData[j] = c;
if (DEBUGOUT) {
putchar (c);
ofs << c;
}
j++;
}
certData[j+1] = '\0';
UPDATE2代碼:好的和壞的certData
20160512_145926 GOOD
-----BEGIN CERTIFICATE-----
MIIDOTCCAiGgAwIBAgIBAjANBgkqhkiG9w0BAQsFADBGMQswCQYDVQQGEwJERTEO
MAwGA1UECgwFR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aRg1fRUyPno0GCIZiMxCY4rWy+AaM3CO7Ua5+KEiAdWKrBP6Jd24hZuH8ZhuZ/
9u5SSvUA1bGAT02eqQ==
-----END CERTIFICATE-----
20160512_150227 PEM_read_bio_X509 failed...
只是爲了得到諒解的問題:代理實際上會驗證TLS連接中的客戶端證書與預期的CA之間的關係,然後將驗證的證書添加到HTTP請求標頭中以供進一步處理。您的(未示出)代碼從請求頭中提取此證書,然後將其提供給您的代碼。你是否證實提取過程正確?你能提供一個PEM_read_bio_X509失敗的情況下的certData的內容嗎? –
代理實際驗證與預期CA TLS連接內部的客戶端證書,然後添加驗證證書,以作進一步處理 HTTP請求頭這是正確的 是否已確認..? 是的,在失敗發生之前多次解析相同的證書而沒有任何問題。它與導致失敗的那個完全相同。 您能否提供certData的內容.. 解析沒有問題的certData和以錯誤結尾的數據沒有區別。這是讓我發瘋的原因。 –
實際上,您正在使用的PEM_read_bio_X509與示例中不同,請參閱https://www.openssl.org/docs/manmaster/crypto/pem.html。在示例中,clientCert參數應該爲NULL或用NULL初始化,即不是分配的X509_new對象。你應該檢查PEM_read_bio_X509的回報。 –