summaryrefslogtreecommitdiff
path: root/src/manager/service/CryptoService.cpp
blob: 39908a34229f9d5df6e4fbc6c479b5ad4aab026e (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
#include <iostream>
#include <exception>
#include <vector>
#include <fstream>
#include <string.h>
#include <memory>

#include <openssl/x509_vfy.h>
#include <openssl/evp.h>
#include <openssl/obj_mac.h>
#include <openssl/ec.h>
#include <openssl/dsa.h>
#include <openssl/dh.h>
#include <openssl/rsa.h>
#include <openssl/bio.h>
#include <openssl/rand.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/x509v3.h>
#include <openssl/obj_mac.h>
#include <ckm/ckm-error.h>
#include <ckm/ckm-type.h>
#include <generic-key.h>
#include <CryptoService.h>
#include <key-manager-util.h>
#include <assert.h>
#include <dpl/log/log.h>

#define OPENSSL_SUCCESS 1       // DO NOTCHANGE THIS VALUE
#define OPENSSL_FAIL    0       // DO NOTCHANGE THIS VALUE

namespace CKM {

CryptoService::CryptoService(){
}

CryptoService::~CryptoService(){
}



int CryptoService::initialize() {
	int mode = 0;
	int rc = 0;
	int hw_rand_ret = 0, u_rand_ret = 0;

	// try to initialize using ERR_load_crypto_strings and OpenSSL_add_all_algorithms
	ERR_load_crypto_strings();
	OpenSSL_add_all_algorithms();

	// turn on FIPS_mode
	mode = FIPS_mode();

	if(mode == 0) {
		rc = FIPS_mode_set(1);

		if(rc == 0) {
			LogError("Error in FIPS_mode_set function");
		}
	}

	// initialize entropy
	std::ifstream ifile(DEV_HW_RANDOM_FILE);
	if(ifile.is_open()) {
		u_rand_ret= RAND_load_file(DEV_HW_RANDOM_FILE, 32);
	}
	if(u_rand_ret != 32 ){
		LogError("Error in HW_RAND file load");
		hw_rand_ret = RAND_load_file(DEV_URANDOM_FILE, 32);

		if(hw_rand_ret != 32) {
			LogError("Error in U_RAND_file_load");
			ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in U_RAND_file_load");
		}
	}

	return CKM_CRYPTO_INIT_SUCCESS;
}



int CryptoService::createKeyPairRSA(const int size, // size in bits [1024, 2048, 4096]
		GenericKey &createdPrivateKey,  // returned value
		GenericKey &createdPublicKey)  // returned value
{
	EVP_PKEY_CTX *ctx = NULL;
	EVP_PKEY *pkey = NULL;
	EVP_PKEY *pparam = NULL;

	// check the parameters of functions
	if(size != 1024 && size !=2048 && size != 4096) {
		LogError("Error in RSA input size");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in RSA input size");
	}

	// check the parameters of functions
	if(&createdPrivateKey == NULL) {
		LogError("Error in createdPrivateKey value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in createdPrivateKey value");
	}

	// check the parameters of functions
	if(&createdPublicKey == NULL) {
		LogError("Error in createdPrivateKey value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in createdPublicKey value");
	}

	Try {
		if(!(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL))) {
			LogError("Error in EVP_PKEY_CTX_new_id function !!");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_CTX_new_id function !!");
		}

		if(EVP_PKEY_keygen_init(ctx) <= 0) {
			LogError("Error in EVP_PKEY_keygen_init function !!");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_keygen_init function !!");
		}

		if(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx,size) <= 0) {
			LogError("Error in EVP_PKEY_CTX_set_rsa_keygen_bits function !!");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_CTX_set_rsa_keygen_bits function !!");
		}

		if(!EVP_PKEY_keygen(ctx, &pkey)) {
			LogError("Error in EVP_PKEY_keygen function !!");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_keygen function !!");
		}
	} Catch(CryptoService::Exception::opensslError) {
		if(pkey) {
			EVP_PKEY_free(pkey);
		}

		if(pparam) {
			EVP_PKEY_free(pparam);
		}

		if(ctx) {
			EVP_PKEY_CTX_free(ctx);
		}

		ReThrowMsg(CryptoService::Exception::opensslError,"Error in opensslError function !!");
	}

	GenericKey::EvpShPtr ptr(pkey, EVP_PKEY_free); // shared ptr will free pkey

	createdPrivateKey = GenericKey(ptr, KeyType::KEY_RSA_PRIVATE);
	createdPublicKey = GenericKey(ptr, KeyType::KEY_RSA_PUBLIC);

	if(pparam) {
		EVP_PKEY_free(pparam);
	}

	if(ctx) {
		EVP_PKEY_CTX_free(ctx);
	}

	return CKM_CRYPTO_CREATEKEY_SUCCESS;
}

