@@ -459,14 +459,64 @@ void *ma_tls_init(MYSQL *mysql)
459
459
return NULL ;
460
460
}
461
461
462
+ unsigned int ma_tls_get_peer_cert_info (MARIADB_TLS * ctls )
463
+ {
464
+ X509 * cert ;
465
+ SSL * ssl ;
466
+
467
+ if (!ctls || !ctls -> ssl )
468
+ return 1 ;
469
+
470
+ /* Did we already read peer cert information ? */
471
+ if (ctls -> cert_info .version )
472
+ return 0 ;
473
+
474
+ ssl = (SSL * )ctls -> ssl ;
475
+
476
+ /* Store peer certificate information */
477
+ if ((cert = SSL_get_peer_certificate (ssl )))
478
+ {
479
+ char fp [33 ];
480
+ #if OPENSSL_VERSION_NUMBER >= 0x10101000L
481
+ const ASN1_TIME * not_before = X509_get0_notBefore (cert ),
482
+ * not_after = X509_get0_notAfter (cert );
483
+ ASN1_TIME_to_tm (not_before , (struct tm * )& ctls -> cert_info .not_before );
484
+ ASN1_TIME_to_tm (not_after , (struct tm * )& ctls -> cert_info .not_after );
485
+ #else
486
+ const ASN1_TIME * not_before = X509_get_notBefore (cert ),
487
+ * not_after = X509_get_notAfter (cert );
488
+ time_t now , from , to ;
489
+ int pday , psec ;
490
+ /* ANS1_TIME_diff returns days and seconds between now and the
491
+ specified ASN1_TIME */
492
+ time (& now );
493
+ ASN1_TIME_diff (& pday , & psec , not_before , NULL );
494
+ from = now - (pday * 86400 + psec );
495
+ gmtime_r (& from , & ctls -> cert_info .not_before );
496
+ ASN1_TIME_diff (& pday , & psec , NULL , not_after );
497
+ to = now + (pday * 86400 + psec );
498
+ gmtime_r (& to , & ctls -> cert_info .not_after );
499
+ #endif
500
+ ctls -> cert_info .subject = X509_NAME_oneline (X509_get_subject_name (cert ), NULL , 0 );
501
+ ctls -> cert_info .issuer = X509_NAME_oneline (X509_get_issuer_name (cert ), NULL , 0 );
502
+ ctls -> cert_info .version = X509_get_version (cert ) + 1 ;
503
+
504
+ ma_tls_get_finger_print (ctls , MA_HASH_SHA256 , fp , 33 );
505
+ mysql_hex_string (ctls -> cert_info .fingerprint , fp , 32 );
506
+
507
+ X509_free (cert );
508
+ return 0 ;
509
+ }
510
+ return 1 ;
511
+ }
512
+
462
513
my_bool ma_tls_connect (MARIADB_TLS * ctls )
463
514
{
464
515
SSL * ssl = (SSL * )ctls -> ssl ;
465
516
my_bool blocking , try_connect = 1 ;
466
517
MYSQL * mysql ;
467
518
MARIADB_PVIO * pvio ;
468
519
int rc ;
469
- X509 * cert ;
470
520
#ifdef OPENSSL_USE_BIOMETHOD
471
521
BIO_METHOD * bio_method = NULL ;
472
522
BIO * bio ;
@@ -533,40 +583,6 @@ my_bool ma_tls_connect(MARIADB_TLS *ctls)
533
583
}
534
584
pvio -> ctls -> ssl = ctls -> ssl = (void * )ssl ;
535
585
536
- /* Store peer certificate information */
537
- if ((cert = SSL_get_peer_certificate (ssl )))
538
- {
539
- char fp [33 ];
540
- #if OPENSSL_VERSION_NUMBER >= 0x10101000L
541
- const ASN1_TIME * not_before = X509_get0_notBefore (cert ),
542
- * not_after = X509_get0_notAfter (cert );
543
- ASN1_TIME_to_tm (not_before , (struct tm * )& ctls -> cert_info .not_before );
544
- ASN1_TIME_to_tm (not_after , (struct tm * )& ctls -> cert_info .not_after );
545
- #else
546
- const ASN1_TIME * not_before = X509_get_notBefore (cert ),
547
- * not_after = X509_get_notAfter (cert );
548
- time_t now , from , to ;
549
- int pday , psec ;
550
- /* ANS1_TIME_diff returns days and seconds between now and the
551
- specified ASN1_TIME */
552
- time (& now );
553
- ASN1_TIME_diff (& pday , & psec , not_before , NULL );
554
- from = now - (pday * 86400 + psec );
555
- gmtime_r (& from , & ctls -> cert_info .not_before );
556
- ASN1_TIME_diff (& pday , & psec , NULL , not_after );
557
- to = now + (pday * 86400 + psec );
558
- gmtime_r (& to , & ctls -> cert_info .not_after );
559
- #endif
560
- ctls -> cert_info .subject = X509_NAME_oneline (X509_get_subject_name (cert ), NULL , 0 );
561
- ctls -> cert_info .issuer = X509_NAME_oneline (X509_get_issuer_name (cert ), NULL , 0 );
562
- ctls -> cert_info .version = X509_get_version (cert ) + 1 ;
563
-
564
- ma_tls_get_finger_print (ctls , MA_HASH_SHA256 , fp , 33 );
565
- mysql_hex_string (ctls -> cert_info .fingerprint , fp , 32 );
566
-
567
- X509_free (cert );
568
- }
569
-
570
586
return 0 ;
571
587
}
572
588
0 commit comments