// base64编码
char * Base64Encode(const char * input, int length, bool with_new_line)
{
BIO * bmem = NULL;
BIO * b64 = NULL;
BUF_MEM * bptr = NULL;
b64 = BIO_new(BIO_f_base64());
if(!with_new_line) {
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
}
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, input, length);
BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
char * buff = (char *)malloc(bptr->length + 1);
memcpy(buff, bptr->data, bptr->length);
buff[bptr->length] = 0;
BIO_free_all(b64);
return buff;
}
// base64解码
char * Base64Decode(char * input, int length, bool with_new_line)
{
BIO * b64 = NULL;
BIO * bmem = NULL;
char * buffer = (char *)malloc(length);
memset(buffer, 0, length);
b64 = BIO_new(BIO_f_base64());
if(!with_new_line) {
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
}
bmem = BIO_new_mem_buf(input, length);
bmem = BIO_push(b64, bmem);
BIO_read(bmem, buffer, length);
BIO_free_all(bmem);
return buffer;
}
int my_load_cert(unsigned char *str, unsigned long *str_len,
const char *verify_cert, const unsigned int cert_len)
{
FILE *fp;
fp = fopen(verify_cert, "rb");
if ( NULL == fp)
{
fprintf(stderr, "fopen fail\n");
return -1;
}
*str_len = fread(str, 1, cert_len, fp);
fclose(fp);
return 0;
}
std::string get_X509_serialNumber()
{
unsigned char ca_der[MAX_LEGTH];
unsigned long ca_der_len;
my_load_cert(ca_der, &ca_der_len, "verify_sign.cer", MAX_LEGTH);
X509 *x = der_to_x509((const unsigned char *)ca_der, ca_der_len);
ASN1_INTEGER *bs = NULL;
char *res = NULL;
BIGNUM *bn = NULL;
bs = X509_get_serialNumber(x);
if (bs->length == 0) {
printf("X509_get_serialNumber() length=0 error!\n");
return 0;
}
bn = ASN1_INTEGER_to_BN(bs, NULL);
res = BN_bn2hex(bn);
printf("serial = %s\n", res);
unsigned long long num = hexToDec(res);
std::string str_num;
char cnum[50];
sprintf(cnum, "%llu", num); // 将整数转换成字符串
str_num=cnum; // 结果
OPENSSL_free(res);
res = NULL;
BN_free(bn);
bn = NULL;
return str_num;
}
std::string testReadRSAFromPEM(std::string& data)
{
std::wstring signature = stringToWstring(data);
char* test = UnicodeToUtf8(signature.c_str());
std::string sig_hex = mysha256(test);
if(test != NULL)
{
free(test);
test = NULL;
}
RSA *prikey = RSA_new();
BIO* priio = BIO_new_file("1_pri_pkcs8.key", "rb");
if (priio == NULL)
return "";
prikey = PEM_read_bio_RSAPrivateKey(priio, &prikey, NULL, NULL);
int nLen = RSA_size(prikey);
EVP_PKEY *evpKey = EVP_PKEY_new();//新建一个EVP_PKEY变量
if(evpKey == NULL)
{
printf("EVP_PKEY_new err\n");
RSA_free(prikey);
BIO_free(priio);
return "";
}
if(EVP_PKEY_set1_RSA(evpKey, prikey) != 1) //保存RSA结构体到EVP_PKEY结构体
{
printf("EVP_PKEY_set1_RSA err\n");
EVP_PKEY_free(evpKey);
RSA_free(prikey);
BIO_free(priio);
return "";
}
EVP_MD_CTX mdctx; //摘要算法上下文变量
//以下是计算签名代码
EVP_MD_CTX_init(&mdctx);//初始化摘要上下文
if(!EVP_SignInit_ex(&mdctx, EVP_sha256(), NULL))//签名初始化,设置摘要算法
{
printf("err\n");
EVP_PKEY_free(evpKey);
RSA_free(prikey);
BIO_free(priio);
return "";
}
if(!EVP_SignUpdate(&mdctx, sig_hex.c_str(), sig_hex.size()))//计算签名(摘要)Update
{
printf("err\n");
EVP_PKEY_free(evpKey);
RSA_free(prikey);
BIO_free(priio);
return "";
}
unsigned char sign_value[1024]; //保存签名值的数组
unsigned int sign_len; //签名值长度
if(!EVP_SignFinal(&mdctx,sign_value,&sign_len,evpKey)) //签名输出
{
printf("err\n");
EVP_PKEY_free(evpKey);
RSA_free(prikey);
BIO_free(priio);
return "";
}
printf("消息\"%s\"的签名值是: \n",sig_hex);
for(int i = 0; i < sign_len; i++)
{
if(i%16==0)
printf("\n%08xH: ",i);
printf("%02x ", sign_value[i]);
}
printf("\n");
EVP_MD_CTX_cleanup(&mdctx);
char* ret_base64 = Base64Encode((const char*)sign_value, sign_len, false);
std::string retu;
if (ret_base64 != NULL){
std::string ret(ret_base64);
free(ret_base64);
retu = ret;
}
RSA_free(prikey);
BIO_free(priio);
return retu;
}