Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / ck_manager / unittest / pki_test.cpp
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 <fcntl.h>
23 #include <iostream>
24 #include <string.h>
25 #include <oic_malloc.h>
26 #include <gtest/gtest.h>
27 #include "ocstack.h"
28
29 #include "cert_generator.h"
30 #include "ck_manager.h"
31 #include "pki.h"
32 #include "sn_store.h"
33 #include "der_dec.h"
34 #include "crl.h"
35 #include "crl_generator.h"
36 #include "crlresource.h"
37 #include "ckm_info.h"
38
39
40 #define RUNS          1
41 #define MAX_LEN     1000
42 #define TEST_SN       50
43 #define READ_WRITE_BLOCK_N 1ul
44 #define N_LENGTH_BYTES 3
45
46 const char *CKMI_PS_FILE_NAME = "CKMInfo.dat";
47
48 #define CRL_DEFAULT_CRL_ID           1
49 #define CRL_DEFAULT_THIS_UPDATE     "150101000000Z"
50 #define CRL_DEFAULT_CRL_DATA        "-"
51
52 #define NUMBER_OF_REVOKED 2
53
54 OCPersistentStorage ps = { NULL, NULL, NULL, NULL, NULL};
55
56 //#define NUM_ACE_FOR_WILDCARD_IN_CKM1_JSON (2)
57
58 FILE* ckm_fopen(const char * /*path*/, const char *mode)
59 {
60     return fopen(CKMI_PS_FILE_NAME, mode);
61 }
62
63 #define STRINGIZE2(x) #x
64 #define STRINGIZE(x) STRINGIZE2(x)
65
66 static char* resolve_file_path(const char* filename )
67 {
68     int len = strlen(STRINGIZE(SECURITY_BUILD_UNITTEST_DIR)) + strlen(filename) + 1;
69     char *filepath = (char *)OICCalloc(1, len);
70
71     if (!filepath)
72     {
73         printf("filepath memory allocation failed. \n");
74         return NULL;
75     }
76     int ret = snprintf(filepath, len, "%s%s", STRINGIZE(SECURITY_BUILD_UNITTEST_DIR), filename);
77     return filepath;
78 }
79
80 void SetPersistentHandler(OCPersistentStorage *ps)
81 {
82     if(ps)
83     {
84         ps->open = ckm_fopen;
85         ps->read = fread;
86         ps->write = fwrite;
87         ps->close = fclose;
88         ps->unlink = unlink;
89     }
90 }
91
92 // Length of test certificate
93 #define SIMPLE_CRT_LEN 469
94
95 class PKITest : public ::testing::Test
96 {
97 public:
98     static void SetUpTestCase()
99     {
100         SetPersistentHandler(&ps);
101         OCStackResult res = OCRegisterPersistentStorageHandler(&ps);
102         ASSERT_TRUE(res == OC_STACK_OK);
103     }
104
105     static void TearDownTestCase()
106     {
107     }
108
109     static CertificateX509  g_certificate;
110
111     static const ByteArray g_caPublicKey;
112
113     static const ByteArray g_derCode ;
114
115     static ByteArray g_serNum;
116 };
117
118 CertificateX509  PKITest::g_certificate;
119
120 const ByteArray PKITest::g_derCode = {(uint8_t[])
121     {
122         0x30, 0x82, 0x01, 0xd1, 0x30, 0x82, 0x01, 0x77, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
123         0xd7, 0x56, 0x8c, 0xfc, 0x53, 0x18, 0xb0, 0xab, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce,
124         0x3d, 0x04, 0x03, 0x02, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
125         0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f,
126         0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04,
127         0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67,
128         0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x1e, 0x17, 0x0d, 0x31,
129         0x35, 0x30, 0x33, 0x31, 0x32, 0x31, 0x32, 0x32, 0x35, 0x31, 0x31, 0x5a, 0x17, 0x0d, 0x31, 0x37,
130         0x30, 0x33, 0x31, 0x31, 0x31, 0x32, 0x32, 0x35, 0x31, 0x31, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30,
131         0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
132         0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31,
133         0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e,
134         0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c,
135         0x74, 0x64, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06,
136         0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x8c, 0xc8, 0x92,
137         0x1d, 0xaa, 0x7f, 0xf0, 0xe4, 0xb2, 0x75, 0xd6, 0x4a, 0xf1, 0xd5, 0x14, 0x3f, 0x1a, 0x09, 0xc5,
138         0x3e, 0x52, 0xd6, 0xda, 0xa0, 0xbf, 0x90, 0x43, 0xd1, 0x6b, 0xfe, 0xd1, 0xb3, 0x75, 0x5c, 0xdd,
139         0x69, 0xac, 0x42, 0xa1, 0xcb, 0x03, 0x16, 0xee, 0xa4, 0x30, 0xa5, 0x8d, 0x36, 0x8f, 0xc5, 0x7b,
140         0xb4, 0xb5, 0x6a, 0x7d, 0x9b, 0x16, 0x04, 0x46, 0xab, 0xae, 0xbb, 0x56, 0xa1, 0xa3, 0x50, 0x30,
141         0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x5c, 0x0e, 0x30, 0xa8,
142         0x8e, 0x7f, 0xc9, 0x02, 0xcd, 0xa8, 0xed, 0x0d, 0x1a, 0x1b, 0xd9, 0x7d, 0xe6, 0xce, 0x2a, 0x59,
143         0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x5c, 0x0e, 0x30,
144         0xa8, 0x8e, 0x7f, 0xc9, 0x02, 0xcd, 0xa8, 0xed, 0x0d, 0x1a, 0x1b, 0xd9, 0x7d, 0xe6, 0xce, 0x2a,
145         0x59, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30,
146         0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x48, 0x00, 0x30, 0x45,
147         0x02, 0x21, 0x00, 0xf6, 0x79, 0xed, 0x69, 0xd5, 0xe5, 0xba, 0x42, 0x14, 0xfc, 0xce, 0x47, 0xf1,
148         0x61, 0x1c, 0x51, 0x11, 0x2b, 0xba, 0x04, 0x70, 0x56, 0x78, 0xaf, 0xa9, 0xa6, 0x98, 0x8f, 0x4b,
149         0xa8, 0x11, 0x67, 0x02, 0x20, 0x3a, 0xdf, 0xf1, 0x74, 0xc9, 0x2f, 0xfb, 0x84, 0x46, 0xde, 0xbc,
150         0x2d, 0xda, 0xe3, 0x05, 0xb4, 0x81, 0x31, 0x45, 0xf7, 0x3d, 0x71, 0x46, 0x07, 0xa7, 0xd8, 0xcb,
151         0xae, 0x1e, 0x1b, 0x1c, 0x5a
152     }, SIMPLE_CRT_LEN };
153
154
155 const ByteArray PKITest::g_caPublicKey = {(uint8_t[])
156 {
157     0x8c, 0xc8, 0x92, 0x1d, 0xaa, 0x7f, 0xf0, 0xe4, 0xb2, 0x75, 0xd6, 0x4a, 0xf1, 0xd5, 0x14, 0x3f,
158     0x1a, 0x09, 0xc5, 0x3e, 0x52, 0xd6, 0xda, 0xa0, 0xbf, 0x90, 0x43, 0xd1, 0x6b, 0xfe, 0xd1, 0xb3,
159     0x75, 0x5c, 0xdd, 0x69, 0xac, 0x42, 0xa1, 0xcb, 0x03, 0x16, 0xee, 0xa4, 0x30, 0xa5, 0x8d, 0x36,
160     0x8f, 0xc5, 0x7b, 0xb4, 0xb5, 0x6a, 0x7d, 0x9b, 0x16, 0x04, 0x46, 0xab, 0xae, 0xbb, 0x56, 0xa1
161 }, PUBLIC_KEY_SIZE };
162
163
164 ByteArray PKITest::g_serNum = {(uint8_t[SERIAL_NUMBER_MAX_LEN]) {0}, SERIAL_NUMBER_MAX_LEN};
165
166 //registering persistent storage test
167 TEST(CKManager, RegisterPersistentStorage)
168 {
169     SetPersistentHandler(&ps);
170     ASSERT_EQ(OC_STACK_OK, OCRegisterPersistentStorageHandler(&ps));
171 }
172
173 //check decoding predefined certificate
174 TEST(X509Certificate, DecodeTest)
175 {
176     ByteArray code = PKITest::g_derCode;
177
178     ASSERT_EQ(DecodeCertificate(code, &PKITest::g_certificate), PKI_SUCCESS);
179     code.data = NULL;
180     ASSERT_NE(DecodeCertificate(code, &PKITest::g_certificate), PKI_SUCCESS);
181 }
182
183 //check decoding of random symbols sequence
184 TEST(X509Certificate, RandomDecode)
185 {
186     srand((unsigned int)time(NULL));
187
188     ByteArray code;
189     INIT_BYTE_ARRAY(code);
190
191     for (unsigned int i = 0; i < RUNS; i++)
192     {
193         code.len = (size_t)((size_t)rand() % MAX_LEN);
194         code.data = (uint8_t *)malloc(code.len * sizeof(uint8_t));
195
196         EXPECT_NE(code.data, (uint8_t *)NULL);
197
198         for (unsigned int j = 0; j < code.len; j++)
199         {
200             code.data[j] = (uint8_t)(rand() % 128 + 1);
201         }
202
203         EXPECT_NE(PKI_SUCCESS, DecodeCertificate(code, &PKITest::g_certificate));
204
205         free(code.data);
206     }
207 }
208 //testing validity check of predefined certificate
209 TEST(X509Certificate, testCheckValidity)
210 {
211     CertificateX509 tempCrt;
212     ASSERT_EQ(PKI_SUCCESS, DecodeCertificate(PKITest::g_derCode, &tempCrt));
213     ASSERT_EQ(PKI_SUCCESS, CheckValidity(tempCrt.validFrom, tempCrt.validTo));
214     ByteArray temp = tempCrt.validTo;
215
216     tempCrt.validTo = tempCrt.validFrom;
217     tempCrt.validFrom = temp;
218     ASSERT_EQ(PKI_CERT_DATE_INVALID, CheckValidity(tempCrt.validFrom, tempCrt.validTo));
219 }
220
221 //testing signature check of predefined certificate
222 TEST(X509Certificate, CheckSignature)
223 {
224     ByteArray code = PKITest::g_derCode;
225
226     ASSERT_EQ(PKI_SUCCESS, CheckCertificate(code, PKITest::g_caPublicKey));
227     code.data = NULL;
228     ASSERT_NE(PKI_SUCCESS, CheckCertificate(code, PKITest::g_caPublicKey));
229 }
230
231 //test saving certificate into file
232 TEST_F(PKITest, DERCertificateFile)
233 {
234     uint8_t derData[ISSUER_MAX_CERT_SIZE] = {0};
235     uint8_t caPubKey[PUBLIC_KEY_SIZE] = {0};
236     uint8_t caPrivKey[PRIVATE_KEY_SIZE] = {0};
237
238     ByteArray certDer = BYTE_ARRAY_INITIALIZER;
239     ByteArray pubKeyIss = BYTE_ARRAY_INITIALIZER;
240     ByteArray privKeyIss = BYTE_ARRAY_INITIALIZER;
241     ByteArray rootName = BYTE_ARRAY_INITIALIZER;
242
243     certDer.data = derData;
244     certDer.len = ISSUER_MAX_CERT_SIZE;
245
246     pubKeyIss.data = caPubKey;
247     pubKeyIss.len = sizeof(caPubKey);
248     privKeyIss.data = caPrivKey;
249     privKeyIss.len = sizeof(caPrivKey);
250     rootName.data = (uint8_t *)"ROOT1";
251     rootName.len = strlen((char *)rootName.data);
252     ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
253
254     for (int i = 1; i <= RUNS; i++)
255     {
256         ASSERT_EQ(PKI_SUCCESS, GenerateCAKeyPair(&privKeyIss, &pubKeyIss));
257         ASSERT_EQ(PKI_SUCCESS, SetSerialNumber(i));
258         ASSERT_EQ(PKI_SUCCESS, SetRootName(rootName));
259         ASSERT_EQ(PKI_SUCCESS, CKMIssueRootCertificate(0, 0, &certDer));
260         ASSERT_EQ(PKI_SUCCESS, CheckCertificate(certDer, pubKeyIss));
261         ASSERT_EQ(PKI_SUCCESS, GenerateDERCertificateFile (&certDer, "der_cert"));
262     }
263     ASSERT_EQ(CloseCKMInfo(), PKI_SUCCESS);
264 }
265
266 //test checking time validity of generated certificate
267 TEST_F(PKITest, TimeValidity)
268 {
269     uint8_t derData[ISSUER_MAX_CERT_SIZE] = {0};
270     uint8_t caPubKey[PUBLIC_KEY_SIZE] = {0};
271     uint8_t caPrivKey[PRIVATE_KEY_SIZE] = {0};
272
273     ByteArray certDer = BYTE_ARRAY_INITIALIZER;
274     ByteArray pubKey = BYTE_ARRAY_INITIALIZER;
275     ByteArray privKey = BYTE_ARRAY_INITIALIZER;
276     ByteArray rootName = BYTE_ARRAY_INITIALIZER;
277
278     privKey.data = caPrivKey;
279     privKey.len = sizeof(caPrivKey);
280
281     certDer.data = derData;
282     certDer.len = sizeof(derData);
283
284     pubKey.data = caPubKey;
285     pubKey.len = sizeof(caPubKey);
286
287     rootName.data = (uint8_t *)"ROOT3";
288     rootName.len = strlen((char *)rootName.data);
289     ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
290
291     for (int i = 1; i <= RUNS; i++)
292     {
293         ASSERT_EQ(PKI_SUCCESS, GenerateCAKeyPair(&privKey, &pubKey));
294         ASSERT_EQ(PKI_SUCCESS, SetSerialNumber(i));
295         ASSERT_EQ(PKI_SUCCESS, SetRootName(rootName));
296
297         ASSERT_EQ(PKI_SUCCESS, CKMIssueRootCertificate(0, 0, &certDer));
298         ASSERT_EQ(PKI_SUCCESS, CheckCertificate(certDer, pubKey));
299
300         certDer.len = sizeof(derData);
301         ASSERT_EQ(PKI_SUCCESS, CKMIssueRootCertificate(0, (uint8_t *)"130101000000Z", &certDer));
302         ASSERT_EQ(PKI_CERT_DATE_INVALID, CheckCertificate(certDer, pubKey));
303
304         certDer.len = sizeof(derData);
305         ASSERT_EQ(PKI_SUCCESS, CKMIssueRootCertificate((uint8_t *)"170101000000Z", 0, &certDer));
306         ASSERT_EQ(PKI_CERT_DATE_INVALID, CheckCertificate(certDer, pubKey));
307     }
308     ASSERT_EQ(PKI_SUCCESS, CloseCKMInfo());
309 }
310
311 //testing certificate generation by certificate signing request
312 TEST_F(PKITest, CertificateSigningRequest)
313 {
314     uint8_t certData[ISSUER_MAX_CERT_SIZE] = {0};
315     uint8_t csrData[CSR_MAX_SIZE] = {0};
316     uint8_t subjPubKey[PUBLIC_KEY_SIZE] = {0};
317     uint8_t subjPrivKey[PRIVATE_KEY_SIZE] = {0};
318     uint8_t caPubKey[PUBLIC_KEY_SIZE] = {0};
319     uint8_t caPrivKey[PRIVATE_KEY_SIZE] = {0};
320     uint8_t *subjName = (uint8_t *)"Subject05";
321
322     ByteArray certDer = BYTE_ARRAY_INITIALIZER;
323     ByteArray csrDer = BYTE_ARRAY_INITIALIZER;
324     ByteArray pubKeyIss = BYTE_ARRAY_INITIALIZER;
325     ByteArray privKeyIss = BYTE_ARRAY_INITIALIZER;
326     ByteArray pubKeySubj = BYTE_ARRAY_INITIALIZER;
327     ByteArray privKeySubj = BYTE_ARRAY_INITIALIZER;
328     ByteArray rootName = BYTE_ARRAY_INITIALIZER;
329
330     certDer.data = certData;
331     certDer.len = sizeof(certData);
332     csrDer.data = csrData;
333     csrDer.len = CSR_MAX_SIZE;
334
335     pubKeyIss.data = caPubKey;
336     pubKeyIss.len = sizeof(caPubKey);
337     privKeyIss.data = caPrivKey;
338     privKeyIss.len = sizeof(caPrivKey);
339     pubKeySubj.data = subjPubKey;
340     pubKeySubj.len = sizeof(subjPubKey);
341     privKeySubj.data = subjPrivKey;
342     privKeySubj.len = sizeof(subjPrivKey);
343     rootName.data = (uint8_t *)"ROOT2";
344     rootName.len = strlen((char *)rootName.data);
345     ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
346
347     ASSERT_EQ(GenerateCAKeyPair(&privKeyIss, &pubKeyIss), PKI_SUCCESS);
348     ASSERT_EQ(SetSerialNumber(1), PKI_SUCCESS);
349     ASSERT_EQ(SetRootName(rootName), PKI_SUCCESS);
350
351     for (int i = 1; i <= RUNS; i++)
352     {
353         ASSERT_EQ(PKI_SUCCESS, GenerateKeyPair(&privKeySubj, &pubKeySubj));
354         ASSERT_EQ(PKI_SUCCESS, GenerateCSR(subjName, subjPubKey, subjPrivKey, &csrDer));
355         ASSERT_EQ(PKI_SUCCESS, GenerateCertificateByCSR(&csrDer, &certDer));
356         ASSERT_EQ(PKI_SUCCESS, CheckCertificate(certDer, pubKeyIss));
357         certDer.data[0]++;
358         ASSERT_NE(PKI_SUCCESS, CheckCertificate(certDer, pubKeyIss));
359         certDer.data[0]--;
360         ASSERT_EQ(PKI_SUCCESS, CheckCertificate(certDer, pubKeyIss));
361     }
362     ASSERT_EQ(PKI_SUCCESS, CloseCKMInfo());
363 }
364
365 //test public key structure parsing
366 TEST(X509Certificate, testParsePublicKey)
367 {
368     ASSERT_EQ(PKI_SUCCESS, ParsePublicKey((ByteArray*)&PKITest::g_caPublicKey));
369
370     size_t length = 3;
371     uint8_t shortAr[length];
372     ByteArray shortArray = {shortAr, length};
373     ASSERT_EQ(PKI_WRONG_ARRAY_LEN, ParsePublicKey(&shortArray));
374
375     uint8_t uncompressed[PUBLIC_KEY_SIZE + 2];
376     uncompressed[0] = 0;
377     uncompressed[1] = ASN1_UNCOMPRESSED_KEY;
378     memcpy(&uncompressed[2], PKITest::g_caPublicKey.data, PUBLIC_KEY_SIZE);
379     ByteArray uncomprArr = {uncompressed, PUBLIC_KEY_SIZE+2};
380     ParsePublicKey(&uncomprArr);
381     ASSERT_EQ((size_t)PUBLIC_KEY_SIZE, uncomprArr.len);
382     ASSERT_EQ(0, memcmp(uncomprArr.data, PKITest::g_caPublicKey.data, PUBLIC_KEY_SIZE));
383 }
384
385 //test checking of certificate generated by OpenSSL
386 TEST(OpenSSLCompatibility, verifyOpenSslCertSign)
387 {
388     struct stat st;
389     char *fpath = NULL;
390     uint8_t crtData[ISSUER_MAX_CERT_SIZE] = {0};
391     uint8_t pubKeyData[PUBLIC_KEY_SIZE] = {0};
392     ByteArray crtDer = BYTE_ARRAY_INITIALIZER;
393     ByteArray pubKey = BYTE_ARRAY_INITIALIZER;
394     CertificateX509 certificate;
395
396     crtDer.data = crtData;
397     crtDer.len = sizeof(crtData);
398     pubKey.data = pubKeyData;
399     pubKey.len = sizeof(pubKeyData);
400
401     //open file
402     fpath = resolve_file_path("/01.der");
403     int fileCert = open(fpath, O_RDONLY);
404     OICFree(fpath);
405
406     ASSERT_TRUE(fileCert != -1);
407     //get status
408     ASSERT_TRUE(fstat(fileCert, &st) == 0);
409     //read the content
410     int res = read(fileCert, crtDer.data, crtDer.len);
411     ASSERT_NE(-1, res);
412     EXPECT_EQ(st.st_size, res);
413     close(fileCert);
414
415      //open file
416     fpath = resolve_file_path("/capub.der");
417     int fileKey = open(fpath, O_RDONLY);
418     OICFree(fpath);
419     ASSERT_TRUE(fileKey != -1);
420     //get status
421     ASSERT_TRUE(fstat(fileKey, &st) == 0);
422
423     if(st.st_size > PUBLIC_KEY_SIZE)
424     {
425         ASSERT_NE(-1, lseek(fileKey, (st.st_size - PUBLIC_KEY_SIZE), SEEK_SET));
426     }
427
428     //read the content
429     res = read(fileKey, pubKey.data, pubKey.len);
430     ASSERT_NE(-1, res);
431     EXPECT_EQ(PUBLIC_KEY_SIZE, res);
432     close(fileKey);
433
434     EXPECT_EQ(PKI_SUCCESS, DecodeCertificate(crtDer, &certificate));
435     EXPECT_EQ(PKI_SUCCESS, CheckCertificate(crtDer, pubKey));
436 }
437
438 //test parsing of certificate chain generated by OpenSSL
439 TEST(CertificateChain, LoadCertificateChain)
440 {
441     char* fpath = NULL;
442     ByteArray crtChainDer[MAX_CHAIN_LEN] = {{0,0},};
443     CertificateX509 crtChain[MAX_CHAIN_LEN] = {{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},};
444     ByteArray msg = BYTE_ARRAY_INITIALIZER;
445     uint8_t chainLength;
446
447     fpath = resolve_file_path("/cert_chain.dat");
448     FILE *file = fopen(fpath, "rb");
449     OICFree(fpath);
450     ASSERT_TRUE(file  != NULL);
451
452     while (!feof (file))
453     {
454         msg.data = (uint8_t *) realloc (msg.data, msg.len + 1);
455         msg.data[msg.len] = fgetc (file);
456         msg.len++;
457     }
458     msg.len--;
459     fclose (file);
460     INC_BYTE_ARRAY(msg, 3);
461     EXPECT_EQ(PKI_SUCCESS, LoadCertificateChain (msg, crtChainDer, &chainLength));
462 #ifdef X509_DEBUG
463     printf("chain len: %d\n", chainLength);
464 #endif
465     EXPECT_EQ(PKI_UNKNOWN_OID, ParseCertificateChain (crtChainDer, crtChain, chainLength));
466
467     free(msg.data - 3);
468 }
469
470 //test checking CA certificate generated by OpenSSL
471 TEST(OpenSSLCompatibility, testOpenSSLCertificate)
472 {
473     char* fpath = NULL;
474     struct stat st;
475     ByteArray crtDer = BYTE_ARRAY_INITIALIZER;
476     uint8_t crtData[ISSUER_MAX_CERT_SIZE] = {0};
477
478     crtDer.data = crtData;
479     crtDer.len = sizeof(crtData);
480
481     //open file
482     fpath = resolve_file_path("/cacert.der");
483     int fd = open(fpath, O_RDONLY);
484     OICFree(fpath);
485     ASSERT_TRUE(fd != -1);
486     //get status
487     ASSERT_TRUE(fstat(fd, &st) == 0);
488
489     //read the content
490     int res = read(fd, crtDer.data, crtDer.len);
491     ASSERT_NE(-1, res);
492     ASSERT_EQ(st.st_size, res);
493     close(fd);
494
495     crtDer.len = st.st_size;
496
497     #ifdef X509_DEBUG
498     printf("Length of cert: %lu\n", crtDer.len);
499     #endif
500     EXPECT_EQ(PKI_SUCCESS, DecodeCertificate(crtDer, &PKITest::g_certificate));
501 }
502
503 //test signatures checking of certificate chain generated by OpenSSL
504 TEST(OpenSSLCompatibility, ParseAndCheckCertificateChain)
505 {
506     struct stat st;
507     ByteArray crtChainDer[MAX_CHAIN_LEN] = {{0,0},};
508     CertificateX509 crtChain[MAX_CHAIN_LEN] = {{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},};
509     ByteArray msg = BYTE_ARRAY_INITIALIZER;
510     uint8_t chainLength;
511
512     char* chainPath = resolve_file_path("/chain.der");
513     FILE *fileChain = fopen(chainPath, "rb");
514     OICFree(chainPath);
515     ASSERT_TRUE(fileChain != NULL);
516
517     //get the length
518     ASSERT_NE(-1, fseek(fileChain, 0, SEEK_END));
519     int len = ftell(fileChain);
520     ASSERT_NE(-1, len);
521     msg.len = (size_t) len;
522     ASSERT_NE(-1, fseek(fileChain, 0, SEEK_SET));
523     //allocate memory
524     msg.data = (uint8_t*)malloc(msg.len+1);
525     //read the content
526     ASSERT_EQ(READ_WRITE_BLOCK_N, fread(msg.data, msg.len, READ_WRITE_BLOCK_N, fileChain));
527
528     fclose (fileChain);
529
530     INC_BYTE_ARRAY(msg, 3);
531     EXPECT_EQ(PKI_SUCCESS, LoadCertificateChain(msg, crtChainDer, &chainLength));
532     EXPECT_EQ(3, chainLength);
533     #ifdef X509_DEBUG
534     printf("Length of the chain: %d\n", chainLength);
535     #endif
536
537     EXPECT_EQ(PKI_SUCCESS, ParseCertificateChain(crtChainDer, crtChain, chainLength));
538
539     ByteArray caPubKey = BYTE_ARRAY_INITIALIZER;
540     uint8_t pubKeyData[PUBLIC_KEY_SIZE] = {0};
541
542     caPubKey.data = pubKeyData;
543     caPubKey.len = sizeof(pubKeyData);
544
545      //open file
546     char* fpath = resolve_file_path("/capub.der");
547     int fileKey = open(fpath, O_RDONLY);
548     OICFree(fpath);
549     ASSERT_TRUE(fileKey != -1);
550     //get status
551     ASSERT_TRUE(fstat(fileKey, &st) == 0);
552
553     if(st.st_size > PUBLIC_KEY_SIZE)
554     {
555         ASSERT_NE(-1, lseek(fileKey, (st.st_size - PUBLIC_KEY_SIZE), SEEK_SET));
556     }
557
558     //read the content
559     int res = read(fileKey, caPubKey.data, caPubKey.len);
560     ASSERT_NE(-1, res);
561     EXPECT_EQ(PUBLIC_KEY_SIZE, res);
562     close(fileKey);
563
564     EXPECT_EQ(PKI_SUCCESS, CheckCertificateChain(crtChain, chainLength, caPubKey));
565
566     free(msg.data - 3);
567 }
568
569 //testing correctness of decoding certificate length from ASN.1 structure
570 TEST(CRL, testDecodeLength)
571 {
572     ByteArray cert = BYTE_ARRAY_INITIALIZER;
573     size_t length(0);
574     EXPECT_EQ(PKI_NULL_PASSED, DecodeLength(&cert, &length));
575
576     //a simple DER
577     size_t derLength = (size_t)rand() % LEN_LONG;
578     cert.len = derLength + 2;
579     uint8_t *certData = (uint8_t*)malloc(cert.len);
580     cert.data = certData;
581     cert.data[0] = (uint8_t)0x30; //mixed types
582     cert.data[1] = (uint8_t)(derLength & 0xff);
583     EXPECT_EQ(PKI_SUCCESS, DecodeLength(&cert, &length));
584     EXPECT_EQ(derLength, length);
585     free(certData);
586 }
587
588 //testing serial number storage
589 TEST(CRL, StoreSerialNumber)
590 {
591     uint8_t data[10] = {0x01, 0x82, 0x01, 0xd1, 0x30, 0x82, 0x01, 0x77, 0xa0, 0x03};
592     const ByteArray sn = { data, sizeof(data) / sizeof(uint8_t)};
593     int i;
594     for (i = 0; i < 400; i++)
595     {
596         sn.data[0] = i % 20;
597         ASSERT_EQ(PKI_SUCCESS, StoreSerialNumber(sn));
598     }
599     ASSERT_EQ(PKI_CERT_REVOKED, CheckSerialNumber(sn));
600
601     sn.data[1] = 0x01;
602     ASSERT_EQ(PKI_SUCCESS, CheckSerialNumber(sn));
603
604     FreeSNStore();
605 }
606 #ifdef ARDUINO_MEMORY_DEBUG
607 //testing memory allocation fault handling at Arduino
608 TEST(SNStore, MemoryOverflow)
609 {
610     uint8_t data[10] = {0x01, 0x82, 0x01, 0xd1, 0x30, 0x82, 0x01, 0x77, 0xa0, 0x03};
611     const ByteArray sn = { data, sizeof(data) / sizeof(uint8_t)};
612     int i;
613     PKIError res;
614     do
615     {
616         res  = StoreSerialNumber(sn);
617     }
618     while (res == PKI_SUCCESS);
619     ASSERT_EQ(PKI_MEMORY_ALLOC_FAILED, res);
620
621     FreeSNStore();
622 }
623 #endif /* ARDUINO_MEMORY_DEBUG */
624
625 //testing next certificate serial number handling by "CKM info" unit
626 TEST_F(PKITest, CAInitAndSerialNum)
627 {
628     ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
629     long serialNum = (long)((long)rand() % (MAX_LEN - 1) + 1);
630     ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
631     //all the serials should start from
632     ASSERT_EQ(PKI_SUCCESS, SetSerialNumber(serialNum));
633     long nextSerial;
634     ASSERT_EQ(PKI_SUCCESS, GetNextSerialNumber(&nextSerial));
635     ASSERT_EQ(nextSerial, serialNum);
636     ASSERT_EQ(PKI_SUCCESS, CloseCKMInfo());
637 }
638
639 //testing CA name handling by "CKM info" unit
640 TEST_F(PKITest, testCAName)
641 {
642     ByteArray caName = BYTE_ARRAY_INITIALIZER;
643     caName.len = (size_t)((size_t)rand() % (ISSUER_MAX_NAME_SIZE - 1) + 1);
644     caName.data = (uint8_t*)malloc(caName.len);
645     size_t i;
646     for(i = 0; i < caName.len; i++){
647             caName.data[i] = (uint8_t)(rand() % 128);
648     }
649     EXPECT_EQ(PKI_SUCCESS, InitCKMInfo());
650     EXPECT_EQ(PKI_SUCCESS, SetRootName(caName));
651     ByteArray getName = BYTE_ARRAY_INITIALIZER;
652     uint8_t uint8CAName[ISSUER_MAX_NAME_SIZE] = {0};
653     getName.data     = uint8CAName;
654     getName.len      = ISSUER_MAX_NAME_SIZE;
655     EXPECT_EQ(PKI_SUCCESS, GetCAName(&getName));
656     EXPECT_EQ(0, memcmp(caName.data, getName.data, caName.len));
657     free(caName.data);
658     ASSERT_EQ(PKI_SUCCESS, CloseCKMInfo());
659 }
660
661 //testing key pair generation and storing by "CKM info" unit
662 TEST_F(PKITest, testKeyPair)
663 {
664     ByteArray rootName = BYTE_ARRAY_INITIALIZER;
665     rootName.data = (uint8_t *)"ROOT";
666     rootName.len = strlen((char *)rootName.data);
667     SetRootName(rootName);
668
669     //first test the GenerateCAKeyPair - this writes to the CA storage
670     ByteArray privKeyIss = BYTE_ARRAY_INITIALIZER;
671     privKeyIss.len = PRIVATE_KEY_SIZE;
672     uint8_t caPrivKey[PRIVATE_KEY_SIZE] = {0};
673     privKeyIss.data = caPrivKey;
674
675     ByteArray pubKeyIss = BYTE_ARRAY_INITIALIZER;
676     pubKeyIss.len = PUBLIC_KEY_SIZE;
677     uint8_t caPubKey[PUBLIC_KEY_SIZE] = {0};
678     pubKeyIss.data = caPubKey;
679
680     ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
681     ASSERT_EQ(PKI_SUCCESS, GenerateCAKeyPair(&privKeyIss, &pubKeyIss));
682
683     ByteArray keyCheck = BYTE_ARRAY_INITIALIZER;
684     keyCheck.len = PUBLIC_KEY_SIZE;
685     uint8_t keyCheckData[PUBLIC_KEY_SIZE] = {0};
686     keyCheck.data = keyCheckData;
687     ASSERT_EQ(PKI_SUCCESS, GetCAPrivateKey(&keyCheck));
688     ASSERT_EQ(0, memcmp(keyCheck.data, privKeyIss.data, PRIVATE_KEY_SIZE));
689
690     ASSERT_EQ(PKI_SUCCESS, GetCAPublicKey(&keyCheck));
691     ASSERT_EQ(0, memcmp(keyCheck.data, pubKeyIss.data, PUBLIC_KEY_SIZE));
692
693     //now test the GenerateKeyPair - does not write to the CA storage
694     ASSERT_EQ(PKI_SUCCESS, GenerateKeyPair(&privKeyIss, &pubKeyIss));
695
696     ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
697     ASSERT_EQ(PKI_SUCCESS, GetCAPrivateKey(&keyCheck));
698     ASSERT_NE(0, memcmp(keyCheck.data, privKeyIss.data, PRIVATE_KEY_SIZE));
699
700     ASSERT_EQ(PKI_SUCCESS, GetCAPublicKey(&keyCheck));
701     ASSERT_NE(0, memcmp(keyCheck.data, pubKeyIss.data, PUBLIC_KEY_SIZE));
702     ASSERT_EQ(PKI_SUCCESS, CloseCKMInfo());
703 }
704
705 //testing CRL encoding
706 TEST_F(PKITest, testEncodeCRL)
707 {
708     CertificateList crl;
709
710     uint8_t *uint8ThisUpdateTime = (uint8_t *)"130101000000Z";
711     uint32_t numberOfRevoked = 0;
712     uint32_t revokedNumbers[2];
713     const uint8_t *revocationDates[2];
714
715     ByteArray code = BYTE_ARRAY_INITIALIZER;
716     ByteArray pubKeyIss =  BYTE_ARRAY_INITIALIZER;
717     ByteArray privKeyIss = BYTE_ARRAY_INITIALIZER;
718     ByteArray rootName = BYTE_ARRAY_INITIALIZER;
719
720     uint8_t caPubKey[PUBLIC_KEY_SIZE] = {0};
721     uint8_t caPrivKey[PRIVATE_KEY_SIZE] = {0};
722
723     pubKeyIss.data = caPubKey;
724     pubKeyIss.len = PUBLIC_KEY_SIZE;
725     privKeyIss.data = caPrivKey;
726     privKeyIss.len = PRIVATE_KEY_SIZE;
727
728     numberOfRevoked = 2;
729
730     revokedNumbers[0] = 100; // serial number of first revoked certificate
731     revokedNumbers[1] = 200; // serial number of second revoked certificate
732     revocationDates[0] = (const uint8_t *)"130101000001Z";
733     revocationDates[1] = (const uint8_t *)"130101000002Z";
734
735     rootName.data = (uint8_t *)"ROOT2";
736     rootName.len = strlen((char *)rootName.data);
737     ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
738     ASSERT_EQ(PKI_SUCCESS, SetRootName(rootName));
739     ASSERT_EQ(PKI_SUCCESS, GenerateCAKeyPair(&privKeyIss, &pubKeyIss));
740
741     code.data = (uint8_t *)calloc(1,
742                 (CRL_MIN_SIZE + numberOfRevoked * (sizeof(CertificateRevocationInfo_t) + 4)));
743     code.len = (CRL_MIN_SIZE + numberOfRevoked * (sizeof(CertificateRevocationInfo_t) + 4));
744
745     EXPECT_EQ(PKI_SUCCESS,CKMIssueCRL(uint8ThisUpdateTime, numberOfRevoked, revokedNumbers,
746                                       revocationDates,&code));
747     EXPECT_EQ(PKI_SUCCESS, DecodeCertificateList (code, &crl, pubKeyIss));
748 #ifdef X509_DEBUG
749     PrintSNStore();
750     PrintCRL(&crl);
751 #endif
752
753     FreeSNStore();
754     free(code.data);
755     ASSERT_EQ(PKI_SUCCESS, CloseCKMInfo());
756 }
757
758 //check correctness of certificate revocation by CKMIssueCRL() and CKMRevocateCertificate()
759 TEST_F(PKITest, testRevocateCertificate)
760 {
761     CertificateList crl;
762
763     uint8_t *uint8ThisUpdateTime = (uint8_t *)"130101000000Z";
764     uint32_t numberOfRevoked = 0;
765     uint32_t revokedNumbers[2];
766     const uint8_t *revocationDates[2];
767
768     ByteArray code = BYTE_ARRAY_INITIALIZER;
769     ByteArray pubKeyIss =  BYTE_ARRAY_INITIALIZER;
770     ByteArray privKeyIss = BYTE_ARRAY_INITIALIZER;
771     ByteArray rootName = BYTE_ARRAY_INITIALIZER;
772
773     uint8_t caPubKey[PUBLIC_KEY_SIZE] = {0};
774     uint8_t caPrivKey[PRIVATE_KEY_SIZE] = {0};
775
776     pubKeyIss.data = caPubKey;
777     pubKeyIss.len = sizeof(caPubKey);
778     privKeyIss.data = caPrivKey;
779     privKeyIss.len = sizeof(caPrivKey);
780
781     numberOfRevoked = 2;
782
783     revokedNumbers[0] = 100; // serial number of first revoked certificate
784     revokedNumbers[1] = 200; // serial number of second revoked certificate
785     revocationDates[0] = (const uint8_t *)"130101000001Z";
786     revocationDates[1] = (const uint8_t *)"130101000002Z";
787
788     rootName.data = (uint8_t *)"ROOT2";
789     rootName.len = strlen((char *)rootName.data);
790     ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
791     ASSERT_EQ(PKI_SUCCESS, SetRootName(rootName));
792     ASSERT_EQ(PKI_SUCCESS, GenerateCAKeyPair(&privKeyIss, &pubKeyIss));
793
794     code.len = CRL_MIN_SIZE + numberOfRevoked * (sizeof(CertificateRevocationInfo_t) + 4);
795     code.data = (uint8_t *)calloc(1, code.len);
796
797     EXPECT_EQ(PKI_SUCCESS, CKMIssueCRL (uint8ThisUpdateTime, numberOfRevoked, revokedNumbers,
798                                         revocationDates, &code));
799     EXPECT_EQ(PKI_SUCCESS, DecodeCertificateList (code, &crl, pubKeyIss));
800     free(code.data);
801     numberOfRevoked++;
802     code.len = CRL_MIN_SIZE + numberOfRevoked * (sizeof(CertificateRevocationInfo_t) + 4);
803     code.data = (uint8_t *)calloc(1, code.len);
804     EXPECT_EQ(PKI_SUCCESS, CKMRevocateCertificate (uint8ThisUpdateTime, 50, &code));
805     EXPECT_EQ(PKI_SUCCESS, DecodeCertificateList (code, &crl, pubKeyIss));
806 #ifdef X509_DEBUG
807     PrintSNStore();
808     PrintCRL(&crl);
809 #endif
810
811     FreeSNStore();
812     free(code.data);
813     ASSERT_EQ(PKI_SUCCESS, CloseCKMInfo());
814 }
815
816 //checck correctness of saving root certificate to binary file
817 TEST_F(PKITest, StoreCKMInfo)
818 {
819     struct stat st;
820     ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
821     uint8_t derData[ISSUER_MAX_CERT_SIZE] = {0};
822     uint8_t caPubKey[PUBLIC_KEY_SIZE] = {0};
823     uint8_t caPrivKey[PRIVATE_KEY_SIZE] = {0};
824     const long serNum  = 48598490;
825     CertificateList crl;
826     uint8_t *uint8ThisUpdateTime = (uint8_t *)"130101000000Z";
827     uint32_t numberOfRevoked = 0;
828     uint32_t revokedNumbers[2];
829     const uint8_t *revocationDates[2];
830
831     ByteArray certDer = BYTE_ARRAY_INITIALIZER;
832     ByteArray pubKeyIss = BYTE_ARRAY_INITIALIZER;
833     ByteArray privKeyIss = BYTE_ARRAY_INITIALIZER;
834     ByteArray rootName = BYTE_ARRAY_INITIALIZER;
835     ByteArray code = BYTE_ARRAY_INITIALIZER;
836
837     certDer.data = derData;
838     certDer.len = ISSUER_MAX_CERT_SIZE;
839     pubKeyIss.data = caPubKey;
840     pubKeyIss.len = PUBLIC_KEY_SIZE;
841     privKeyIss.data = caPrivKey;
842     privKeyIss.len = PRIVATE_KEY_SIZE;
843     rootName.data = (uint8_t *)"ROOT";
844     rootName.len = strlen((char *)rootName.data);
845
846     //generate CA Certificate
847     ASSERT_EQ(PKI_SUCCESS, GenerateCAKeyPair(&privKeyIss, &pubKeyIss));
848     ASSERT_EQ(PKI_SUCCESS, SetSerialNumber(serNum));
849     ASSERT_EQ(PKI_SUCCESS, SetRootName(rootName));
850     ASSERT_EQ(PKI_SUCCESS, CKMIssueRootCertificate(0, 0, &certDer));
851
852     //generate CRL
853     numberOfRevoked = NUMBER_OF_REVOKED;
854
855     revokedNumbers[0] = 100; // serial number of first revoked certificate
856     revokedNumbers[1] = 200; // serial number of second revoked certificate
857     revocationDates[0] = (const uint8_t *)"130101000001Z";
858     revocationDates[1] = (const uint8_t *)"130101000002Z";
859
860     code.data = (uint8_t *)calloc(1,
861                 (CRL_MIN_SIZE + numberOfRevoked * (sizeof(CertificateRevocationInfo_t) + 4)));
862     code.len = (CRL_MIN_SIZE + numberOfRevoked * (sizeof(CertificateRevocationInfo_t) + 4));
863
864     ASSERT_EQ(PKI_SUCCESS, CKMIssueCRL (uint8ThisUpdateTime, numberOfRevoked, revokedNumbers,
865                                         revocationDates, &code));
866
867     // Check Certificate file
868     CertificateX509 certificate;
869     ByteArray crtDer = BYTE_ARRAY_INITIALIZER;
870     uint8_t crtDerData[ISSUER_MAX_CERT_SIZE] = {0};
871
872     crtDer.data = crtDerData;
873     crtDer.len = sizeof(crtDerData);
874
875     //open file
876     int fd = open(CA_STORAGE_CRT_FILE, O_RDONLY);
877     ASSERT_TRUE(fd != -1);
878     //get status
879     ASSERT_TRUE(fstat(fd, &st) == 0);
880     //read the content
881     int res = read(fd, crtDer.data, crtDer.len);
882     ASSERT_NE(-1, res);
883     EXPECT_EQ(st.st_size > ISSUER_MAX_CERT_SIZE ? ISSUER_MAX_CERT_SIZE : st.st_size, res);
884     close(fd);
885
886     ByteArray crtCheck;
887     crtCheck.data = crtDer.data + 3;    //now file contains length of certificate
888     crtCheck.len = crtDer.len - 3;
889     EXPECT_EQ(PKI_SUCCESS, DecodeCertificate(crtCheck, &certificate));
890 #ifdef X509_DEBUG
891     PrintCertificate(&certificate);
892 #endif
893
894     //check CRL
895     ByteArray crlDer = BYTE_ARRAY_INITIALIZER;
896     crlDer.len = (CRL_MIN_SIZE + numberOfRevoked * (sizeof(CertificateRevocationInfo_t) + 4));
897     crlDer.data = (uint8_t *)malloc(crlDer.len);
898
899     EXPECT_EQ(PKI_SUCCESS, GetCertificateRevocationList(&crlDer));
900
901     EXPECT_EQ(PKI_SUCCESS, DecodeCertificateList (crlDer, &crl, pubKeyIss));
902 #ifdef X509_DEBUG
903        PrintCRL(&crl);
904 #endif
905     EXPECT_EQ(PKI_SUCCESS, CloseCKMInfo());
906     free(crlDer.data);
907     free(code.data);
908 }
909
910 //check correctness of root certificate generation
911 TEST_F(PKITest, GenerateRootCertificate)
912 {
913     uint8_t derData[ISSUER_MAX_CERT_SIZE] = {0};
914     uint8_t caPubKey[PUBLIC_KEY_SIZE] = {0};
915     uint8_t caPrivKey[PRIVATE_KEY_SIZE] = {0};
916
917     ByteArray certDer = BYTE_ARRAY_INITIALIZER;
918     ByteArray pubKeyIss = BYTE_ARRAY_INITIALIZER;
919     ByteArray privKeyIss = BYTE_ARRAY_INITIALIZER;
920     ByteArray rootName = BYTE_ARRAY_INITIALIZER;
921
922     certDer.data = derData;
923     certDer.len = sizeof(derData);
924
925     pubKeyIss.data = caPubKey;
926     pubKeyIss.len = sizeof(caPubKey);
927     privKeyIss.data = caPrivKey;
928     privKeyIss.len = sizeof(caPrivKey);
929     rootName.data = (uint8_t *)"ROOT";
930     rootName.len = strlen((char *)rootName.data);
931     ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
932
933     for (int i = 1; i <= RUNS; i++)
934     {
935         ASSERT_EQ(PKI_SUCCESS, GenerateCAKeyPair(&privKeyIss, &pubKeyIss));
936         ASSERT_EQ(PKI_SUCCESS, SetSerialNumber(i));
937         ASSERT_EQ(PKI_SUCCESS, SetRootName(rootName));
938         ASSERT_EQ(PKI_SUCCESS, CKMIssueRootCertificate(0, 0, &certDer));
939
940         ASSERT_EQ(PKI_SUCCESS, CheckCertificate(certDer, pubKeyIss));
941         certDer.data[0]++;
942         ASSERT_NE(PKI_SUCCESS, CheckCertificate(certDer, pubKeyIss));
943         certDer.data[0]--;
944         ASSERT_EQ(PKI_SUCCESS, CheckCertificate(certDer, pubKeyIss));
945     }
946     ASSERT_EQ(PKI_SUCCESS, CloseCKMInfo());
947 }
948
949 //check correctness of ordinal device certificate generation
950 TEST_F(PKITest, GenerateDeviceCertificate)
951 {
952     uint8_t derData[ISSUER_MAX_CERT_SIZE] = {0};
953     uint8_t subjPubKey[PUBLIC_KEY_SIZE] = {0};
954     uint8_t subjPrivKey[PRIVATE_KEY_SIZE] = {0};
955     uint8_t caPubKey[PUBLIC_KEY_SIZE] = {0};
956     uint8_t caPrivKey[PRIVATE_KEY_SIZE] = {0};
957     uint8_t *subjName = (uint8_t *)"Subject Name";
958
959     ByteArray certDer = BYTE_ARRAY_INITIALIZER;
960     ByteArray pubKeyIss = BYTE_ARRAY_INITIALIZER;
961     ByteArray privKeyIss = BYTE_ARRAY_INITIALIZER;
962     ByteArray pubKeySubj = BYTE_ARRAY_INITIALIZER;
963     ByteArray privKeySubj = BYTE_ARRAY_INITIALIZER;
964     ByteArray rootName = BYTE_ARRAY_INITIALIZER;
965
966     certDer.data = derData;
967     certDer.len = ISSUER_MAX_CERT_SIZE;
968
969     pubKeyIss.data = caPubKey;
970     pubKeyIss.len = sizeof(caPubKey);
971     privKeyIss.data = caPrivKey;
972     privKeyIss.len = sizeof(caPrivKey);
973     pubKeySubj.data = subjPubKey;
974     pubKeySubj.len = sizeof(subjPubKey);
975     privKeySubj.data = subjPrivKey;
976     privKeySubj.len = sizeof(subjPrivKey);
977     rootName.data = (uint8_t *)"ROOT2";
978     rootName.len = strlen((char *)rootName.data);
979     ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
980
981     ASSERT_EQ(GenerateCAKeyPair(&privKeyIss, &pubKeyIss), PKI_SUCCESS);
982     for (int i = 1; i <= RUNS; i++)
983     {
984         ASSERT_EQ(PKI_SUCCESS, GenerateKeyPair(&privKeySubj, &pubKeySubj));
985         ASSERT_EQ(PKI_SUCCESS, SetSerialNumber(i));
986         ASSERT_EQ(PKI_SUCCESS, SetRootName(rootName));
987         ASSERT_EQ(PKI_SUCCESS, CKMIssueDeviceCertificate(subjName, 0, 0, subjPubKey, &certDer));
988
989         ASSERT_EQ(PKI_SUCCESS, CheckCertificate(certDer, pubKeyIss));
990         certDer.data[0]++;
991         ASSERT_NE(PKI_SUCCESS, CheckCertificate(certDer, pubKeyIss));
992         certDer.data[0]--;
993         ASSERT_EQ(PKI_SUCCESS, CheckCertificate(certDer, pubKeyIss));
994     }
995     ASSERT_EQ(CloseCKMInfo(), PKI_SUCCESS);
996 }
997
998 //check correctness of saving CRL to storage and loading CRL from storage
999 TEST_F(PKITest, CRLSetGet)
1000 {
1001     OicSecCrl_t *defaultCrl = NULL;
1002     defaultCrl = (OicSecCrl_t *)OICCalloc(1, sizeof(OicSecCrl_t));
1003     ASSERT_NE(defaultCrl, (OicSecCrl_t *) NULL);
1004     defaultCrl->CrlId = CRL_DEFAULT_CRL_ID;
1005     defaultCrl->CrlData.data = (uint8_t *)CRL_DEFAULT_CRL_DATA;
1006     defaultCrl->CrlData.len = strlen(CRL_DEFAULT_CRL_DATA);
1007     defaultCrl->ThisUpdate.data = (uint8_t *)CRL_DEFAULT_THIS_UPDATE;
1008     defaultCrl->ThisUpdate.len = strlen(CRL_DEFAULT_THIS_UPDATE);
1009     EXPECT_EQ(OC_STACK_OK, UpdateCRLResource(defaultCrl));
1010
1011     EXPECT_NE((void *)NULL, GetCrl());
1012     OICFree(defaultCrl);
1013 }
1014
1015 //CKMGetCRL test
1016 TEST_F(PKITest, CKMGetCRL)
1017 {
1018     const size_t CRL_MAX_SIZE = 1024;
1019     ByteArray certificateRevocationList;
1020     certificateRevocationList.data = (uint8_t*)OICCalloc(1,CRL_MAX_SIZE);
1021     certificateRevocationList.len = CRL_MAX_SIZE;
1022     EXPECT_EQ(PKI_SUCCESS, CKMGetCRL(&certificateRevocationList));
1023     OICFree(certificateRevocationList.data);
1024 }
1025
1026 //CKMSetCAInfo test
1027 TEST_F(PKITest, CKMSetCAInfo)
1028 {
1029     ByteArray rootName = BYTE_ARRAY_INITIALIZER;
1030     rootName.data = (uint8_t *)"ROOT2";
1031     rootName.len = strlen((char *)rootName.data);
1032     const long serNum = 100;
1033     EXPECT_EQ(PKI_SUCCESS, CKMSetCAInfo(serNum, rootName));
1034 }
1035
1036 int main(int argc, char **argv)
1037 {
1038     int urandom;
1039     unsigned int seed = 0;
1040
1041     urandom = open ("/dev/urandom", O_RDONLY);
1042     if(urandom)
1043     {
1044         fprintf(stderr, "Cannot open /dev/urandom\n");
1045     }
1046     else
1047     {
1048         if(read(urandom, &seed, sizeof(seed)) != sizeof(seed))
1049         {
1050             fprintf(stderr, "Error read from /dev/urandom\n");
1051         }
1052         close(urandom);
1053     }
1054
1055     srand(seed);
1056
1057     ::testing::InitGoogleTest(&argc, argv);
1058     return RUN_ALL_TESTS();
1059 }