Home > Back-end >  C the openssl library reference for signatures, and validation issues
C the openssl library reference for signatures, and validation issues

Time:05-24

Problem: when a digital signature, different input, part of the input validation will fail, some input validation is successful, (feeling is the length of the digital signature), could you tell me what the reason, how to solve it, using SHA1withRSA signature
The results in the screenshot

 

String COpenSslApi: : signedRSA (const string& SMsg, const string& SPirvateKeyPath, const E_ALGO & amp; EAlgo)
{
BIO * bufio=NULL;//key cache buff
RSA * RSA=NULL;//rsa structure variable
EVP_PKEY * evpKey=NULL;//EVP KEY structure variable
Const EVP_MD * e_algo=nullptr;//the algorithm support sha1 md5 etc, specific see enum
EVP_MD_CTX * MDCTX=NULL;//the context variable
Unsigned char * pSign=nullptr;//the encrypted content
Unsigned int iSignLen=0;//sign length
String sSignRet;//the return value

Try
{
//the judgment refs
If (sMsg. Empty () | | sPirvateKeyPath. Empty ())
{
Cout & lt; <"The empty MSG or keypath" & lt; Goto safe_exit;
}
//open the key file buff
Bufio=BIO_new (BIO_s_file ());
BIO_read_filename (bufio, sPirvateKeyPath c_str ());
If (bufio==NULL)
{
Cout & lt; <" BIO_read_filename error "& lt; Goto safe_exit;
}
//get the rsa
Rsa=PEM_read_bio_RSAPrivateKey (bufio, NULL, NULL, NULL);
If (rsa==NULL)
{
Cout & lt; <"PEM_read_bio_RSAPrivateKey error" & lt; Goto safe_exit;
}
//evp_key structure variable initialization
EvpKey=EVP_PKEY_new ();
If (evpKey==NULL)
{
Cout & lt; <"EVP_PKEY_new error" & lt; Goto safe_exit;
}
//save the RSA structure to EVP_PKEY structure
If (EVP_PKEY_set1_RSA (evpKey, rsa)!
=1){
Cout & lt; <"EVP_PKEY_set1_RSA error" & lt; Goto safe_exit;
}
//initialize the context
MDCTX=EVP_MD_CTX_new ();
If (MDCTX==NULL)
{
Cout & lt; <" EVP_MD_CTX_new error "& lt; Goto safe_exit;
}
EVP_MD_CTX_init (MDCTX);
The switch (eAlgo)
{
Case E_SHA1:
E_algo=EVP_sha1 ();
break;
Case E_MD5:
E_algo=EVP_md5 ();
break;
Default:
break;
}
//signature initialization, set the algorithm
if(! EVP_SignInit_ex (MDCTX e_algo, NULL))
{
Cout & lt; <" EVP_SignInit_ex error "& lt; Goto safe_exit;
}
Cout & lt; <"Input_msg=" & lt; Goto safe_exit;
}
//the application memory
ISignLen=EVP_PKEY_size (evpKey);
PSign=(unsigned char *) malloc (iSignLen + 1);
Memset (pSign, 0, iSignLen + 1);
If (pSign==nullptr | | iSignLen==0)
{
Cout & lt; <" EVP_SignFinal error "& lt; Goto safe_exit;
}
Cout & lt; <"EVP_PKEY. Length=" & lt; //signature output
if(! EVP_SignFinal (MDCTX pSign, & amp; ISignLen evpKey))
{
Cout & lt; <" EVP_SignFinal error "& lt; Goto safe_exit;
}
Cout & lt; <"[after signs] signature. Size=" & lt; PSign sSignRet=(char *);

Safe_exit:
If (MDCTX)
{
EVP_MD_CTX_reset (MDCTX);
EVP_MD_CTX_free (MDCTX);
MDCTX=NULL;
}
//EVP_MD_CTX_cleanup (MDCTX);
If (bufio)
{
BIO_free_all (bufio);
Bufio=NULL;
}
If (rsa)
{
RSA_free (rsa);
Rsa=NULL;
}
If (evpKey)
{
EVP_PKEY_free (evpKey);
EvpKey=NULL;
}
If (pSign)
{
Free (pSign);
PSign=NULL;
}
}
The catch (const STD: : exception& E)
{
STD: : cout & lt; }


Return STD: : move (sSignRet);
}

CodePudding user response:

https://www.kancloud.cn/kancloud/rsa_algorithm/48484
  • Related