mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-09 15:33:58 +08:00
update saf
This commit is contained in:
@@ -63,6 +63,7 @@ int SAF_GenEccKeyPair(
|
||||
unsigned int uiExportFlag)
|
||||
{
|
||||
int ret = -1;
|
||||
SAF_APP *app = (SAF_APP *)hAppHandle;
|
||||
|
||||
/* check arguments */
|
||||
if (!hAppHandle || !pucContainerName) {
|
||||
@@ -88,13 +89,37 @@ int SAF_GenEccKeyPair(
|
||||
return SAR_KeyUsageErr;
|
||||
}
|
||||
|
||||
/* set return value */
|
||||
ret = SAR_Ok;
|
||||
/* process */
|
||||
EVP_PKEY_CTX *pctx = NULL;
|
||||
EVP_PKEY *pkey = NULL;
|
||||
|
||||
if (!(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, app->engine))
|
||||
|| EVP_PKEY_keygen_init(pctx) <= 0
|
||||
|| EVP_PKEY_keygen(pctx, &pkey) <= 0) {
|
||||
SAFerr(SAF_F_SAF_GENECCKEYPAIR, ERR_R_EVP_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = SAR_Ok;
|
||||
end:
|
||||
EVP_PKEY_CTX_free(pctx);
|
||||
EVP_PKEY_free(pkey);
|
||||
return ret;
|
||||
}
|
||||
|
||||
const char *SGD_GetKeyUsageName(unsigned int uiKeyUsage)
|
||||
{
|
||||
switch (uiKeyUsage) {
|
||||
case SGD_PK_SIGN:
|
||||
return "sign";
|
||||
case SGD_PK_ENC:
|
||||
return "enc";
|
||||
case SGD_PK_DH:
|
||||
return "dh";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* 7.3.24 */
|
||||
int SAF_GetEccPublicKey(
|
||||
void *hAppHandle,
|
||||
@@ -106,7 +131,6 @@ int SAF_GetEccPublicKey(
|
||||
{
|
||||
int ret = SAR_UnknownErr;
|
||||
SAF_APP *app = (SAF_APP *)hAppHandle;
|
||||
int rv;
|
||||
|
||||
/* check arguments */
|
||||
if (!hAppHandle || !pucContainerName || !pucPublicKey ||
|
||||
@@ -134,13 +158,30 @@ int SAF_GetEccPublicKey(
|
||||
return SAR_IndataErr;
|
||||
}
|
||||
|
||||
/* load public key */
|
||||
/* process */
|
||||
EVP_PKEY *pkey = NULL;
|
||||
char key_id[1024];
|
||||
int len;
|
||||
|
||||
snprintf(key_id, sizeof(key_id), "%s.%s", (char *)pucContainerName,
|
||||
SGD_GetKeyUsageName(uiKeyUsage));
|
||||
|
||||
if (!(pkey = ENGINE_load_public_key(app->engine, key_id, NULL, NULL))) {
|
||||
SAFerr(SAF_F_SAF_GETECCPUBLICKEY, ERR_R_ENGINE_LIB);
|
||||
goto end;
|
||||
}
|
||||
if ((len = i2d_PUBKEY(pkey, &pucPublicKey)) <= 0) {
|
||||
SAFerr(SAF_F_SAF_GETECCPUBLICKEY, ERR_R_X509_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
*puiPublicKeyLen = (unsigned int)len;
|
||||
|
||||
/* set return value */
|
||||
ret = SAR_Ok;
|
||||
|
||||
end:
|
||||
EVP_PKEY_free(pkey);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -156,8 +197,7 @@ int SAF_EccSign(
|
||||
unsigned int *puiSignDataLen)
|
||||
{
|
||||
int ret = SAR_UnknownErr;
|
||||
void *hSessionHandle = NULL;
|
||||
unsigned int uiISKIndex;
|
||||
SAF_APP *app = (SAF_APP *)hAppHandle;
|
||||
|
||||
/* check arguments */
|
||||
if (!hAppHandle || !pucContainerName || !pucInData ||
|
||||
@@ -185,8 +225,28 @@ int SAF_EccSign(
|
||||
return SAR_IndataErr;
|
||||
}
|
||||
|
||||
/* process */
|
||||
char key_id[1024];
|
||||
EVP_PKEY *pkey = NULL;
|
||||
EVP_PKEY_CTX *pctx = NULL;
|
||||
size_t siglen;
|
||||
|
||||
snprintf(key_id, sizeof(key_id), "%s.sign", (char *)pucContainerName);
|
||||
|
||||
if (!(pkey = ENGINE_load_private_key(app->engine, key_id, NULL, NULL))
|
||||
|| !(pctx = EVP_PKEY_CTX_new(pkey, app->engine))
|
||||
|| EVP_PKEY_sign_init(pctx) <= 0
|
||||
|| EVP_PKEY_sign(pctx, pucSignData, &siglen, pucInData, (size_t)uiInDataLen) <= 0) {
|
||||
SAFerr(SAF_F_SAF_ECCSIGN, ERR_R_EVP_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
*puiSignDataLen = (unsigned int)siglen;
|
||||
|
||||
ret = SAR_Ok;
|
||||
end:
|
||||
EVP_PKEY_free(pkey);
|
||||
EVP_PKEY_CTX_free(pctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -224,9 +284,22 @@ int SAF_EccVerifySign(
|
||||
return SAR_IndataLenErr;
|
||||
}
|
||||
|
||||
ret = SAR_Ok;
|
||||
/* process */
|
||||
EVP_PKEY *pkey = NULL;
|
||||
EVP_PKEY_CTX *pctx = NULL;
|
||||
|
||||
if (!(pkey = d2i_PUBKEY(NULL, (const unsigned char **)&pucPublicKey, (long)uiPublicKeyLen))
|
||||
|| !(pctx = EVP_PKEY_CTX_new(pkey, NULL))
|
||||
|| EVP_PKEY_verify_init(pctx) <= 0
|
||||
|| EVP_PKEY_verify(pctx, pucSignData, uiSignDataLen, pucInData, uiInDataLen) <= 0) {
|
||||
SAFerr(SAF_F_SAF_ECCVERIFYSIGN, ERR_R_EVP_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = SAR_Ok;
|
||||
end:
|
||||
EVP_PKEY_free(pkey);
|
||||
EVP_PKEY_CTX_free(pctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -269,8 +342,25 @@ int SAF_EccPublicKeyEnc(
|
||||
return SAR_IndataLenErr;
|
||||
}
|
||||
|
||||
/* precess */
|
||||
EVP_PKEY *pkey = NULL;
|
||||
EVP_PKEY_CTX *pctx = NULL;
|
||||
size_t outlen = *puiOutDataLen;
|
||||
|
||||
if (!(pkey = d2i_PUBKEY(NULL, (const unsigned char **)&pucPublicKey, (long)uiPublicKeyLen))
|
||||
|| !(pctx = EVP_PKEY_CTX_new(pkey, NULL))
|
||||
|| EVP_PKEY_decrypt_init(pctx) <= 0
|
||||
|| EVP_PKEY_decrypt(pctx, pucOutData, &outlen, pucInData, uiInDataLen) <= 0) {
|
||||
SAFerr(SAF_F_SAF_ECCPUBLICKEYENC, ERR_R_EVP_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
*puiOutDataLen = (unsigned int)outlen;
|
||||
|
||||
ret = SAR_Ok;
|
||||
end:
|
||||
EVP_PKEY_free(pkey);
|
||||
EVP_PKEY_CTX_free(pctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -285,8 +375,6 @@ int SAF_EccPublicKeyEncByCert(
|
||||
unsigned int *puiOutDataLen)
|
||||
{
|
||||
int ret = SAR_UnknownErr;
|
||||
ECCrefPublicKey publicKey;
|
||||
int rv;
|
||||
|
||||
/* check arguments */
|
||||
if (!pucCertificate || !pucInData || !pucOutData || !puiOutDataLen) {
|
||||
@@ -315,9 +403,36 @@ int SAF_EccPublicKeyEncByCert(
|
||||
return SAR_IndataLenErr;
|
||||
}
|
||||
|
||||
/* process */
|
||||
X509 *x509 = NULL;
|
||||
unsigned char pubkey[1024];
|
||||
unsigned char *p = pubkey;
|
||||
int len;
|
||||
|
||||
if (!(x509 = d2i_X509(NULL, (const unsigned char **)&pucCertificate, (long)uiCertificateLen))) {
|
||||
SAFerr(SAF_F_SAF_ECCPUBLICKEYENCBYCERT, ERR_R_X509_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if ((len = i2d_PUBKEY(X509_get0_pubkey(x509), &p)) <= 0) {
|
||||
SAFerr(SAF_F_SAF_ECCPUBLICKEYENCBYCERT, ERR_R_X509_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = SAF_EccPublicKeyEnc(
|
||||
pubkey,
|
||||
(unsigned int)len,
|
||||
uiAlgorithmID,
|
||||
pucInData,
|
||||
uiInDataLen,
|
||||
pucOutData,
|
||||
puiOutDataLen);
|
||||
|
||||
|
||||
/* set return value */
|
||||
ret = SAR_Ok;
|
||||
end:
|
||||
X509_free(x509);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -332,8 +447,6 @@ int SAF_EccVerifySignByCert(
|
||||
unsigned int uiSignDataLen)
|
||||
{
|
||||
int ret = SAR_UnknownErr;
|
||||
ECCrefPublicKey publicKey;
|
||||
int rv;
|
||||
|
||||
/* check arguments */
|
||||
if (!pucCertificate || !pucInData || !pucSignData) {
|
||||
@@ -362,11 +475,39 @@ int SAF_EccVerifySignByCert(
|
||||
return SAR_IndataLenErr;
|
||||
}
|
||||
|
||||
/* load public key form cert */
|
||||
/* process */
|
||||
X509 *x509 = NULL;
|
||||
unsigned char pucPublicKey[1024];
|
||||
unsigned int uiPublicKeyLen;
|
||||
unsigned char *p = pucPublicKey;
|
||||
int len;
|
||||
|
||||
if (!(x509 = d2i_X509(NULL, (const unsigned char **)&pucCertificate, (long)uiCertificateLen))) {
|
||||
SAFerr(SAF_F_SAF_ECCVERIFYSIGNBYCERT, ERR_R_X509_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if ((len = i2d_PUBKEY(X509_get0_pubkey(x509), &p)) <= 0) {
|
||||
SAFerr(SAF_F_SAF_ECCVERIFYSIGNBYCERT, ERR_R_X509_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
uiPublicKeyLen = (unsigned int)len;
|
||||
|
||||
ret = SAF_EccVerifySign(
|
||||
pucPublicKey,
|
||||
uiPublicKeyLen,
|
||||
uiAlgorithmID,
|
||||
pucInData,
|
||||
uiInDataLen,
|
||||
pucSignData,
|
||||
uiSignDataLen);
|
||||
|
||||
|
||||
/* set return value */
|
||||
ret = SAR_Ok;
|
||||
end:
|
||||
X509_free(x509);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -385,10 +526,9 @@ int SAF_GenerateAgreementDataWithECC(
|
||||
void **phAgreementHandle)
|
||||
{
|
||||
int ret = -1;
|
||||
unsigned int uiISKIndex;
|
||||
|
||||
ret = SAR_Ok;
|
||||
end:
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -405,7 +545,7 @@ int SAF_GenerateKeyWithECC(
|
||||
{
|
||||
int ret = -1;
|
||||
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* 7.3.35 */
|
||||
|
||||
Reference in New Issue
Block a user