Skip to content

Commit cfb664f

Browse files
committed
X.509: Move the trust validation code out to its own file
Move the X.509 trust validation code out to its own file so that it can be generalised. Signed-off-by: David Howells <dhowells@redhat.com>
1 parent 5f7f5c8 commit cfb664f

File tree

4 files changed

+116
-80
lines changed

4 files changed

+116
-80
lines changed

crypto/asymmetric_keys/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
obj-$(CONFIG_ASYMMETRIC_KEY_TYPE) += asymmetric_keys.o
66

7-
asymmetric_keys-y := asymmetric_type.o signature.o
7+
asymmetric_keys-y := \
8+
asymmetric_type.o \
9+
restrict.o \
10+
signature.o
811

912
obj-$(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o
1013

crypto/asymmetric_keys/restrict.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/* Instantiate a public key crypto key from an X.509 Certificate
2+
*
3+
* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4+
* Written by David Howells (dhowells@redhat.com)
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU General Public Licence
8+
* as published by the Free Software Foundation; either version
9+
* 2 of the Licence, or (at your option) any later version.
10+
*/
11+
12+
#define pr_fmt(fmt) "X.509: "fmt
13+
#include <linux/module.h>
14+
#include <linux/kernel.h>
15+
#include <linux/slab.h>
16+
#include <linux/err.h>
17+
#include <linux/mpi.h>
18+
#include <linux/asn1_decoder.h>
19+
#include <keys/asymmetric-subtype.h>
20+
#include <keys/asymmetric-parser.h>
21+
#include <keys/system_keyring.h>
22+
#include <crypto/hash.h>
23+
#include <crypto/public_key.h>
24+
#include "asymmetric_keys.h"
25+
#include "x509_parser.h"
26+
27+
static bool use_builtin_keys;
28+
static struct asymmetric_key_id *ca_keyid;
29+
30+
#ifndef MODULE
31+
static struct {
32+
struct asymmetric_key_id id;
33+
unsigned char data[10];
34+
} cakey;
35+
36+
static int __init ca_keys_setup(char *str)
37+
{
38+
if (!str) /* default system keyring */
39+
return 1;
40+
41+
if (strncmp(str, "id:", 3) == 0) {
42+
struct asymmetric_key_id *p = &cakey.id;
43+
size_t hexlen = (strlen(str) - 3) / 2;
44+
int ret;
45+
46+
if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
47+
pr_err("Missing or invalid ca_keys id\n");
48+
return 1;
49+
}
50+
51+
ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
52+
if (ret < 0)
53+
pr_err("Unparsable ca_keys id hex string\n");
54+
else
55+
ca_keyid = p; /* owner key 'id:xxxxxx' */
56+
} else if (strcmp(str, "builtin") == 0) {
57+
use_builtin_keys = true;
58+
}
59+
60+
return 1;
61+
}
62+
__setup("ca_keys=", ca_keys_setup);
63+
#endif
64+
65+
/*
66+
* Check the new certificate against the ones in the trust keyring. If one of
67+
* those is the signing key and validates the new certificate, then mark the
68+
* new certificate as being trusted.
69+
*
70+
* Return 0 if the new certificate was successfully validated, 1 if we couldn't
71+
* find a matching parent certificate in the trusted list and an error if there
72+
* is a matching certificate but the signature check fails.
73+
*/
74+
int x509_validate_trust(struct x509_certificate *cert,
75+
struct key *trust_keyring)
76+
{
77+
struct public_key_signature *sig = cert->sig;
78+
struct key *key;
79+
int ret = 1;
80+
81+
if (!sig->auth_ids[0] && !sig->auth_ids[1])
82+
return 1;
83+
84+
if (!trust_keyring)
85+
return -EOPNOTSUPP;
86+
if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
87+
return -EPERM;
88+
if (cert->unsupported_sig)
89+
return -ENOPKG;
90+
91+
key = find_asymmetric_key(trust_keyring,
92+
sig->auth_ids[0], sig->auth_ids[1],
93+
false);
94+
if (IS_ERR(key))
95+
return PTR_ERR(key);
96+
97+
if (!use_builtin_keys ||
98+
test_bit(KEY_FLAG_BUILTIN, &key->flags)) {
99+
ret = verify_signature(key, cert->sig);
100+
if (ret == -ENOPKG)
101+
cert->unsupported_sig = true;
102+
}
103+
key_put(key);
104+
return ret;
105+
}
106+
EXPORT_SYMBOL_GPL(x509_validate_trust);

