#include
#include "cryptlib.h"
#include
#include
#include
#include
#include "asn1_locl.h"
#ifndef OPENSSL_NO_CMS
# include
#endif
extern const EVP_PKEY_ASN1_METHOD dhx_asn1_meth;
/*编码,证书公私钥编码解码,dh参数编码解码,dh拷贝打印等操作,CMS对消息的加密解密*/
/*
* i2d/d2i like DH parameter functions which use the appropriate routine for
* PKCS#3 DH or X9.42 DH.
*/
//编码转换函数i2d和d2i,i2d将一个内部的结构(c语言结构体)转换成DER编码(描述证书)的对象,
//d2i系列函数将一个DER编码的对象转换为一个内部的结构(c语言结构体)
static DH *d2i_dhp(const EVP_PKEY *pkey, const unsigned char **pp,
long length) //pp是指向DER编码字符指针的指针,length是*pp数据长度
{
{
if (pkey->ameth == &dhx_asn1_meth)
return d2i_DHxparams(NULL, pp, length);
return d2i_DHparams(NULL, pp, length);
}
//a指向结构体,pp是指向DER编码字符指针的指针
static int i2d_dhp(const EVP_PKEY *pkey, const DH *a, unsigned char **pp)
{
if (pkey->ameth == &dhx_asn1_meth)
return i2d_DHxparams(a, pp);
return i2d_DHparams(a, pp);
}
static void int_dh_free(EVP_PKEY *pkey)
{
DH_free(pkey->pkey.dh);
}
//EVP_PKEY 系列函数定义了一个密钥管理的结构体
static int dh_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)//X509数字证书
{
const unsigned char *p, *pm;
int pklen, pmlen;
int ptype;
void *pval;
ASN1_STRING *pstr;
X509_ALGOR *palg;
ASN1_INTEGER *public_key = NULL; //ASN1_INTEGER 类型对象
DH *dh = NULL;
if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
return 0;
X509_ALGOR_get0(NULL, &ptype, &pval, palg); //获取算法识别数据
if (ptype != V_ASN1_SEQUENCE) { //数据结构编码是一个SEQUENCE
DHerr(DH_F_DH_PUB_DECODE, DH_R_PARAMETER_ENCODING_ERROR);
goto err;
}
pstr = pval;
pm = pstr->data; //ASN1_STRING类型数据
pmlen = pstr->length; //ASN1_STRING类型长度
//编码表示转为结构体表示,pm是编码,pmlen是长度
if (!(dh = d2i_dhp(pkey, &pm, pmlen))) {
DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR);
goto err;
}
if (!(public_key = d2i_ASN1_INTEGER(NULL, &p, pklen))) {
DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR);
goto err;
}
/* We have parameters now set public key */
//该函数将ASN1_INTEGER 类型的整型数据转换为BIGNUM形式的,返回新生成的BIGNUM数据的指针
if (!(dh->pub_key = ASN1_INTEGER_to_BN(public_key, NULL))) {
DHerr(DH_F_DH_PUB_DECODE, DH_R_BN_DECODE_ERROR);
goto err;
}
ASN1_INTEGER_free(public_key);
EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh); //将dh对象赋给EVP_PKEY结构
return 1;
err:
if (public_key)
ASN1_INTEGER_free(public_key); //错误,释放public_key
if (dh)
DH_free(dh);
return 0;
}
//公钥编码,pkey->pkey.dh->pub_key
static int dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
{
DH *dh;
int ptype;
unsigned char *penc = NULL;
int penclen;
ASN1_STRING *str;
ASN1_INTEGER *pub_key = NULL;
dh = pkey->pkey.dh;
str = ASN1_STRING_new(); //openssl 里一个很基本的ASN.1 对象结构
if(!str) {
DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
goto err;
}
str->length = i2d_dhp(pkey, dh, &str->data);
if (str->length <= 0) {
DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
goto err;
}
/*它由一个值为num 的ASN1_INTEGER类型和一个值为len字节data
数据的ASN1_OCTET_STRING 类型组成。
事实上??最后的结构是创建了一个新的ASN1_SEQUENCE 类型结构*/
ptype = V_ASN1_SEQUENCE;
//该函数将一个BIGNUM 类型的数据转换为ASN1_INTEGER 类型的数据
pub_key = BN_to_ASN1_INTEGER(dh->pub_key, NULL);
if (!pub_key)
goto err;
penclen = i2d_ASN1_INTEGER(pub_key, &penc);
ASN1_INTEGER_free(pub_key);
if (penclen <= 0) {
DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
goto err;
}
if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
ptype, str, penc, penclen))
return 1;
err:
if (penc)
OPENSSL_free(penc);
if (str)
ASN1_STRING_free(str);
return 0;
}
/*
* PKCS#8 DH is defined in PKCS#11 of all places. It is similar to DH in that
* the AlgorithmIdentifier contains the paramaters, the private key is
* explcitly included and the pubkey must be recalculated.
*/
static int dh_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
{
const unsigned char *p, *pm;
int pklen, pmlen;
int ptype;
void *pval;
ASN1_STRING *pstr;
X509_ALGOR *palg;
ASN1_INTEGER *privkey = NULL; //ASN1_INTEGER 类型对象
DH *dh = NULL;
if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
return 0;
X509_ALGOR_get0(NULL, &ptype, &pval, palg); //获取算法识别数据
if (ptype != V_ASN1_SEQUENCE)
goto decerr;
if (!(privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)))
goto decerr;
pstr = pval;
pm = pstr->data;
pmlen = pstr->length;
//编码表示转为结构体表示,pm是编码ÿ