CWE Rule 798
Description
Rule Description
The software contains hard-coded credentials, such as a password or cryptographic key, which it uses for its own inbound authentication, outbound communication to external components, or encryption of internal data.
Polyspace Implementation
The rule checker checks for these issues:
Constant cipher key
Hard-coded sensitive data
Examples
This issue occurs when you use a constant for the encryption or decryption key.
If you use a constant for the encryption or decryption key, an attacker can retrieve your key easily.
You use a key to encrypt and later decrypt your data. If a key is easily retrieved, data encrypted using that key is not secure.
Produce a random key by using a strong random number generator.
For a list of random number generators that are cryptographically
weak, see Vulnerable pseudo-random
number generator
.
#include <openssl/evp.h>
#include <stdlib.h>
#define SIZE16 16
int func(EVP_CIPHER_CTX *ctx, unsigned char *iv){
unsigned char key[SIZE16] = {'1', '2', '3', '4','5','6','b','8','9',
'1','2','3','4','5','6','7'};
return EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv, 1); //Noncompliant
}
In this example, the cipher key, key
, has
constants only. An attacker can easily retrieve a constant key.
Use a strong random number generator to produce the cipher key.
The corrected code here uses the function RAND_bytes
declared
in openssl/rand.h
.
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <stdlib.h>
#define SIZE16 16
int func(EVP_CIPHER_CTX *ctx, unsigned char *iv){
unsigned char key[SIZE16];
RAND_bytes(key, 16);
return EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv, 1);
}
This issue occurs when data that is potentially sensitive is directly exposed in the code, for instance, as string literals. The checker identifies certain data as sensitive from their use in certain functions such as password encryption functions.
Following data can be potentially sensitive.
Type of Data | Functions That Indicate Sensitive Nature of Information |
---|---|
Host name |
|
Password |
|
Database |
|
User name |
|
Salt | crypt , crypt_r (2nd argument) |
Cryptography keys and initialization vectors | OpenSSL:
|
Seed |
|
Information that is hardcoded can be queried from binaries generated from the code.
Avoid hard coding sensitive information.
// Typically, you include the header "mysql.h" with function and type declarations.
// In this example, only the required lines from the header are quoted.
typedef struct _MYSQL MYSQL;
MYSQL *mysql_real_connect(MYSQL *mysql,
const char *host, const char *user, const char *passwd,
const char *db, unsigned int port, const char *unix_socket,
unsigned long client_flag);
typedef void * DbHandle;
extern MYSQL *sql;
// File that uses functions from "mysql.h"
const char *host = "localhost";
char *user = "guest";
char *passwd;
DbHandle connect_to_database_server(const char *db)
{
passwd = (char*)"guest";
return (DbHandle)
mysql_real_connect (sql, host, user, passwd, db, 0, 0x0, 0); //Noncompliant
}
In this example, the arguments host
(host name), user
(user name), and passwd
(password) are string literals and directly exposed in the code.
Querying the generated binary for ASCII strings can reveal this information.
One possible correction is to read the data from a configuration file. In the following corrected example, the call to function connect_to_database_server_init
presumably reads the host name, user name, and password into its arguments from a secured configuration file.
// Typically, you include the header "mysql.h" with function and type declarations.
// In this example, only the required lines from the header are quoted.
typedef struct _MYSQL MYSQL;
MYSQL *mysql_real_connect(MYSQL *mysql,
const char *host, const char *user, const char *passwd,
const char *db, unsigned int port, const char *unix_socket,
unsigned long client_flag);
typedef void * DbHandle;
extern MYSQL *sql;
// File that uses functions from "mysql.h"
int connect_to_database_server_init(const char **host,
const char **user,
const char **passwd,
const char **db);
DbHandle connect_to_database_server(const char *db)
{
const char *host_from_cfg;
const char *user_from_cfg;
const char *passwd_from_cfg;
const char *db_from_cfg;
if (connect_to_database_server_init(&host_from_cfg,
&user_from_cfg,
&passwd_from_cfg,
&db_from_cfg))
{
return (DbHandle)
mysql_real_connect (sql, host_from_cfg, user_from_cfg,
passwd_from_cfg, db_from_cfg, 0, 0x0, 0);
}
else
return (DbHandle)0x0;
}
Check Information
Category: Credentials Management Errors |
Version History
Introduced in R2023a
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)