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_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.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)