EVP_PKEY from char buffer in x509 (PKCS7)

前端 未结 2 1478
忘掉有多难
忘掉有多难 2021-01-03 09:30

I have a DER certificate from which I am retrieving the Public key in unsigned char buffer as following, is it the right way of getting?

pStoredPublicKey =          


        
相关标签:
2条回答
  • 2021-01-03 09:36

    The following openssl API works for unsigned char buffer to EVP_PKEY,

    EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, unsigned char **pp, long length);
    int i2d_PublicKey(EVP_PKEY *a, unsigned char **pp);
    

    And, the following works for Convert EVP_PKEY to unsigned char buffer.

    int pkeyLen;
    unsigned char *ucBuf, *uctempBuf;
    pkeyLen = i2d_PublicKey(pkey, NULL);
    ucBuf = (unsigned char *)malloc(pkeyLen+1);
    uctempBuf = ucBuf;
    i2d_PublicKey(pkey, &uctempBuf);
    int ii;
    for (ii = 0; ii < pkeyLen; ii++)
    {
            printf("%02x\n", (unsigned char) ucBuf[ii]);
    }
    

    Thanks-opensid

    0 讨论(0)
  • 2021-01-03 09:58

    Convert EVP_PKEY to character buffer.

    char *EVP_PKEY_to_PEM (EVP_PKEY *pkey)
    {
        BIO *bio = NULL;
        char *pem = NULL;
    
        if (NULL == pkey)
          return NULL;
    
        if ((bio = BIO_new(BIO_s_mem())) == NULL)
          return NULL;
    
        if (0 == PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL)){
          BIO_free(bio);
          return NULL;
        }
    
        pem = (char *) calloc(1, bio->num_write + 1);
        BIO_read(bio, pem, bio->num_write);
        BIO_free(bio);
    
        return pem;
    }
    
    0 讨论(0)
提交回复
热议问题