!44 增加napi接口X509Cert.getCertSerialNumber实现

Merge pull request !44 from zhao_zhen_zhou/master
This commit is contained in:
openharmony_ci 2023-07-27 13:39:29 +00:00 committed by Gitee
commit 26e8fdb314
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 52 additions and 1 deletions

View File

@ -21,6 +21,7 @@
#define LOG_PRINT_MAX_LEN 1024 // log max length limit
#define HCF_MAX_BUFFER_LEN 8192
#define SERIAL_NUMBER_HEDER_SIZE 2
#define MAX_SN_BYTE_CNT 20 // the maximum number of bytes of the serial number
#define INVALID_VERSION (-1)
#define INVALID_CONSTRAINTS_LEN (-1)

View File

@ -28,7 +28,7 @@ constexpr size_t ARGS_SIZE_FOUR = 4;
constexpr int32_t PARAM0 = 0;
constexpr int32_t PARAM1 = 1;
constexpr int32_t PARAM2 = 2;
constexpr uint32_t MAX_SN_BYTE_CNT = 100;
constexpr uint32_t BYTE_TO_BIT_CNT = 8;
constexpr uint32_t QUAD_WORD_ALIGN_UP = 3;
const std::string CERT_TAG_DATA = "data";

View File

@ -51,6 +51,7 @@ napi_value ConvertEncodingBlobToNapiValue(napi_env env, CfEncodingBlob *encoding
napi_value CertGenerateBusinessError(napi_env env, int32_t errCode, const char *errMsg);
napi_value ConvertBlobToNapiValue(napi_env env, const CfBlob *blob);
napi_value ConvertBlobToBigIntWords(napi_env env, const CfBlob &blob);
napi_value ConvertBlobToInt64(napi_env env, const CfBlob &blob);
} // namespace CertFramework
} // namespace OHOS
#endif

View File

@ -42,6 +42,7 @@ public:
napi_value CheckValidityWithDate(napi_env env, napi_callback_info info);
napi_value GetVersion(napi_env env, napi_callback_info info);
napi_value GetSerialNumber(napi_env env, napi_callback_info info);
napi_value GetCertSerialNumber(napi_env env, napi_callback_info info);
napi_value GetIssuerName(napi_env env, napi_callback_info info);
napi_value GetSubjectName(napi_env env, napi_callback_info info);
napi_value GetNotBeforeTime(napi_env env, napi_callback_info info);

View File

@ -17,6 +17,7 @@
#include "cf_log.h"
#include "cf_memory.h"
#include "config.h"
#include "securec.h"
#include "cipher.h"
#include "napi_cert_defines.h"
@ -588,5 +589,22 @@ napi_value ConvertBlobToBigIntWords(napi_env env, const CfBlob &blob)
CfFree(words);
return result;
}
napi_value ConvertBlobToInt64(napi_env env, const CfBlob &blob)
{
if (blob.data == nullptr || blob.size == 0 || blob.size > sizeof(int64_t)) {
LOGE("Invalid blob!");
return nullptr;
}
uint64_t serialNumber = 0;
for (uint32_t i = 0; i < blob.size; ++i) {
serialNumber = ((serialNumber << (BYTE_TO_BIT_CNT * i)) | static_cast<uint64_t>(blob.data[i]));
}
napi_value result = nullptr;
napi_create_int64(env, static_cast<long>(serialNumber), &result);
return result;
}
} // namespace CertFramework
} // namespace OHOS

View File

@ -350,6 +350,22 @@ napi_value NapiX509Certificate::GetSerialNumber(napi_env env, napi_callback_info
return nullptr;
}
napi_value result = ConvertBlobToInt64(env, blob);
CfBlobDataFree(&blob);
return result;
}
napi_value NapiX509Certificate::GetCertSerialNumber(napi_env env, napi_callback_info info)
{
HcfX509Certificate *cert = GetX509Cert();
CfBlob blob = { 0, nullptr };
CfResult ret = cert->getSerialNumber(cert, &blob);
if (ret != CF_SUCCESS) {
napi_throw(env, CertGenerateBusinessError(env, ret, "cert get serial num failed"));
LOGE("cert get serial num failed!");
return nullptr;
}
napi_value result = ConvertBlobToBigIntWords(env, blob);
CfBlobDataFree(&blob);
return result;
@ -723,6 +739,19 @@ static napi_value NapiGetSerialNumber(napi_env env, napi_callback_info info)
return x509Cert->GetSerialNumber(env, info);
}
static napi_value NapiGetCertSerialNumber(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
NapiX509Certificate *x509Cert = nullptr;
napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Cert));
if (x509Cert == nullptr) {
LOGE("x509Cert is nullptr!");
return nullptr;
}
return x509Cert->GetCertSerialNumber(env, info);
}
static napi_value NapiGetIssuerName(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
@ -1023,6 +1052,7 @@ void NapiX509Certificate::DefineX509CertJSClass(napi_env env, napi_value exports
DECLARE_NAPI_FUNCTION("checkValidityWithDate", NapiCheckValidityWithDate),
DECLARE_NAPI_FUNCTION("getVersion", NapiGetVersion),
DECLARE_NAPI_FUNCTION("getSerialNumber", NapiGetSerialNumber),
DECLARE_NAPI_FUNCTION("getCertSerialNumber", NapiGetCertSerialNumber),
DECLARE_NAPI_FUNCTION("getIssuerName", NapiGetIssuerName),
DECLARE_NAPI_FUNCTION("getSubjectName", NapiGetSubjectName),
DECLARE_NAPI_FUNCTION("getNotBeforeTime", NapiGetNotBeforeTime),