int CryptoService::createKeyPairECDSA(ElipticCurve type,
		GenericKey &createdPrivateKey,  // returned value
		GenericKey &createdPublicKey)  // returned value
{
	int ecCurve = NOT_DEFINED;
	EVP_PKEY_CTX *pctx = NULL;
	EVP_PKEY_CTX *kctx = NULL;
	EVP_PKEY *pkey = NULL;
	EVP_PKEY *pparam = NULL;

	switch(type) {
	case ElipticCurve::prime192v1:
		ecCurve = NID_X9_62_prime192v1;
		break;
	case ElipticCurve::prime256v1:
		ecCurve = NID_X9_62_prime256v1;
		break;
	case ElipticCurve::secp384r1:
		ecCurve = NID_secp384r1;
		break;
	default:
		LogError("Error in EC type");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in EC type");
	}

	// check the parameters of functions
	if(&createdPrivateKey == NULL) {
		LogError("Error in createdPrivateKey value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in createdPrivateKey value");
	}

	// check the parameters of functions
	if(&createdPublicKey == NULL) {
		LogError("Error in createdPrivateKey value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in createdPublicKey value");
	}

	Try {
		/* Create the context for generating the parameters */
		if(!(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL))) {
			LogError("Error in EVP_PKEY_CTX_new_id function");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_CTX_new_id function");
		}

		if(EVP_SUCCESS != EVP_PKEY_paramgen_init(pctx)) {
			LogError("Error in EVP_PKEY_paramgen_init function");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_paramgen_init function");
		}

		if(EVP_SUCCESS != EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, ecCurve)) {
			LogError("Error in EVP_PKEY_CTX_set_ec_paramgen_curve_nid function");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_CTX_set_ec_paramgen_curve_nid function");
		}

		/* Generate parameters */
		if(EVP_SUCCESS != EVP_PKEY_paramgen(pctx, &pparam)) {
			LogError("Error in EVP_PKEY_paramgen function");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_paramgen function");
		}

		// Start to generate key
		if(!(kctx = EVP_PKEY_CTX_new(pparam, NULL))) {
			LogError("Error in EVP_PKEY_CTX_new function");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_CTX_new function");
		}

		if(EVP_SUCCESS != EVP_PKEY_keygen_init(kctx)) {
			LogError("Error in EVP_PKEY_keygen_init function");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_keygen_init function");
		}

		/* Generate the key */
		if(EVP_SUCCESS != EVP_PKEY_keygen(kctx, &pkey)) {
			LogError("Error in EVP_PKEY_keygen function");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_keygen function");
		}
	} Catch(CryptoService::Exception::opensslError) {
		if(pkey) {
			EVP_PKEY_free(pkey);
		}

		if(pparam) {
			EVP_PKEY_free(pparam);
		}

		if(pctx) {
			EVP_PKEY_CTX_free(pctx);
		}

		if(kctx) {
			EVP_PKEY_CTX_free(kctx);
		}

		ReThrowMsg(CryptoService::Exception::opensslError,"Error in openssl function !!");
	}

	GenericKey::EvpShPtr ptr(pkey, EVP_PKEY_free); // shared ptr will free pkey

	createdPrivateKey = GenericKey(ptr, KeyType::KEY_ECDSA_PRIVATE);
	createdPublicKey = GenericKey(ptr, KeyType::KEY_ECDSA_PUBLIC);

	if(pparam) {
		EVP_PKEY_free(pparam);
	}

	if(pctx) {
		EVP_PKEY_CTX_free(pctx);
	}

	if(kctx) {
		EVP_PKEY_CTX_free(kctx);
	}

	return CKM_CRYPTO_CREATEKEY_SUCCESS;
}

