Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / ck_manager / tools / x509_check.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 "pki_errors.h"
28 #include "pki.h"
29 #include "crl.h"
30 #include "oic_malloc.h"
31
32 #define SUCCESS_RES            0
33 #define FAIL_RES               1
34
35 const char COMMAND_CRT[] = "crt";
36 const char COMMAND_CRL[] = "crl";
37
38 /**
39  * Shows program usage hint.
40  */
41 void Usage()
42 {
43     printf("Use next command:\n");
44     printf("x509_check -c <crt/crl> -f <path to crt/crl file> -s <path to CA certificate>\n");
45     printf("\t[-c]\t command name crt | crl\n");
46     printf("\t[-f]\t path to crt/crl file\n");
47     printf("\t[-s]\t path to CA certificate>\n");
48 }
49
50 /**
51  * Converts DER file to byte array.
52  *
53  * @param[in]  filePath - path to DER file (Certificate or CRL)
54  * @param[out] caPublicKey - ByteArray with DER encoded CRT or CRL
55  */
56 void FileToByteArray(const char *filePath, ByteArray *out)
57 {
58     if (!filePath)
59     {
60         printf("File path is NULL!\n");
61         exit(0);
62     }
63
64     FILE *inFile = fopen(filePath, "rb");
65
66     if (!inFile)
67     {
68         printf("Specified file doesn't exist!\n");
69         exit(0);
70     }
71
72     fseek(inFile, 0, SEEK_END);
73     out->len = ftell(inFile);
74     rewind(inFile);
75     out->data = (uint8_t *)OICMalloc(out->len);
76
77     if (!out->data)
78     {
79         printf("Impossible to allocate memory!\n");
80         exit(0);
81     }
82
83     if (!fread(out->data, sizeof(uint8_t), out->len, inFile))
84     {
85         printf("No info in file!\n");
86         exit(0);
87     }
88     fclose(inFile);
89 }
90
91 /**
92  * Main function.
93  *
94  * Checks certificate and certificate revocation list
95  *
96  * @param[in]  argc An integer argument count of the command line arguments
97  * @param[in]  argv An argument vector of the command line arguments
98  *
99  * @return[out] an integer 0 upon exit success
100  */
101 int main(int argc, char *argv[])
102 {
103     int isCrt = 0;
104     const char *testedFileName = 0;
105     const char *caCrtFileName = 0;
106
107     // Parse command line arguments
108     int opt;
109
110     while ((opt = getopt(argc, argv, "c:f:s:")) != -1)
111     {
112         switch (opt)
113         {
114             case 'c':
115                 if (!strcmp(COMMAND_CRT, optarg))
116                 {
117                     isCrt = 1;
118                 }
119                 else if (!strcmp(COMMAND_CRL, optarg))
120                 {
121                     isCrt = 0;
122                 }
123                 else
124                 {
125                     printf("Wrong command(-c)!\n");
126                     Usage();
127                     exit(0);
128                 }
129                 break;
130             case 'f':
131                 testedFileName = optarg;
132                 break;
133             case 's':
134                 caCrtFileName = optarg;
135                 break;
136             default:
137                 Usage();
138                 exit(0);
139         }
140     }
141
142     if (testedFileName == NULL || caCrtFileName == NULL)
143     {
144         printf("Wrong file name\n");
145         Usage();
146         exit(0);
147     }
148
149     // main
150     ByteArray testedCrtCrl = BYTE_ARRAY_INITIALIZER; // Could contain either Certificate or CRL
151     ByteArray caCrt = BYTE_ARRAY_INITIALIZER;
152
153     // Copy DER files, specified in command line to ByteArray structure
154     FileToByteArray(testedFileName, &testedCrtCrl);
155     FileToByteArray(caCrtFileName, &caCrt);
156
157     // Decoding CA certificate
158     CertificateX509 caCrtStuct = CERTIFICATE_X509_INITIALIZER;
159     PKIError errorCode = DecodeCertificate(caCrt, &caCrtStuct);
160
161     if (errorCode != PKI_SUCCESS)
162     {
163         printf("Unable to decode CA Certificate!\n");
164         printf("Error code: %d\n", errorCode);
165     }
166
167     ByteArray caPublicKey = BYTE_ARRAY_INITIALIZER;
168     // Verifies Certificate or CRL depending on request
169     if (isCrt)
170     {
171         errorCode = CheckCertificate(testedCrtCrl, caCrtStuct.pubKey);
172     }
173     else
174     {
175         caPublicKey.data = caCrtStuct.pubKey.data;
176         caPublicKey.len = caCrtStuct.pubKey.len;
177         ParsePublicKey(&caPublicKey);
178         CertificateList crlStruct = CRL_INITIALIZER;
179         errorCode = DecodeCertificateList(testedCrtCrl, &crlStruct, caPublicKey);
180     }
181
182     if (errorCode == PKI_SUCCESS)
183     {
184         printf("Verification SUCCESS!\n");
185     }
186     else
187     {
188         printf("Verification FAILED!\n");
189         printf("Error code: %d\n", errorCode);
190     }
191
192     // Free the allocated memory
193     OICFree(testedCrtCrl.data);
194     OICFree(caCrt.data);
195
196     return 0;
197 }