我正在尝试从.p8格式的文件PKCS#8文件本地获取签名字符串。当我打开它时,可以在语句之间看到一个字符串
-----BEGIN PRIVATE KEY-----
// strings line 64 characters
// strings line 64 characters
// strings line 64 characters
// strings line 8 characters
-----END PRIVATE KEY-----
我要转换此文件并登录ecdsa以获取签名。
如何使用openssl以c语言实现这一目标
答案 0 :(得分:1)
使用PEM_read_PrivateKey()
(或PEM_read_bio_PrivateKey()
从PKCS#8文件中读取密钥数据。这会将密钥作为EVP_PKEY
对象。这些功能的文档在这里:< / p>
https://www.openssl.org/docs/man1.1.1/man3/PEM_read_PrivateKey.html
通常在签名时,您通常要先使用某些摘要功能(例如SHA256)来摘要要签名的数据,然后再执行签名操作(在这种情况下为ECDSA)。假设这就是您要执行的操作,则应使用EVP_DigestSign*
系列函数。这些功能的文档在这里:
https://www.openssl.org/docs/man1.1.1/man3/EVP_DigestSign.html
代码可能看起来像这样(未经测试):
EVP_PKEY *pkey = PEM_read_PrivateKey(myfile, NULL, NULL, NULL);
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
size_t siglen = 0;
unsigned char *sig;
if (mdctx == NULL || pkey == NULL)
goto err;
if (!EVP_DigestSignInit(mdctx, NULL, EVP_sha256(), NULL, pkey))
goto err;
if(!EVP_DigestSignUpdate(mdctx, tobesigned, tobesignedlen))
goto err;
/* Find out the signature length */
if(!EVP_DigestSignFinal(mdctx, NULL, &siglen))
goto err;
/* Allocate memory for the signature length */
sig = OPENSSL_malloc(siglen);
if (sig == NULL)
goto err;
/* Now get the signature */
if(!EVP_DigestSignFinal(mdctx, sig, &siglen))
goto err;
EVP_MD_CTX_free(mdctx);
EVP_PKEY_free(pkey);
/* Free "sig" when you've finished with it */