Skip to content

Commit 590bfb5

Browse files
ebiggersjarkkojs
authored andcommitted
KEYS: asymmetric: properly validate hash_algo and encoding
It is insecure to allow arbitrary hash algorithms and signature encodings to be used with arbitrary signature algorithms. Notably, ECDSA, ECRDSA, and SM2 all sign/verify raw hash values and don't disambiguate between different hash algorithms like RSA PKCS#1 v1.5 padding does. Therefore, they need to be restricted to certain sets of hash algorithms (ideally just one, but in practice small sets are used). Additionally, the encoding is an integral part of modern signature algorithms, and is not supposed to vary. Therefore, tighten the checks of hash_algo and encoding done by software_key_determine_akcipher(). Also rearrange the parameters to software_key_determine_akcipher() to put the public_key first, as this is the most important parameter and it often determines everything else. Fixes: 299f561 ("x509: Add support for parsing x509 certs with ECDSA keys") Fixes: 2155256 ("X.509: support OSCCA SM2-with-SM3 certificate verification") Fixes: 0d7a786 ("crypto: ecrdsa - add EC-RDSA (GOST 34.10) algorithm") Cc: stable@vger.kernel.org Tested-by: Stefan Berger <stefanb@linux.ibm.com> Tested-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com> Signed-off-by: Eric Biggers <ebiggers@google.com> Reviewed-by: Vitaly Chikunov <vt@altlinux.org> Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
1 parent 2abc9c2 commit 590bfb5

1 file changed

Lines changed: 76 additions & 35 deletions

File tree

crypto/asymmetric_keys/public_key.c

Lines changed: 76 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -60,39 +60,83 @@ static void public_key_destroy(void *payload0, void *payload3)
6060
}
6161

6262
/*
63-
* Determine the crypto algorithm name.
63+
* Given a public_key, and an encoding and hash_algo to be used for signing
64+
* and/or verification with that key, determine the name of the corresponding
65+
* akcipher algorithm. Also check that encoding and hash_algo are allowed.
6466
*/
65-
static
66-
int software_key_determine_akcipher(const char *encoding,
67-
const char *hash_algo,
68-
const struct public_key *pkey,
69-
char alg_name[CRYPTO_MAX_ALG_NAME])
67+
static int
68+
software_key_determine_akcipher(const struct public_key *pkey,
69+
const char *encoding, const char *hash_algo,
70+
char alg_name[CRYPTO_MAX_ALG_NAME])
7071
{
7172
int n;
7273

73-
if (strcmp(encoding, "pkcs1") == 0) {
74-
/* The data wangled by the RSA algorithm is typically padded
75-
* and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
76-
* sec 8.2].
74+
if (!encoding)
75+
return -EINVAL;
76+
77+
if (strcmp(pkey->pkey_algo, "rsa") == 0) {
78+
/*
79+
* RSA signatures usually use EMSA-PKCS1-1_5 [RFC3447 sec 8.2].
80+
*/
81+
if (strcmp(encoding, "pkcs1") == 0) {
82+
if (!hash_algo)
83+
n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
84+
"pkcs1pad(%s)",
85+
pkey->pkey_algo);
86+
else
87+
n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
88+
"pkcs1pad(%s,%s)",
89+
pkey->pkey_algo, hash_algo);
90+
return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0;
91+
}
92+
if (strcmp(encoding, "raw") != 0)
93+
return -EINVAL;
94+
/*
95+
* Raw RSA cannot differentiate between different hash
96+
* algorithms.
97+
*/
98+
if (hash_algo)
99+
return -EINVAL;
100+
} else if (strncmp(pkey->pkey_algo, "ecdsa", 5) == 0) {
101+
if (strcmp(encoding, "x962") != 0)
102+
return -EINVAL;
103+
/*
104+
* ECDSA signatures are taken over a raw hash, so they don't
105+
* differentiate between different hash algorithms. That means
106+
* that the verifier should hard-code a specific hash algorithm.
107+
* Unfortunately, in practice ECDSA is used with multiple SHAs,
108+
* so we have to allow all of them and not just one.
77109
*/
78110
if (!hash_algo)
79-
n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
80-
"pkcs1pad(%s)",
81-
pkey->pkey_algo);
82-
else
83-
n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
84-
"pkcs1pad(%s,%s)",
85-
pkey->pkey_algo, hash_algo);
86-
return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0;
87-
}
88-
89-
if (strcmp(encoding, "raw") == 0 ||
90-
strcmp(encoding, "x962") == 0) {
91-
strcpy(alg_name, pkey->pkey_algo);
92-
return 0;
111+
return -EINVAL;
112+
if (strcmp(hash_algo, "sha1") != 0 &&
113+
strcmp(hash_algo, "sha224") != 0 &&
114+
strcmp(hash_algo, "sha256") != 0 &&
115+
strcmp(hash_algo, "sha384") != 0 &&
116+
strcmp(hash_algo, "sha512") != 0)
117+
return -EINVAL;
118+
} else if (strcmp(pkey->pkey_algo, "sm2") == 0) {
119+
if (strcmp(encoding, "raw") != 0)
120+
return -EINVAL;
121+
if (!hash_algo)
122+
return -EINVAL;
123+
if (strcmp(hash_algo, "sm3") != 0)
124+
return -EINVAL;
125+
} else if (strcmp(pkey->pkey_algo, "ecrdsa") == 0) {
126+
if (strcmp(encoding, "raw") != 0)
127+
return -EINVAL;
128+
if (!hash_algo)
129+
return -EINVAL;
130+
if (strcmp(hash_algo, "streebog256") != 0 &&
131+
strcmp(hash_algo, "streebog512") != 0)
132+
return -EINVAL;
133+
} else {
134+
/* Unknown public key algorithm */
135+
return -ENOPKG;
93136
}
94-
95-
return -ENOPKG;
137+
if (strscpy(alg_name, pkey->pkey_algo, CRYPTO_MAX_ALG_NAME) < 0)
138+
return -EINVAL;
139+
return 0;
96140
}
97141

98142
static u8 *pkey_pack_u32(u8 *dst, u32 val)
@@ -113,9 +157,8 @@ static int software_key_query(const struct kernel_pkey_params *params,
113157
u8 *key, *ptr;
114158
int ret, len;
115159

116-
ret = software_key_determine_akcipher(params->encoding,
117-
params->hash_algo,
118-
pkey, alg_name);
160+
ret = software_key_determine_akcipher(pkey, params->encoding,
161+
params->hash_algo, alg_name);
119162
if (ret < 0)
120163
return ret;
121164

@@ -179,9 +222,8 @@ static int software_key_eds_op(struct kernel_pkey_params *params,
179222

180223
pr_devel("==>%s()\n", __func__);
181224

182-
ret = software_key_determine_akcipher(params->encoding,
183-
params->hash_algo,
184-
pkey, alg_name);
225+
ret = software_key_determine_akcipher(pkey, params->encoding,
226+
params->hash_algo, alg_name);
185227
if (ret < 0)
186228
return ret;
187229

@@ -340,9 +382,8 @@ int public_key_verify_signature(const struct public_key *pkey,
340382
return -EKEYREJECTED;
341383
}
342384

343-
ret = software_key_determine_akcipher(sig->encoding,
344-
sig->hash_algo,
345-
pkey, alg_name);
385+
ret = software_key_determine_akcipher(pkey, sig->encoding,
386+
sig->hash_algo, alg_name);
346387
if (ret < 0)
347388
return ret;
348389

0 commit comments

Comments
 (0)