Skip to content

Commit 4e8ae72

Browse files
committed
X.509: Make algo identifiers text instead of enum
Make the identifier public key and digest algorithm fields text instead of enum. Signed-off-by: David Howells <dhowells@redhat.com> Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent d43de6c commit 4e8ae72

12 files changed

Lines changed: 61 additions & 97 deletions

File tree

‎crypto/asymmetric_keys/mscode_parser.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,25 @@ int mscode_note_digest_algo(void *context, size_t hdrlen,
8686
oid = look_up_OID(value, vlen);
8787
switch (oid) {
8888
case OID_md4:
89-
ctx->digest_algo = HASH_ALGO_MD4;
89+
ctx->digest_algo = "md4";
9090
break;
9191
case OID_md5:
92-
ctx->digest_algo = HASH_ALGO_MD5;
92+
ctx->digest_algo = "md5";
9393
break;
9494
case OID_sha1:
95-
ctx->digest_algo = HASH_ALGO_SHA1;
95+
ctx->digest_algo = "sha1";
9696
break;
9797
case OID_sha256:
98-
ctx->digest_algo = HASH_ALGO_SHA256;
98+
ctx->digest_algo = "sha256";
9999
break;
100100
case OID_sha384:
101-
ctx->digest_algo = HASH_ALGO_SHA384;
101+
ctx->digest_algo = "sha384";
102102
break;
103103
case OID_sha512:
104-
ctx->digest_algo = HASH_ALGO_SHA512;
104+
ctx->digest_algo = "sha512";
105105
break;
106106
case OID_sha224:
107-
ctx->digest_algo = HASH_ALGO_SHA224;
107+
ctx->digest_algo = "sha224";
108108
break;
109109

110110
case OID__NR:

‎crypto/asymmetric_keys/pkcs7_parser.c‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,25 +218,25 @@ int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen,
218218

219219
switch (ctx->last_oid) {
220220
case OID_md4:
221-
ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_MD4;
221+
ctx->sinfo->sig.hash_algo = "md4";
222222
break;
223223
case OID_md5:
224-
ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_MD5;
224+
ctx->sinfo->sig.hash_algo = "md5";
225225
break;
226226
case OID_sha1:
227-
ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA1;
227+
ctx->sinfo->sig.hash_algo = "sha1";
228228
break;
229229
case OID_sha256:
230-
ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA256;
230+
ctx->sinfo->sig.hash_algo = "sha256";
231231
break;
232232
case OID_sha384:
233-
ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA384;
233+
ctx->sinfo->sig.hash_algo = "sha384";
234234
break;
235235
case OID_sha512:
236-
ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA512;
236+
ctx->sinfo->sig.hash_algo = "sha512";
237237
break;
238238
case OID_sha224:
239-
ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA224;
239+
ctx->sinfo->sig.hash_algo = "sha224";
240240
default:
241241
printk("Unsupported digest algo: %u\n", ctx->last_oid);
242242
return -ENOPKG;
@@ -255,7 +255,7 @@ int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
255255

256256
switch (ctx->last_oid) {
257257
case OID_rsaEncryption:
258-
ctx->sinfo->sig.pkey_algo = PKEY_ALGO_RSA;
258+
ctx->sinfo->sig.pkey_algo = "rsa";
259259
break;
260260
default:
261261
printk("Unsupported pkey algo: %u\n", ctx->last_oid);
@@ -615,8 +615,6 @@ int pkcs7_sig_note_signature(void *context, size_t hdrlen,
615615
{
616616
struct pkcs7_parse_context *ctx = context;
617617

618-
BUG_ON(ctx->sinfo->sig.pkey_algo != PKEY_ALGO_RSA);
619-
620618
ctx->sinfo->sig.s = kmemdup(value, vlen, GFP_KERNEL);
621619
if (!ctx->sinfo->sig.s)
622620
return -ENOMEM;

‎crypto/asymmetric_keys/pkcs7_verify.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,15 @@ static int pkcs7_digest(struct pkcs7_message *pkcs7,
3131
void *digest;
3232
int ret;
3333

34-
kenter(",%u,%u", sinfo->index, sinfo->sig.pkey_hash_algo);
34+
kenter(",%u,%s", sinfo->index, sinfo->sig.hash_algo);
3535

36-
if (sinfo->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
37-
!hash_algo_name[sinfo->sig.pkey_hash_algo])
36+
if (!sinfo->sig.hash_algo)
3837
return -ENOPKG;
3938

4039
/* Allocate the hashing algorithm we're going to need and find out how
4140
* big the hash operational data will be.
4241
*/
43-
tfm = crypto_alloc_shash(hash_algo_name[sinfo->sig.pkey_hash_algo],
44-
0, 0);
42+
tfm = crypto_alloc_shash(sinfo->sig.hash_algo, 0, 0);
4543
if (IS_ERR(tfm))
4644
return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
4745

‎crypto/asymmetric_keys/public_key.c‎

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,6 @@
2424

2525
MODULE_LICENSE("GPL");
2626

27-
const char *const pkey_algo_name[PKEY_ALGO__LAST] = {
28-
[PKEY_ALGO_DSA] = "dsa",
29-
[PKEY_ALGO_RSA] = "rsa",
30-
};
31-
EXPORT_SYMBOL_GPL(pkey_algo_name);
32-
33-
const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST] = {
34-
[PKEY_ID_PGP] = "PGP",
35-
[PKEY_ID_X509] = "X509",
36-
[PKEY_ID_PKCS7] = "PKCS#7",
37-
};
38-
EXPORT_SYMBOL_GPL(pkey_id_type_name);
39-
4027
/*
4128
* Provide a part of a description of the key for /proc/keys.
4229
*/
@@ -46,9 +33,7 @@ static void public_key_describe(const struct key *asymmetric_key,
4633
struct public_key *key = asymmetric_key->payload.data[asym_crypto];
4734

4835
if (key)
49-
seq_printf(m, "%s.%s",
50-
pkey_id_type_name[key->id_type],
51-
pkey_algo_name[key->pkey_algo]);
36+
seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
5237
}
5338

5439
/*
@@ -103,15 +88,14 @@ int public_key_verify_signature(const struct public_key *pkey,
10388
BUG_ON(!sig->digest);
10489
BUG_ON(!sig->s);
10590

106-
alg_name = pkey_algo_name[sig->pkey_algo];
107-
if (sig->pkey_algo == PKEY_ALGO_RSA) {
91+
alg_name = sig->pkey_algo;
92+
if (strcmp(sig->pkey_algo, "rsa") == 0) {
10893
/* The data wangled by the RSA algorithm is typically padded
10994
* and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
11095
* sec 8.2].
11196
*/
11297
if (snprintf(alg_name_buf, CRYPTO_MAX_ALG_NAME,
113-
"pkcs1pad(rsa,%s)",
114-
hash_algo_name[sig->pkey_hash_algo]
98+
"pkcs1pad(rsa,%s)", sig->hash_algo
11599
) >= CRYPTO_MAX_ALG_NAME)
116100
return -EINVAL;
117101
alg_name = alg_name_buf;

‎crypto/asymmetric_keys/verify_pefile.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,12 @@ static int pefile_digest_pe(const void *pebuf, unsigned int pelen,
328328
void *digest;
329329
int ret;
330330

331-
kenter(",%u", ctx->digest_algo);
331+
kenter(",%s", ctx->digest_algo);
332332

333333
/* Allocate the hashing algorithm we're going to need and find out how
334334
* big the hash operational data will be.
335335
*/
336-
tfm = crypto_alloc_shash(hash_algo_name[ctx->digest_algo], 0, 0);
336+
tfm = crypto_alloc_shash(ctx->digest_algo, 0, 0);
337337
if (IS_ERR(tfm))
338338
return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
339339

‎crypto/asymmetric_keys/verify_pefile.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct pefile_context {
2828
/* PKCS#7 MS Individual Code Signing content */
2929
const void *digest; /* Digest */
3030
unsigned digest_len; /* Digest length */
31-
enum hash_algo digest_algo; /* Digest algorithm */
31+
const char *digest_algo; /* Digest algorithm */
3232
};
3333

3434
#define kenter(FMT, ...) \

‎crypto/asymmetric_keys/x509_cert_parser.c‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,33 +188,33 @@ int x509_note_pkey_algo(void *context, size_t hdrlen,
188188
return -ENOPKG; /* Unsupported combination */
189189

190190
case OID_md4WithRSAEncryption:
191-
ctx->cert->sig.pkey_hash_algo = HASH_ALGO_MD5;
192-
ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
191+
ctx->cert->sig.hash_algo = "md4";
192+
ctx->cert->sig.pkey_algo = "rsa";
193193
break;
194194

195195
case OID_sha1WithRSAEncryption:
196-
ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA1;
197-
ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
196+
ctx->cert->sig.hash_algo = "sha1";
197+
ctx->cert->sig.pkey_algo = "rsa";
198198
break;
199199

200200
case OID_sha256WithRSAEncryption:
201-
ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA256;
202-
ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
201+
ctx->cert->sig.hash_algo = "sha256";
202+
ctx->cert->sig.pkey_algo = "rsa";
203203
break;
204204

205205
case OID_sha384WithRSAEncryption:
206-
ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA384;
207-
ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
206+
ctx->cert->sig.hash_algo = "sha384";
207+
ctx->cert->sig.pkey_algo = "rsa";
208208
break;
209209

210210
case OID_sha512WithRSAEncryption:
211-
ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA512;
212-
ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
211+
ctx->cert->sig.hash_algo = "sha512";
212+
ctx->cert->sig.pkey_algo = "rsa";
213213
break;
214214

215215
case OID_sha224WithRSAEncryption:
216-
ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA224;
217-
ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
216+
ctx->cert->sig.hash_algo = "sha224";
217+
ctx->cert->sig.pkey_algo = "rsa";
218218
break;
219219
}
220220

@@ -396,7 +396,7 @@ int x509_extract_key_data(void *context, size_t hdrlen,
396396
if (ctx->last_oid != OID_rsaEncryption)
397397
return -ENOPKG;
398398

399-
ctx->cert->pub->pkey_algo = PKEY_ALGO_RSA;
399+
ctx->cert->pub->pkey_algo = "rsa";
400400

401401
/* Discard the BIT STRING metadata */
402402
ctx->key = value + 1;

‎crypto/asymmetric_keys/x509_public_key.c‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ int x509_get_sig_params(struct x509_certificate *cert)
176176
/* Allocate the hashing algorithm we're going to need and find out how
177177
* big the hash operational data will be.
178178
*/
179-
tfm = crypto_alloc_shash(hash_algo_name[cert->sig.pkey_hash_algo], 0, 0);
179+
tfm = crypto_alloc_shash(cert->sig.hash_algo, 0, 0);
180180
if (IS_ERR(tfm)) {
181181
if (PTR_ERR(tfm) == -ENOENT) {
182182
cert->unsupported_crypto = true;
@@ -291,21 +291,20 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)
291291
pr_devel("Cert Issuer: %s\n", cert->issuer);
292292
pr_devel("Cert Subject: %s\n", cert->subject);
293293

294-
if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
295-
cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
296-
cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
297-
!hash_algo_name[cert->sig.pkey_hash_algo]) {
294+
if (!cert->pub->pkey_algo ||
295+
!cert->sig.pkey_algo ||
296+
!cert->sig.hash_algo) {
298297
ret = -ENOPKG;
299298
goto error_free_cert;
300299
}
301300

302-
pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
301+
pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
303302
pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
304303
pr_devel("Cert Signature: %s + %s\n",
305-
pkey_algo_name[cert->sig.pkey_algo],
306-
hash_algo_name[cert->sig.pkey_hash_algo]);
304+
cert->sig.pkey_algo,
305+
cert->sig.hash_algo);
307306

308-
cert->pub->id_type = PKEY_ID_X509;
307+
cert->pub->id_type = "X509";
309308

310309
/* Check the signature on the key if it appears to be self-signed */
311310
if ((!cert->akid_skid && !cert->akid_id) ||

‎include/crypto/public_key.h‎

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,6 @@
1414
#ifndef _LINUX_PUBLIC_KEY_H
1515
#define _LINUX_PUBLIC_KEY_H
1616

17-
#include <crypto/hash_info.h>
18-
19-
enum pkey_algo {
20-
PKEY_ALGO_DSA,
21-
PKEY_ALGO_RSA,
22-
PKEY_ALGO__LAST
23-
};
24-
25-
extern const char *const pkey_algo_name[PKEY_ALGO__LAST];
26-
27-
/* asymmetric key implementation supports only up to SHA224 */
28-
#define PKEY_HASH__LAST (HASH_ALGO_SHA224 + 1)
29-
30-
enum pkey_id_type {
31-
PKEY_ID_PGP, /* OpenPGP generated key ID */
32-
PKEY_ID_X509, /* X.509 arbitrary subjectKeyIdentifier */
33-
PKEY_ID_PKCS7, /* Signature in PKCS#7 message */
34-
PKEY_ID_TYPE__LAST
35-
};
36-
37-
extern const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST];
38-
3917
/*
4018
* The use to which an asymmetric key is being put.
4119
*/
@@ -59,8 +37,8 @@ extern const char *const key_being_used_for[NR__KEY_BEING_USED_FOR];
5937
struct public_key {
6038
void *key;
6139
u32 keylen;
62-
enum pkey_algo pkey_algo : 8;
63-
enum pkey_id_type id_type : 8;
40+
const char *id_type;
41+
const char *pkey_algo;
6442
};
6543

6644
extern void public_key_destroy(void *payload);
@@ -73,8 +51,8 @@ struct public_key_signature {
7351
u32 s_size; /* Number of bytes in signature */
7452
u8 *digest;
7553
u8 digest_size; /* Number of bytes in digest */
76-
enum pkey_algo pkey_algo : 8;
77-
enum hash_algo pkey_hash_algo : 8;
54+
const char *pkey_algo;
55+
const char *hash_algo;
7856
};
7957

8058
extern struct asymmetric_key_subtype public_key_subtype;

‎kernel/module_signing.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
#include <crypto/public_key.h>
1717
#include "module-internal.h"
1818

19+
enum pkey_id_type {
20+
PKEY_ID_PGP, /* OpenPGP generated key ID */
21+
PKEY_ID_X509, /* X.509 arbitrary subjectKeyIdentifier */
22+
PKEY_ID_PKCS7, /* Signature in PKCS#7 message */
23+
};
24+
1925
/*
2026
* Module signature information block.
2127
*

0 commit comments

Comments
 (0)