1 /* Instantiate a public key crypto key from an X.509 Certificate
3 * Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #define pr_fmt(fmt) "ASYM: "fmt
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/err.h>
16 #include <crypto/public_key.h>
17 #include "asymmetric_keys.h"
19 static bool use_builtin_keys
;
20 static struct asymmetric_key_id
*ca_keyid
;
24 struct asymmetric_key_id id
;
25 unsigned char data
[10];
28 static int __init
ca_keys_setup(char *str
)
30 if (!str
) /* default system keyring */
33 if (strncmp(str
, "id:", 3) == 0) {
34 struct asymmetric_key_id
*p
= &cakey
.id
;
35 size_t hexlen
= (strlen(str
) - 3) / 2;
38 if (hexlen
== 0 || hexlen
> sizeof(cakey
.data
)) {
39 pr_err("Missing or invalid ca_keys id\n");
43 ret
= __asymmetric_key_hex_to_key_id(str
+ 3, p
, hexlen
);
45 pr_err("Unparsable ca_keys id hex string\n");
47 ca_keyid
= p
; /* owner key 'id:xxxxxx' */
48 } else if (strcmp(str
, "builtin") == 0) {
49 use_builtin_keys
= true;
54 __setup("ca_keys=", ca_keys_setup
);
58 * restrict_link_by_signature - Restrict additions to a ring of public keys
59 * @trust_keyring: A ring of keys that can be used to vouch for the new cert.
60 * @type: The type of key being added.
61 * @payload: The payload of the new key.
63 * Check the new certificate against the ones in the trust keyring. If one of
64 * those is the signing key and validates the new certificate, then mark the
65 * new certificate as being trusted.
67 * Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a
68 * matching parent certificate in the trusted list, -EKEYREJECTED if the
69 * signature check fails or the key is blacklisted and some other error if
70 * there is a matching certificate but the signature check cannot be performed.
72 int restrict_link_by_signature(struct key
*trust_keyring
,
73 const struct key_type
*type
,
74 const union key_payload
*payload
)
76 const struct public_key_signature
*sig
;
80 pr_devel("==>%s()\n", __func__
);
85 if (type
!= &key_type_asymmetric
)
88 sig
= payload
->data
[asym_auth
];
89 if (!sig
->auth_ids
[0] && !sig
->auth_ids
[1])
92 if (ca_keyid
&& !asymmetric_key_id_partial(sig
->auth_ids
[1], ca_keyid
))
95 /* See if we have a key that signed this one. */
96 key
= find_asymmetric_key(trust_keyring
,
97 sig
->auth_ids
[0], sig
->auth_ids
[1],
102 if (use_builtin_keys
&& !test_bit(KEY_FLAG_BUILTIN
, &key
->flags
))
105 ret
= verify_signature(key
, sig
);