@@ -91,20 +91,29 @@ struct NetworkContext
91
91
static int32_t opensslError ( void );
92
92
93
93
/**
94
- * @brief Add X509 certificate to the trusted list of root certificates.
95
- *
96
- * OpenSSL does not provide a single function for reading and loading
97
- * certificates from files into stores, so the file API must be called. Start
98
- * with the root certificate.
94
+ * @brief Add X509 certificate from a file to the trusted list of root certificates.
99
95
*
100
96
* @param[out] pSslContext SSL context to which the trusted server root CA is to
101
97
* be added.
102
98
* @param[in] pRootCaPath Filepath string to the trusted server root CA.
103
99
*
104
100
* @return 1 on success; -1, 0 on failure.
105
101
*/
106
- static int32_t setRootCa ( const SSL_CTX * pSslContext ,
107
- const char * pRootCaPath );
102
+ static int32_t setRootCaFromFile ( const SSL_CTX * pSslContext ,
103
+ const char * pRootCaPath );
104
+
105
+ /**
106
+ * @brief Add X509 certificate from a PKCS#11 token to the trusted list of root certificates.
107
+ *
108
+ * @param[out] pSslContext SSL context to which the trusted server root CA is to
109
+ * be added.
110
+ * @param[in] pRootCaURI Filepath string to the trusted server root CA.
111
+ *
112
+ * @return 1 on success; -1, 0 on failure.
113
+ */
114
+ static int32_t setRootCaFromPkcs11 ( const SSL_CTX * pSslContext ,
115
+ ENGINE * pEngine ,
116
+ const char * pRootCaURI );
108
117
109
118
/**
110
119
* @brief Load an X509 client certificate from a given file.
@@ -123,13 +132,13 @@ static int32_t setClientCertificateFromFile( SSL_CTX * pSslContext,
123
132
*
124
133
* @param[out] pSslContext SSL context to which the client certificate is to be
125
134
* set.
126
- * @param[in] pClientCertPath PKCS11 URI string for the client certificate object.
135
+ * @param[in] pClientCertURI PKCS11 URI string for the client certificate object.
127
136
*
128
137
* @return 1 on success; 0 failure.
129
138
*/
130
139
static int32_t setClientCertificateFromPkcs11 ( SSL_CTX * pSslContext ,
131
140
ENGINE * pEngine ,
132
- const char * pClientCertPath );
141
+ const char * pClientCertURI );
133
142
134
143
/**
135
144
* @brief Load a private key into the ssl context from a given file path.
@@ -147,7 +156,7 @@ static int32_t setPrivateKeyFromFile( SSL_CTX * pSslContext,
147
156
*
148
157
* @param[out] pSslContext SSL context to add the private key to.
149
158
* @param[in] pEngine Openssl engine handle to read the key from.
150
- * @param[in] pPrivateKeyPath PKCS#11 URI to load the private key from.
159
+ * @param[in] pPrivateKeyURI PKCS#11 URI to load the private key from.
151
160
*
152
161
* @return 1 on success; 0 on failure.
153
162
*/
@@ -355,8 +364,9 @@ static OpensslStatus_t tlsHandshake( const ServerInfo_t * pServerInfo,
355
364
356
365
return returnStatus ;
357
366
}
367
+ /*-----------------------------------------------------------*/
358
368
359
- static int32_t setRootCa ( const SSL_CTX * pSslContext ,
369
+ static int32_t setRootCaFromFile ( const SSL_CTX * pSslContext ,
360
370
const char * pRootCaPath )
361
371
{
362
372
int32_t sslStatus = 1 ;
@@ -445,6 +455,79 @@ static int32_t setRootCa( const SSL_CTX * pSslContext,
445
455
}
446
456
/*-----------------------------------------------------------*/
447
457
458
+ static int32_t loadCertificateFromPkcs11 ( X509 * * ppX509Cert ,
459
+ ENGINE * pEngine ,
460
+ const char * pCertURI )
461
+ {
462
+ int32_t sslStatus = 1 ;
463
+ struct
464
+ {
465
+ const char * pCertURI ;
466
+ X509 * pX509Cert ;
467
+ } loadCertParams ;
468
+
469
+ assert ( ppX509Cert != NULL );
470
+ assert ( pEngine != NULL );
471
+ assert ( pCertURI != NULL );
472
+
473
+ loadCertParams .pCertURI = pCertURI ;
474
+ loadCertParams .pX509Cert = NULL ;
475
+
476
+ sslStatus = ENGINE_ctrl_cmd ( pEngine , "LOAD_CERT_CTRL" , 0 ,
477
+ & loadCertParams , NULL , 0 );
478
+
479
+ if ( loadCertParams .pX509Cert == NULL )
480
+ {
481
+ LogError ( ( "ENGINE_ctrl_cmd returned an empty certificate object "
482
+ "from URI: %s ." , pCertURI ) );
483
+ sslStatus = 0 ;
484
+ }
485
+ else if ( sslStatus != 1 )
486
+ {
487
+ LogError ( ( "ENGINE_ctrl_cmd failed to read "
488
+ "certificate from URI: %s ." , pCertURI ) );
489
+ sslStatus = opensslError ();
490
+ }
491
+ else
492
+ {
493
+ * ppX509Cert = loadCertParams .pX509Cert ;
494
+ }
495
+ return sslStatus ;
496
+ }
497
+ /*-----------------------------------------------------------*/
498
+
499
+ static int32_t setRootCaFromPkcs11 ( const SSL_CTX * pSslContext ,
500
+ ENGINE * pEngine ,
501
+ const char * pRootCaURI )
502
+ {
503
+ int32_t sslStatus = 1 ;
504
+ X509 * pRootCa = NULL ;
505
+
506
+ assert ( pSslContext != NULL );
507
+ assert ( pEngine != NULL );
508
+ assert ( pRootCaURI != NULL );
509
+
510
+ sslStatus = loadCertificateFromPkcs11 ( & pRootCa , pEngine , pRootCaURI );
511
+
512
+ if ( sslStatus == 1 )
513
+ {
514
+ LogInfo ( ( "Successfully read client certificate from URI: %s ." ,
515
+ pClientCertURI ) );
516
+
517
+ /* Import cert into context */
518
+ sslStatus = X509_STORE_add_cert ( SSL_CTX_get_cert_store ( pSslContext ), pRootCa );
519
+
520
+ if ( sslStatus != 1 )
521
+ {
522
+ LogError ( ( "Failed to add Root CA certificate to SSL context from URI : %s ." ,
523
+ pRootCaURI ) );
524
+ }
525
+ }
526
+
527
+ return sslStatus ;
528
+ }
529
+ /*-----------------------------------------------------------*/
530
+
448
531
static int32_t setClientCertificateFromFile ( SSL_CTX * pSslContext ,
449
532
const char * pClientCertPath )
450
533
{
@@ -481,47 +564,27 @@ static int32_t setClientCertificateFromPkcs11( SSL_CTX * pSslContext,
481
564
const char * pClientCertURI )
482
565
{
483
566
int32_t sslStatus = 1 ;
484
- struct
485
- {
486
- const char * pCertURI ;
487
- X509 * pX509Cert ;
488
- } loadCertParams ;
567
+ X509 * pX509Cert = NULL ;
489
568
490
569
assert ( pSslContext != NULL );
491
570
assert ( pEngine != NULL );
492
571
assert ( pClientCertURI != NULL );
493
572
494
- loadCertParams .pCertURI = pClientCertURI ;
495
- loadCertParams .pX509Cert = NULL ;
573
+ sslStatus = loadCertificateFromPkcs11 ( & pX509Cert , pEngine , pClientCertURI );
496
574
497
- sslStatus = ENGINE_ctrl_cmd ( pEngine , "LOAD_CERT_CTRL" , 0 ,
498
- & loadCertParams , NULL , 0 );
499
-
500
- if ( loadCertParams .pX509Cert == NULL )
501
- {
502
- LogError ( ( "ENGINE_ctrl_cmd returned an empty certificate object "
503
- "from URI: %s ." , pClientCertURI ) );
504
- sslStatus = 0 ;
505
- }
506
- else if ( sslStatus != 1 )
507
- {
508
- LogError ( ( "ENGINE_ctrl_cmd failed to read client "
509
- "certificate from URI: %s ." , pClientCertURI ) );
510
- sslStatus = opensslError ();
511
- }
512
- else
575
+ if ( sslStatus == 1 )
513
576
{
514
577
LogInfo ( ( "Successfully read client certificate from URI: %s ." ,
515
578
pClientCertURI ) );
516
579
517
580
/* Import cert into context */
518
- sslStatus = SSL_CTX_use_certificate ( pSslContext , loadCertParams .pX509Cert );
519
- }
581
+ sslStatus = SSL_CTX_use_certificate ( pSslContext , pX509Cert );
520
582
521
- if ( sslStatus != 1 )
522
- {
523
- LogError ( ( "Failed to copy certificate into SSL context from URI : %s ." ,
524
- pClientCertURI ) );
583
+ if ( sslStatus != 1 )
584
+ {
585
+ LogError ( ( "Failed to copy certificate into SSL context from URI : %s ." ,
586
+ pClientCertURI ) );
587
+ }
525
588
}
526
589
527
590
return sslStatus ;
@@ -636,9 +699,6 @@ static int32_t initializePkcs11Engine( ENGINE ** ppEngine,
636
699
637
700
assert ( ppEngine != NULL );
638
701
639
- /* Initialize pkcs11 config and engine */
640
- // ENGINE_add_conf_module();
641
-
642
702
ENGINE_load_builtin_engines ();
643
703
644
704
/* Acquire a structural reference for the pkcs11 engine */
@@ -708,45 +768,61 @@ static int32_t setCredentials( SSL_CTX * pSslContext,
708
768
{
709
769
int32_t sslStatus = 1 ;
710
770
ENGINE * pEngine = NULL ;
711
- bool certFromEngine = false;
712
- bool pkeyFromEngine = false;
771
+ bool certFromP11 = false;
772
+ bool pkeyFromP11 = false;
773
+ bool rootCaFromP11 = false;
713
774
714
775
assert ( pSslContext != NULL );
715
776
assert ( pOpensslCredentials != NULL );
716
777
717
- if ( pOpensslCredentials -> pRootCaPath != NULL )
718
- {
719
- sslStatus = setRootCa ( pSslContext , pOpensslCredentials -> pRootCaPath );
720
- }
721
-
722
778
/* Initialize the pkcs11 engine if needed */
723
- if ( ( sslStatus == 1 ) &&
724
- ( pOpensslCredentials -> pPrivateKeyPath != NULL ) &&
779
+ if ( ( pOpensslCredentials -> pPrivateKeyPath != NULL ) &&
725
780
( pOpensslCredentials -> pClientCertPath != NULL ) )
726
781
{
727
782
if ( strncmp ( pOpensslCredentials -> pPrivateKeyPath ,
728
783
PKCS11_URI_PREFIX , sizeof ( PKCS11_URI_PREFIX ) - 1 ) == 0 )
729
784
{
730
- pkeyFromEngine = true;
785
+ pkeyFromP11 = true;
786
+ }
787
+
788
+ if ( strncmp ( pOpensslCredentials -> pRootCaPath ,
789
+ PKCS11_URI_PREFIX , sizeof ( PKCS11_URI_PREFIX ) - 1 ) == 0 )
790
+ {
791
+ rootCaFromP11 = true;
731
792
}
732
793
733
794
if ( strncmp ( pOpensslCredentials -> pClientCertPath ,
734
795
PKCS11_URI_PREFIX , sizeof ( PKCS11_URI_PREFIX ) - 1 ) == 0 )
735
796
{
736
- certFromEngine = true;
797
+ certFromP11 = true;
737
798
}
738
799
739
- if ( pkeyFromEngine == true || certFromEngine == true )
800
+ if ( pkeyFromP11 == true || certFromP11 == true || rootCaFromP11 == true )
740
801
{
741
802
sslStatus = initializePkcs11Engine ( & pEngine ,
742
803
pOpensslCredentials -> pP11ModulePath ,
743
804
pOpensslCredentials -> pP11ModulePin );
744
805
}
745
806
}
746
807
808
+ if ( ( sslStatus == 1 ) && ( pOpensslCredentials -> pRootCaPath != NULL ) )
809
+ {
810
+ if ( rootCaFromP11 == true )
811
+ {
812
+ sslStatus = setRootCaFromPkcs11 ( pSslContext ,
813
+ pEngine ,
814
+ pOpensslCredentials -> pRootCaPath );
815
+ }
816
+ else
817
+ {
818
+ sslStatus = setRootCaFromFile ( pSslContext ,
819
+ pOpensslCredentials -> pRootCaPath );
820
+ }
821
+ }
822
+
747
823
if ( ( sslStatus == 1 ) && ( pOpensslCredentials -> pClientCertPath != NULL ) )
748
824
{
749
- if ( pkeyFromEngine == true )
825
+ if ( pkeyFromP11 == true )
750
826
{
751
827
sslStatus = setClientCertificateFromPkcs11 ( pSslContext ,
752
828
pEngine ,
@@ -761,7 +837,7 @@ static int32_t setCredentials( SSL_CTX * pSslContext,
761
837
762
838
if ( ( sslStatus == 1 ) && ( pOpensslCredentials -> pPrivateKeyPath != NULL ) )
763
839
{
764
- if ( pkeyFromEngine == true )
840
+ if ( pkeyFromP11 == true )
765
841
{
766
842
sslStatus = setPrivateKeyFromPkcs11 ( pSslContext ,
767
843
pEngine ,
0 commit comments