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