@@ -147,6 +147,10 @@ namespace swoole {
147
147
#define HTTP2_H2_14_ALPN " \x05 h2-14"
148
148
#define HTTP1_NPN " \x08 http/1.1"
149
149
150
+ #define ssl_error (str, ...) \
151
+ long _ssl_error = ERR_get_error(); \
152
+ swoole_warning (str " , Error: %s[%ld]" , ##__VA_ARGS__, ERR_reason_error_string(_ssl_error), _ssl_error);
153
+
150
154
#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
151
155
static int ssl_alpn_advertised (SSL *ssl, const uchar **out, uchar *outlen, const uchar *in, uint32_t inlen, void *arg) {
152
156
unsigned int protos_len;
@@ -201,8 +205,7 @@ bool SSLContext::create() {
201
205
}
202
206
context = SSL_CTX_new (method);
203
207
if (context == nullptr ) {
204
- int error = ERR_get_error ();
205
- swoole_warning (" SSL_CTX_new() failed, Error: %s[%d]" , ERR_reason_error_string (error), error);
208
+ ssl_error (" SSL_CTX_new() failed" );
206
209
return false ;
207
210
}
208
211
@@ -310,23 +313,15 @@ bool SSLContext::create() {
310
313
* set the local certificate from CertFile
311
314
*/
312
315
if (SSL_CTX_use_certificate_file (context, cert_file.c_str (), SSL_FILETYPE_PEM) <= 0 ) {
313
- int error = ERR_get_error ();
314
- swoole_warning (" SSL_CTX_use_certificate_file(%s) failed, Error: %s[%d]" ,
315
- cert_file.c_str (),
316
- ERR_reason_error_string (error),
317
- error);
316
+ ssl_error (" SSL_CTX_use_certificate_file(%s) failed" , cert_file.c_str ());
318
317
return true ;
319
318
}
320
319
/*
321
320
* if the crt file have many certificate entry ,means certificate chain
322
321
* we need call this function
323
322
*/
324
323
if (SSL_CTX_use_certificate_chain_file (context, cert_file.c_str ()) <= 0 ) {
325
- int error = ERR_get_error ();
326
- swoole_warning (" SSL_CTX_use_certificate_chain_file(%s) failed, Error: %s[%d]" ,
327
- cert_file.c_str (),
328
- ERR_reason_error_string (error),
329
- error);
324
+ ssl_error (" SSL_CTX_use_certificate_chain_file(%s) failed" , cert_file.c_str ());
330
325
return false ;
331
326
}
332
327
}
@@ -335,18 +330,14 @@ bool SSLContext::create() {
335
330
* set the private key from KeyFile (may be the same as CertFile)
336
331
*/
337
332
if (SSL_CTX_use_PrivateKey_file (context, key_file.c_str (), SSL_FILETYPE_PEM) <= 0 ) {
338
- int error = ERR_get_error ();
339
- swoole_warning (" SSL_CTX_use_PrivateKey_file(%s) failed, Error: %s[%d]" ,
340
- key_file.c_str (),
341
- ERR_reason_error_string (error),
342
- error);
333
+ ssl_error (" SSL_CTX_use_PrivateKey_file(%s) failed" , key_file.c_str ());
343
334
return false ;
344
335
}
345
336
/*
346
337
* verify private key
347
338
*/
348
339
if (!SSL_CTX_check_private_key (context)) {
349
- swoole_warning ( " Private key does not match the public certificate " );
340
+ ssl_error ( " SSL_CTX_check_private_key() failed " );
350
341
return false ;
351
342
}
352
343
}
@@ -370,6 +361,19 @@ bool SSLContext::create() {
370
361
}
371
362
372
363
if (http || http_v2) {
364
+ unsigned int protos_len;
365
+ const char *protos;
366
+ if (http_v2) {
367
+ protos = HTTP2_H2_ALPN HTTP1_NPN;
368
+ protos_len = sizeof (HTTP2_H2_ALPN HTTP1_NPN) - 1 ;
369
+ } else {
370
+ protos = HTTP1_NPN;
371
+ protos_len = sizeof (HTTP1_NPN) - 1 ;
372
+ }
373
+ if (SSL_CTX_set_alpn_protos (context, (const uchar *) protos, protos_len) < 0 ) {
374
+ ssl_error (" SSL_CTX_set_alpn_protos(%s) failed" , protos);
375
+ return false ;
376
+ }
373
377
#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
374
378
SSL_CTX_set_alpn_select_cb (context, ssl_alpn_advertised, (void *) this );
375
379
#endif
@@ -382,7 +386,6 @@ bool SSLContext::create() {
382
386
#endif
383
387
384
388
if (!client_cert_file.empty () && !set_client_certificate ()) {
385
- swoole_warning (" set_client_certificate() error" );
386
389
return false ;
387
390
}
388
391
@@ -403,7 +406,7 @@ bool SSLContext::set_capath() {
403
406
}
404
407
} else {
405
408
if (!SSL_CTX_set_default_verify_paths (context)) {
406
- swoole_warning ( " Unable to set default verify locations and no CA settings specified " );
409
+ ssl_error ( " SSL_CTX_set_default_verify_paths() failed " );
407
410
return false ;
408
411
}
409
412
}
@@ -422,11 +425,12 @@ bool SSLContext::set_ciphers() {
422
425
423
426
if (!ciphers.empty ()) {
424
427
if (SSL_CTX_set_cipher_list (context, ciphers.c_str ()) == 0 ) {
425
- swoole_warning (" SSL_CTX_set_cipher_list(\" %s\" ) failed" , ciphers.c_str ());
428
+ ssl_error (" SSL_CTX_set_cipher_list(\" %s\" ) failed" , ciphers.c_str ());
426
429
return false ;
427
430
}
428
- if (prefer_server_ciphers) {
429
- SSL_CTX_set_options (context, SSL_OP_CIPHER_SERVER_PREFERENCE);
431
+ if (prefer_server_ciphers && !SSL_CTX_set_options (context, SSL_OP_CIPHER_SERVER_PREFERENCE)) {
432
+ ssl_error (" SSL_CTX_set_options(SSL_OP_CIPHER_SERVER_PREFERENCE) failed" );
433
+ return false ;
430
434
}
431
435
}
432
436
@@ -454,14 +458,14 @@ bool SSLContext::set_client_certificate() {
454
458
SSL_CTX_set_verify_depth (context, depth);
455
459
456
460
if (SSL_CTX_load_verify_locations (context, cert_file, nullptr ) == 0 ) {
457
- swoole_warning (" SSL_CTX_load_verify_locations(\" %s\" ) failed" , cert_file);
461
+ ssl_error (" SSL_CTX_load_verify_locations(\" %s\" ) failed" , cert_file);
458
462
return false ;
459
463
}
460
464
461
465
ERR_clear_error ();
462
466
list = SSL_load_client_CA_file (cert_file);
463
467
if (list == nullptr ) {
464
- swoole_warning (" SSL_load_client_CA_file(\" %s\" ) failed" , cert_file);
468
+ ssl_error (" SSL_load_client_CA_file(\" %s\" ) failed" , cert_file);
465
469
return false ;
466
470
}
467
471
@@ -540,28 +544,28 @@ bool SSLContext::set_dhparam() {
540
544
541
545
bio = BIO_new_file (file, " r" );
542
546
if (bio == nullptr ) {
543
- swoole_warning (" BIO_new_file(%s) failed" , file);
547
+ ssl_error (" BIO_new_file(%s) failed" , file);
544
548
return false ;
545
549
}
546
550
547
551
#if OPENSSL_VERSION_MAJOR >= 3
548
552
EVP_PKEY *pkey = PEM_read_bio_Parameters (bio, nullptr );
549
553
if (pkey == nullptr ) {
550
- swoole_warning (" PEM_read_bio_Parameters('%s') failed" , file);
554
+ ssl_error (" PEM_read_bio_Parameters('%s') failed" , file);
551
555
BIO_free (bio);
552
556
return false ;
553
557
}
554
558
555
559
if (SSL_CTX_set0_tmp_dh_pkey (context, pkey) != 1 ) {
556
- swoole_warning (" SSL_CTX_set0_tmp_dh_pkey('%s') failed" , file);
560
+ ssl_error (" SSL_CTX_set0_tmp_dh_pkey('%s') failed" , file);
557
561
EVP_PKEY_free (pkey);
558
562
BIO_free (bio);
559
563
return false ;
560
564
}
561
565
#else
562
566
DH *dh = PEM_read_bio_DHparams (bio, nullptr , nullptr , nullptr );
563
567
if (dh == nullptr ) {
564
- swoole_warning (" PEM_read_bio_DHparams(%s) failed" , file);
568
+ ssl_error (" PEM_read_bio_DHparams(%s) failed" , file);
565
569
BIO_free (bio);
566
570
return false ;
567
571
}
0 commit comments