Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / ck_manager / tools / x509_issue.c
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 #include <stdio.h>
23 #include <stdint.h>
24 #include <getopt.h>
25
26 #include "byte_array.h"
27 #include "ck_manager.h"
28 #include "crl.h"
29
30 #define SUCCESS_RES            0
31 #define FAIL_RES               1
32
33 const char COMMAND_CRT[] = "crt";
34 const char COMMAND_CRL[] = "crl";
35
36 #define DEFAULT_SN             1
37
38 #define DEFAULT_CA_CRT_NAME           "ca_crt.der"
39 #define DEFAULT_CA_PUBLIC_KEY_NAME    "ca_public.key"
40 #define DEFAULT_USER_PRIVATE_KEY_NAME "user_private.key"
41 #define DEFAULT_USER_CRT_NAME         "user_crt.der"
42 #define DEFAULT_CRL_NAME              "crl.der"
43
44 #define DEFAULT_DER_DATA_SIZE  1024
45 #define SET_OF_SEQUENCE_SIZE   4
46 #define NUMBER_OF_REVOKED      1
47
48 /**
49  * Shows program usage hint.
50  */
51 void Usage()
52 {
53     printf("Use next command:\n");
54     printf("x509_issue -c <crt/crl> [-n]\n");
55     printf("\t[-c]\t command name crt | crl\n");
56     printf("\t[-n]\t generate new CA key pair\n");
57 }
58
59 /**
60  * Generates CA Certificate File, writes CA private and public keys to storage
61  *
62  * @param[in]  updateCAkeys use new or old keys
63 */
64 int GenerateCACertificateFile(const int updateCAkeys)
65 {
66     // Variables definition
67     uint8_t derData[DEFAULT_DER_DATA_SIZE] = {0};
68     uint8_t caPubKey[PUBLIC_KEY_SIZE] = {0};
69     uint8_t caPrivKey[PRIVATE_KEY_SIZE] = {0};
70     uint8_t defaultCaName[] = "Default_CA_Name";
71
72     ByteArray certDer = BYTE_ARRAY_CONSTRUCTOR(derData);
73     ByteArray pubKeyIss = BYTE_ARRAY_CONSTRUCTOR(caPubKey);
74     ByteArray privKeyIss = BYTE_ARRAY_CONSTRUCTOR(caPrivKey);
75     ByteArray rootName = BYTE_ARRAY_CONSTRUCTOR(defaultCaName);
76
77     // Generate
78     if (updateCAkeys)
79     {
80         GenerateCAKeyPair(&privKeyIss, &pubKeyIss);
81         printf("CA key pair was changed!\n");
82         if (GenerateDERCertificateFile(&pubKeyIss, DEFAULT_CA_PUBLIC_KEY_NAME) != PKI_SUCCESS)
83         {
84             printf("Unable to generate CA public key file!\n");
85             exit(0);
86         }
87         else
88         {
89             printf("CA public key file generated: %s\n", DEFAULT_CA_PUBLIC_KEY_NAME);
90         };
91     }
92
93     SetSerialNumber(DEFAULT_SN);
94     SetRootName(rootName);
95     CKMIssueRootCertificate(0, 0, &certDer);
96
97     // Writes ByteArray to file
98     if (GenerateDERCertificateFile(&certDer, DEFAULT_CA_CRT_NAME) != PKI_SUCCESS)
99     {
100         printf("Unable to generate CA Certificate file!\n");
101         exit(0);
102     }
103     else
104     {
105         printf("CA Certificate File generated: %s\n", DEFAULT_CA_CRT_NAME);
106     };
107     return 0;
108 }
109
110 /**
111  * Generates User Certificate File
112  */
113 void GenerateUserCertificateFile()
114 {
115     uint8_t subjPubKey[PUBLIC_KEY_SIZE] = {0};
116     uint8_t subjPrivKey[PRIVATE_KEY_SIZE] = {0};
117
118     ByteArray pubKeySubj = BYTE_ARRAY_CONSTRUCTOR(subjPubKey);
119     ByteArray privKeySubj = BYTE_ARRAY_CONSTRUCTOR(subjPrivKey);
120
121     // TODO: Uncomment GenerateKeyPair
122     GenerateKeyPair(&privKeySubj, &pubKeySubj);
123
124     if (GenerateDERCertificateFile(&privKeySubj, DEFAULT_USER_PRIVATE_KEY_NAME) != PKI_SUCCESS)
125     {
126         printf("Unable to generate user private key file!\n");
127         exit(0);
128     }
129     else
130     {
131         printf("User private key file generated: %s\n", DEFAULT_USER_PRIVATE_KEY_NAME);
132     };
133
134     uint8_t derData[DEFAULT_DER_DATA_SIZE] = {0};
135     ByteArray certDer = BYTE_ARRAY_CONSTRUCTOR(derData);
136
137     const uint8_t defaultUserName[]   = "Default_USER_Name";
138
139     CKMIssueDeviceCertificate(defaultUserName, 0, 0, subjPubKey, &certDer);
140
141     if (GenerateDERCertificateFile(&certDer, DEFAULT_USER_CRT_NAME) != PKI_SUCCESS)
142     {
143         printf("Unable to generate User Certificate file!\n");
144         exit(0);
145     }
146     else
147     {
148         printf("User Certificate File generated: %s\n", DEFAULT_USER_CRT_NAME);
149     };
150 }
151
152 /**
153  * Generates Certificate Revocation List File
154  */
155 void GenerateCRLFile()
156 {
157     const uint8_t *uint8ThisUpdateTime = (const uint8_t *)"130101000000Z";
158     uint32_t revokedNumbers[NUMBER_OF_REVOKED] = {100};
159     const uint8_t *revocationDates[NUMBER_OF_REVOKED] =
160     {
161         (const uint8_t *)"130101000001Z"
162     };
163
164     ByteArray code =
165     {
166         .len = CRL_MIN_SIZE + NUMBER_OF_REVOKED * (sizeof(CertificateRevocationInfo_t) + SET_OF_SEQUENCE_SIZE),
167         .data = (uint8_t *)calloc(1, CRL_MIN_SIZE + NUMBER_OF_REVOKED * (sizeof(CertificateRevocationInfo_t) + SET_OF_SEQUENCE_SIZE))
168     };
169
170     if (!code.data)
171     {
172         printf("calloc error\n");
173         exit(0);
174     }
175
176     int errorCode = CKMIssueCRL(uint8ThisUpdateTime, NUMBER_OF_REVOKED, revokedNumbers, revocationDates,
177                                 &code);
178
179     printf("Gen CRL err code: %d\n", errorCode);
180
181     //CRL ByteArray to file
182     if (GenerateDERCertificateFile(&code, DEFAULT_CRL_NAME) != PKI_SUCCESS)
183     {
184         printf("Unable to generate CRL file!\n");
185         free(code.data);
186         exit(0);
187     }
188     else
189     {
190         printf("CRL File generated: %s\n", DEFAULT_CRL_NAME);
191     };
192
193     free(code.data);
194 }
195
196 /**
197  * Main function.
198  *
199  * Generates certificate and certificate revocation list
200  *
201  * @param[in]  argc An integer argument count of the command line arguments
202  * @param[in]  argv An argument vector of the command line arguments
203  *
204  * @return[out] an integer 0 upon exit success
205  */
206 int main(int argc, char *argv[])
207 {
208     int isCrt = 0;
209     int updateCAkeys = 0;
210
211     // Parse command line arguments
212     int opt;
213
214     while ((opt = getopt(argc, argv, "c:n")) != -1)
215     {
216         switch (opt)
217         {
218             case 'c':
219                 if (!strcmp(COMMAND_CRT, optarg))
220                 {
221                     isCrt = 1;
222                 }
223                 else if (!strcmp(COMMAND_CRL, optarg))
224                 {
225                     isCrt = 0;
226                 }
227                 else
228                 {
229                     printf("Wrong command(-c)!\n");
230                     Usage();
231                     exit(0);
232                 }
233                 break;
234             case 'n':
235                 updateCAkeys = 1;
236                 break;
237             default:
238                 Usage();
239                 exit(0);
240         }
241     }
242
243     // main
244     GenerateCACertificateFile(updateCAkeys);
245
246     if (isCrt)
247     {
248         GenerateUserCertificateFile();
249     }
250     else
251     {
252         GenerateCRLFile();
253     }
254
255     return 0;
256 }