CWE Rule 304
Description
Rule Description
The product implements an authentication technique, but it skips a step that weakens the technique.
Polyspace Implementation
The rule checker checks for TLS/SSL connection method not set.
Examples
TLS/SSL connection method not set
The issue occurs when you call one of these functions without explicitly setting the connection method of the TLS/SSL context.
SSL_read
SSL_write
SSL_do_handshake
The communication between server and client entities that use a TLS/SSL connection begins with a handshake. During the handshake, the parties exchange information and establish the encryption algorithm and session keys the parties use during the session. The connection methods for the server and client use different routines for the handshake.
The checker raises no defect if:
You use
SSL_connect
(client) andSSL_accept
(server) functions. These functions set the correct handshake routines automatically.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.
You cannot begin a handshake if the SSL engine does not know which connection method routines to call.
For client handshake routines, call
SSL_set_connect_state
before you begin the handshake.For server handshake routines, call
SSL_set_accept_state
before you begin the handshake.
#include <string.h> #include <stdio.h> #include <stdlib.h> #include <openssl/ssl.h> #define fatal_error() exit(-1) int len; unsigned char buf; volatile int rd; const SSL_METHOD* set_method() { return SSLv23_server_method(); } void func() { int ret; SSL_CTX* ctx; SSL* ssl; const SSL_METHOD* method = set_method(); ctx = SSL_CTX_new(method); ssl = SSL_new(ctx); switch (rd) { case 1: ret = SSL_read(ssl, (void*)buf, len); //Noncompliant if (ret <= 0) fatal_error(); break; case 2: ret = SSL_do_handshake(ssl); //Noncompliant if (ret <= 0) fatal_error(); break; default: ret = SSL_write(ssl, (void*)buf, len); //Noncompliant if (ret <= 0) fatal_error(); break; } }
In this example, the SSL context ctx
is generated with server connection method SSLv23_server_method
. However, the connection method is not set explicitly for the SSL structure ssl
before the attempt to read from the connection, initiate a handshake, or write to the connection.
One possible correction is to call SSL_set_accept_state
to set the server role for the SSL structure ssl
before you begin the handshake.
#include <string.h> #include <stdio.h> #include <stdlib.h> #include <openssl/ssl.h> #define fatal_error() exit(-1) int len; unsigned char buf; volatile int rd; const SSL_METHOD* set_method() { return SSLv23_server_method(); } void func() { int ret; SSL_CTX* ctx; SSL* ssl; const SSL_METHOD* method = set_method(); ctx = SSL_CTX_new(method); ssl = SSL_new(ctx); SSL_set_accept_state(ssl); switch (rd) { case 1: ret = SSL_read(ssl, (void*)buf, len); if (ret <= 0) fatal_error(); break; case 2: ret = SSL_do_handshake(ssl); if (ret <= 0) fatal_error(); break; default: ret = SSL_write(ssl, (void*)buf, len); if (ret <= 0) fatal_error(); break; } }
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 (한국어)