int CryptoService::createSignature(const GenericKey &privateKey,
		const RawBuffer &message,
		const HashAlgorithm hashAlgo,
		const RSAPaddingAlgorithm padAlgo,
		RawBuffer &signature)
{
	EVP_MD_CTX *mdctx = NULL;
	EVP_PKEY_CTX *pctx = NULL;
	int rsa_padding = NOT_DEFINED;
	RawBuffer data;
	const EVP_MD *md_algo = NULL;

	// check the parameters of functions
	if(&privateKey == NULL) {
		LogError("Error in privateKey value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in privateKey value");
	}

	if(&message == NULL) {
		LogError("Error in message value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in message value");
	}

	switch(hashAlgo) {
	case HashAlgorithm::SHA1:
		md_algo = EVP_sha1();
		break;
	case HashAlgorithm::SHA256:
		md_algo = EVP_sha256();
		break;
	case HashAlgorithm::SHA384:
		md_algo = EVP_sha384();
		break;
	case HashAlgorithm::SHA512:
		md_algo = EVP_sha512();
		break;
	default:
		LogError("Error in hashAlgorithm value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in hashAlgorithm value");
	}

	if((privateKey.getType() != KeyType::KEY_RSA_PRIVATE) && (privateKey.getType() != KeyType::KEY_ECDSA_PRIVATE)) {
		LogError("Error in private key type");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in private key type");
	}

	if(privateKey.getType()==KeyType::KEY_RSA_PRIVATE) {
		switch(padAlgo) {
		case RSAPaddingAlgorithm::PKCS1:
			rsa_padding = RSA_PKCS1_PADDING;
			break;
		case RSAPaddingAlgorithm::X931:
			rsa_padding = RSA_X931_PADDING;
			break;
		default:
			LogError("Error in padding Algorithm value");
			ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in padding Algorithm value");
		}
	}

	auto shrPKey = privateKey.getEvpShPtr();

	Try {
		if (NULL == shrPKey.get()) {
			LogError("Error in EVP_PKEY_keygen function");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_keygen function");
		}

		// Create the Message Digest Context
		if(!(mdctx = EVP_MD_CTX_create())) {
			LogError("Error in EVP_MD_CTX_create function");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_MD_CTX_create function");
		}

		if(EVP_SUCCESS != EVP_DigestSignInit(mdctx, &pctx, md_algo, NULL, shrPKey.get())) {
			LogError("Error in EVP_DigestSignInit function");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_DigestSignInit function");
		}

		/* Set padding algorithm */
		if(privateKey.getType()==KeyType::KEY_RSA_PRIVATE) {
			if(EVP_SUCCESS != EVP_PKEY_CTX_set_rsa_padding(pctx, rsa_padding)) {
				LogError("Error in EVP_PKEY_CTX_set_rsa_padding function");
				ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_CTX_set_rsa_padding function");
			}
		}

		/* Call update with the message */
		char msg[message.size()];
		memcpy(msg, message.data(),message.size());
		if(EVP_SUCCESS != EVP_DigestSignUpdate(mdctx, msg, message.size())) {
			LogError("Error in EVP_DigestSignUpdate function");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_DigestSignUpdate function");
		}

		/* Finalize the DigestSign operation */
		/* First call EVP_DigestSignFinal with a NULL sig parameter to obtain the length of the
		 * signature. Length is returned in slen */
		size_t slen;
		if(EVP_SUCCESS != EVP_DigestSignFinal(mdctx, NULL, &slen)) {
			LogError("Error in EVP_DigestSignFinal function");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_DigestSignFinal function");
		}
		/* Allocate memory for the signature based on size in slen */
		unsigned char sig[slen];

		/* Obtain the signature */
		if(EVP_SUCCESS != EVP_DigestSignFinal(mdctx, sig, &slen)) {
			LogError("Error in EVP_DigestSignFinal function");
			ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_DigestSignFinal function");
		}

		// Set value to return RawData
		signature.assign(sig, sig+slen);
	} Catch(CryptoService::Exception::opensslError) {
		if(mdctx != NULL) {
			EVP_MD_CTX_destroy(mdctx);
		}

		ReThrowMsg(CryptoService::Exception::opensslError,"Error in openssl function !!");
	}

	if(mdctx != NULL) {
		EVP_MD_CTX_destroy(mdctx);
	}

	return CKM_CREATE_SIGNATURE_SUCCESS;
}

