1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "x509_distinguished_name.h"
17 #include "x509_distinguished_name_spi.h"
18
19 #include <securec.h>
20
21 #include "config.h"
22 #include "cf_log.h"
23 #include "cf_memory.h"
24 #include "utils.h"
25 #include "x509_distinguished_name_openssl.h"
26
27 #define HCF_X509_DISTINGUISHED_NAME_CLASS "HcfX509DistinguishedName"
28 typedef CfResult (*HcfX509DistinguishedNameSpiCreateFunc)(const CfBlob *, const bool, HcfX509DistinguishedNameSpi **);
29
30 typedef struct {
31 HcfX509DistinguishedNameSpiCreateFunc createFunc;
32 } HcfX509DistinguishedNameFuncSet;
33
34 typedef struct {
35 char *certType;
36 HcfX509DistinguishedNameFuncSet funcSet;
37 } HcfDistiNameFactoryAbility;
38
GetX509DistinguishedNameClass(void)39 static const char *GetX509DistinguishedNameClass(void)
40 {
41 return HCF_X509_DISTINGUISHED_NAME_CLASS;
42 }
43
44 static const HcfDistiNameFactoryAbility X509_DISTINGUISHED_NAME_ABILITY_SET[] = {
45 { "X509DistinguishedName", { OpensslX509DistinguishedNameSpiCreate, } }
46 };
47
FindAbility(const char * certType)48 static const HcfX509DistinguishedNameFuncSet *FindAbility(const char *certType)
49 {
50 if (certType == NULL) {
51 LOGE("CertType is null!");
52 return NULL;
53 }
54 for (uint32_t i = 0; i < sizeof(X509_DISTINGUISHED_NAME_ABILITY_SET) / sizeof(HcfDistiNameFactoryAbility); i++) {
55 if (strcmp(X509_DISTINGUISHED_NAME_ABILITY_SET[i].certType, certType) == 0) {
56 return &(X509_DISTINGUISHED_NAME_ABILITY_SET[i].funcSet);
57 }
58 }
59 LOGE("Cert not support! [cert]: %s", certType);
60 return NULL;
61 }
62
DestroyX509DistinguishedName(CfObjectBase * self)63 static void DestroyX509DistinguishedName(CfObjectBase *self)
64 {
65 if (self == NULL) {
66 LOGE("Invalid input parameter.");
67 return;
68 }
69 if (!CfIsClassMatch(self, GetX509DistinguishedNameClass())) {
70 LOGE("Class is not match.");
71 return;
72 }
73 HcfX509DistinguishedNameImpl *impl = (HcfX509DistinguishedNameImpl *)self;
74 CfObjDestroy(impl->spiObj);
75 CfFree(impl);
76 }
77
GetEncoded(HcfX509DistinguishedName * self,CfEncodingBlob * out)78 static CfResult GetEncoded(HcfX509DistinguishedName *self, CfEncodingBlob *out)
79 {
80 if ((self == NULL) || (out == NULL)) {
81 LOGE("Invalid input parameter.");
82 return CF_INVALID_PARAMS;
83 }
84 if (!CfIsClassMatch((CfObjectBase *)self, GetX509DistinguishedNameClass())) {
85 LOGE("Class is not match.");
86 return CF_INVALID_PARAMS;
87 }
88 return ((HcfX509DistinguishedNameImpl *)self)->spiObj->engineGetEncode(
89 ((HcfX509DistinguishedNameImpl *)self)->spiObj, out);
90 }
91
GetName(HcfX509DistinguishedName * self,CfBlob * type,CfBlob * out,CfArray * outArr)92 static CfResult GetName(HcfX509DistinguishedName *self, CfBlob *type, CfBlob *out, CfArray *outArr)
93 {
94 if (self == NULL) {
95 LOGE("Invalid input parameter.");
96 return CF_INVALID_PARAMS;
97 }
98 if (!CfIsClassMatch((CfObjectBase *)self, GetX509DistinguishedNameClass())) {
99 LOGE("Class is not match.");
100 return CF_INVALID_PARAMS;
101 }
102 return ((HcfX509DistinguishedNameImpl *)self)->spiObj->engineGetName(
103 ((HcfX509DistinguishedNameImpl *)self)->spiObj, type, out, outArr);
104 }
105
HcfX509DistinguishedNameCreate(const CfBlob * inStream,bool bString,HcfX509DistinguishedName ** returnObj)106 CfResult HcfX509DistinguishedNameCreate(const CfBlob *inStream, bool bString, HcfX509DistinguishedName **returnObj)
107 {
108 if ((inStream == NULL) || (returnObj == NULL)) {
109 return CF_INVALID_PARAMS;
110 }
111 const HcfX509DistinguishedNameFuncSet *funcSet = FindAbility("X509DistinguishedName");
112 if (funcSet == NULL) {
113 return CF_NOT_SUPPORT;
114 }
115 HcfX509DistinguishedNameSpi *spiObj = NULL;
116 CfResult res = funcSet->createFunc(inStream, bString, &spiObj);
117 if (res != CF_SUCCESS) {
118 LOGE("Failed to create spi object!");
119 return res;
120 }
121 HcfX509DistinguishedNameImpl *x509NameImpl =
122 (HcfX509DistinguishedNameImpl *)CfMalloc(sizeof(HcfX509DistinguishedNameImpl), 0);
123 if (x509NameImpl == NULL) {
124 LOGE("Failed to allocate x509DistinguishedNameImpl memory!");
125 CfObjDestroy(spiObj);
126 return CF_ERR_MALLOC;
127 }
128 x509NameImpl->base.base.getClass = GetX509DistinguishedNameClass;
129 x509NameImpl->base.base.destroy = DestroyX509DistinguishedName;
130 x509NameImpl->base.getEncode = GetEncoded;
131 x509NameImpl->base.getName = GetName;
132 x509NameImpl->spiObj = spiObj;
133 *returnObj = (HcfX509DistinguishedName *)x509NameImpl;
134 return CF_SUCCESS;
135 }