Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / ck_manager / unittest / csr_generator_test.cpp
1 /******************************************************************
2  *
3  * Copyright 2016 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 #include <gtest/gtest.h>
22 #include "ck_manager.h"
23 #include "crl_generator.h"
24 #include "pki.h"
25 #include "oic_malloc.h"
26 #include "ckm_info.h"
27 #define ASN1_UNCOMPRESSED_KEY_ID   (0x04)
28
29 //EncodeDecodeCSR test
30 TEST(CSRGeneratorTest, EncodeDecodeCSR)
31 {
32     uint8_t csrData[CSR_MAX_SIZE] = {0};
33     uint8_t subjPubKey[PUBLIC_KEY_SIZE] = {0};
34     uint8_t subjPrivKey[PRIVATE_KEY_SIZE] = {0};
35     ByteArray pubKeySubj = BYTE_ARRAY_INITIALIZER;
36     ByteArray privKeySubj = BYTE_ARRAY_INITIALIZER;
37     uint8_t *subjName = (uint8_t *)"Subject05";
38     UTF8String_t* subjectName = NULL;
39     BIT_STRING_t* subjectPublicKey = NULL;
40     BIT_STRING_t* subjectPrivateKey = NULL;
41     ByteArray csrDer = BYTE_ARRAY_INITIALIZER;
42     BIT_STRING_t* subjectPublicKeyOut = NULL;
43     UTF8String_t* subjectNameOut = NULL;
44     uint8_t uint8SubjectName[ISSUER_MAX_NAME_SIZE];
45     uint8_t uint8SubjectPublicKey[PUBLIC_KEY_SIZE + 1];
46
47     csrDer.data = csrData;
48     csrDer.len = CSR_MAX_SIZE;
49
50     pubKeySubj.data = subjPubKey;
51     pubKeySubj.len = sizeof(subjPubKey);
52     privKeySubj.data = subjPrivKey;
53     privKeySubj.len = sizeof(subjPrivKey);
54
55     ASSERT_EQ(PKI_SUCCESS, GenerateKeyPair(&privKeySubj, &pubKeySubj));
56
57     subjectName = (UTF8String_t*)OICCalloc(1, sizeof(UTF8String_t));
58     EXPECT_TRUE(NULL != subjectName);
59
60     subjectPublicKey = (BIT_STRING_t*)OICCalloc(1, sizeof(BIT_STRING_t));
61     EXPECT_TRUE(NULL != subjectPublicKey);
62
63     subjectPrivateKey = (BIT_STRING_t*)OICCalloc(1, sizeof(BIT_STRING_t));
64     EXPECT_TRUE(NULL != subjectPrivateKey);
65
66     if(NULL != subjectName && NULL != subjectPublicKey && NULL != subjectPrivateKey)
67     {
68         subjectName->buf  = (uint8_t *)subjName;
69         subjectName->size = strlen((const char *)subjectName->buf);
70
71         subjectPrivateKey->size = PRIVATE_KEY_SIZE + 1;
72         subjectPrivateKey->buf = (uint8_t *)OICCalloc((subjectPrivateKey->size), sizeof(uint8_t));
73         EXPECT_TRUE(NULL != subjectPrivateKey->buf);
74
75         subjectPublicKey->size = PUBLIC_KEY_SIZE + 1;
76         subjectPublicKey->buf = (uint8_t *)OICCalloc(subjectPublicKey->size, sizeof(uint8_t));
77         EXPECT_TRUE(NULL != subjectPublicKey->buf);
78
79         if(NULL!= subjectPublicKey->buf && NULL != subjectPrivateKey->buf)
80         {
81             memcpy((subjectPrivateKey->buf) + 1, subjPrivKey, PRIVATE_KEY_SIZE);
82             memcpy((subjectPublicKey->buf) + 1, subjPubKey, PUBLIC_KEY_SIZE);
83
84             EXPECT_EQ(PKI_SUCCESS, EncodeCSR(subjectName, subjectPublicKey, subjectPrivateKey, &csrDer));
85         }
86     }
87
88     subjectPublicKeyOut = (BIT_STRING_t*)OICCalloc(1, sizeof(BIT_STRING_t));
89     EXPECT_TRUE(NULL != subjectPublicKey);
90
91     subjectNameOut = (UTF8String_t*)OICCalloc(1, sizeof(UTF8String_t));
92     EXPECT_TRUE(NULL != subjectNameOut);
93
94     if(NULL != subjectNameOut && NULL != subjectPublicKeyOut)
95     {
96         subjectNameOut->buf = uint8SubjectName;
97         subjectPublicKeyOut->buf = uint8SubjectPublicKey;
98         EXPECT_EQ(PKI_SUCCESS, DecodeCSR(&csrDer, subjectNameOut, subjectPublicKeyOut));
99     }
100
101     OICFree(subjectName);
102     OICFree(subjectPublicKey);
103     OICFree(subjectNameOut);
104     OICFree(subjectPublicKeyOut);
105     if (subjectPrivateKey)
106     {
107         OICFree(subjectPrivateKey->buf);
108         OICFree(subjectPrivateKey);
109     }
110 }