int CryptoService::verifySignature(const GenericKey &publicKey,
		const RawBuffer &message,
		const RawBuffer &signature,
		const HashAlgorithm hashAlgo,
		const RSAPaddingAlgorithm padAlgo)
{
	EVP_PKEY_CTX *pctx = NULL;
	int rsa_padding = NOT_DEFINED;
	const EVP_MD *md_algo;
    int retCode = CKM_API_ERROR_VERIFICATION_FAILED;
    std::unique_ptr<EVP_MD_CTX, std::function<void(EVP_MD_CTX*)>> mdctx(nullptr, EVP_MD_CTX_destroy);

    if(&publicKey == NULL) {
		LogError("Error in publicKey value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in publicKey value");
	}

	if(&message == NULL) {
		LogError("Error in message value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in message value");
	}

	if(&signature == NULL) {
		LogError("Error in signature value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in signature value");
	}

	switch(hashAlgo) {
	case HashAlgorithm::SHA1:
		md_algo = EVP_sha1();
		break;
	case HashAlgorithm::SHA256:
		md_algo = EVP_sha256();
		break;
	case HashAlgorithm::SHA384:
		md_algo = EVP_sha384();
		break;
	case HashAlgorithm::SHA512:
		md_algo = EVP_sha512();
		break;
	default:
		LogError("Error in hashAlgorithm value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in hashAlgorithm value");
	}

	if((publicKey.getType() != KeyType::KEY_RSA_PUBLIC) && (publicKey.getType() != KeyType::KEY_ECDSA_PUBLIC)) {
		LogError("Error in private key type");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in private key type");
	}

	if(publicKey.getType()==KeyType::KEY_RSA_PUBLIC) {
		switch(padAlgo) {
		case RSAPaddingAlgorithm::PKCS1:
			rsa_padding = RSA_PKCS1_PADDING;
			break;
		case RSAPaddingAlgorithm::X931:
			rsa_padding = RSA_X931_PADDING;
			break;
		default:
			LogError("Error in padding Algorithm value");
			ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in padding Algorithm value");
		}
	}

    auto public_pkey = publicKey.getEvpShPtr();

    if (NULL == public_pkey.get()) {
        LogError("Error in getEvpShPtr function");
        ThrowMsg(CryptoService::Exception::opensslError, "Error in getEvpShPtr function");
    }

    /* Create the Message Digest Context */
    mdctx.reset(EVP_MD_CTX_create());

    if(!(mdctx.get())) {
        LogError("Error in EVP_MD_CTX_create function");
        ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_MD_CTX_create function");
    }

    if(EVP_SUCCESS != EVP_DigestVerifyInit(mdctx.get(), &pctx, md_algo, NULL, public_pkey.get())) {
        LogError("Error in EVP_DigestVerifyInit function");
        ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_DigestVerifyInit function");
    }

    if(publicKey.getType()==KeyType::KEY_RSA_PUBLIC) {
        if(EVP_SUCCESS != EVP_PKEY_CTX_set_rsa_padding(pctx, rsa_padding))  {
            LogError("Error in EVP_PKEY_CTX_set_rsa_padding function");
            ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_PKEY_CTX_set_rsa_padding function");
        }
    }

    if(EVP_SUCCESS != EVP_DigestVerifyUpdate(mdctx.get(), message.data(), message.size()) ) {
        LogError("Error in EVP_DigestVerifyUpdate function");
        ThrowMsg(CryptoService::Exception::opensslError, "Error in EVP_DigestVerifyUpdate function");
    }

    if(EVP_SUCCESS == EVP_DigestVerifyFinal(mdctx.get(), const_cast<unsigned char*>(signature.data()), signature.size()) ) {
        retCode = CKM_API_SUCCESS;
    } else {
        LogError("Error in EVP_DigestVerifyFinal function");
    }

	return retCode;
}


