19b5081c045052123ea20eaaaf2be40a8ef3460b
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / ck_manager / include / ck_manager.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 #ifndef INCLUDE_MASTER_ISSUER_H_
22 #define INCLUDE_MASTER_ISSUER_H_
23
24 #include "byte_array.h"
25 #include "cert_generator.h"
26 #include "ckm_info.h"
27 #include "csr_generator.h"
28 #include "ecc.h"
29 #include "crl_generator.h"
30 #include "pki_errors.h"
31 #include <stdio.h>
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #define ISSUER_DEFAULT_CA_NAME          "CA_name"
38 #define ISSUER_DEFAULT_SUBJECT_NAME     "Subj_name"
39 #define ISSUER_DEFAULT_NOT_BEFORE       "130101000000Z"
40 #define ISSUER_DEFAULT_NOT_AFTER        "490101000000Z"
41 #define ISSUER_DEFAULT_THIS_UPDATE      "150601000000Z"
42
43 /**
44  * Set serial number for next certificate and save it in the CA storage.
45  *
46  * Used by CKMSetCAInfo()
47  *
48  * @param[in] serNum certificate serial number to be set
49  * @return PKI_SUCCESS if success, error code otherwise
50  */
51 PKIError SetSerialNumber (const long serNum);
52
53 /**
54  * Set CA common name and save it in the CA storage.
55  *
56  * Used by CKMSetCAInfo()
57  *
58  * @param[in] rootName CA common name to be set
59  * @return PKI_SUCCESS if success, error code otherwise
60  */
61 PKIError SetRootName (const ByteArray rootName);
62
63 /**
64  * Set CA info and save it in the CA storage.
65  *
66  * Should be called before certificate generation
67  *
68  * @param[in] serNum certificate serial number to be set
69  * @param[in] rootName CA common name to be set
70  * @return PKI_SUCCESS if success, error code otherwise
71  */
72 PKIError CKMSetCAInfo (const long serNum, const ByteArray rootName);
73
74 /**
75  * Generate key pair for CA.
76  *
77  * Should be called before certificate generation
78  *
79  * @param[out] caPrivateKey pointer to allocated ByteArray for CA private key
80  * @param[out] caPublicKey pointer to allocated ByteArray for CA public key
81  * @return PKI_SUCCESS if success, error code otherwise
82  */
83 PKIError GenerateCAKeyPair (ByteArray *caPrivateKey, ByteArray *caPublicKey);
84
85 /**
86  * Issues X.509 certificate with specified parameters.
87  *
88  * SetSerialNumber, SetRootName and GenerateCAKeyPair should be called before.
89  *
90  * @param[in] uint8NotBefore pointer to string with certificate validity limit
91  *                             or 0 to use default value
92  * @param[in] uint8NotAfter pointer to string with certificate validity limit
93  *                            or 0 to use default value
94  * @param[out] issuedCertificate pointer to allocated memory for DER encoded certificate
95  *          (ISSUER_MAX_CERT_SIZE bytes should be allocated)
96  * @return PKI_SUCCESS if success, error code otherwise
97  */
98 PKIError CKMIssueRootCertificate (const uint8_t *uint8NotBefore, const uint8_t *uint8NotAfter,
99                                      ByteArray *issuedRootCertificate);
100
101 /**
102  * Generate key pair for ordinary device.
103  *
104  * @param[out] privateKey pointer to allocated ByteArray for private key
105  * @param[out] publicKey pointer to allocated ByteArray for public key
106  * @return PKI_SUCCESS if success, error code otherwise
107  */
108 PKIError GenerateKeyPair (ByteArray *privateKey, ByteArray *publicKey);
109
110 /**
111  * Issues X.509 certificate with specified parameters.
112  *
113  * SetSerialNumber, SetRootName and GenerateCAKeyPair should be called before.
114  *
115  * @param[in] uint8SubjectName pointer to string with serial subject's common name
116  *                               or 0 to use default value
117  * @param[in] uint8NotBefore pointer to string with certificate validity limit
118  *                             or 0 to use default value
119  * @param[in] uint8NotAfter pointer to string with certificate validity limit
120  *                            or 0 to use default value
121  * @param[in] uint8SubjectPublicKey pointer to array with subject's public key to be signed
122  * @param[out] issuedCertificate pointer to allocated memory for DER encoded certificate
123  *          (ISSUER_MAX_CERT_SIZE bytes should be allocated)
124  * @return PKI_SUCCESS if success, error code otherwise
125  */
126 PKIError CKMIssueDeviceCertificate (const uint8_t *uint8SubjectName,
127                                     const uint8_t *uint8NotBefore, const uint8_t *uint8NotAfter,
128                                     const uint8_t *uint8SubjectPublicKey,
129                                     ByteArray *issuedCertificate);
130
131 /**
132  * Write certificate into specified file.
133  *
134  * @param[in] certificate pointer to DER-encoded certificate that should be written into the file
135  * @param[in] certFileName pointer to null-terminated string with file name
136  * @return PKI_SUCCESS if success, error code otherwise
137  */
138 PKIError GenerateDERCertificateFile (const ByteArray *certificate, const char * const certFileName);
139
140 /**
141  * Issues certificate signing request with specified parameters.
142  *
143  * @param[in] uint8SubjectName pointer to string with serial subject's common name
144  *                               or 0 to use default value
145  * @param[in] uint8SubjectPublicKey pointer to array with subject's public key to be signed
146  * @param[in] uint8SubjectPrivateKey pointer to array with subject's private key to be signed
147  * @param[out] encodedCSR pointer to allocated memory for DER encoded certificate signing request
148  *          (CSR_MAX_SIZE bytes should be allocated)
149  * @return PKI_SUCCESS if success, error code otherwise
150  */
151 PKIError GenerateCSR (const uint8_t *uint8SubjectName,
152                          const uint8_t *uint8SubjectPublicKey,
153                          const uint8_t *uint8SubjectPrivateKey,
154                          ByteArray *encodedCSR);
155
156 /**
157  * Issues X.509 certificate based on certificate signing request.
158  *
159  * @param[in] encodedCSR pointer to array with DER encoded certificate signing request
160  * @param[out] issuedCertificate pointer to allocated memory for DER encoded certificate
161  *          (MAX_CERT_SIZE bytes should be allocated)
162  * @return PKI_SUCCESS if success, error code otherwise
163  */
164 PKIError GenerateCertificateByCSR (const ByteArray *encodedCSR, ByteArray *issuedCertificate);
165
166 /**
167  * Generate certificate revocation list with specified parameters.
168  *
169  * @param[in] thisUpdateTime pointer to time of issuing CRL or 0 to use default
170  * @param[in] nuberOfRevoked number of revoked certificates
171  * @param[in] revokedNumbers array with numbers of revoked certificates
172  * @param[in] revocationDates
173  * @param[out] encodedCRL pointer to allocated memory for DER encoded certificate revocation list
174  *          ( (CRL_MIN_SIZE + nuberOfRevoked * (sizeof(CertificateRevocationInfo_t) + 4)) bytes
175  *          should be allocated)
176  * @return PKI_SUCCESS if success, error code otherwise
177  */
178 PKIError CKMIssueCRL (const uint8_t *uint8ThisUpdateTime, const uint32_t nuberOfRevoked,
179                       const uint32_t *revokedNumbers, const uint8_t **revocationDates,
180                       ByteArray *encodedCRL);
181
182 /**
183  * Gets current certificate revocation list.
184  *
185  * @param[out] certificateRevocationList pointer to the certificate revocation list to get
186  *            (Memory should be allocated before call)
187  * @return PKI_SUCCESS if success, error code otherwise
188  */
189 PKIError CKMGetCRL (ByteArray *certificateRevocationList);
190
191 /**
192  * Add specified certificate into certificate revocation list.
193  *
194  * @param[in] thisUpdateTime pointer to time of issuing CRL or 0 to use default
195  * @param[in] revokedNumber number of certificate to revoke
196  * @param[out] encodedCRL pointer to allocated memory for DER encoded certificate revocation list
197  *          ( (CRL_MIN_SIZE + number of ALL revoked certificates *
198  *           (sizeof(CertificateRevocationInfo_t) + 4)) bytes should be allocated)
199  * @return PKI_SUCCESS if success, error code otherwise
200  */
201 PKIError CKMRevocateCertificate (const uint8_t *uint8ThisUpdateTime, const long revokedNumber,
202                                  ByteArray *encodedCRL);
203
204 #ifdef __cplusplus
205 }
206 #endif
207
208 #endif /* INCLUDE_MASTER_ISSUER_H_ */