iOS 使用Gmssl实现SM2证书读取公私钥
读取sm2证书,使用的openssl里标准接口去解析p12或pfx文件,大概的步骤是:
1.解析p12,提取里面的参数信息(包含公私钥,算法,位数等信息)
2.将第一步的参数信息写入一个txt文件
废话不多说,直接上代码:
# include "pkcs12.h"
EVP_PKEY * BaseLoadKey (BIO * bio, char *strPwd){EVP_PKEY *pkey = NULL;
PKCS12 *p12 = d2i_PKCS12_bio (bio, NULL);
PKCS12_parse (p12, strPwd, &pkey, NULL, NULL);
PKCS12_free (p12);
p12 = NULL;
return pkey;
}
int getprivateKeyTxt(const char * p12Path, const char * p12Password, const char *privateKeyTxtPath){
BIO *in = BIO_new_file(p12Path, "r");
EVP_PKEY * key = BaseLoadKey(in, (char *)p12Password);
BIO *out = BIO_new_file(privateKeyTxtPath, "w");
EVP_PKEY_print_private(out, key, 0, NULL);
if(!out){ return 0; }else{ BIO_printf(out, "");
BIO_free(out); return 1;
}
}