1 /*
2  * Copyright (c) 2023-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 #ifndef CF_CERTIFICATE_OPENSSL_COMMON_H
17 #define CF_CERTIFICATE_OPENSSL_COMMON_H
18 
19 #include <openssl/x509.h>
20 #include <stdint.h>
21 
22 #include "cf_blob.h"
23 #include "cf_result.h"
24 #include "x509_cert_match_parameters.h"
25 
26 
27 #define CF_OPENSSL_SUCCESS 1 /* openssl return 1: success */
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 typedef enum {
34     NAME_TYPE_SUBJECT,
35     NAME_TYPE_ISSUER,
36     NAME_TYPE_AUKEYID,
37     NAME_TYPE_SUBKEYID
38 } X509NameType;
39 
40 const char *GetAlgorithmName(const char *oid);
41 void CfPrintOpensslError(void);
42 CfResult DeepCopyDataToBlob(const unsigned char *data, uint32_t len, CfBlob *outBlob);
43 CfResult DeepCopyBlobToBlob(const CfBlob *inBlob, CfBlob **outBlob);
44 CfResult CopyExtensionsToBlob(const X509_EXTENSIONS *exts, CfBlob *outBlob);
45 CfResult CompareDateWithCertTime(const X509 *x509, const ASN1_TIME *inputDate);
46 CfResult ConvertNameDerDataToString(const unsigned char *data, uint32_t derLen, CfBlob *out);
47 CfResult CompareNameObject(const X509 *cert, const CfBlob *derBlob, X509NameType type, bool *compareRes);
48 CfResult CompareBigNum(const CfBlob *lhs, const CfBlob *rhs, int *out);
49 uint8_t *GetX509EncodedDataStream(const X509 *certificate, int *dataLength);
50 char *Asn1TimeToStr(const ASN1_GENERALIZEDTIME *time);
51 bool CfArrayContains(const CfArray *self, const CfArray *sub);
52 CfResult DeepCopyDataToOut(const char *data, uint32_t len, CfBlob *out);
53 void SubAltNameArrayDataClearAndFree(SubAltNameArray *array);
54 bool CheckIsSelfSigned(const X509 *cert);
55 bool CheckIsLeafCert(X509 *cert);
56 CfResult IsOrderCertChain(STACK_OF(X509) * certsChain, bool *isOrder);
57 CfResult CheckSelfPubkey(X509 *cert, const EVP_PKEY *pubKey);
58 X509 *FindCertificateBySubject(STACK_OF(X509) * certs, X509_NAME *subjectName);
59 CfResult GetPubKeyDataFromX509(X509 *x509, CfBlob **pub);
60 CfResult GetSubjectNameFromX509(X509 *cert, CfBlob **sub);
61 CfResult GetNameConstraintsFromX509(X509 *cert, CfBlob **name);
62 #ifdef __cplusplus
63 }
64 #endif
65 
66 #endif
67