代码解析 static HcfResult GetSigAlgNameX509Openssl(HcfX509CertificateSpi *self, HcfBlob *outName) { if ((self == NULL) || (outName == NULL)) { LOGE("[GetSigAlgName openssl] The input data is null!"); return HCF_INVALID_PARAMS; } if (!IsClassMatch((HcfObjectBase *)self, GetX509CertClass())) { LOGE("[GetSigAlgName openssl] Input wrong class type!"); return HCF_INVALID_PARAMS; } HcfOpensslX509Cert *realCert = (HcfOpensslX509Cert *)self; X509 *x509 = realCert->x509; const X509_ALGOR *alg; X509_get0_signature(NULL, &alg, x509); const ASN1_OBJECT *oidObj; X509_ALGOR_get0(&oidObj, NULL, NULL, alg); char oidStr[OID_STR_MAX_LEN] = { 0 }; int32_t resLen = OBJ_obj2txt(oidStr, OID_STR_MAX_LEN, oidObj, 1); if ((resLen < 0) || (resLen >= OID_STR_MAX_LEN)) { LOGE("Failed to convert x509 object to text!"); CfPrintOpensslError(); return HCF_ERR_CRYPTO_OPERATION; } const char *algName = GetAlgorithmName(oidStr); if (algName == NULL) { return HCF_ERR_CRYPTO_OPERATION; } uint32_t len = strlen(algName) + 1; return DeepCopyDataToOut(algName, len, outName); }
时间: 2024-04-27 19:23:05 浏览: 140
数字证书X.509的OpenSSL代码实现Demo
这是一个静态函数,用于获取 X.509 证书的签名算法名称。该函数首先对传入的参数进行判空处理,如果输入数据为空,则返回 HCF_INVALID_PARAMS 错误码。接下来,该函数通过调用 IsClassMatch 函数判断传入的 self 对象是否是 HcfX509CertificateSpi 类型,如果不是则返回 HCF_INVALID_PARAMS 错误码。然后,该函数将 self 对象转换为 HcfOpensslX509Cert 类型,并从该对象中获取 X509 证书。接着,该函数通过调用 X509_get0_signature 函数获取证书的签名算法,再通过调用 OBJ_obj2txt 函数将签名算法转换为字符串形式。如果转换失败,则返回 HCF_ERR_CRYPTO_OPERATION 错误码。否则,该函数调用 GetAlgorithmName 函数获取签名算法名称,并通过调用 DeepCopyDataToOut 函数将签名算法名称拷贝到 outName 对象中。如果获取签名算法名称失败,则返回 HCF_ERR_CRYPTO_OPERATION 错误码。最后,该函数返回签名算法名称的长度。
阅读全文