GB35114---SM3withSM2证书生成及读取(二)
接上篇
作为相机,我们需要读取设备端(自己)的私钥:
1、对random2+random1+sip服务器id做数字签名;
2、对cryptkey进行解密得到VKEK;
3、对METHOD+From+to+CallID+Date+VKEK+消息体进行散列运算。
以及服务端的公钥:
1、校验sign2签名结果;
现在我们用openssl库来处理。
来看看我这边的代码
int gb35114_module_init()
{
X509 *x = NULL;
EVP_PKEY *pkey = NULL;
BIO *priio = NULL;
EC_KEY *prikey = NULL;
do
{
auth = malloc(GB_AUTH_T_LEN);
if(NULL == auth)
{
return 0;
}
memset(auth,0,GB_AUTH_T_LEN);
x = load_cert(GB35114_SERCER_FILE, FORMAT_PEM, "Certificate");
if(NULL == x)
{
DBG_WARN("load cert ERR
");
break;
}
//read cer pubkey
pkey = X509_get_pubkey(x);
if(NULL == pkey)
{
DBG_WARN("get_pubkey ERR
");
break;
}
auth->key.ec_skey = (EC_KEY *)pkey->pkey.ec;
/* 证书验签暂时不做,未完成 bob 20191219
if(!gb35114_verf_sercers(x))
{
break;
}
*/
//read dev prikey
priio = BIO_new_file(GB35114_PRIV_KEY, "rb");
if( NULL == priio)
{
DBG_ERR("open private key file err
");
break;
}
prikey = PEM_read_bio_ECPrivateKey(priio, &prikey, NULL, NULL);
if( NULL == prikey)
{
DBG_ERR("read private key file err
");
break;
}
auth->key.ec_dkey = prikey;
auth->init_flag = 1;
gb35114_load_conf();
DBG_INFO("35114 module init
");
}while(0);
if(1 != auth->init_flag)
{
DBG_ERR("35114 module init fail
");
}
//增加eckey不释放数据 bob
//X509_free(x);
//BIO_free(priio);
//EC_KEY_free(prikey);
return 1;
}
openssl提供了
PEM_read_bio_ECPrivateKey函数来读取私钥文件;
通过
X509 *load_cert(const char *file, int format, const char *cert_descrip); EVP_PKEY *X509_get_pubkey(X509 *x);
函数来获取证书中的公钥。 另注:gb35114的证书的交换是通过与认证机构进行物理交换所完成的。跟ssl认证不一样。
有错误请留言,谢谢。
bob 2020-03-24
