How to programmatically extract information from certificate?

后端 未结 2 1346
时光说笑
时光说笑 2021-01-15 01:57

I have a generated a certificate, but I would like to be able to extract the information from the certificate, as for example the country, the validity, the public key and s

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-15 02:32

    See x509.h of OpenSSL (example here). You will find plenty of useful functions. Example:

    #define     X509_get_version(x) ASN1_INTEGER_get((x)->cert_info->version)
    /* #define  X509_get_serialNumber(x) ((x)->cert_info->serialNumber) */
    #define     X509_get_notBefore(x) ((x)->cert_info->validity->notBefore)
    #define     X509_get_notAfter(x) ((x)->cert_info->validity->notAfter)
    #define     X509_extract_key(x) X509_get_pubkey(x) /*****/
    #define     X509_REQ_get_version(x) ASN1_INTEGER_get((x)->req_info->version)
    #define     X509_REQ_get_subject_name(x) ((x)->req_info->subject)
    #define     X509_REQ_extract_key(a) X509_REQ_get_pubkey(a)
    #define     X509_name_cmp(a,b)  X509_NAME_cmp((a),(b))
    #define     X509_get_signature_type(x) EVP_PKEY_type(OBJ_obj2nid((x)->sig_alg->algorithm))
    
    #define     X509_CRL_get_version(x) ASN1_INTEGER_get((x)->crl->version)
    #define     X509_CRL_get_lastUpdate(x) ((x)->crl->lastUpdate)
    #define     X509_CRL_get_nextUpdate(x) ((x)->crl->nextUpdate)
    #define     X509_CRL_get_issuer(x) ((x)->crl->issuer)
    #define     X509_CRL_get_REVOKED(x) ((x)->crl->revoked)
    
    /* This one is only used so that a binary form can output, as in
     * i2d_X509_NAME(X509_get_X509_PUBKEY(x),&buf) */
    #define     X509_get_X509_PUBKEY(x) ((x)->cert_info->key)
    

提交回复
热议问题