• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifndef CERT_MANAGER_X509_H
17 #define CERT_MANAGER_X509_H
18 
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <openssl/x509.h>
22 #include <openssl/safestack.h>
23 #include "cm_type.h"
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define SN_MAX_SIZE 64
29 #define TIME_FORMAT_MAX_SIZE 16
30 #define NAME_MAX_SIZE 256
31 #define FINGERPRINT_MAX_SIZE  128
32 #define NAME_DELIMITER_SIZE 2
33 #define NAME_ANS1TIME_LEN   12
34 
35 #define CM_SUBJECT_NAME_NULL "CN=,OU=,O="
36 #define CM_COMMON_NAME "CN"
37 #define CM_SURNAME   "SN"
38 #define CM_COUNTRY_NAME "C"
39 #define CM_LOCALITY_NAME "L"
40 #define CM_STATE_OR_PROVINCE_NAME "ST"
41 #define CM_STREET_ADDRESS "street"
42 #define CM_ORGANIZATION_NAME "O"
43 #define CM_ORGANIZATION_UNIT_NAME  "OU"
44 
45 #define ASN1_TAG_TYPE_SEQ 0x30
46 
47 DEFINE_STACK_OF(char)
48 
49 enum CmCertFormat {
50     CM_CERT_FORMAT_PEM,
51     CM_CERT_FORMAT_DER
52 };
53 
54 struct DataTime {
55     uint32_t year;
56     uint32_t month;
57     uint32_t day;
58     uint32_t hour;
59     uint32_t min;
60     uint32_t second;
61 };
62 
63 X509 *InitCertContext(const uint8_t *certBuf, uint32_t size);
64 
65 /**
66  * @brief Create STACKOF(X509) from a buffer
67  *
68  * @param[in] certBuf P7B file buffer.
69  * @param[in] size Buffer's size.
70  * @return STACK_OF(X509)* Stack of X509 certificate.
71  */
72 STACK_OF(X509) *InitCertStackContext(const uint8_t *certBuf, uint32_t size);
73 
74 int32_t GetX509SerialNumber(X509 *x509cert, char *outBuf, uint32_t outBufMaxSize);
75 
76 int32_t GetX509SubjectName(const X509 *x509cert, const char *subjectObjName, char *outBuf, uint32_t outBufMaxSize);
77 
78 int32_t GetX509SubjectNameLongFormat(const X509 *x509cert, char *outBuf, uint32_t outBufMaxSize);
79 
80 int32_t GetSubjectNameAndAlias(X509 *x509cert, const struct CmBlob *certAlias,
81     struct CmBlob *subjectName, struct CmBlob *displayName);
82 
83 int32_t GetX509IssueNameLongFormat(const X509 *x509cert, char* outBuf, uint32_t outBufMaxSize);
84 
85 int32_t GetX509NotBefore(const X509 *x509cert, char* outBuf, uint32_t outBufMaxSize);
86 int32_t GetX509NotAfter(const X509 *x509cert, char* outBuf, uint32_t outBufMaxSize);
87 
88 int32_t GetX509Fingerprint(const X509 *x509cert, char *outBuf, uint32_t outBufMaxSize);
89 
90 void FreeCertContext(X509 *x509cert);
91 #ifdef __cplusplus
92 }
93 #endif
94 #endif
95