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 "cm_type.h"
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define SN_MAX_SIZE 64
28 #define TIME_FORMAT_MAX_SIZE 16
29 #define NAME_MAX_SIZE 256
30 #define FINGERPRINT_MAX_SIZE  128
31 #define NAME_DELIMITER_SIZE 2
32 #define NAME_ANS1TIME_LEN   12
33 
34 #define CM_SUBJECT_NAME_NULL "CN=,OU=,O="
35 #define CM_COMMON_NAME "CN"
36 #define CM_SURNAME   "SN"
37 #define CM_COUNTRY_NAME "C"
38 #define CM_LOCALITY_NAME "L"
39 #define CM_STATE_OR_PROVINCE_NAME "ST"
40 #define CM_STREET_ADDRESS "street"
41 #define CM_ORGANIZATION_NAME "O"
42 #define CM_ORGANIZATION_UNIT_NAME  "OU"
43 
44 #define ASN1_TAG_TYPE_SEQ 0x30
45 enum CmCertFormat {
46     CM_CERT_FORMAT_PEM,
47     CM_CERT_FORMAT_DER
48 };
49 
50 struct DataTime {
51     uint32_t year;
52     uint32_t month;
53     uint32_t day;
54     uint32_t hour;
55     uint32_t min;
56     uint32_t second;
57 };
58 
59 X509 *InitCertContext(const uint8_t *certBuf, uint32_t size);
60 
61 int32_t GetX509SerialNumber(X509 *x509cert, char *outBuf, uint32_t outBufMaxSize);
62 
63 int32_t GetX509SubjectName(const X509 *x509cert, const char *subjectObjName, char *outBuf, uint32_t outBufMaxSize);
64 
65 int32_t GetX509SubjectNameLongFormat(const X509 *x509cert, char *outBuf, uint32_t outBufMaxSize);
66 
67 int32_t GetSubjectNameAndAlias(X509 *x509cert, const struct CmBlob *certAlias,
68     struct CmBlob *subjectName, struct CmBlob *displaytName);
69 
70 int32_t GetX509IssueNameLongFormat(const X509 *x509cert, char* outBuf, uint32_t outBufMaxSize);
71 
72 int32_t GetX509NotBefore(const X509 *x509cert, char* outBuf, uint32_t outBufMaxSize);
73 int32_t GetX509NotAfter(const X509 *x509cert, char* outBuf, uint32_t outBufMaxSize);
74 
75 int32_t GetX509Fingerprint(const X509 *x509cert, char* outBuf, uint32_t outBufMaxSize);
76 
77 void FreeCertContext(X509 *x509cert);
78 #ifdef __cplusplus
79 }
80 #endif
81 #endif
82