int CryptoService::verifyCertificateChain(const CertificateImpl &certificate,
		const CertificateImplVector &untrustedCertificates,
		const CertificateImplVector &userTrustedCertificates,
		CertificateImplVector &certificateChainVector) {

	X509 *cert = X509_new();
	X509 *tempCert;
	rawBufferToX509(&cert, certificate.getDER());

	std::vector<X509 *> trustedCerts;
	std::vector<X509 *> userTrustedCerts;
	std::vector<X509 *> untrustedChain;

	STACK_OF(X509) *sysCerts = loadSystemCerts(CKM_SYSTEM_CERTS_PATH);

	// check the parameters of functions
	if(&certificate == NULL) {
		LogError("Error in certificate value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in certificate value");
	}

	// check the parameters of functions
	if(&untrustedCertificates == NULL) {
		LogError("Error in untrustedCertificates value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in untrustedCertificates value");
	}

	// check the parameters of functions
	if(&userTrustedCertificates == NULL) {
		LogError("Error in userTrustedCertificates value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in userTrustedCertificates value");
	}

	// check the parameters of functions
	if(&certificateChainVector == NULL) {
		LogError("Error in certificateChainVector value");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in certificateChainVector value");
	}

	Try {
		while((tempCert = sk_X509_pop(sysCerts)) != NULL) {
			trustedCerts.push_back(tempCert);
		}

		for(unsigned int i=0;i<userTrustedCertificates.size();i++) {
			if((tempCert = X509_new()) == NULL) {
				LogError("Error in X509_new function");
				ThrowMsg(CryptoService::Exception::opensslError, "Error in X509_new function");
			}
			rawBufferToX509(&tempCert, userTrustedCertificates[i].getDER());
			userTrustedCerts.push_back(tempCert);
		}

		for(unsigned int i=0;i<untrustedCertificates.size();i++) {
			if((tempCert = X509_new()) == NULL) {
				LogError("Error in X509_new function");
				ThrowMsg(CryptoService::Exception::opensslError, "Error in X509_new function");
			}
			rawBufferToX509(&tempCert, untrustedCertificates[i].getDER());
			untrustedChain.push_back(tempCert);
		}

		std::vector<X509 *> chain = verifyCertChain(cert, trustedCerts, userTrustedCerts, untrustedChain);

		RawBuffer tmpBuf;
		for(unsigned int i=0;i<chain.size();i++) {
			x509ToRawBuffer(tmpBuf, chain[i]);
			CertificateImpl tmpCertImpl((const RawBuffer)tmpBuf, DataFormat::FORM_DER);
			certificateChainVector.push_back(tmpCertImpl);
		}
	} Catch(CryptoService::Exception::opensslError) {
		if(cert != NULL) {
			X509_free(cert);
		}

		for(unsigned int i=0;i<trustedCerts.size();i++) {
			if(trustedCerts[i] != NULL) {
				X509_free(trustedCerts[i]);
			}
		}

		for(unsigned int i=0;i<untrustedChain.size();i++) {
			if(untrustedChain[i] != NULL) {
				X509_free(untrustedChain[i]);
			}
		}

		for(unsigned int i=0;i<userTrustedCerts.size();i++) {
			if(userTrustedCerts[i] != NULL) {
				X509_free(userTrustedCerts[i]);
			}
		}
		ReThrowMsg(CryptoService::Exception::opensslError,"Error in openssl function !!");
	}

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

	for(unsigned int i=0;i<trustedCerts.size();i++) {
		if(trustedCerts[i] != NULL) {
			X509_free(trustedCerts[i]);
		}
	}

	for(unsigned int i=0;i<untrustedChain.size();i++) {
		if(untrustedChain[i] != NULL) {
			X509_free(untrustedChain[i]);
		}
	}

	for(unsigned int i=0;i<userTrustedCerts.size();i++) {
		if(userTrustedCerts[i] != NULL) {
			X509_free(userTrustedCerts[i]);
		}
	}

	return CKM_VERIFY_CHAIN_SUCCESS;
}

