@@ -46,6 +46,7 @@ Config::Config() {
46
46
mbedtls_entropy_init (&m_entropy);
47
47
mbedtls_ctr_drbg_init (&m_ctr_drbg);
48
48
mbedtls_x509_crt_init (&m_srvcert);
49
+ mbedtls_x509_crt_init (&m_clientcert);
49
50
mbedtls_x509_crt_init (&m_cachain);
50
51
mbedtls_pk_init (&m_privateKey);
51
52
@@ -65,6 +66,7 @@ Config::~Config() {
65
66
66
67
mbedtls_ctr_drbg_free (&m_ctr_drbg);
67
68
mbedtls_x509_crt_free (&m_srvcert);
69
+ mbedtls_x509_crt_free (&m_clientcert);
68
70
mbedtls_x509_crt_free (&m_cachain);
69
71
70
72
mbedtls_pk_free (&m_privateKey);
@@ -151,6 +153,64 @@ std::shared_ptr<Config> Config::createDefaultClientConfigShared(bool throwOnVeri
151
153
152
154
}
153
155
156
+ std::shared_ptr<Config> Config::createDefaultClientConfigShared (bool throwOnVerificationFailed, std::string caRootCert, std::string clientCert, std::string privateKey) {
157
+ auto result = createShared ();
158
+ v_int32 res;
159
+
160
+ #if defined(OATPP_MBEDTLS_DEBUG)
161
+ mbedtls_ssl_conf_dbg ( &result->m_config , mbedtlsDebug, (void *)" Client" );
162
+ mbedtls_debug_set_threshold ( OATPP_MBEDTLS_DEBUG );
163
+ #endif
164
+
165
+ result->m_throwOnVerificationFailed = throwOnVerificationFailed;
166
+
167
+ res = mbedtls_ssl_config_defaults (&result->m_config , MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
168
+ if (res != 0 ) {
169
+ OATPP_LOGD (" [oatpp::mbedtls::Config::createDefaultClientConfigShared()]" , " Error. Call to mbedtls_ssl_config_defaults() failed, return value=%d." , res);
170
+ throw std::runtime_error (" [oatpp::mbedtls::Config::createDefaultClientConfigShared()]: Error. Call to mbedtls_ssl_config_defaults() failed." );
171
+ }
172
+
173
+ if (caRootCert.size ())
174
+ {
175
+ res = mbedtls_x509_crt_parse (&result->m_cachain , (const unsigned char *)caRootCert.data (), caRootCert.size ()+1 );
176
+ if (res != 0 ) {
177
+ OATPP_LOGD (" [oatpp::mbedtls::Config::createDefaultClientConfigShared()]" , " Error. Call to mbedtls_x509_crt_parse() failed, return value=%d." , res);
178
+ throw std::runtime_error (" [oatpp::mbedtls::Config::createDefaultClientConfigShared()]: Error. Call to mbedtls_x509_crt_parse() failed." );
179
+ }
180
+ mbedtls_ssl_conf_authmode (&result->m_config , MBEDTLS_SSL_VERIFY_REQUIRED);
181
+ mbedtls_ssl_conf_ca_chain (&result->m_config , &result->m_cachain , nullptr );
182
+ } else {
183
+ mbedtls_ssl_conf_authmode (&result->m_config , MBEDTLS_SSL_VERIFY_NONE);
184
+ }
185
+ mbedtls_ssl_conf_rng (&result->m_config , mbedtls_ctr_drbg_random, &result->m_ctr_drbg );
186
+
187
+ if (clientCert.size ())
188
+ {
189
+ res = mbedtls_x509_crt_parse (&result->m_clientcert , (const unsigned char *)clientCert.data (), clientCert.size ()+1 );
190
+ if (res != 0 ) {
191
+ OATPP_LOGD (" [oatpp::mbedtls::Config::createDefaultClientConfigShared()]" , " Error. Call to mbedtls_x509_crt_parse() failed, return value=%d." , res);
192
+ throw std::runtime_error (" [oatpp::mbedtls::Config::createDefaultClientConfigShared()]: Error. Call to mbedtls_x509_crt_parse() failed." );
193
+ }
194
+ }
195
+
196
+ if (privateKey.size ())
197
+ {
198
+ res = mbedtls_pk_parse_key (&result->m_privateKey , (const unsigned char *)privateKey.data (), privateKey.size ()+1 , NULL , 0 );
199
+ if (res != 0 ) {
200
+ OATPP_LOGD (" [oatpp::mbedtls::Config::createDefaultClientConfigShared()]" , " Error. Call to mbedtls_pk_parse_key() failed, return value=%d." , res);
201
+ throw std::runtime_error (" [oatpp::mbedtls::Config::createDefaultClientConfigShared()]: Error. Call to mbedtls_pk_parse_key() failed." );
202
+ }
203
+ }
204
+
205
+ res = mbedtls_ssl_conf_own_cert (&result->m_config , &result->m_clientcert , &result->m_privateKey );
206
+ if (res != 0 ) {
207
+ OATPP_LOGD (" [oatpp::mbedtls::Config::createDefaultClientConfigShared()]" , " Error. Call to mbedtls_ssl_conf_own_cert() failed, return value=%d." , res);
208
+ throw std::runtime_error (" [oatpp::mbedtls::Config::createDefaultClientConfigShared()]: Error. Call to mbedtls_ssl_conf_own_cert() failed." );
209
+ }
210
+
211
+ return result;
212
+ }
213
+
154
214
mbedtls_ssl_config* Config::getTLSConfig () {
155
215
return &m_config;
156
216
}
0 commit comments