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
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_nameSSL_set1_hostSSL_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.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)