12

opensslコマンドラインを使用すると、人間が読める形式で、.pem証明書に含まれるすべての情報を抽出できます。あれは:

openssl x509 -noout -in <MyCertificate>.pem  -text

openssl APIを使用してこの情報を抽出するための適切な手順は何ですか?

よろしく、

4

2 に答える 2

12

関数のX509_print_exファミリーがあなたの答えです。

#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/bio.h>

int main(int argc, char **argv)
{
    X509 *x509;
    BIO *i = BIO_new(BIO_s_file());
    BIO *o = BIO_new_fp(stdout,BIO_NOCLOSE);

    if((argc < 2) ||
       (BIO_read_filename(i, argv[1]) <= 0) ||
       ((x509 = PEM_read_bio_X509_AUX(i, NULL, NULL, NULL)) == NULL)) {
        return -1;
    }

    X509_print_ex(o, x509, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
}
于 2011-06-28T21:42:29.903 に答える
5

この質問に関連する追加情報として、PEMの代わりにDER形式の証明書を持っている場合。次のコードを使用して、人間が読めるモードで情報を抽出することもできます。

//Assuming that the DER certificate binary information is stored in
//a byte array (unsigned char) called "pData" whose size is "lenData"
X509* x509;
BIO* input = BIO_new_mem_buf((void*)pData, lenData);
//d2i_X509_bio: Decodes the binary DER certificate
//and parses it to a X509 structure
x509 = d2i_X509_bio(input, NULL);
if (x509 == NULL)
{
   //Error in d2i_X509_bio
}
else
{
    //"certificateFile" is the full path file
    //where to store the certificate information
    //in a human readable mode (instead of stdout)
    FILE* fd = fopen(certificateFile, "w+");
    BIO* output = BIO_new_fp(fd, BIO_NOCLOSE);
    X509_print_ex(output, x509, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
    fclose(fd);
    BIO_free_all(output);
}
BIO_free_all(input);
于 2011-06-30T08:14:53.913 に答える