@@ -39,24 +39,35 @@ static void PrintSSLErr(const char* str) {
39
39
char error_string[SecureSocketUtils::SSL_ERROR_MESSAGE_BUFFER_SIZE];
40
40
ERR_error_string_n (error, error_string,
41
41
SecureSocketUtils::SSL_ERROR_MESSAGE_BUFFER_SIZE);
42
- Syslog::PrintErr (" %s ERROR: %d % s\n " , str, error , error_string);
42
+ Syslog::PrintErr (" %s % s\n " , str, error_string);
43
43
}
44
44
45
- // Add certificates from Windows trusted root store.
46
- static bool AddCertificatesFromRootStore (X509_STORE* store) {
47
- // The UWP platform doesn't support CertEnumCertificatesInStore hence
48
- // this function cannot work when compiled in UWP mode.
49
- #ifdef TARGET_OS_WINDOWS_UWP
50
- return false ;
51
- #else
52
- // Open root system store.
53
- // Note that only current user certificates are accessible using this method,
54
- // not the local machine store.
55
- HCERTSTORE cert_store = CertOpenSystemStore (NULL , L" ROOT" );
45
+ #ifndef TARGET_OS_WINDOWS_UWP
46
+ static bool AddCertificatesFromNamedSystemStore (const wchar_t * name,
47
+ DWORD store_type,
48
+ X509_STORE* store) {
49
+ ASSERT (store_type == CERT_SYSTEM_STORE_CURRENT_USER ||
50
+ store_type == CERT_SYSTEM_STORE_LOCAL_MACHINE);
51
+
52
+ if (SSL_LOG_STATUS) {
53
+ Syslog::Print (" AddCertificatesFromNamedSystemStore %ls type: %s\n " , name,
54
+ store_type == CERT_SYSTEM_STORE_CURRENT_USER
55
+ ? " Current User"
56
+ : " Local Machine" );
57
+ }
58
+
59
+ HCERTSTORE cert_store =
60
+ CertOpenStore (CERT_STORE_PROV_SYSTEM,
61
+ 0 , // the encoding type is not needed
62
+ NULL , // use the default HCRYPTPROV
63
+ store_type | CERT_STORE_READONLY_FLAG, name);
64
+
56
65
if (cert_store == NULL ) {
57
66
if (SSL_LOG_STATUS) {
58
67
DWORD error = GetLastError ();
59
- Syslog::PrintErr (" Failed to open Windows root store due to %d\n " , error);
68
+ Syslog::PrintErr (
69
+ " Failed to open Windows root store %ls type %d due to %d\n " , name,
70
+ store_type, error);
60
71
}
61
72
return false ;
62
73
}
@@ -84,10 +95,34 @@ static bool AddCertificatesFromRootStore(X509_STORE* store) {
84
95
continue ;
85
96
}
86
97
BIO_free (root_cert_bio);
98
+
99
+ if (SSL_LOG_STATUS) {
100
+ auto s_name = X509_get_subject_name (root_cert);
101
+ auto s_issuer_name = X509_get_issuer_name (root_cert);
102
+ auto serial_number = X509_get_serialNumber (root_cert);
103
+ BIGNUM* bn = ASN1_INTEGER_to_BN (serial_number, nullptr );
104
+ char * hex = BN_bn2hex (bn);
105
+ Syslog::Print (" Considering root certificate serial: %s subject name: " ,
106
+ hex);
107
+ OPENSSL_free (hex);
108
+ X509_NAME_print_ex_fp (stdout, s_name, 4 , 0 );
109
+ Syslog::Print (" issuer:" );
110
+ X509_NAME_print_ex_fp (stdout, s_issuer_name, 4 , 0 );
111
+ Syslog::Print (" \n " );
112
+ }
113
+
87
114
int status = X509_STORE_add_cert (store, root_cert);
88
115
if (status == 0 ) {
116
+ int error = ERR_get_error ();
117
+ if (ERR_GET_REASON (error) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
118
+ if (SSL_LOG_STATUS) {
119
+ Syslog::Print (" ...duplicate\n " );
120
+ }
121
+ X509_free (root_cert);
122
+ continue ;
123
+ }
89
124
if (SSL_LOG_STATUS) {
90
- PrintSSLErr (" Fail to add certificate to trust store" );
125
+ PrintSSLErr (" Failed to add certificate to x509 trust store" );
91
126
}
92
127
X509_free (root_cert);
93
128
CertFreeCertificateContext (cert_context);
@@ -104,6 +139,42 @@ static bool AddCertificatesFromRootStore(X509_STORE* store) {
104
139
}
105
140
return false ;
106
141
}
142
+ return true ;
143
+ }
144
+
145
+ static bool AddCertificatesFromSystemStore (DWORD store_type,
146
+ X509_STORE* store) {
147
+ if (!AddCertificatesFromNamedSystemStore (L" ROOT" , store_type, store)) {
148
+ return false ;
149
+ }
150
+ if (!AddCertificatesFromNamedSystemStore (L" CA" , store_type, store)) {
151
+ return false ;
152
+ }
153
+ if (!AddCertificatesFromNamedSystemStore (L" TRUST" , store_type, store)) {
154
+ return false ;
155
+ }
156
+ if (!AddCertificatesFromNamedSystemStore (L" MY" , store_type, store)) {
157
+ return false ;
158
+ }
159
+ return true ;
160
+ }
161
+ #endif // ifdef TARGET_OS_WINDOWS_UWP
162
+
163
+ // Add certificates from Windows trusted root store.
164
+ static bool AddCertificatesFromRootStore (X509_STORE* store) {
165
+ // The UWP platform doesn't support CertEnumCertificatesInStore hence
166
+ // this function cannot work when compiled in UWP mode.
167
+ #ifdef TARGET_OS_WINDOWS_UWP
168
+ return false ;
169
+ #else
170
+ if (!AddCertificatesFromSystemStore (CERT_SYSTEM_STORE_CURRENT_USER, store)) {
171
+ return false ;
172
+ }
173
+
174
+ if (!AddCertificatesFromSystemStore (CERT_SYSTEM_STORE_LOCAL_MACHINE, store)) {
175
+ return false ;
176
+ }
177
+
107
178
return true ;
108
179
#endif // ifdef TARGET_OS_WINDOWS_UWP
109
180
}
0 commit comments