2012-11-28 14 views
6

döndürmez. Ben doğrulayarak am sertifika kendinden imzalı ve dolmuş olduğundan, X509_verify_cert() hatası döndüren bekliyoruz (geri dönüş değeri 1'dir ve store_ctx-> hata yerine X509_V_OK ayarlanır). çıkışları 'Openssl my_pem_cert_file doğrulamak': Yanlış yapıyorumDoğrulama kendi imzasını/süresi dolmuş sertifika Ben openssl kütüphanesi kullanarak C sertifika doğrulama işlevini yazmaya çalışıyorum hata

error 18 at 0 depth lookup:self signed certificate 
error 10 at 0 depth lookup:certificate has expired 

?

static int cert_verify_callback(int ok, X509_STORE_CTX *ctx) 
{ 
    /* Tolerate self-signed certificate */ 
    if (X509_STORE_CTX_get_error(ctx) == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) { 
     return 1; 
    } 

    /* Otherwise don't override */ 
    return ok; 
} 

int cert_validate(const char* certFileName) 
{ 
    BIO *pBio = NULL; 
    X509 *pX509 = NULL; 
    X509 *CA = NULL; 
    X509_STORE *cert_store = NULL; 
    X509_STORE_CTX *store_ctx = NULL; 
    STACK_OF(X509) *stack_of_x509 = NULL; 
    time_t check_time; 
    int store_ctx_error; 
    int store_ctx_error_depth; 


    pBio = BIO_new(BIO_s_file_internal()); 
    if(pBio == NULL) 
     /* error handling */ 

    if(BIO_read_filename(pBio, certFileName) <= 0) 
     /* error handling */ 

    pX509 = PEM_read_bio_X509(pBio, NULL, NULL, NULL); 
    if (pX509 == NULL) 
     /* error handling */ 

    if((cert_store= X509_STORE_new()) == NULL) 
     /* error handling */ 

    if((store_ctx= X509_STORE_CTX_new()) == NULL) 
     /* error handling */ 

    /* edit1: this was wrong: don't add the certificate being verified to the trusted cert list */ 
    /* if(!X509_STORE_add_cert(cert_store, pX509)) */ 
     /* error handling */ 

    if(!X509_STORE_CTX_init(store_ctx, cert_store, CA, stack_of_x509)) 
     /* error handling */ 

    X509_STORE_CTX_set_cert(store_ctx, pX509); 

    /* edit1: this was missing: set the verify time in order to check the certificate for expiry */ 
    time(&check_time); 
    X509_STORE_CTX_set_time(store_ctx, 0, check_time); 
    X509_STORE_CTX_set_flags(store_ctx, X509_V_FLAG_USE_CHECK_TIME); 

    /* edit1: add callback function for ignoring self-signed error 
    * now, I'd like the validation to fail because of the expiry */ 
    X509_STORE_set_verify_cb_func(store_ctx, cert_verify_callback); 

    switch(X509_verify_cert(store_ctx)) { 
     /* the certificate is valid */ 
     case 1: 
      printf("The certificate is valid\n"); 

      break; 

     /* the certificate cannot be validated */ 
     case -1: 
     case 0: 
      printf("The certificate is not valid\n"); 

      store_ctx_error= X509_STORE_CTX_get_error(store_ctx); 
      store_ctx_error_depth= X509_STORE_CTX_get_error_depth(store_ctx); 
      printf("Error %d at %d depth: %s\n", store_ctx_error, store_ctx_error_depth, X509_verify_cert_error_string(store_ctx->error)); 

     default: 
      break; 
    } 

    /* free data ... */ 
} 

kendinden imzalı ve dolmuş sertifika doğrulayarak, benim fonksiyon baskılar: Hata 0 0 derinlikte: Tamam

cevap

5

X509_STORE_add_cert() için güvenilir bir sertifika olarak gelen sertifikayı ekler işlevi İşte benim kodudur doğrulama, bu nedenle bu satırı:

X509_STORE_add_cert(cert_store, pX509) 

sizin pX509 sertifika doğrulama için güvenilen söylüyor - ama bu yüzden kendinden imzalı cer yüzden o, test etmek istediğiniz sertifika var tificate doğrulama geçiyor.

Herhangi bir doğrulama süresini ayarlayarak değildir - süresi dolmuş bir sertifika algılanmadığından yüzden. Doğrulama süresini X509_STORE_CTX_set_time() ile ayarlayın.

+0

Çok teşekkür ederim! Bu hataları düzelten kodumu güncelledim. Ortaya çıkan bir başka sorun daha var: Kendinden imzalı bir hatayı tolere etmek istediğimden, süresi geçmiş sertifika hatasını nasıl algılayabilirim? X509_verify_cert() işlevini çağırdıktan sonra karşılaştığınız ilk hatayı algıladığınızı varsayalım. 'Openssl doğrulama' komutu gibi tüm sertifika hatalarını nasıl algılayabilirim? – claudiu

+0

@caf, bu soru ile bana yardım edin (benim cert_validate() sertifikanın süresi olduğu için 'sertifika geçerli değil' yazdırmak şimdi istiyorum): http://stackoverflow.com/questions/27599985/ openssl-doğrulamak-hatakodu-20-yapamaz-to-get-yerel-issuer sertifikası –

İlgili konular