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 <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" 22 23 /* 24 * Set up the signature parameters in an X.509 certificate. This involves 25 * digesting the signed data and extracting the signature. 26 */ 27 int x509_get_sig_params(struct x509_certificate *cert) 28 { 29 struct public_key_signature *sig = cert->sig; 30 struct crypto_shash *tfm; 31 struct shash_desc *desc; 32 size_t desc_size; 33 int ret; 34 35 pr_devel("==>%s()\n", __func__); 36 37 if (!cert->pub->pkey_algo) 38 cert->unsupported_key = true; 39 40 if (!sig->pkey_algo) 41 cert->unsupported_sig = true; 42 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; 46 return 0; 47 } 48 49 sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL); 50 if (!sig->s) 51 return -ENOMEM; 52 53 sig->s_size = cert->raw_sig_size; 54 55 /* Allocate the hashing algorithm we're going to need and find out how 56 * big the hash operational data will be. 57 */ 58 tfm = crypto_alloc_shash(sig->hash_algo, 0, 0); 59 if (IS_ERR(tfm)) { 60 if (PTR_ERR(tfm) == -ENOENT) { 61 cert->unsupported_sig = true; 62 return 0; 63 } 64 return PTR_ERR(tfm); 65 } 66 67 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); 68 sig->digest_size = crypto_shash_digestsize(tfm); 69 70 ret = -ENOMEM; 71 sig->digest = kmalloc(sig->digest_size, GFP_KERNEL); 72 if (!sig->digest) 73 goto error; 74 75 desc = kzalloc(desc_size, GFP_KERNEL); 76 if (!desc) 77 goto error; 78 79 desc->tfm = tfm; 80 81 ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, sig->digest); 82 if (ret < 0) 83 goto error_2; 84 85 ret = is_hash_blacklisted(sig->digest, sig->digest_size, "tbs"); 86 if (ret == -EKEYREJECTED) { 87 pr_err("Cert %*phN is blacklisted\n", 88 sig->digest_size, sig->digest); 89 cert->blacklisted = true; 90 ret = 0; 91 } 92 93 error_2: 94 kfree(desc); 95 error: 96 crypto_free_shash(tfm); 97 pr_devel("<==%s() = %d\n", __func__, ret); 98 return ret; 99 } 100 101 /* 102 * Check for self-signedness in an X.509 cert and if found, check the signature 103 * immediately if we can. 104 */ 105 int x509_check_for_self_signed(struct x509_certificate *cert) 106 { 107 int ret = 0; 108 109 pr_devel("==>%s()\n", __func__); 110 111 if (cert->raw_subject_size != cert->raw_issuer_size || 112 memcmp(cert->raw_subject, cert->raw_issuer, 113 cert->raw_issuer_size) != 0) 114 goto not_self_signed; 115 116 if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) { 117 /* If the AKID is present it may have one or two parts. If 118 * both are supplied, both must match. 119 */ 120 bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]); 121 bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]); 122 123 if (!a && !b) 124 goto not_self_signed; 125 126 ret = -EKEYREJECTED; 127 if (((a && !b) || (b && !a)) && 128 cert->sig->auth_ids[0] && cert->sig->auth_ids[1]) 129 goto out; 130 } 131 132 ret = -EKEYREJECTED; 133 if (strcmp(cert->pub->pkey_algo, cert->sig->pkey_algo) != 0) 134 goto out; 135 136 ret = public_key_verify_signature(cert->pub, cert->sig); 137 if (ret < 0) { 138 if (ret == -ENOPKG) { 139 cert->unsupported_sig = true; 140 ret = 0; 141 } 142 goto out; 143 } 144 145 pr_devel("Cert Self-signature verified"); 146 cert->self_signed = true; 147 148 out: 149 pr_devel("<==%s() = %d\n", __func__, ret); 150 return ret; 151 152 not_self_signed: 153 pr_devel("<==%s() = 0 [not]\n", __func__); 154 return 0; 155 } 156 157 /* 158 * Attempt to parse a data blob for a key as an X509 certificate. 159 */ 160 static int x509_key_preparse(struct key_preparsed_payload *prep) 161 { 162 struct asymmetric_key_ids *kids; 163 struct x509_certificate *cert; 164 const char *q; 165 size_t srlen, sulen; 166 char *desc = NULL, *p; 167 int ret; 168 169 cert = x509_cert_parse(prep->data, prep->datalen); 170 if (IS_ERR(cert)) 171 return PTR_ERR(cert); 172 173 pr_devel("Cert Issuer: %s\n", cert->issuer); 174 pr_devel("Cert Subject: %s\n", cert->subject); 175 176 if (cert->unsupported_key) { 177 ret = -ENOPKG; 178 goto error_free_cert; 179 } 180 181 pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo); 182 pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to); 183 184 cert->pub->id_type = "X509"; 185 186 if (cert->unsupported_sig) { 187 public_key_signature_free(cert->sig); 188 cert->sig = NULL; 189 } else { 190 pr_devel("Cert Signature: %s + %s\n", 191 cert->sig->pkey_algo, cert->sig->hash_algo); 192 } 193 194 /* Don't permit addition of blacklisted keys */ 195 ret = -EKEYREJECTED; 196 if (cert->blacklisted) 197 goto error_free_cert; 198 199 /* Propose a description */ 200 sulen = strlen(cert->subject); 201 if (cert->raw_skid) { 202 srlen = cert->raw_skid_size; 203 q = cert->raw_skid; 204 } else { 205 srlen = cert->raw_serial_size; 206 q = cert->raw_serial; 207 } 208 209 ret = -ENOMEM; 210 desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL); 211 if (!desc) 212 goto error_free_cert; 213 p = memcpy(desc, cert->subject, sulen); 214 p += sulen; 215 *p++ = ':'; 216 *p++ = ' '; 217 p = bin2hex(p, q, srlen); 218 *p = 0; 219 220 kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL); 221 if (!kids) 222 goto error_free_desc; 223 kids->id[0] = cert->id; 224 kids->id[1] = cert->skid; 225 226 /* We're pinning the module by being linked against it */ 227 __module_get(public_key_subtype.owner); 228 prep->payload.data[asym_subtype] = &public_key_subtype; 229 prep->payload.data[asym_key_ids] = kids; 230 prep->payload.data[asym_crypto] = cert->pub; 231 prep->payload.data[asym_auth] = cert->sig; 232 prep->description = desc; 233 prep->quotalen = 100; 234 235 /* We've finished with the certificate */ 236 cert->pub = NULL; 237 cert->id = NULL; 238 cert->skid = NULL; 239 cert->sig = NULL; 240 desc = NULL; 241 ret = 0; 242 243 error_free_desc: 244 kfree(desc); 245 error_free_cert: 246 x509_free_certificate(cert); 247 return ret; 248 } 249 250 static struct asymmetric_key_parser x509_key_parser = { 251 .owner = THIS_MODULE, 252 .name = "x509", 253 .parse = x509_key_preparse, 254 }; 255 256 /* 257 * Module stuff 258 */ 259 static int __init x509_key_init(void) 260 { 261 return register_asymmetric_key_parser(&x509_key_parser); 262 } 263 264 static void __exit x509_key_exit(void) 265 { 266 unregister_asymmetric_key_parser(&x509_key_parser); 267 } 268 269 module_init(x509_key_init); 270 module_exit(x509_key_exit); 271 272 MODULE_DESCRIPTION("X.509 certificate parser"); 273 MODULE_AUTHOR("Red Hat, Inc."); 274 MODULE_LICENSE("GPL"); 275