Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / pkix / pki.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      LICENSE-2.0" target="_blank">http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19
20  ******************************************************************/
21
22
23 #ifndef _PKI_H_
24 #define _PKI_H_
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #include "byte_array.h"
31 #include "pki_errors.h"
32 #include "crypto_adapter.h"
33 #include "cert.h"
34
35 /**
36  * Maximal number of certificates in trust chain.
37  */
38 #define MAX_CHAIN_LEN    (3)
39
40 /**
41  * Maximal length of the TLS certificate message.
42  */
43 #define MAX_CERT_MESSAGE_LEN (2048)
44
45 #ifdef X509_DEBUG
46 /**
47  * Prints Certificate to console.
48  *
49  * @param crt - pointer to Certificate structure
50  * @return PKI_SUCCESS if success, error code otherwise
51  */
52 PKIError PrintCertificate(const CertificateX509 *const crt);
53 #endif
54
55 /**
56  * Checks certificate validity period.
57  *
58  * @param dateFrom - array with not before field
59  * @param dateTo - array with not after field
60  * @return PKI_SUCCESS if valid, error code otherwise
61  */
62 PKIError CheckValidity(ByteArray dateFrom, ByteArray dateTo);
63
64 /**
65  * Checks certificate date and sign.
66  *
67  * @param[in] certDerCode - Byte array with DER encoded certificate
68  * @param[in] caPublicKey - CA public key
69  * @return  0 if successful
70  */
71 PKIError CheckCertificate(ByteArray certDerCode, ByteArray caPublicKey);
72
73 /**
74  * Parses each certificates from list.
75  *
76  * @param[in] chainDerCode Array of DER encoded certificates
77  * @param[out] chainCrt Array of parsed certificates
78  * @param[in] chainLen Lengths of array
79  * @returns  PKI_SUCCESS if no error is occurred
80  */
81 PKIError ParseCertificateChain (ByteArray *chainDerCode, CertificateX509 *chainCrt,
82                                 uint8_t chainLen);
83
84 /**
85  * Loads certificates in DER format from TLS message to array.
86  *
87  * @param[in] msg TLS message with certificate's chain
88  * @param[out] chain Array of DER encoded certificates
89  * @param[out] chainLen Lengths of array
90  * @returns  PKI_SUCCESS if no error is occurred
91  */
92 PKIError LoadCertificateChain (ByteArray msg, ByteArray *chain, uint8_t *chainLength);
93
94 /**
95  * Checks the signature of each certificate in chain.
96  *
97  * @param[in] chainCrt Chain of certificates structures
98  * @param[in] chainLen Number of certificates in the chain
99  * @param[in] caPubKey Public key which sign the last certificate from chain
100  * @returns PKI_SUCCESS if no error is occurred
101  */
102 PKIError CheckCertificateChain (CertificateX509 *chainCrt, uint8_t chainLen, ByteArray caPubKey);
103
104 #ifdef __cplusplus
105 }
106 #endif //__cplusplus
107 #endif // _PKI_H_