--- /dev/null
+/******************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * LICENSE-2.0" target="_blank">http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+
+ ******************************************************************/
+
+#include <oic_malloc.h>
+#include <gtest/gtest.h>
+#include "ckm_info.h"
+
+#define ISSUER_NAME_SIZE (16)
+#define CERT_LEN 469
+
+class CKMInfoTest : public ::testing::Test
+{
+public:
+ static void SetUpTestCase()
+ {
+ }
+
+ static void TearDownTestCase()
+ {
+ }
+
+ static const ByteArray publicKey;
+ static const ByteArray privateKey;
+ static const ByteArray derCode ;
+ static const long nextSN;
+ static const long crlSerialNum;
+ static const ByteArray caName;
+};
+
+const ByteArray CKMInfoTest::publicKey = {(uint8_t[])
+{
+ 0x8c, 0xc8, 0x92, 0x1d, 0xaa, 0x7f, 0xf0, 0xe4, 0xb2, 0x75, 0xd6, 0x4a, 0xf1, 0xd5, 0x14, 0x3f,
+ 0x1a, 0x09, 0xc5, 0x3e, 0x52, 0xd6, 0xda, 0xa0, 0xbf, 0x90, 0x43, 0xd1, 0x6b, 0xfe, 0xd1, 0xb3,
+ 0x75, 0x5c, 0xdd, 0x69, 0xac, 0x42, 0xa1, 0xcb, 0x03, 0x16, 0xee, 0xa4, 0x30, 0xa5, 0x8d, 0x36,
+ 0x8f, 0xc5, 0x7b, 0xb4, 0xb5, 0x6a, 0x7d, 0x9b, 0x16, 0x04, 0x46, 0xab, 0xae, 0xbb, 0x56, 0xa1
+}, PUBLIC_KEY_SIZE };
+
+const ByteArray CKMInfoTest::privateKey = {(uint8_t[])
+{
+ 0xd6, 0xc8, 0x92, 0x16, 0x36, 0x7f, 0xf0, 0xe4, 0xb2, 0x75, 0xd6, 0x4a, 0xf1, 0x3f, 0x14, 0x30,
+ 0x1a, 0x69, 0xc5, 0x3e, 0x52, 0xd6, 0xda, 0xa0, 0xbf, 0xae, 0x43, 0xd1, 0x6b, 0xfe, 0xd1, 0x36
+}, PRIVATE_KEY_SIZE };
+
+const ByteArray CKMInfoTest::derCode = {(uint8_t[])
+ {
+ 0x30, 0x82, 0x01, 0xd1, 0x30, 0x82, 0x01, 0x77, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
+ 0xd7, 0x56, 0x8c, 0xfc, 0x53, 0x18, 0xb0, 0xab, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce,
+ 0x3d, 0x04, 0x03, 0x02, 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
+ 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f,
+ 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04,
+ 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67,
+ 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x1e, 0x17, 0x0d, 0x31,
+ 0x35, 0x30, 0x33, 0x31, 0x32, 0x31, 0x32, 0x32, 0x35, 0x31, 0x31, 0x5a, 0x17, 0x0d, 0x31, 0x37,
+ 0x30, 0x33, 0x31, 0x31, 0x31, 0x32, 0x32, 0x35, 0x31, 0x31, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30,
+ 0x09, 0x06, 0x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xd9, 0x7d, 0xe6, 0xce, 0x2a, 0x59,
+ 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x5c, 0x0e, 0x30,
+ 0xa8, 0x8e, 0x7f, 0xc9, 0x02, 0xcd, 0xa8, 0xed, 0x0d, 0x1a, 0x1b, 0xd9, 0x7d, 0xe6, 0xce, 0x2a,
+ 0x59, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30,
+ 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x48, 0x00, 0x30, 0x45,
+ 0x02, 0x21, 0x00, 0xf6, 0x79, 0xed, 0x69, 0xd5, 0xe5, 0xba, 0x42, 0x14, 0xfc, 0xce, 0x47, 0xf1,
+ 0x61, 0x1c, 0x51, 0x11, 0x2b, 0xba, 0x04, 0x70, 0x56, 0x78, 0xaf, 0xa9, 0xa6, 0x98, 0x8f, 0x4b,
+ 0xa8, 0x11, 0x67, 0x02, 0x20, 0x3a, 0xdf, 0xf1, 0x74, 0xc9, 0x2f, 0xfb, 0x84, 0x46, 0xde, 0xbc,
+ 0x2d, 0xda, 0xe3, 0x05, 0xb4, 0x81, 0x31, 0x45, 0xf7, 0x3d, 0x71, 0x46, 0x07, 0xa7, 0xd8, 0xcb,
+ 0xae, 0x1e, 0x1b, 0x1c, 0x5a
+ }, CERT_LEN };
+
+const long CKMInfoTest::nextSN = 0x111111;
+
+const long CKMInfoTest::crlSerialNum = 0x22222;
+
+const ByteArray CKMInfoTest::caName = {(uint8_t[])
+{
+ 0x1a, 0x1a, 0x1a, 0x1d, 0xaa, 0x7f, 0xf0, 0xe4, 0xb2, 0x75, 0xd6, 0x4a, 0xf1, 0xd5, 0x14, 0x3f
+}, ISSUER_NAME_SIZE };
+
+//InitCRT test
+TEST(CKMInfoTest, InitCRT)
+{
+ ASSERT_EQ(PKI_SUCCESS, InitCRT());
+}
+
+//SaveCRT test
+TEST(CKMInfoTest, SaveCRT)
+{
+ ASSERT_EQ(PKI_SUCCESS, SaveCRT());
+}
+
+//InitCRL test
+TEST(CKMInfoTest, InitCRL)
+{
+ ASSERT_EQ(PKI_SUCCESS, InitCRL());
+}
+
+//SaveCRL test
+TEST(CKMInfoTest, SaveCRL)
+{
+ ASSERT_EQ(PKI_SUCCESS, SaveCRL());
+}
+
+//InitCKMInfo test
+TEST(CKMInfoTest, InitCKMInfo)
+{
+ ASSERT_EQ(PKI_SUCCESS, InitCKMInfo());
+}
+
+//SaveCKMInfo test
+TEST(CKMInfoTest, SaveCKMInfo)
+{
+ ASSERT_EQ(PKI_SUCCESS, SaveCKMInfo());
+}
+
+//SetGetNextSerialNumber test
+TEST(CKMInfoTest, SetGetNextSerialNumber)
+{
+ ASSERT_EQ(PKI_SUCCESS, SetNextSerialNumber(CKMInfoTest::nextSN));
+ long nSn = 0;
+ ASSERT_EQ(PKI_SUCCESS, GetNextSerialNumber(&nSn));
+ EXPECT_TRUE(nSn == CKMInfoTest::nextSN);
+}
+
+//SetGetCAPrivateKey test
+TEST(CKMInfoTest, SetGetCAPrivateKey)
+{
+ ASSERT_EQ(PKI_SUCCESS, SetCAPrivateKey(&CKMInfoTest::privateKey));
+
+ ByteArray privateKey = {0,0};
+ privateKey.data = (uint8_t*)OICMalloc(PRIVATE_KEY_SIZE);
+ privateKey.len = PRIVATE_KEY_SIZE;
+ EXPECT_TRUE(NULL != privateKey.data);
+
+ EXPECT_EQ(PKI_SUCCESS, GetCAPrivateKey(&privateKey));
+ EXPECT_TRUE(0 == memcmp(CKMInfoTest::privateKey.data, privateKey.data, PRIVATE_KEY_SIZE));
+ OICFree(privateKey.data);
+}
+
+//SetGetCAPublicKey test
+TEST(CKMInfoTest, SetGetCAPublicKey)
+{
+ ASSERT_EQ(PKI_SUCCESS, SetCAPublicKey(&CKMInfoTest::publicKey));
+
+ ByteArray publicKey = {0,0};
+ publicKey.data = (uint8_t*)OICMalloc(PUBLIC_KEY_SIZE);
+ publicKey.len = PUBLIC_KEY_SIZE;
+ EXPECT_TRUE(NULL != publicKey.data);
+
+ EXPECT_EQ(PKI_SUCCESS, GetCAPublicKey(&publicKey));
+ EXPECT_TRUE(0 == memcmp(CKMInfoTest::publicKey.data, publicKey.data, PUBLIC_KEY_SIZE));
+ OICFree(publicKey.data);
+}
+
+//SetGetCAName test
+TEST(CKMInfoTest, SetGetCAName)
+{
+ ASSERT_EQ(PKI_SUCCESS, SetCAName(&CKMInfoTest::caName));
+
+ ByteArray caName = {0,0};
+ caName.data = (uint8_t*)OICMalloc(ISSUER_NAME_SIZE);
+ caName.len = ISSUER_NAME_SIZE;
+ EXPECT_TRUE(NULL != caName.data);
+
+ EXPECT_EQ(PKI_SUCCESS, GetCAName(&caName));
+ EXPECT_TRUE(0 == memcmp(CKMInfoTest::caName.data, caName.data, ISSUER_NAME_SIZE));
+ OICFree(caName.data);
+}
+
+//SetGetCKMInfo test
+TEST(CKMInfoTest, SetGetCKMInfo)
+{
+ ASSERT_EQ(PKI_SUCCESS, SetCKMInfo(CKMInfoTest::nextSN, CKMInfoTest::crlSerialNum,
+ &CKMInfoTest::privateKey, &CKMInfoTest::publicKey, &CKMInfoTest::caName));
+
+ long nSn = 0;
+ long serialNum = 0;
+
+ ByteArray publicKey = {0,0};
+ publicKey.data = (uint8_t*)OICMalloc(PUBLIC_KEY_SIZE);
+ publicKey.len = PUBLIC_KEY_SIZE;
+ EXPECT_TRUE(NULL != publicKey.data);
+
+ ByteArray privateKey = {0,0};
+ privateKey.data = (uint8_t*)OICMalloc(PRIVATE_KEY_SIZE);
+ privateKey.len = PRIVATE_KEY_SIZE;
+ EXPECT_TRUE(NULL != privateKey.data);
+
+ ByteArray caName = {0,0};
+ caName.data = (uint8_t*)OICMalloc(ISSUER_NAME_SIZE);
+ caName.len = ISSUER_NAME_SIZE;
+ EXPECT_TRUE(NULL != caName.data);
+
+ EXPECT_EQ(PKI_SUCCESS, GetCKMInfo(&nSn, &serialNum,
+ &privateKey, &publicKey, &caName));
+
+ EXPECT_TRUE(nSn == CKMInfoTest::nextSN);
+ EXPECT_TRUE(serialNum == CKMInfoTest::crlSerialNum);
+ EXPECT_TRUE(0 == memcmp(CKMInfoTest::privateKey.data, privateKey.data, PRIVATE_KEY_SIZE));
+ EXPECT_TRUE(0 == memcmp(CKMInfoTest::publicKey.data, publicKey.data, PUBLIC_KEY_SIZE));
+ EXPECT_TRUE(0 == memcmp(CKMInfoTest::caName.data, caName.data, ISSUER_NAME_SIZE));
+
+ OICFree(publicKey.data);
+ OICFree(privateKey.data);
+ OICFree(caName.data);
+}
+
+//SetGetCACertificate test
+TEST(CKMInfoTest, SetGetCACertificate)
+{
+ ASSERT_EQ(PKI_SUCCESS, SetCACertificate(&CKMInfoTest::derCode));
+
+ ByteArray der = {0,0};
+ der.data = (uint8_t*)OICMalloc(CERT_LEN);
+ der.len = CERT_LEN;
+ EXPECT_TRUE(NULL != der.data);
+ EXPECT_EQ(PKI_SUCCESS, GetCACertificate(&der));
+ EXPECT_TRUE(0 == memcmp(CKMInfoTest::derCode.data, der.data, CERT_LEN));
+
+ OICFree(der.data);
+}
+
+//SetGetCRLSerialNumber test
+TEST(CKMInfoTest, SetGetCRLSerialNumber)
+{
+ ASSERT_EQ(PKI_SUCCESS, SetCRLSerialNumber(CKMInfoTest::crlSerialNum));
+ long serialNum = 0;
+ ASSERT_EQ(PKI_SUCCESS, GetCRLSerialNumber(&serialNum));
+ EXPECT_TRUE(serialNum == CKMInfoTest::crlSerialNum);
+}
+
+//SetGetNumberOfRevoked test
+TEST(CKMInfoTest, SetGetNumberOfRevoked)
+{
+ const long NUM_OF_REVOKED = 5;
+ ASSERT_EQ(PKI_SUCCESS, SetNumberOfRevoked(NUM_OF_REVOKED));
+ long numRev = 0;
+ ASSERT_EQ(PKI_SUCCESS, GetNumberOfRevoked(&numRev));
+ EXPECT_TRUE(numRev == NUM_OF_REVOKED);
+}
+
+//SetCertificateRevocationList test
+TEST(CKMInfoTest, SetCertificateRevocationList)
+{
+ ASSERT_EQ(PKI_SUCCESS, SetCertificateRevocationList(&CKMInfoTest::derCode));
+}
+
+//CloseCKMInfo test
+TEST(CKMInfoTest, CloseCKMInfo)
+{
+ ASSERT_EQ(PKI_SUCCESS, CloseCKMInfo());
+}
return fopen(CKMI_PS_FILE_NAME, mode);
}
+#define STRINGIZE2(x) #x
+#define STRINGIZE(x) STRINGIZE2(x)
+
+static char* resolve_file_path(const char* filename )
+{
+ int len = strlen(STRINGIZE(SECURITY_BUILD_UNITTEST_DIR)) + strlen(filename) + 1;
+ char *filepath = (char *)OICCalloc(1, len);
+
+ if (!filepath)
+ {
+ printf("filepath memory allocation failed. \n");
+ return NULL;
+ }
+ int ret = snprintf(filepath, len, "%s%s", STRINGIZE(SECURITY_BUILD_UNITTEST_DIR), filename);
+ return filepath;
+}
+
void SetPersistentHandler(OCPersistentStorage *ps)
{
if(ps)
TEST(OpenSSLCompatibility, verifyOpenSslCertSign)
{
struct stat st;
+ char *fpath = NULL;
uint8_t crtData[ISSUER_MAX_CERT_SIZE] = {0};
uint8_t pubKeyData[PUBLIC_KEY_SIZE] = {0};
ByteArray crtDer = BYTE_ARRAY_INITIALIZER;
pubKey.len = sizeof(pubKeyData);
//open file
- int fileCert = open("01.der", O_RDONLY);
+ fpath = resolve_file_path("/01.der");
+ int fileCert = open(fpath, O_RDONLY);
+ OICFree(fpath);
+
ASSERT_TRUE(fileCert != -1);
//get status
ASSERT_TRUE(fstat(fileCert, &st) == 0);
close(fileCert);
//open file
- int fileKey = open("capub.der", O_RDONLY);
+ fpath = resolve_file_path("/capub.der");
+ int fileKey = open(fpath, O_RDONLY);
+ OICFree(fpath);
ASSERT_TRUE(fileKey != -1);
//get status
ASSERT_TRUE(fstat(fileKey, &st) == 0);
//test parsing of certificate chain generated by OpenSSL
TEST(CertificateChain, LoadCertificateChain)
{
+ char* fpath = NULL;
ByteArray crtChainDer[MAX_CHAIN_LEN] = {{0,0},};
CertificateX509 crtChain[MAX_CHAIN_LEN] = {{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},};
ByteArray msg = BYTE_ARRAY_INITIALIZER;
uint8_t chainLength;
- FILE *file = fopen("cert_chain.dat", "rb");
-
+ fpath = resolve_file_path("/cert_chain.dat");
+ FILE *file = fopen(fpath, "rb");
+ OICFree(fpath);
ASSERT_TRUE(file != NULL);
while (!feof (file))
//test checking CA certificate generated by OpenSSL
TEST(OpenSSLCompatibility, testOpenSSLCertificate)
{
+ char* fpath = NULL;
struct stat st;
ByteArray crtDer = BYTE_ARRAY_INITIALIZER;
uint8_t crtData[ISSUER_MAX_CERT_SIZE] = {0};
crtDer.len = sizeof(crtData);
//open file
- int fd = open("cacert.der", O_RDONLY);
+ fpath = resolve_file_path("/cacert.der");
+ int fd = open(fpath, O_RDONLY);
+ OICFree(fpath);
ASSERT_TRUE(fd != -1);
//get status
ASSERT_TRUE(fstat(fd, &st) == 0);
ByteArray msg = BYTE_ARRAY_INITIALIZER;
uint8_t chainLength;
- const char* chainPath = {"chain.der"};
+ char* chainPath = resolve_file_path("/chain.der");
FILE *fileChain = fopen(chainPath, "rb");
+ OICFree(chainPath);
ASSERT_TRUE(fileChain != NULL);
//get the length
caPubKey.len = sizeof(pubKeyData);
//open file
- int fileKey = open("capub.der", O_RDONLY);
+ char* fpath = resolve_file_path("/capub.der");
+ int fileKey = open(fpath, O_RDONLY);
+ OICFree(fpath);
ASSERT_TRUE(fileKey != -1);
//get status
ASSERT_TRUE(fstat(fileKey, &st) == 0);
EXPECT_NE((void *)NULL, GetCrl());
OICFree(defaultCrl);
+}
+//CKMGetCRL test
+TEST_F(PKITest, CKMGetCRL)
+{
+ const size_t CRL_MAX_SIZE = 1024;
+ ByteArray certificateRevocationList;
+ certificateRevocationList.data = (uint8_t*)OICCalloc(1,CRL_MAX_SIZE);
+ certificateRevocationList.len = CRL_MAX_SIZE;
+ EXPECT_EQ(PKI_SUCCESS, CKMGetCRL(&certificateRevocationList));
+ OICFree(certificateRevocationList.data);
+}
+//CKMSetCAInfo test
+TEST_F(PKITest, CKMSetCAInfo)
+{
+ ByteArray rootName = BYTE_ARRAY_INITIALIZER;
+ rootName.data = (uint8_t *)"ROOT2";
+ rootName.len = strlen((char *)rootName.data);
+ const long serNum = 100;
+ EXPECT_EQ(PKI_SUCCESS, CKMSetCAInfo(serNum, rootName));
}
int main(int argc, char **argv)