1 /******************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
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
11 * LICENSE-2.0" target="_blank">http://www.apache.org/licenses/LICENSE-2.0
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.
20 ******************************************************************/
21 #include <gtest/gtest.h>
22 #include "ck_manager.h"
23 #include "crl_generator.h"
25 #include "oic_malloc.h"
27 #define ASN1_UNCOMPRESSED_KEY_ID (0x04)
29 //EncodeDecodeCSR test
30 TEST(CSRGeneratorTest, EncodeDecodeCSR)
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];
47 csrDer.data = csrData;
48 csrDer.len = CSR_MAX_SIZE;
50 pubKeySubj.data = subjPubKey;
51 pubKeySubj.len = sizeof(subjPubKey);
52 privKeySubj.data = subjPrivKey;
53 privKeySubj.len = sizeof(subjPrivKey);
55 ASSERT_EQ(PKI_SUCCESS, GenerateKeyPair(&privKeySubj, &pubKeySubj));
57 subjectName = (UTF8String_t*)OICCalloc(1, sizeof(UTF8String_t));
58 EXPECT_TRUE(NULL != subjectName);
60 subjectPublicKey = (BIT_STRING_t*)OICCalloc(1, sizeof(BIT_STRING_t));
61 EXPECT_TRUE(NULL != subjectPublicKey);
63 subjectPrivateKey = (BIT_STRING_t*)OICCalloc(1, sizeof(BIT_STRING_t));
64 EXPECT_TRUE(NULL != subjectPrivateKey);
66 if(NULL != subjectName && NULL != subjectPublicKey && NULL != subjectPrivateKey)
68 subjectName->buf = (uint8_t *)subjName;
69 subjectName->size = strlen((const char *)subjectName->buf);
71 subjectPrivateKey->size = PRIVATE_KEY_SIZE + 1;
72 subjectPrivateKey->buf = (uint8_t *)OICCalloc((subjectPrivateKey->size), sizeof(uint8_t));
73 EXPECT_TRUE(NULL != subjectPrivateKey->buf);
75 subjectPublicKey->size = PUBLIC_KEY_SIZE + 1;
76 subjectPublicKey->buf = (uint8_t *)OICCalloc(subjectPublicKey->size, sizeof(uint8_t));
77 EXPECT_TRUE(NULL != subjectPublicKey->buf);
79 if(NULL!= subjectPublicKey->buf && NULL != subjectPrivateKey->buf)
81 memcpy((subjectPrivateKey->buf) + 1, subjPrivKey, PRIVATE_KEY_SIZE);
82 memcpy((subjectPublicKey->buf) + 1, subjPubKey, PUBLIC_KEY_SIZE);
84 EXPECT_EQ(PKI_SUCCESS, EncodeCSR(subjectName, subjectPublicKey, subjectPrivateKey, &csrDer));
88 subjectPublicKeyOut = (BIT_STRING_t*)OICCalloc(1, sizeof(BIT_STRING_t));
89 EXPECT_TRUE(NULL != subjectPublicKey);
91 subjectNameOut = (UTF8String_t*)OICCalloc(1, sizeof(UTF8String_t));
92 EXPECT_TRUE(NULL != subjectNameOut);
94 if(NULL != subjectNameOut && NULL != subjectPublicKeyOut)
96 subjectNameOut->buf = uint8SubjectName;
97 subjectPublicKeyOut->buf = uint8SubjectPublicKey;
98 EXPECT_EQ(PKI_SUCCESS, DecodeCSR(&csrDer, subjectNameOut, subjectPublicKeyOut));
101 OICFree(subjectName);
102 OICFree(subjectPublicKey);
103 OICFree(subjectNameOut);
104 OICFree(subjectPublicKeyOut);
105 if (subjectPrivateKey)
107 OICFree(subjectPrivateKey->buf);
108 OICFree(subjectPrivateKey);