The certificate Common Name (CN) does not match with the expected CN

秒速五厘米 2021-12-23 19:15 820阅读 0赞

原文地址:https://tls.mbed.org/discussions/bug-report-issues/verifying-peer-x-509-cert

Verifying peer X.509 Cert

Jan 20, 2016 21:05
Dan
05d166ec91902cd4532b8d0a0aae3c74_s_30_d_mm

I am using a modified version of ssl_client1.c to access yahoo for testing purposes. I assume their certs are installed correctly, but for some reason I keep getting the following error:

“The certificate Common Name (CN) does not match with the expected CN”

My modification to the ssl_client1.c is as follows:

  1. /*
  2. * 0.1 Initialize certificates
  3. */
  4. mbedtls_printf( " . Loading the CA root certificate ..." );
  5. fflush( stdout );
  6. char cwd_buff[PATH_MAX + 1];
  7. getcwd( cwd_buff, PATH_MAX + 1 );
  8. strcat(cwd_buff, "\\Debug\\yahoo.crt");
  9. mbedtls_printf("CA File: %s ", cwd_buff);
  10. ret = mbedtls_x509_crt_parse_file(&cacert, cwd_buff);
  11. if( ret < 0 )
  12. {
  13. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
  14. goto exit;
  15. }
  16. mbedtls_printf( " ok (%d skipped)\n", ret );

I don’t get any errors loading the cert and I do get the HTTP of Yahoo, its just the cert that seems to be off.

Jan 21, 2016 01:59
Dan
05d166ec91902cd4532b8d0a0aae3c74_s_30_d_mm

Interesting…..I just tried the ssl_client2.c program and it works fine. I guess I’m not doing something correct with using the cert. Any ideas why ssl_client1.c gives the CN error?

Feb 10, 2016 22:07
moraine
475613998183680ada592cba2cbb6086_s_30_d_mm

I reproduced the same issue using unmodified ssl_client1 and ssl_server example programs for the following versions : v2.2.1, v2.2.0 ,v2.1.4 , v1.3.16, but not with v1.2.19

For information, please find below the output of ssl_client1 when I meet the issue :

  1. . Seeding the random number generator... ok
  2. . Loading the CA root certificate ... ok (0 skipped)
  3. . Connecting to tcp/localhost/4433... ok
  4. . Setting up the SSL/TLS structure... ok
  5. . Performing the SSL/TLS handshake.../home/bmoraine/Desktop/mbed/mbedtls-2.2.0/library/ssl_tls.c:4400: x509_verify_cert() returned -9984 (-0x2700)
  6. ok
  7. . Verifying peer X.509 certificate... failed
  8. ! The certificate Common Name (CN) does not match with the expected CN
  9. > Write to server: 18 bytes written
  10. GET / HTTP/1.0
  11. < Read from server: 150 bytes read
  12. HTTP/1.0 200 OK
  13. Content-Type: text/html
  14. <h2>mbed TLS Test Server</h2>
  15. <p>Successful connection using: TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384</p>
  16. /home/bmoraine/Desktop/mbed/mbedtls-2.2.0/library/ssl_tls.c:6509: mbedtls_ssl_read_record() returned -30848 (-0x7880)
  17. Last error was: -30848 - SSL - The peer notified us that the connection is going to be closed

Regarding ssl_server output no error is displayed :

  1. . Loading the server cert. and key... ok
  2. . Bind on https://localhost:4433/ ... ok
  3. . Seeding the random number generator... ok
  4. . Setting up the SSL data.... ok
  5. . Waiting for a remote connection ... ok
  6. . Performing the SSL/TLS handshake... ok
  7. < Read from client: 18 bytes read
  8. GET / HTTP/1.0
  9. > Write to client: 150 bytes written
  10. HTTP/1.0 200 OK
  11. Content-Type: text/html
  12. <h2>mbed TLS Test Server</h2>
  13. <p>Successful connection using: TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384</p>
  14. . Closing the connection... ok
  15. . Waiting for a remote connection ...

Is there a regression in ssl_client1 example or in the library itself?

Feb 12, 2016 11:49
moraine
475613998183680ada592cba2cbb6086_s_30_d_mm

It seems I fix the issue by replacing hostname parameter in the call of mbedtls_ssl_set_hostname() on line 180

I replace :

  1. if( ( ret = mbedtls_ssl_set_hostname( &ssl, "mbed TLS Server 1" ) ) != 0 )
  2. {
  3. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
  4. goto exit;
  5. }

by

  1. if( ( ret = mbedtls_ssl_set_hostname( &pms->ssl, SERVER_NAME ) ) != 0 )
  2. {
  3. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
  4. goto exit;
  5. }

For information, SERVER_NAME is defined on line 63

  1. #define SERVER_NAME "localhost"

and is used previously used by mbedtls_net_connect() on line 141

按上文中所述将if( ( ret = mbedtls_ssl_set_hostname( &pms->ssl, localhost) ) != 0 ) 即可

发表评论

表情:
评论列表 (有 0 条评论,820人围观)

还没有评论,来说两句吧...

相关阅读