openssl pkcs12命令以编程方式Objective-C/C

openssl pkcs12命令以编程方式Objective-C/C,c,openssl,key,extract,private,C,Openssl,Key,Extract,Private,我试图从C语言中的p12文件中获取私钥。 在控制台中,我只需键入 openssl pkcs12-in some.p12-out kestore.pem-节点 在pem里我有私钥 我设法像这样加载p12文件 NSString *path = [[NSBundle bundleForClass:self.class] pathForResource:@"some" ofType:@"p12"]; const char *pathCString = [path cStringUsing

我试图从C语言中的p12文件中获取私钥。 在控制台中,我只需键入

openssl pkcs12-in some.p12-out kestore.pem-节点

在pem里我有私钥

我设法像这样加载p12文件

    NSString *path = [[NSBundle bundleForClass:self.class] pathForResource:@"some" ofType:@"p12"];
    const char *pathCString = [path cStringUsingEncoding:NSUTF8StringEncoding];
    FILE *p12File = fopen(pathCString, "rb");
    if (p12File == nil) {
        return nil;
    }
    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();

    PKCS12 *p12 = NULL;
    p12 = d2i_PKCS12_fp(p12File, NULL);

    if (p12 != NULL) {
        const char *pw = "supersecretpass";
        //int PKCS12_parse(PKCS12 * p12, const char *pass, EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) * *ca);
        EVP_PKEY *pkey;
        X509 *cert;
        STACK_OF(X509) * ca;
        int result = PKCS12_parse(p12, pw, &pkey, &cert, &ca);
        NSLog(@"result %d", result);
        PKCS12_free(p12);
    }

    fclose(p12File);
    return nil;

现在如何获取私钥?

您的密钥在
EVP\u PKEY*PKEY
对象中。此对象是一个抽象密钥(EVP=Envelope),但您可以从中提取真正的密钥,具体取决于您拥有的密钥类型:

 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey);
 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey);
 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey);
 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey);
以下是关于它的文档:

例如,如果您有一个RSA密钥,那么私钥/公钥对中的所有组件都会存储在
BIGNUM
对象中,这些组件都是非常大的数字:

 const BIGNUM *RSA_get0_n(const RSA *d);
 const BIGNUM *RSA_get0_e(const RSA *d);
 const BIGNUM *RSA_get0_d(const RSA *d);
 const BIGNUM *RSA_get0_p(const RSA *d);
 const BIGNUM *RSA_get0_q(const RSA *d);
 const BIGNUM *RSA_get0_dmp1(const RSA *r);
 const BIGNUM *RSA_get0_dmq1(const RSA *r);
 const BIGNUM *RSA_get0_iqmp(const RSA *r);
 void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d);
 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q);
以下是关于它的文档: