summaryrefslogtreecommitdiff
path: root/src/vcore/pkcs12.cpp
blob: 57340049de1302d2485c30ea7c4786686d656f64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
/**
 * Copyright (c) 2016 Samsung Electronics Co., Ltd 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
 *
 *    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.
 */
/*
 * @file        pkcs12.cpp
 * @author      Jacek Migacz (j.migacz@samsung.com)
 * @author      Kyungwook Tak (k.tak@samsung.com)
 * @version     1.0
 * @brief       PKCS#12 container manipulation routines.
 */

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <string>
#include <memory>
#include <functional>

#include <openssl/err.h>
#include <openssl/pkcs12.h>
#include <openssl/sha.h>
#include <openssl/x509.h>
#include <openssl/pem.h>

#include "dpl/log/log.h"
#include "cert-svc/cerror.h"

#include "vcore/Certificate.h"
#include "vcore/Client.h"
#include "vcore/pkcs12.h"

#define SYSCALL(call) while (((call) == -1) && (errno == EINTR))

namespace {

static const std::string START_CERT    = "-----BEGIN CERTIFICATE-----";
static const std::string END_CERT      = "-----END CERTIFICATE-----";
static const std::string START_TRUSTED = "-----BEGIN TRUSTED CERTIFICATE-----";
static const std::string END_TRUSTED   = "-----END TRUSTED CERTIFICATE-----";
static const std::string START_KEY     = "-----BEGIN PRIVATE KEY-----";
static const std::string END_KEY       = "-----END PRIVATE KEY-----";

using ValidationCore::CertificatePtr;
using ValidationCore::Certificate;

using FileUniquePtr      = std::unique_ptr<FILE, std::function<int(FILE *)>>;
using BioUniquePtr       = std::unique_ptr<BIO, std::function<void(BIO *)>>;
using PKEYUniquePtr      = std::unique_ptr<EVP_PKEY, std::function<void(EVP_PKEY *)>>;
using X509UniquePtr      = std::unique_ptr<X509, std::function<void(X509 *)>>;
using X509StackUniquePtr = std::unique_ptr<STACK_OF(X509), std::function<void(STACK_OF(X509) *)>>;

void X509_stack_free(STACK_OF(X509) *stack)
{
	sk_X509_free(stack);
}

inline bool hasStore(CertStoreType types, CertStoreType type)
{
	return (types & type) != 0;
}

inline CertStoreType nextStore(CertStoreType type)
{
	switch (type) {
	case NONE_STORE:
		return VPN_STORE;

	case VPN_STORE:
		return WIFI_STORE;

	case WIFI_STORE:
		return EMAIL_STORE;

	case EMAIL_STORE:
		return SYSTEM_STORE;

	case SYSTEM_STORE:
		return NONE_STORE;

	default:
		return NONE_STORE;
	}
}

CertSvcStoreCertList *createStoreListNode(const std::string &gname, const std::string &title,
										  CertStoreType storeType)
{
	CertSvcStoreCertList *node = (CertSvcStoreCertList *)malloc(sizeof(CertSvcStoreCertList));

	if (node == NULL)
		return NULL;

	node->gname = strdup(gname.c_str());
	node->title = strdup(title.c_str());
	node->status = ENABLED;
	node->storeType = storeType;
	node->next = NULL;

	if (node->gname == NULL || node->title == NULL) {
		free(node->gname);
		free(node->title);
		free(node);
		return NULL;
	}

	return node;
}

void destroyStoreList(CertSvcStoreCertList **certList, size_t *length)
{
	if (certList == NULL || length == NULL) {
		return;
	}

	CertSvcStoreCertList *list = *certList;

	while (list) {
		CertSvcStoreCertList *next = list->next;
		free(list->gname);
		free(list->title);
		free(list);
		list = next;
	}

	*length = 0;
}

void addStoreListNode(CertSvcStoreCertList **list, CertSvcStoreCertList *node)
{
	node->next = *list;
	*list = node;
}

int appendStoreListNode(CertSvcStoreCertList **certList, size_t *length,
						 const std::string &gname, const std::string &alias,
						 CertStoreType storeType)
{
	if (certList == NULL || length == NULL)
		return CERTSVC_SUCCESS;

	CertSvcStoreCertList *node = createStoreListNode(gname, alias, storeType);
	if (node == NULL) {
		return CERTSVC_BAD_ALLOC;
	}

	addStoreListNode(certList, node);
	(*length)++;

	return CERTSVC_SUCCESS;
}

std::string generateGname(void)
{
	int generator;
	int64_t random;
	SHA_CTX ctx;
	unsigned char d[SHA_DIGEST_LENGTH];
	int result;
	char *gname = NULL;
	SYSCALL(generator = open("/dev/urandom", O_RDONLY));

	if (generator == -1)
		return std::string();

	SYSCALL(result = read(generator, &random, sizeof(random)));

	if (result == -1) {
		SYSCALL(close(generator));
		return std::string();
	}

	SYSCALL(result = close(generator));

	if (result == -1)
		return std::string();

	SHA1_Init(&ctx);
	SHA1_Update(&ctx, &random, sizeof(random));
	SHA1_Final(d, &ctx);
	result = asprintf(&gname,
					  "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
					  "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
					  d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], d[8], d[9],
					  d[10], d[11], d[12], d[13], d[14], d[15], d[16], d[17], d[18], d[19]);

	if (result == -1)
		return std::string();

	std::string ret(gname);
	free(gname);
	return ret;
}

std::string getCommonName(CertType type, const std::string &cert)
{
	BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);

	if (bio.get() == NULL) {
		LogError("Failed to allocate memory.");
		return std::string();
	}

	auto readCount = BIO_write(bio.get(), (const void *)cert.data(), (int)cert.length());

	if (readCount < 1) {
		LogError("Failed to load cert into bio.");
		return std::string();
	}

	X509 *x509 = NULL;

	switch (type) {
	case P12_TRUSTED:
	case P12_INTERMEDIATE:
		x509 = PEM_read_bio_X509_AUX(bio.get(), NULL, 0, NULL);
		break;

	default:
		x509 = PEM_read_bio_X509(bio.get(), NULL, 0, NULL);
		break;
	}

	if (x509 == NULL) {
		LogError("Failed to create x509 structure.");
		return std::string();
	}

	X509UniquePtr x509Ptr(x509, X509_free);
	const char *subject_c = X509_NAME_oneline(x509->cert_info->subject, NULL, 0);

	if (subject_c == NULL) {
		LogError("Failed to parse x509 structure");
		return std::string();
	}

	return std::string(subject_c);
}

/*
 *  column           / common name / associated gname / prikey gname /
 *  PEM_CRT          : common name / gname            / none         /
 *  P12_END_USER     : alias       / gname            / prikey gname /
 *  P12_TRUSTED      : common name / end cert gname   / none         /
 *  P12_INTERMEDIATE : common name / end cert gname   / none         /
 */

int installPKEY(CertStoreType storeType,
				const std::string &key,
				const std::string &gname)
{
	return vcore_client_install_certificate_to_store(
			   storeType,
			   gname.c_str(),
			   NULL,
			   NULL,
			   NULL,
			   key.c_str(),
			   key.length(),
			   P12_PKEY);
}

int installEndCert(CertStoreType storeType,
				   const std::string &cert,
				   const std::string &alias,
				   const std::string &gname,
				   const std::string &prikeyGname)
{
	return vcore_client_install_certificate_to_store(
			   storeType,
			   gname.c_str(),
			   alias.c_str(),
			   prikeyGname.c_str(),
			   gname.c_str(),
			   cert.c_str(),
			   cert.length(),
			   P12_END_USER);
}

int installChainCert(CertStoreType storeType,
					 const std::string &cert,
					 const std::string &gname,
					 const std::string &endCertGname,
					 const std::string &commonName,
					 CertType type)
{
	return vcore_client_install_certificate_to_store(
			   storeType,
			   gname.c_str(),
			   commonName.c_str(),
			   NULL,
			   endCertGname.c_str(),
			   cert.c_str(),
			   cert.length(),
			   type);
}
int installCert(CertStoreType storeType,
				const std::string &cert,
				const std::string &gname,
				const std::string &alias)
{
	return vcore_client_install_certificate_to_store(
			   storeType,
			   gname.c_str(),
			   alias.c_str(),
			   NULL,
			   NULL,
			   cert.c_str(),
			   cert.length(),
			   PEM_CRT);
}

std::string readFromFile(const std::string &path)
{
	FILE *fp = NULL;

	if ((fp = fopen(path.c_str(), "rb")) == NULL) {
		LogError("Fail to open file for reading : " << path);
		return std::string();
	}

	FileUniquePtr filePtr(fp, fclose);
	fseek(fp, 0L, SEEK_END);
	int len = ftell(fp);

	if (len <= 0 || len == INT_MAX) {
		LogError("Fail to get proper certificate.");
		return std::string();
	}

	rewind(fp);
	char *content = (char *)malloc(sizeof(char) * (len + 1));

	if (content == NULL) {
		LogError("Fail to allocate memory");
		return std::string();
	}

	memset(content, 0x00, len + 1);
	size_t readLen = fread(content, sizeof(char), (size_t)len, fp);

	if (readLen != (size_t)len) {
		LogError("Fail to read file : " << path);
		free(content);
		return std::string();
	}

	content[len] = '\0';
	std::string ret(content);
	free(content);
	return ret;
}

std::string parseCRT(const std::string &cert)
{
	size_t from = 0;
	size_t to = 0;
	size_t tailLen = 0;
	from = cert.find(START_CERT);
	to = cert.find(END_CERT);
	tailLen = END_CERT.length();

	if (from == std::string::npos || to == std::string::npos || from > to) {
		from = cert.find(START_TRUSTED);
		to = cert.find(END_TRUSTED);
		tailLen = END_TRUSTED.length();
	}

	if (from == std::string::npos || to == std::string::npos || from > to)
		return std::string();

	return std::string(cert, from, to - from + tailLen);
}

#define _CERT_SVC_VERIFY_PKCS12
int verify_cert_details(X509 *cert, STACK_OF(X509) *certv)
{
	int result = CERTSVC_SUCCESS;
	char *pSubject = NULL;
	char *pIssuerName = NULL;
	X509_STORE_CTX *cert_ctx = NULL;
	X509_STORE *cert_store = NULL;
	int res = 0;
#ifdef _CERT_SVC_VERIFY_PKCS12

	if (certv == NULL) {
		pSubject = X509_NAME_oneline(cert->cert_info->subject, NULL, 0);

		if (!pSubject) {
			LogError("Failed to get subject name");
			result = CERTSVC_FAIL;
			goto free_memory;
		}

		pIssuerName = X509_NAME_oneline(cert->cert_info->issuer, NULL, 0);

		if (!pIssuerName) {
			LogError("Failed to get issuer name");
			result = CERTSVC_FAIL;
			goto free_memory;
		}

		if (strcmp((const char *)pSubject, (const char *)pIssuerName) == 0) {
			/*self signed.. */
			EVP_PKEY *pKey = NULL;
			pKey = X509_get_pubkey(cert);

			if (!pKey) {
				LogError("Failed to get public key");
				result = CERTSVC_FAIL;
				goto free_memory;
			}

			if (X509_verify(cert, pKey) <= 0) {
				LogError("P12 verification failed");
				EVP_PKEY_free(pKey);
				result = CERTSVC_FAIL;
				goto free_memory;
			}

			LogDebug("P12 verification Success");
			EVP_PKEY_free(pKey);
		} else {
			cert_store = X509_STORE_new();

			if (!cert_store) {
				LogError("Memory allocation failed");
				result = CERTSVC_FAIL;
				goto free_memory;
			}

			res = X509_STORE_load_locations(cert_store, NULL, TZ_SYS_CA_CERTS);

			if (res != 1) {
				LogError("P12 load certificate store failed");
				X509_STORE_free(cert_store);
				result = CERTSVC_FAIL;
				goto free_memory;
			}

			res = X509_STORE_set_default_paths(cert_store);

			if (res != 1) {
				LogError("P12 load certificate store path failed");
				X509_STORE_free(cert_store);
				result = CERTSVC_FAIL;
				goto free_memory;
			}

			/* initialise store and store context */
			cert_ctx = X509_STORE_CTX_new();

			if (cert_ctx == NULL) {
				LogError("Memory allocation failed");
				result = CERTSVC_FAIL;
				goto free_memory;
			}

			/* construct store context */
			if (!X509_STORE_CTX_init(cert_ctx, cert_store, cert, NULL)) {
				LogError("Memory allocation failed");
				result = CERTSVC_FAIL;
				goto free_memory;
			}

#ifdef P12_VERIFICATION_NEEDED
			res = X509_verify_cert(cert_ctx);

			if (res != 1) {
				LogError("P12 verification failed");
				result = CERTSVC_FAIL;
				goto free_memory;
			}

			LogDebug("P12 verification Success");
#endif
		}
	} else if (certv != NULL) {
		/* Cert Chain */
		cert_store = X509_STORE_new();

		if (!cert_store) {
			LogError("Memory allocation failed");
			result = CERTSVC_FAIL;
			goto free_memory;
		}

		res = X509_STORE_load_locations(cert_store, NULL, TZ_SYS_CA_CERTS);

		if (res != 1) {
			LogError("P12 load certificate store failed");
			result = CERTSVC_FAIL;
			goto free_memory;
		}

		res = X509_STORE_set_default_paths(cert_store);

		if (res != 1) {
			LogError("P12 load certificate path failed");
			result = CERTSVC_FAIL;
			goto free_memory;
		}

		/* initialise store and store context */
		cert_ctx = X509_STORE_CTX_new();

		if (cert_ctx == NULL) {
			LogError("Memory allocation failed");
			result = CERTSVC_FAIL;
			goto free_memory;
		}

		/* construct store context */
		if (!X509_STORE_CTX_init(cert_ctx, cert_store, cert, NULL)) {
			LogError("Memory allocation failed");
			result = CERTSVC_FAIL;
			goto free_memory;
		}

		X509_STORE_CTX_trusted_stack(cert_ctx, certv);
#ifdef P12_VERIFICATION_NEEDED
		res = X509_verify_cert(cert_ctx);

		if (res != 1) {
			LogError("P12 verification failed");
			result = CERTSVC_FAIL;
			goto free_memory;
		}

		LogDebug("P12 verification Success");
#endif
	}

#endif //_CERT_SVC_VERIFY_PKCS12
free_memory:

	if (cert_store != NULL)
		X509_STORE_free(cert_store);

	if (cert_ctx)
		X509_STORE_CTX_free(cert_ctx);

	free(pSubject);
	free(pIssuerName);
	return result;
}

enum class OsslType : int {
	PKEY = 1,
	X509,
	X509AUX
};

std::string osslToPEM(OsslType type, void *data)
{
	std::vector<char> buf(4096);
	BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);

	if (bio.get() == NULL)
		return std::string();

	switch (type) {
	case OsslType::PKEY:
		PEM_write_bio_PrivateKey(bio.get(), static_cast<EVP_PKEY *>(data), NULL, NULL, 0, NULL, NULL);
		break;

	case OsslType::X509:
		PEM_write_bio_X509(bio.get(), static_cast<X509 *>(data));
		break;

	case OsslType::X509AUX:
		PEM_write_bio_X509_AUX(bio.get(), static_cast<X509 *>(data));
		break;

	default:
		break;
	}

	int size = BIO_read(bio.get(), buf.data(), 4096);

	if (size <= 0)
		return std::string();

	buf[size] = '\0';
	return std::string(buf.data());
}

int extractPkcs12(const std::string &path,
				  const std::string &password,
				  PKEYUniquePtr &keyPtr,
				  X509UniquePtr &certPtr,
				  X509StackUniquePtr &certvPtr)
{
	FILE *stream = NULL;

	if ((stream = fopen(path.c_str(), "rb")) == NULL) {
		LogError("Unable to open the file for reading : " << path);
		return CERTSVC_IO_ERROR;
	}

	PKCS12 *container = d2i_PKCS12_fp(stream, NULL);
	fclose(stream);

	if (container == NULL) {
		LogError("Failed to parse the input file passed.");
		return CERTSVC_FAIL;
	}

	EVP_PKEY *key = NULL;
	X509 *cert = NULL;
	STACK_OF(X509) *certv = NULL;
	int result = PKCS12_parse(container, password.c_str(), &key, &cert, &certv);
	PKCS12_free(container);

	if (result != 1) {
		unsigned long e = ERR_get_error();
		char buf[1024];
		ERR_error_string_n(e, buf, 1023);
		LogError("Failed to parse the file passed. openssl err: " << buf);
		return CERTSVC_FAIL;
	}

	keyPtr.reset(key);
	certPtr.reset(cert);
	certvPtr.reset(certv);
	return CERTSVC_SUCCESS;
}

void rollbackStore(CertStoreType storeTypes, const std::string &endCertName)
{
	for (CertStoreType storeType = VPN_STORE; storeType < SYSTEM_STORE;
			storeType = nextStore(storeType)) {
		if (!hasStore(storeTypes, storeType))
			continue;

		char **certChainName = NULL;
		size_t ncerts = 0;
		int result = vcore_client_load_certificates_from_store(storeType, endCertName.c_str(),
					 &certChainName, &ncerts);

		if (result != CERTSVC_SUCCESS) {
			LogError("Unable to load certificates from store. result : " << result);
			continue;
		}

		for (size_t i = 0; i < ncerts; i++) {
			if (certChainName[i] == NULL)
				continue;

			vcore_client_delete_certificate_from_store(storeType, certChainName[i]);
			free(certChainName[i]);
		}

		vcore_client_delete_certificate_from_store(storeType, endCertName.c_str());
	}
}

int insertToStore(CertStoreType storeTypes,
				  const std::string &alias,
				  const std::string &prikeyName,
				  const std::string &prikeyBuffer,
				  const std::string &endCertName,
				  const std::string &endCertBuffer,
				  const std::vector<std::string> &certChainName,
				  const std::vector<std::string> &certChainBuffer,
				  CertSvcStoreCertList **certList,
				  size_t *length)
{
	size_t ncerts = certChainName.size();

	for (CertStoreType storeType = VPN_STORE; storeType < SYSTEM_STORE;
			storeType = nextStore(storeType)) {
		if (!hasStore(storeTypes, storeType))
			continue;

		LogDebug("Processing store type : " << storeType);
		int result = installPKEY(storeType, prikeyBuffer, prikeyName);

		if (result != CERTSVC_SUCCESS) {
			LogError("Failed to store the private key contents. result : " << result);
			return result;
		}

		result = installEndCert(storeType, endCertBuffer, alias, endCertName, prikeyName);

		if (result != CERTSVC_SUCCESS) {
			LogError("Failed to install the end user certificate. result : " << result);
			return result;
		}

		int res = appendStoreListNode(certList, length, endCertName, alias, storeType);
		if (res != CERTSVC_SUCCESS) {
			LogError("Failed to append store list node.");
			return result;
		}

		for (size_t i = 0; i < ncerts; i++) {
			CertType type = (i == ncerts - 1) ? P12_TRUSTED : P12_INTERMEDIATE;

			std::string commonName = getCommonName(type, certChainBuffer[i]);

			result = installChainCert(storeType, certChainBuffer[i], certChainName[i], endCertName,
										  commonName, type);

			if (result != CERTSVC_SUCCESS) {
				LogError("Failed to install the ca certificates. result : " << result);
				return result;
			}

			int res = appendStoreListNode(certList, length, certChainName[i], commonName, storeType);
			if (res != CERTSVC_SUCCESS) {
				LogError("Failed to append store list node.");
				return result;
			}
		}
	}

	LogDebug("Success to insert extracted pkcs12 data to db");
	return CERTSVC_SUCCESS;
}

int insertToStorePEM(CertStoreType storeTypes, const std::string &path, const std::string &gname,
					 const std::string &alias, CertSvcStoreCertList **certList, size_t *length)
{
	std::string content = readFromFile(path);

	if (content.empty()) {
		LogError("Failed to read the file : " << path);
		return CERTSVC_IO_ERROR;
	}

	std::string parsed = parseCRT(content);

	if (parsed.empty()) {
		LogError("Failed to parse CRT : " << path);
		return CERTSVC_FAIL;
	}

	for (CertStoreType storeType = VPN_STORE; storeType < SYSTEM_STORE;
			storeType = nextStore(storeType)) {
		if (!hasStore(storeTypes, storeType))
			continue;

		int result = installCert(storeType, parsed, gname, alias);

		if (result != CERTSVC_SUCCESS) {
			LogError("Failed to install PEM/CRT to db store : " << storeType << " result : " << result);
			rollbackStore(storeTypes, gname);
			return result;
		}

		int res = appendStoreListNode(certList, length, gname, alias, storeType);
		if (res != CERTSVC_SUCCESS) {
			rollbackStore(storeTypes, gname);
			LogError("Failed to append store list node.");
			return result;
		}

		LogDebug("Success to install PEM/CRT to db store : " << storeType);
	}

	LogDebug("Success to install PEM/CRT to db stores : " << storeTypes);
	return CERTSVC_SUCCESS;
}

} // namespace anonymous


