CWE Rule 297
Description
Rule Description
The product communicates with a host that provides a certificate, but the product does not properly ensure that the certificate is actually associated with that host.
Polyspace Implementation
The rule checker checks for Server certificate common name not checked.
Examples
Server certificate common name not checked
The issue occurs when you do not check the common name provided in the server certificate against the domain name of the server.
Typically, when a client connects to a server, the server sends a digital certificate to the client that identifies the server as a trusted entity. The certificate contains information about the server, including the common name of the server. The common name matches the server domain name that the certificate identifies as a trusted entity.
The checker raises no defect if:
You pass the SSL context as an argument to the function that calls
SSL_new
.You declare the SSL context outside the scope of the function handling the connection.
A malicious attacker might use a valid certificate to impersonate a trusted host, resulting in the client interacting with an untrusted server.
Use one of these functions to specify the server domain name that the program checks against the common name provided in the server certificate.
SSL_set_tlsext_host_name
SSL_set1_host
SSL_add1_host
#include <stdio.h> #include <stdlib.h> #include <openssl/ssl.h> #define fatal_error() exit(-1) void check_certificate(SSL_CTX* ctx, SSL* ssl) { /* Check for Client authentication error */ if (!SSL_get_peer_certificate(ssl)) { printf("SSL Client Authentication error\n"); SSL_free(ssl); SSL_CTX_free(ctx); exit(0); } /* Check for Client authentication error */ if (SSL_get_verify_result(ssl) != X509_V_OK) { printf("SSL Client Authentication error\n"); SSL_free(ssl); SSL_CTX_free(ctx); exit(0); } } void func() { int ret; SSL_CTX* ctx; SSL* ssl; /* creation context for the SSL protocol */ ctx = SSL_CTX_new(SSLv23_client_method()); if (ctx == NULL) fatal_error(); /* Handle connection */ ssl = SSL_new(ctx); SSL_set_connect_state(ssl); check_certificate(ctx, ssl); ret = SSL_connect(ssl); //Noncompliant if (ret <= 0) fatal_error(); SSL_free(ssl); SSL_CTX_free(ctx); }
In this example, an SSL structure is initiated with a client connection method. The client validates the server certificate with check_certificate
. However, the client does not check that the certificate common name matches the domain name of the server. An attacker might use the valid certificate to impersonate the trusted server.
One possible correction is to use SSL_set1_host
to specify the expected domain name that the program checks against the server certificate common name.
#include <stdio.h> #include <stdlib.h> #include <openssl/ssl.h> #define fatal_error() exit(-1) void check_certificate(SSL_CTX* ctx, SSL* ssl) { /* Check for Client authentication error */ if (!SSL_get_peer_certificate(ssl)) { printf("SSL Client Authentication error\n"); SSL_free(ssl); SSL_CTX_free(ctx); exit(0); } /* Check for Client authentication error */ if (SSL_get_verify_result(ssl) != X509_V_OK) { printf("SSL Client Authentication error\n"); SSL_free(ssl); SSL_CTX_free(ctx); exit(0); } } void func() { int ret; SSL_CTX* ctx; SSL* ssl; /* creation context for the SSL protocol */ ctx = SSL_CTX_new(SSLv23_client_method()); if (ctx == NULL) fatal_error(); /* Handle connection */ ssl = SSL_new(ctx); SSL_set_connect_state(ssl); check_certificate(ctx, ssl); ret = SSL_set1_host(ssl, "www.mysite.com"); if (ret <= 0) fatal_error(); ret = SSL_connect(ssl); if (ret <= 0) fatal_error(); SSL_free(ssl); SSL_CTX_free(ctx); }
Check Information
Category: Others |
Version History
Introduced in R2024a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)