blob: 09b1374dc6191b04b4c892b1cc0529c1e7a444ce [file] [log] [blame]
David Howellscfb664f2016-04-06 16:14:26 +01001/* Instantiate a public key crypto key from an X.509 Certificate
2 *
David Howellsa511e1a2016-04-06 16:14:26 +01003 * Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved.
David Howellscfb664f2016-04-06 16:14:26 +01004 * 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
David Howellsa511e1a2016-04-06 16:14:26 +010012#define pr_fmt(fmt) "ASYM: "fmt
David Howellscfb664f2016-04-06 16:14:26 +010013#include <linux/module.h>
14#include <linux/kernel.h>
David Howellscfb664f2016-04-06 16:14:26 +010015#include <linux/err.h>
David Howellscfb664f2016-04-06 16:14:26 +010016#include <crypto/public_key.h>
17#include "asymmetric_keys.h"
David Howellscfb664f2016-04-06 16:14:26 +010018
19static bool use_builtin_keys;
20static struct asymmetric_key_id *ca_keyid;
21
22#ifndef MODULE
23static struct {
24 struct asymmetric_key_id id;
25 unsigned char data[10];
26} cakey;
27
28static int __init ca_keys_setup(char *str)
29{
30 if (!str) /* default system keyring */
31 return 1;
32
33 if (strncmp(str, "id:", 3) == 0) {
34 struct asymmetric_key_id *p = &cakey.id;
35 size_t hexlen = (strlen(str) - 3) / 2;
36 int ret;
37
38 if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
39 pr_err("Missing or invalid ca_keys id\n");
40 return 1;
41 }
42
43 ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
44 if (ret < 0)
45 pr_err("Unparsable ca_keys id hex string\n");
46 else
47 ca_keyid = p; /* owner key 'id:xxxxxx' */
48 } else if (strcmp(str, "builtin") == 0) {
49 use_builtin_keys = true;
50 }
51
52 return 1;
53}
54__setup("ca_keys=", ca_keys_setup);
55#endif
56
David Howellsa511e1a2016-04-06 16:14:26 +010057/**
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.
62 *
David Howellscfb664f2016-04-06 16:14:26 +010063 * 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.
66 *
David Howellsa511e1a2016-04-06 16:14:26 +010067 * 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
Eric Biggersf2915982018-02-26 10:17:15 -080069 * signature check fails or the key is blacklisted, -ENOPKG if the signature
70 * uses unsupported crypto, or some other error if there is a matching
71 * certificate but the signature check cannot be performed.
David Howellscfb664f2016-04-06 16:14:26 +010072 */
David Howellsa511e1a2016-04-06 16:14:26 +010073int restrict_link_by_signature(struct key *trust_keyring,
74 const struct key_type *type,
75 const union key_payload *payload)
David Howellscfb664f2016-04-06 16:14:26 +010076{
David Howellsa511e1a2016-04-06 16:14:26 +010077 const struct public_key_signature *sig;
David Howellscfb664f2016-04-06 16:14:26 +010078 struct key *key;
David Howellsa511e1a2016-04-06 16:14:26 +010079 int ret;
David Howellscfb664f2016-04-06 16:14:26 +010080
David Howellsa511e1a2016-04-06 16:14:26 +010081 pr_devel("==>%s()\n", __func__);
David Howellscfb664f2016-04-06 16:14:26 +010082
83 if (!trust_keyring)
David Howellsa511e1a2016-04-06 16:14:26 +010084 return -ENOKEY;
85
86 if (type != &key_type_asymmetric)
David Howellscfb664f2016-04-06 16:14:26 +010087 return -EOPNOTSUPP;
David Howellsa511e1a2016-04-06 16:14:26 +010088
89 sig = payload->data[asym_auth];
Eric Biggersf2915982018-02-26 10:17:15 -080090 if (!sig)
91 return -ENOPKG;
David Howellsa511e1a2016-04-06 16:14:26 +010092 if (!sig->auth_ids[0] && !sig->auth_ids[1])
Mat Martineauacddc722016-07-18 00:10:55 +010093 return -ENOKEY;
David Howellsa511e1a2016-04-06 16:14:26 +010094
David Howellscfb664f2016-04-06 16:14:26 +010095 if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
96 return -EPERM;
David Howellscfb664f2016-04-06 16:14:26 +010097
David Howellsa511e1a2016-04-06 16:14:26 +010098 /* See if we have a key that signed this one. */
David Howellscfb664f2016-04-06 16:14:26 +010099 key = find_asymmetric_key(trust_keyring,
100 sig->auth_ids[0], sig->auth_ids[1],
101 false);
102 if (IS_ERR(key))
David Howellsa511e1a2016-04-06 16:14:26 +0100103 return -ENOKEY;
David Howellscfb664f2016-04-06 16:14:26 +0100104
David Howellsa511e1a2016-04-06 16:14:26 +0100105 if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags))
106 ret = -ENOKEY;
107 else
108 ret = verify_signature(key, sig);
David Howellscfb664f2016-04-06 16:14:26 +0100109 key_put(key);
110 return ret;
111}