int pkcs12_import_from_file_to_store(CertStoreType storeTypes,
									 const char *_path,
									 const char *_password,
									 const char *_alias,
									 CertSvcStoreCertList **certList,
									 size_t *length)
{
	int result = 0;

	if (_alias == NULL || _path == NULL || strlen(_path) < 4) {
		LogError("Invalid input parameter.");
		return CERTSVC_WRONG_ARGUMENT;
	}

	std::string path(_path);
	std::string alias(_alias);
	std::string password;

	if (_password != NULL)
		password = std::string(_password);

	LogDebug("pkcs12_import_from_file_to_store start. path[" << path << "] password[" << password <<
			 "] alias[" << alias << "]");

	if (storeTypes & SYSTEM_STORE) {
		LogError("User should not install any form of certificates in SYSTEM_STORE.");
		return CERTSVC_INVALID_STORE_TYPE;
	}

	/*
	 * Installs CRT and PEM files.
	 * We will passing NULL for private_key_gname and associated_gname parameter
	 * in installFilePEM(). Which means that there is no private key involved
	 * in the certificate which we are installing and there are no other
	 * certificates related with the current certificate which is installed
	 */
	std::string suffix = path.substr(path.length() - 4, 4);

	if (strcasecmp(suffix.c_str(), ".pem") == 0 || strcasecmp(suffix.c_str(), ".crt") == 0) {
		std::string gnamePEM = generateGname();
		result = insertToStorePEM(storeTypes, path, gnamePEM, alias, certList, length);

		if (result != CERTSVC_SUCCESS) {
			destroyStoreList(certList, length);
			LogError("Failed to install PEM/CRT file to store. gname : " << gnamePEM << " result : " << result);
		}

		return result;;
	}

	LogDebug("Convert ossl type to string start");
	/* 0. extract pkcs12 data from file */
	PKEYUniquePtr key(nullptr, EVP_PKEY_free);
	X509UniquePtr cert(nullptr, X509_free);
	X509StackUniquePtr certv(nullptr, X509_stack_free);
	result = extractPkcs12(path, password, key, cert, certv);

	if (result != CERTSVC_SUCCESS) {
		LogError("Failed to extract pkcs12 file. result : " << result);
		return result;
	}

	LogDebug("extract pkcs12 to unique ptr success");
	result = verify_cert_details(cert.get(), certv.get());

	if (result != CERTSVC_SUCCESS) {
		LogError("Failed to verify p12 certificate. result : " << result);
		return result;
	}

	/* 1. handling private key */
	std::string prikeyName = generateGname();
	std::string prikeyBuffer = osslToPEM(OsslType::PKEY, key.get());

	if (prikeyName.empty() || prikeyBuffer.empty()) {
		LogError("Failed to transform pkey to PEM. result : " << result);
		return CERTSVC_FAIL;
	}

	LogDebug("Convert pkey to string success");
	/* 2. handling end user certificate */
	std::string endCertName = generateGname();
	std::string endCertBuffer = osslToPEM(OsslType::X509, cert.get());

	if (endCertName.empty() || endCertBuffer.empty()) {
		LogError("Failed to transform x509 to PEM. result : " << result);
		return CERTSVC_FAIL;
	}

	LogDebug("Convert end cert to string success");
	/* 3. handling certificate chain */
	std::vector<std::string> certChainName;
	std::vector<std::string> certChainBuffer;
	int ncerts = certv ? sk_X509_num(certv.get()) : 0;

	for (int i = 0; i < ncerts; i++) {
		std::string tempName = generateGname();
		std::string tempBuffer = osslToPEM(OsslType::X509AUX, sk_X509_value(certv.get(), i));

		if (tempName.empty() || tempBuffer.empty()) {
			LogError("Failed to transform x509 AUX to PEM");
			return CERTSVC_FAIL;
		}

		certChainName.push_back(std::move(tempName));
		certChainBuffer.push_back(std::move(tempBuffer));
	}

	LogDebug("Convert cert chain to string success");
	/* 4. insert extracted pkcs12 data to db */
	result = insertToStore(storeTypes,
						   alias,
						   prikeyName,
						   prikeyBuffer,
						   endCertName,
						   endCertBuffer,
						   certChainName,
						   certChainBuffer,
						   certList,
						   length);

	if (result != CERTSVC_SUCCESS) {
		destroyStoreList(certList, length);
		rollbackStore(storeTypes, endCertName);
	}

	LogDebug("Success to import pkcs12 to store");
	return result;
}

int pkcs12_has_password(const char *filepath, int *passworded)
{
	if (filepath == NULL || passworded == NULL)
		return CERTSVC_WRONG_ARGUMENT;

	FILE *stream;

	if ((stream = fopen(filepath, "rb")) == NULL)
		return CERTSVC_IO_ERROR;

	PKCS12 *container = d2i_PKCS12_fp(stream, NULL);
	fclose(stream);

	if (container == NULL)
		return CERTSVC_FAIL;

	EVP_PKEY *pkey = NULL;
	X509 *cert = NULL;
	int result = PKCS12_parse(container, NULL, &pkey, &cert, NULL);
	PKCS12_free(container);

	if (pkey != NULL)
		EVP_PKEY_free(pkey);

	if (cert != NULL)
		X509_free(cert);

	if (result != 1) {
		unsigned long e = ERR_get_error();
		if (ERR_GET_REASON(e) == PKCS12_R_MAC_VERIFY_FAILURE) {
			LogInfo("verify failed without password. file(" << filepath << ") is password-protected.");
			*passworded = CERTSVC_TRUE;
		} else {
			char buf[1024];
			ERR_error_string_n(e, buf, 1023);
			LogError("Error on PKCS12_pasre file(" << filepath << "): " << buf);
			return CERTSVC_FAIL;
		}
	} else {
		*passworded = CERTSVC_FALSE;
	}

	return CERTSVC_SUCCESS;
}