x509 certificate verification in C

Posted by sid on Stack Overflow See other posts from Stack Overflow or by sid
Published on 2010-05-03T06:55:29Z Indexed on 2010/05/03 7:18 UTC
Read the original article Hit count: 289

Filed under:

Hi All, I do have certificates in DER and PEM format, My goal is to retrieve the fields of Issuer and Subject And verify the Certificate with the CA public key and simultaneously verify CA certificate with the Root public key. I am able to retrieve all the details of issuer and subject But unable to verify the certificate. Please help. The API's used,

x509 = d2i_X509_fp (fp, &x509); //READING DER Format
x509 = PEM_read_X509 (fp, &x509, NULL, NULL); //READING PEM Format
X509_NAME_oneline(X509_get_subject_name(x509), subject, sizeof (subject)); //to retrive the Subject
X509_NAME_oneline(X509_get_issuer_name(x509), issuer, sizeof (issuer)); //to retrive the Issuer

// to store the CA public key (in unsigned char *key)that will be used to verify the certificate (My case Always sha1WithRSAEncryption)

RSA *x = X509_get_pubkey(x509)->pkey.rsa;
 bn = x->n;
 //extracts the bytes from public key & convert into unsigned char buffer
 buf_len = (size_t) BN_num_bytes (bn);
 stored_CA_pubKey = (unsigned char *)malloc (buf_len);
 i_n = BN_bn2bin (bn, (unsigned char *)stored_CA_pubKey);
 if (i_n != buf_len)
  LOG(ERROR," : key error\n");
 if (key[0] & 0x80)
  LOG(DEBUG, "00\n");


 stored_CA_pubKeyLen = EVP_PKEY_size(X509_get_pubkey(x509));

For Verification I went through different approaches but unable to verify

 a) i_x509_verify = X509_verify(cert_x509, ca_pubkey);
 b) /* verify the signature */
  int iRet1, iRet2, iReason;
  iRet1 = EVP_VerifyInit(&md_ctx, EVP_sha1());
  iRet2 = EVP_VerifyUpdate(&md_ctx, cert_code, cert_code_len);
  rv = EVP_VerifyFinal(&md_ctx, (const unsigned char *)stored_CA_pubKey, stored_CA_pubKeyLen, cert_pubkey);

NOTE : cert_code & stored_CA_pubKey is unsigned char buffer.

Thanks in Advance

© Stack Overflow or respective owner

Related posts about openssl