/*
 * truestedCerts means the system certificate list stored in system securely.
 * return : std::vector<X509 *> certChain; the order is user cert, middle ca certs, and root ca cert.
 */

std::vector<X509 *> CryptoService::verifyCertChain(X509 *cert,
		std::vector<X509 *> &trustedCerts,
		std::vector<X509 *> &userTrustedCerts,
		std::vector<X509 *> &untrustedchain){

	std::vector<X509 *> certChain;
	X509_STORE *tstore = X509_STORE_new();
	STACK_OF(X509) *uchain = sk_X509_new_null();
	std::vector<X509 *>::iterator iVec_it;

	for(iVec_it = trustedCerts.begin(); iVec_it != trustedCerts.end(); iVec_it++) {
		X509_STORE_add_cert(tstore, *iVec_it);
	}
	for(iVec_it = userTrustedCerts.begin(); iVec_it != userTrustedCerts.end(); iVec_it++) {
		X509_STORE_add_cert(tstore, *iVec_it);
	}

	for(iVec_it = untrustedchain.begin(); iVec_it != untrustedchain.end(); iVec_it++) {
		sk_X509_push(uchain, *iVec_it);
	}

	// Create the context to verify the certificate.
	X509_STORE_CTX *ctx = X509_STORE_CTX_new();

	// Initial the store to verify the certificate.
	X509_STORE_CTX_init(ctx, tstore, cert, uchain);

	int verified = X509_verify_cert(ctx);

	if(verified == OPENSSL_SUCCESS) {
		STACK_OF(X509) *chain = X509_STORE_CTX_get1_chain(ctx);
		X509 *cert;
		while((cert = sk_X509_pop(chain))) {
			certChain.insert(certChain.begin(),cert);
		}
	}

	X509_STORE_CTX_cleanup(ctx);
	X509_STORE_CTX_free(ctx);
	X509_STORE_free(tstore);
	sk_X509_free(uchain);
	ctx = NULL;
	tstore = NULL;
	uchain = NULL;

	if(verified != OPENSSL_SUCCESS) {
		LogError("Error in verifying certification chain");
		ThrowMsg(CryptoService::Exception::Crypto_internal, "Error in verifying certification chain");
	}

	return certChain;
}

bool CryptoService::hasValidCAFlag(std::vector<X509 *> &certChain) {
	// KeyUsage if present should allow cert signing;
	// If basicConstraints says not a CA then say so.

	X509 *cert = NULL;
	int isCA;

	if(certChain.size() < 2) // certChain should have more than 2 certs.
		return false;

	std::vector<X509 *>::iterator it;
	for(it = certChain.begin()+1; it != certChain.end(); it++) { // start from the second cert
		cert = *it;
		isCA = X509_check_ca(cert);
		// For MDPP compliance.
		// if it returns 1, this means that the cert has the basicConstraints and CAFlag=true.
		// X509_check_ca can return 0(is not CACert), 1(is CACert), 3, 4, 5(may be CACert).
		if(isCA != 1) {
			return false;
		}
	}

	return true;
}
}