mirror of
https://gitee.com/openharmony/security_certificate_framework
synced 2024-11-27 08:40:45 +00:00
!44 增加napi接口X509Cert.getCertSerialNumber实现
Merge pull request !44 from zhao_zhen_zhou/master
This commit is contained in:
commit
26e8fdb314
@ -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)
|
||||
|
||||
|
@ -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";
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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),
|
||||
|
Loading…
Reference in New Issue
Block a user