X509 structure to human readable string

前端 未结 1 1640
情深已故
情深已故 2020-12-19 04:53

How would I go about converting a X509 certificate in C into human readable string. For example:

X509* cert;

into

-----BEGI         


        
相关标签:
1条回答
  • 2020-12-19 05:17

    Conversion of X509 structure to the string with PEM encoded certificate:

    char *X509_to_PEM(X509 *cert) {
    
        BIO *bio = NULL;
        char *pem = NULL;
    
        if (NULL == cert) {
            return NULL;
        }
    
        bio = BIO_new(BIO_s_mem());
        if (NULL == bio) {
            return NULL;
        }
    
        if (0 == PEM_write_bio_X509(bio, cert)) {
            BIO_free(bio);
            return NULL;
        }
    
        pem = (char *) malloc(bio->num_write + 1);
        if (NULL == pem) {
            BIO_free(bio);
            return NULL;    
        }
    
        memset(pem, 0, bio->num_write + 1);
        BIO_read(bio, pem, bio->num_write);
        BIO_free(bio);
        return pem;
    }
    

    Conversion of PEM encoded certificate to the X509 structure:

    X509 *PEM_to_X509(char *pem) {
    
        X509 *cert = NULL;
        BIO *bio = NULL;
    
        if (NULL == pem) {
            return NULL;
        }
    
        bio = BIO_new_mem_buf(pem, strlen(pem));
        if (NULL == bio) {
            return NULL;
        }
    
        cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
        BIO_free(bio);
        return cert;
    }
    
    0 讨论(0)
提交回复
热议问题