2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FSecCert_CertExtension.cpp
20 * @brief This file contains implementation of X509 Certificate Extensions.
28 #include <unique_ptr.h>
32 #include <FBaseSysLog.h>
33 #include <FBaseByteBuffer.h>
34 #include <FBaseResult.h>
35 #include "FSecCert_CertExtension.h"
36 #include "FSecCert_CertOidDef.h"
38 namespace Tizen { namespace Security { namespace Cert
47 const _ExtTable mExtTable[_MAX_EXT_TABLESIZE] =
49 {_CERT_EXT_BASIC_CONTRAINTS, reinterpret_cast< byte* >(const_cast< char* >(_OID_BASIC_CONSTRAINTS))},
50 {_CERT_EXT_CRL_DISTRIBUTION_POINTS, reinterpret_cast< byte* >(const_cast< char* >(_OID_CRL_DISTRIBUTION_POINT))},
51 {_CERT_EXT_KEY_USAGE, reinterpret_cast< byte* >(const_cast< char* >(_OID_KEY_USAGE))},
52 {_CERT_EXT_AUTHORITY_KEY_IDENTIFIER, reinterpret_cast< byte* >(const_cast< char* >(_OID_AUTHORITY_KEY_IDENTIFIER))},
53 {_CERT_EXT_SUBJECT_KEY_IDENTIFIER, reinterpret_cast< byte* >(const_cast< char* >(_OID_SUBJECT_KEY_IDENTIFIER))},
54 {_CERT_EXT_CERTIFICATE_POLICIES, reinterpret_cast< byte* >(const_cast< char* >(_OID_CERTIFICATE_POLICIES))},
55 {_CERT_EXT_EXT_KEY_USAGE, reinterpret_cast< byte* >(const_cast< char* >(_OID_EXT_KEY_USAGE))},
56 {_CERT_EXT_OCSP_NONCE, reinterpret_cast< byte* >(const_cast< char* >(_OID_OCSP_NONCE))},
57 {_CERT_EXT_OCSP_NO_CHECK, reinterpret_cast< byte* >(const_cast< char* >(_OID_OCSP_NO_CHECK))},
58 {_CERT_EXT_OCSP, reinterpret_cast< byte* >(const_cast< char* >(_OID_OCSP))},
59 {_CERT_EXT_OCSP_SIGNING, reinterpret_cast< byte* >(const_cast< char* >(_OID_OCSP_SIGNING))},
60 {_CERT_EXT_AUTHORITY_INFO_ACCESS, reinterpret_cast< byte* >(const_cast< char* >(_OID_AUTHORITY_INFO_ACCESS))},
61 {_CERT_EXT_OMA_DRM_AGENT, reinterpret_cast< byte* >(const_cast< char* >(_OID_DRM_AGENT))},
62 {_CERT_EXT_OMA_RIGHTS_ISSUER, reinterpret_cast< byte* >(const_cast< char* >(_OID_OMA_KP_RIGHTS_ISSUER))},
63 {_CERT_EXT_REVOKED_CERTIFICATE_RANGE, reinterpret_cast< byte* >(const_cast< char* >(_OID_REVOKED_CERTIFICATE_RANGE))},
64 {_CERT_EXT_ISSUING_DISTRIBUTION_POINT, reinterpret_cast< byte* >(const_cast< char* >(_OID_ISSUING_DISTRIBUTION_POINT))},
65 {_CERT_EXT_CRL_NUMBER, reinterpret_cast< byte* >(const_cast< char* >(_OID_CRL_NUMBER))},
66 {_CERT_EXT_CA_ISSUERS, reinterpret_cast< byte* >(const_cast< char* >(_OID_CA_ISSUERS))},
67 {_CERT_EXT_SUB_ALT_NAME, reinterpret_cast< byte* >(const_cast< char* >(_OID_SUBJECT_ALT_NAME))},
68 {_CERT_EXT_SUB_DIR_ATTR, reinterpret_cast< byte* >(const_cast< char* >(_OID_SUBJECT_DIRECTORY_ATTRIBUTES))},
69 {_CERT_EXT_PRI_KEY_USAGE_PER, reinterpret_cast< byte* >(const_cast< char* >(_OID_PRIVATE_KEY_USAGE_PERIOD))},
70 {_CERT_EXT_ISSUER_ALT_NAME, reinterpret_cast< byte* >(const_cast< char* >(_OID_ISSUER_ALT_NAME))},
71 {_CERT_EXT_REASON_CODE, reinterpret_cast< byte* >(const_cast< char* >(_OID_CRL_REASON_CODE))},
72 {_CERT_EXT_INSTRUCTION_CODE, reinterpret_cast< byte* >(const_cast< char* >(_OID_INSTRUCTION_CODE))},
73 {_CERT_EXT_INVALID_DATE, reinterpret_cast< byte* >(const_cast< char* >(_OID_INVALIDITY_DATE))},
74 {_CERT_EXT_DELTA_CRL_INDI, reinterpret_cast< byte* >(const_cast< char* >(_OID_DELTA_CRL_INDICATOR))},
75 {_CERT_EXT_CERTIFICATE_ISSUER, reinterpret_cast< byte* >(const_cast< char* >(_OID_CERTIFICATE_ISSUER))},
76 {_CERT_EXT_NAME_CONSTRAINT, reinterpret_cast< byte* >(const_cast< char* >(_OID_NAME_CONSTRAINT))},
77 {_CERT_EXT_POLICY_MAPPING, reinterpret_cast< byte* >(const_cast< char* >(_OID_POLICY_MAPPINGS))},
78 {_CERT_EXT_POLICY_CONSTRAINTS, reinterpret_cast< byte* >(const_cast< char* >(_OID_POLICY_CONSTRAINTS))},
79 {_CERT_EXT_UNKNOWN, reinterpret_cast< byte* >(const_cast< char* >(""))}
83 // _CertExtension class
85 _CertExtension::_CertExtension(void)
92 _CertExtension::~_CertExtension(void)
96 _CertExtensionInfo* pTmpExt = __pExts;
97 _CertExtensionInfo* pNextExt = __pExts;
100 for (getId = 0; getId < num; getId++)
104 pNextExt = pTmpExt->pCertExtNext;
105 delete[] pTmpExt->CertExtValue;
106 pTmpExt->CertExtValue = null;
114 _CertExtension::AddExt(byte* pOid, bool critical, byte* pValue, int len)
116 SysTryReturnVoidResult(NID_SEC_CERT, pOid != null, E_INVALID_ARG, "Invalid input parameter.");
117 SysTryReturnVoidResult(NID_SEC_CERT, pValue != null, E_INVALID_ARG, "Invalid input parameter.");
119 std::unique_ptr< _CertExtensionInfo > pAddExt(new (std::nothrow) _CertExtensionInfo());
120 SysTryReturnVoidResult(NID_SEC_CERT, pAddExt != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
123 pAddExt->CertExtType = GetExtType(pOid);
125 pAddExt->CertExtCritical = critical;
127 pAddExt->CertExtValue = new (std::nothrow) byte[len + 1];
128 SysTryReturnVoidResult(NID_SEC_CERT, pAddExt->CertExtValue != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
130 memset(pAddExt->CertExtValue, 0, len + 1);
131 memcpy(pAddExt->CertExtValue, pValue, len);
132 pAddExt->CertExtLength = len;
133 pAddExt->pCertExtNext = null;
137 __pExts = pAddExt.release();
141 _CertExtensionInfo* pTmpExt = __pExts;
142 __pExts = pAddExt.release();
143 __pExts->pCertExtNext = pTmpExt;
149 _CertExtension::GetExt(byte* pOid)
151 byte* pExtOid = null;
152 SysTryReturn(NID_SEC_CERT, pOid != null, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input parameter.");
154 _CertExtensionInfo* pTmpExt = __pExts;
157 pExtOid = GetExtOid(pTmpExt->CertExtType);
160 if (strcmp(reinterpret_cast< const char* >(pExtOid), reinterpret_cast< const char* >(pOid)) == 0)
165 pTmpExt = pTmpExt->pCertExtNext;
172 _CertExtension::GetExt(_CertExt type)
174 _CertExtensionInfo* pTmpExt = __pExts;
177 if (pTmpExt->CertExtType == type)
181 pTmpExt = pTmpExt->pCertExtNext;
187 _CertExtension::GetExtNum(void)
189 _CertExtensionInfo* pTmpExt = __pExts;
194 pTmpExt = pTmpExt->pCertExtNext;
200 _CertExtension::GetExtEntry(short getId)
202 _CertExtensionInfo* pTmpExt = __pExts;
205 for (index = 1; index < getId; index++)
209 pTmpExt = pTmpExt->pCertExtNext;
216 _CertExtension::GetExtType(byte* pOid)
220 SysTryReturn(NID_SEC_CERT, pOid != null, _CERT_EXT_UNKNOWN, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input parameter.");
222 for (index = 0; index < _MAX_EXT_TABLESIZE; index++)
224 if (strcmp(reinterpret_cast< const char* >(mExtTable[index].pOid), reinterpret_cast< const char* >(pOid)) == 0)
226 return mExtTable[index].type;
229 return _CERT_EXT_UNKNOWN;
233 _CertExtension::GetExtOid(_CertExt type)
237 for (index = 0; index < _MAX_EXT_TABLESIZE; index++)
239 if (mExtTable[index].type == type)
241 return mExtTable[index].pOid;
247 } } } //Tizen::Security::Cert