1 /* Instantiate a public key crypto key from an X.509 Certificate
3 * Copyright (C) 2012 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) "X.509: "fmt
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <keys/asymmetric-subtype.h>
17 #include <keys/asymmetric-parser.h>
18 #include <keys/system_keyring.h>
19 #include <crypto/hash.h>
20 #include "asymmetric_keys.h"
21 #include "x509_parser.h"
24 * Set up the signature parameters in an X.509 certificate. This involves
25 * digesting the signed data and extracting the signature.
27 int x509_get_sig_params(struct x509_certificate
*cert
)
29 struct public_key_signature
*sig
= cert
->sig
;
30 struct crypto_shash
*tfm
;
31 struct shash_desc
*desc
;
35 pr_devel("==>%s()\n", __func__
);
37 if (!cert
->pub
->pkey_algo
)
38 cert
->unsupported_key
= true;
41 cert
->unsupported_sig
= true;
43 /* We check the hash if we can - even if we can't then verify it */
44 if (!sig
->hash_algo
) {
45 cert
->unsupported_sig
= true;
49 sig
->s
= kmemdup(cert
->raw_sig
, cert
->raw_sig_size
, GFP_KERNEL
);
53 sig
->s_size
= cert
->raw_sig_size
;
55 /* Allocate the hashing algorithm we're going to need and find out how
56 * big the hash operational data will be.
58 tfm
= crypto_alloc_shash(sig
->hash_algo
, 0, 0);
60 if (PTR_ERR(tfm
) == -ENOENT
) {
61 cert
->unsupported_sig
= true;
67 desc_size
= crypto_shash_descsize(tfm
) + sizeof(*desc
);
68 sig
->digest_size
= crypto_shash_digestsize(tfm
);
71 sig
->digest
= kmalloc(sig
->digest_size
, GFP_KERNEL
);
75 desc
= kzalloc(desc_size
, GFP_KERNEL
);
80 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
82 ret
= crypto_shash_init(desc
);
86 ret
= crypto_shash_finup(desc
, cert
->tbs
, cert
->tbs_size
, sig
->digest
);
91 crypto_free_shash(tfm
);
92 pr_devel("<==%s() = %d\n", __func__
, ret
);
97 * Check for self-signedness in an X.509 cert and if found, check the signature
98 * immediately if we can.
100 int x509_check_for_self_signed(struct x509_certificate
*cert
)
104 pr_devel("==>%s()\n", __func__
);
106 if (cert
->raw_subject_size
!= cert
->raw_issuer_size
||
107 memcmp(cert
->raw_subject
, cert
->raw_issuer
,
108 cert
->raw_issuer_size
) != 0)
109 goto not_self_signed
;
111 if (cert
->sig
->auth_ids
[0] || cert
->sig
->auth_ids
[1]) {
112 /* If the AKID is present it may have one or two parts. If
113 * both are supplied, both must match.
115 bool a
= asymmetric_key_id_same(cert
->skid
, cert
->sig
->auth_ids
[1]);
116 bool b
= asymmetric_key_id_same(cert
->id
, cert
->sig
->auth_ids
[0]);
119 goto not_self_signed
;
122 if (((a
&& !b
) || (b
&& !a
)) &&
123 cert
->sig
->auth_ids
[0] && cert
->sig
->auth_ids
[1])
128 if (cert
->pub
->pkey_algo
!= cert
->sig
->pkey_algo
)
131 ret
= public_key_verify_signature(cert
->pub
, cert
->sig
);
133 if (ret
== -ENOPKG
) {
134 cert
->unsupported_sig
= true;
140 pr_devel("Cert Self-signature verified");
141 cert
->self_signed
= true;
144 pr_devel("<==%s() = %d\n", __func__
, ret
);
148 pr_devel("<==%s() = 0 [not]\n", __func__
);
153 * Attempt to parse a data blob for a key as an X509 certificate.
155 static int x509_key_preparse(struct key_preparsed_payload
*prep
)
157 struct asymmetric_key_ids
*kids
;
158 struct x509_certificate
*cert
;
161 char *desc
= NULL
, *p
;
164 cert
= x509_cert_parse(prep
->data
, prep
->datalen
);
166 return PTR_ERR(cert
);
168 pr_devel("Cert Issuer: %s\n", cert
->issuer
);
169 pr_devel("Cert Subject: %s\n", cert
->subject
);
171 if (cert
->unsupported_key
) {
173 goto error_free_cert
;
176 pr_devel("Cert Key Algo: %s\n", cert
->pub
->pkey_algo
);
177 pr_devel("Cert Valid period: %lld-%lld\n", cert
->valid_from
, cert
->valid_to
);
179 cert
->pub
->id_type
= "X509";
181 if (cert
->unsupported_sig
) {
182 public_key_signature_free(cert
->sig
);
185 pr_devel("Cert Signature: %s + %s\n",
186 cert
->sig
->pkey_algo
, cert
->sig
->hash_algo
);
189 /* Propose a description */
190 sulen
= strlen(cert
->subject
);
191 if (cert
->raw_skid
) {
192 srlen
= cert
->raw_skid_size
;
195 srlen
= cert
->raw_serial_size
;
196 q
= cert
->raw_serial
;
200 desc
= kmalloc(sulen
+ 2 + srlen
* 2 + 1, GFP_KERNEL
);
202 goto error_free_cert
;
203 p
= memcpy(desc
, cert
->subject
, sulen
);
207 p
= bin2hex(p
, q
, srlen
);
210 kids
= kmalloc(sizeof(struct asymmetric_key_ids
), GFP_KERNEL
);
212 goto error_free_desc
;
213 kids
->id
[0] = cert
->id
;
214 kids
->id
[1] = cert
->skid
;
216 /* We're pinning the module by being linked against it */
217 __module_get(public_key_subtype
.owner
);
218 prep
->payload
.data
[asym_subtype
] = &public_key_subtype
;
219 prep
->payload
.data
[asym_key_ids
] = kids
;
220 prep
->payload
.data
[asym_crypto
] = cert
->pub
;
221 prep
->payload
.data
[asym_auth
] = cert
->sig
;
222 prep
->description
= desc
;
223 prep
->quotalen
= 100;
225 /* We've finished with the certificate */
236 x509_free_certificate(cert
);
240 static struct asymmetric_key_parser x509_key_parser
= {
241 .owner
= THIS_MODULE
,
243 .parse
= x509_key_preparse
,
249 static int __init
x509_key_init(void)
251 return register_asymmetric_key_parser(&x509_key_parser
);
254 static void __exit
x509_key_exit(void)
256 unregister_asymmetric_key_parser(&x509_key_parser
);
259 module_init(x509_key_init
);
260 module_exit(x509_key_exit
);
262 MODULE_DESCRIPTION("X.509 certificate parser");
263 MODULE_LICENSE("GPL");