crypto/asymmetric_keys/x509_parser.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ extern int x509_decode_time(time64_t *_t, size_t hdrlen,
5858
*/
5959
extern int x509_get_sig_params(struct x509_certificate *cert);
6060
extern int x509_check_for_self_signed(struct x509_certificate *cert);
61+
62+
/*
63+
* public_key_trust.c
64+
*/
65+
extern int x509_validate_trust(struct x509_certificate *cert,
66+
struct key *trust_keyring);

crypto/asymmetric_keys/x509_public_key.c

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,6 @@
2020
#include "asymmetric_keys.h"
2121
#include "x509_parser.h"
2222

23-
static bool use_builtin_keys;
24-
static struct asymmetric_key_id *ca_keyid;
25-
26-
#ifndef MODULE
27-
static struct {
28-
struct asymmetric_key_id id;
29-
unsigned char data[10];
30-
} cakey;
31-
32-
static int __init ca_keys_setup(char *str)
33-
{
34-
if (!str) /* default system keyring */
35-
return 1;
36-
37-
if (strncmp(str, "id:", 3) == 0) {
38-
struct asymmetric_key_id *p = &cakey.id;
39-
size_t hexlen = (strlen(str) - 3) / 2;
40-
int ret;
41-
42-
if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
43-
pr_err("Missing or invalid ca_keys id\n");
44-
return 1;
45-
}
46-
47-
ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
48-
if (ret < 0)
49-
pr_err("Unparsable ca_keys id hex string\n");
50-
else
51-
ca_keyid = p; /* owner key 'id:xxxxxx' */
52-
} else if (strcmp(str, "builtin") == 0) {
53-
use_builtin_keys = true;
54-
}
55-
56-
return 1;
57-
}
58-
__setup("ca_keys=", ca_keys_setup);
59-
#endif
60-
6123
/*
6224
* Set up the signature parameters in an X.509 certificate. This involves
6325
* digesting the signed data and extracting the signature.
@@ -187,47 +149,6 @@ int x509_check_for_self_signed(struct x509_certificate *cert)
187149
return 0;
188150
}
189151

190-
/*
191-
* Check the new certificate against the ones in the trust keyring. If one of
192-
* those is the signing key and validates the new certificate, then mark the
193-
* new certificate as being trusted.
194-
*
195-
* Return 0 if the new certificate was successfully validated, 1 if we couldn't
196-
* find a matching parent certificate in the trusted list and an error if there
197-
* is a matching certificate but the signature check fails.
198-
*/
199-
static int x509_validate_trust(struct x509_certificate *cert,
200-
struct key *trust_keyring)
201-
{
202-
struct public_key_signature *sig = cert->sig;
203-
struct key *key;
204-
int ret = 1;
205-
206-
if (!sig->auth_ids[0] && !sig->auth_ids[1])
207-
return 1;
208-
209-
if (!trust_keyring)
210-
return -EOPNOTSUPP;
211-
if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
212-
return -EPERM;
213-
if (cert->unsupported_sig)
214-
return -ENOPKG;
215-
216-
key = find_asymmetric_key(trust_keyring,
217-
sig->auth_ids[0], sig->auth_ids[1], false);
218-
if (IS_ERR(key))
219-
return PTR_ERR(key);
220-
221-
if (!use_builtin_keys ||
222-
test_bit(KEY_FLAG_BUILTIN, &key->flags)) {
223-
ret = verify_signature(key, cert->sig);
224-
if (ret == -ENOPKG)
225-
cert->unsupported_sig = true;
226-
}
227-
key_put(key);
228-
return ret;
229-
}
230-
231152
/*
232153
* Attempt to parse a data blob for a key as an X509 certificate.
233154
*/

0 commit comments

Comments
 (0)