AKASHI Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* X.509 certificate parser |
| 3 | * |
| 4 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
| 6 | */ |
| 7 | |
| 8 | #define pr_fmt(fmt) "X.509: "fmt |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 10 | #include <dm/devres.h> |
AKASHI Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #ifndef __UBOOT__ |
| 13 | #include <linux/export.h> |
| 14 | #include <linux/slab.h> |
| 15 | #endif |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/oid_registry.h> |
| 18 | #ifdef __UBOOT__ |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 19 | #include <linux/printk.h> |
AKASHI Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 20 | #include <linux/string.h> |
| 21 | #endif |
| 22 | #include <crypto/public_key.h> |
AKASHI Takahiro | 6ec6767 | 2020-04-21 09:38:17 +0900 | [diff] [blame] | 23 | #ifdef __UBOOT__ |
| 24 | #include <crypto/x509_parser.h> |
| 25 | #else |
AKASHI Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 26 | #include "x509_parser.h" |
AKASHI Takahiro | 6ec6767 | 2020-04-21 09:38:17 +0900 | [diff] [blame] | 27 | #endif |
AKASHI Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 28 | #include "x509.asn1.h" |
| 29 | #include "x509_akid.asn1.h" |
| 30 | |
| 31 | struct x509_parse_context { |
| 32 | struct x509_certificate *cert; /* Certificate being constructed */ |
| 33 | unsigned long data; /* Start of data */ |
| 34 | const void *cert_start; /* Start of cert content */ |
| 35 | const void *key; /* Key data */ |
| 36 | size_t key_size; /* Size of key data */ |
| 37 | const void *params; /* Key parameters */ |
| 38 | size_t params_size; /* Size of key parameters */ |
| 39 | enum OID key_algo; /* Public key algorithm */ |
| 40 | enum OID last_oid; /* Last OID encountered */ |
| 41 | enum OID algo_oid; /* Algorithm OID */ |
| 42 | unsigned char nr_mpi; /* Number of MPIs stored */ |
| 43 | u8 o_size; /* Size of organizationName (O) */ |
| 44 | u8 cn_size; /* Size of commonName (CN) */ |
| 45 | u8 email_size; /* Size of emailAddress */ |
| 46 | u16 o_offset; /* Offset of organizationName (O) */ |
| 47 | u16 cn_offset; /* Offset of commonName (CN) */ |
| 48 | u16 email_offset; /* Offset of emailAddress */ |
| 49 | unsigned raw_akid_size; |
| 50 | const void *raw_akid; /* Raw authorityKeyId in ASN.1 */ |
| 51 | const void *akid_raw_issuer; /* Raw directoryName in authorityKeyId */ |
| 52 | unsigned akid_raw_issuer_size; |
| 53 | }; |
| 54 | |
| 55 | /* |
| 56 | * Free an X.509 certificate |
| 57 | */ |
| 58 | void x509_free_certificate(struct x509_certificate *cert) |
| 59 | { |
| 60 | if (cert) { |
| 61 | public_key_free(cert->pub); |
| 62 | public_key_signature_free(cert->sig); |
| 63 | kfree(cert->issuer); |
| 64 | kfree(cert->subject); |
| 65 | kfree(cert->id); |
| 66 | kfree(cert->skid); |
| 67 | kfree(cert); |
| 68 | } |
| 69 | } |
| 70 | EXPORT_SYMBOL_GPL(x509_free_certificate); |
| 71 | |
| 72 | /* |
| 73 | * Parse an X.509 certificate |
| 74 | */ |
| 75 | struct x509_certificate *x509_cert_parse(const void *data, size_t datalen) |
| 76 | { |
| 77 | struct x509_certificate *cert; |
| 78 | struct x509_parse_context *ctx; |
| 79 | struct asymmetric_key_id *kid; |
| 80 | long ret; |
| 81 | |
| 82 | ret = -ENOMEM; |
| 83 | cert = kzalloc(sizeof(struct x509_certificate), GFP_KERNEL); |
| 84 | if (!cert) |
| 85 | goto error_no_cert; |
| 86 | cert->pub = kzalloc(sizeof(struct public_key), GFP_KERNEL); |
| 87 | if (!cert->pub) |
| 88 | goto error_no_ctx; |
| 89 | cert->sig = kzalloc(sizeof(struct public_key_signature), GFP_KERNEL); |
| 90 | if (!cert->sig) |
| 91 | goto error_no_ctx; |
| 92 | ctx = kzalloc(sizeof(struct x509_parse_context), GFP_KERNEL); |
| 93 | if (!ctx) |
| 94 | goto error_no_ctx; |
| 95 | |
| 96 | ctx->cert = cert; |
| 97 | ctx->data = (unsigned long)data; |
| 98 | |
| 99 | /* Attempt to decode the certificate */ |
| 100 | ret = asn1_ber_decoder(&x509_decoder, ctx, data, datalen); |
| 101 | if (ret < 0) |
| 102 | goto error_decode; |
| 103 | |
| 104 | /* Decode the AuthorityKeyIdentifier */ |
| 105 | if (ctx->raw_akid) { |
| 106 | pr_devel("AKID: %u %*phN\n", |
| 107 | ctx->raw_akid_size, ctx->raw_akid_size, ctx->raw_akid); |
| 108 | ret = asn1_ber_decoder(&x509_akid_decoder, ctx, |
| 109 | ctx->raw_akid, ctx->raw_akid_size); |
| 110 | if (ret < 0) { |
| 111 | pr_warn("Couldn't decode AuthKeyIdentifier\n"); |
| 112 | goto error_decode; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | ret = -ENOMEM; |
| 117 | cert->pub->key = kmemdup(ctx->key, ctx->key_size, GFP_KERNEL); |
| 118 | if (!cert->pub->key) |
| 119 | goto error_decode; |
| 120 | |
| 121 | cert->pub->keylen = ctx->key_size; |
| 122 | |
| 123 | cert->pub->params = kmemdup(ctx->params, ctx->params_size, GFP_KERNEL); |
| 124 | if (!cert->pub->params) |
| 125 | goto error_decode; |
| 126 | |
| 127 | cert->pub->paramlen = ctx->params_size; |
| 128 | cert->pub->algo = ctx->key_algo; |
| 129 | |
| 130 | /* Grab the signature bits */ |
| 131 | ret = x509_get_sig_params(cert); |
| 132 | if (ret < 0) |
| 133 | goto error_decode; |
| 134 | |
| 135 | /* Generate cert issuer + serial number key ID */ |
| 136 | kid = asymmetric_key_generate_id(cert->raw_serial, |
| 137 | cert->raw_serial_size, |
| 138 | cert->raw_issuer, |
| 139 | cert->raw_issuer_size); |
| 140 | if (IS_ERR(kid)) { |
| 141 | ret = PTR_ERR(kid); |
| 142 | goto error_decode; |
| 143 | } |
| 144 | cert->id = kid; |
| 145 | |
AKASHI Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 146 | /* Detect self-signed certificates */ |
| 147 | ret = x509_check_for_self_signed(cert); |
| 148 | if (ret < 0) |
| 149 | goto error_decode; |
AKASHI Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 150 | |
| 151 | kfree(ctx); |
| 152 | return cert; |
| 153 | |
| 154 | error_decode: |
| 155 | kfree(ctx); |
| 156 | error_no_ctx: |
| 157 | x509_free_certificate(cert); |
| 158 | error_no_cert: |
| 159 | return ERR_PTR(ret); |
| 160 | } |
| 161 | EXPORT_SYMBOL_GPL(x509_cert_parse); |
| 162 | |
| 163 | /* |
| 164 | * Note an OID when we find one for later processing when we know how |
| 165 | * to interpret it. |
| 166 | */ |
| 167 | int x509_note_OID(void *context, size_t hdrlen, |
| 168 | unsigned char tag, |
| 169 | const void *value, size_t vlen) |
| 170 | { |
| 171 | struct x509_parse_context *ctx = context; |
| 172 | |
| 173 | ctx->last_oid = look_up_OID(value, vlen); |
| 174 | if (ctx->last_oid == OID__NR) { |
| 175 | char buffer[50]; |
| 176 | sprint_oid(value, vlen, buffer, sizeof(buffer)); |
| 177 | pr_debug("Unknown OID: [%lu] %s\n", |
| 178 | (unsigned long)value - ctx->data, buffer); |
| 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Save the position of the TBS data so that we can check the signature over it |
| 185 | * later. |
| 186 | */ |
| 187 | int x509_note_tbs_certificate(void *context, size_t hdrlen, |
| 188 | unsigned char tag, |
| 189 | const void *value, size_t vlen) |
| 190 | { |
| 191 | struct x509_parse_context *ctx = context; |
| 192 | |
| 193 | pr_debug("x509_note_tbs_certificate(,%zu,%02x,%ld,%zu)!\n", |
| 194 | hdrlen, tag, (unsigned long)value - ctx->data, vlen); |
| 195 | |
| 196 | ctx->cert->tbs = value - hdrlen; |
| 197 | ctx->cert->tbs_size = vlen + hdrlen; |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Record the public key algorithm |
| 203 | */ |
| 204 | int x509_note_pkey_algo(void *context, size_t hdrlen, |
| 205 | unsigned char tag, |
| 206 | const void *value, size_t vlen) |
| 207 | { |
| 208 | struct x509_parse_context *ctx = context; |
| 209 | |
| 210 | pr_debug("PubKey Algo: %u\n", ctx->last_oid); |
| 211 | |
| 212 | switch (ctx->last_oid) { |
| 213 | case OID_md2WithRSAEncryption: |
| 214 | case OID_md3WithRSAEncryption: |
| 215 | default: |
| 216 | return -ENOPKG; /* Unsupported combination */ |
| 217 | |
| 218 | case OID_md4WithRSAEncryption: |
| 219 | ctx->cert->sig->hash_algo = "md4"; |
| 220 | goto rsa_pkcs1; |
| 221 | |
| 222 | case OID_sha1WithRSAEncryption: |
| 223 | ctx->cert->sig->hash_algo = "sha1"; |
| 224 | goto rsa_pkcs1; |
| 225 | |
| 226 | case OID_sha256WithRSAEncryption: |
| 227 | ctx->cert->sig->hash_algo = "sha256"; |
| 228 | goto rsa_pkcs1; |
| 229 | |
| 230 | case OID_sha384WithRSAEncryption: |
| 231 | ctx->cert->sig->hash_algo = "sha384"; |
| 232 | goto rsa_pkcs1; |
| 233 | |
| 234 | case OID_sha512WithRSAEncryption: |
| 235 | ctx->cert->sig->hash_algo = "sha512"; |
| 236 | goto rsa_pkcs1; |
| 237 | |
| 238 | case OID_sha224WithRSAEncryption: |
| 239 | ctx->cert->sig->hash_algo = "sha224"; |
| 240 | goto rsa_pkcs1; |
| 241 | |
| 242 | case OID_gost2012Signature256: |
| 243 | ctx->cert->sig->hash_algo = "streebog256"; |
| 244 | goto ecrdsa; |
| 245 | |
| 246 | case OID_gost2012Signature512: |
| 247 | ctx->cert->sig->hash_algo = "streebog512"; |
| 248 | goto ecrdsa; |
| 249 | } |
| 250 | |
| 251 | rsa_pkcs1: |
| 252 | ctx->cert->sig->pkey_algo = "rsa"; |
| 253 | ctx->cert->sig->encoding = "pkcs1"; |
| 254 | ctx->algo_oid = ctx->last_oid; |
| 255 | return 0; |
| 256 | ecrdsa: |
| 257 | ctx->cert->sig->pkey_algo = "ecrdsa"; |
| 258 | ctx->cert->sig->encoding = "raw"; |
| 259 | ctx->algo_oid = ctx->last_oid; |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Note the whereabouts and type of the signature. |
| 265 | */ |
| 266 | int x509_note_signature(void *context, size_t hdrlen, |
| 267 | unsigned char tag, |
| 268 | const void *value, size_t vlen) |
| 269 | { |
| 270 | struct x509_parse_context *ctx = context; |
| 271 | |
| 272 | pr_debug("Signature type: %u size %zu\n", ctx->last_oid, vlen); |
| 273 | |
| 274 | if (ctx->last_oid != ctx->algo_oid) { |
| 275 | pr_warn("Got cert with pkey (%u) and sig (%u) algorithm OIDs\n", |
| 276 | ctx->algo_oid, ctx->last_oid); |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | |
| 280 | if (strcmp(ctx->cert->sig->pkey_algo, "rsa") == 0 || |
| 281 | strcmp(ctx->cert->sig->pkey_algo, "ecrdsa") == 0) { |
| 282 | /* Discard the BIT STRING metadata */ |
| 283 | if (vlen < 1 || *(const u8 *)value != 0) |
| 284 | return -EBADMSG; |
| 285 | |
| 286 | value++; |
| 287 | vlen--; |
| 288 | } |
| 289 | |
| 290 | ctx->cert->raw_sig = value; |
| 291 | ctx->cert->raw_sig_size = vlen; |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Note the certificate serial number |
| 297 | */ |
| 298 | int x509_note_serial(void *context, size_t hdrlen, |
| 299 | unsigned char tag, |
| 300 | const void *value, size_t vlen) |
| 301 | { |
| 302 | struct x509_parse_context *ctx = context; |
| 303 | ctx->cert->raw_serial = value; |
| 304 | ctx->cert->raw_serial_size = vlen; |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Note some of the name segments from which we'll fabricate a name. |
| 310 | */ |
| 311 | int x509_extract_name_segment(void *context, size_t hdrlen, |
| 312 | unsigned char tag, |
| 313 | const void *value, size_t vlen) |
| 314 | { |
| 315 | struct x509_parse_context *ctx = context; |
| 316 | |
| 317 | switch (ctx->last_oid) { |
| 318 | case OID_commonName: |
| 319 | ctx->cn_size = vlen; |
| 320 | ctx->cn_offset = (unsigned long)value - ctx->data; |
| 321 | break; |
| 322 | case OID_organizationName: |
| 323 | ctx->o_size = vlen; |
| 324 | ctx->o_offset = (unsigned long)value - ctx->data; |
| 325 | break; |
| 326 | case OID_email_address: |
| 327 | ctx->email_size = vlen; |
| 328 | ctx->email_offset = (unsigned long)value - ctx->data; |
| 329 | break; |
| 330 | default: |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Fabricate and save the issuer and subject names |
| 339 | */ |
| 340 | static int x509_fabricate_name(struct x509_parse_context *ctx, size_t hdrlen, |
| 341 | unsigned char tag, |
| 342 | char **_name, size_t vlen) |
| 343 | { |
| 344 | const void *name, *data = (const void *)ctx->data; |
| 345 | size_t namesize; |
| 346 | char *buffer; |
| 347 | |
| 348 | if (*_name) |
| 349 | return -EINVAL; |
| 350 | |
| 351 | /* Empty name string if no material */ |
| 352 | if (!ctx->cn_size && !ctx->o_size && !ctx->email_size) { |
| 353 | buffer = kmalloc(1, GFP_KERNEL); |
| 354 | if (!buffer) |
| 355 | return -ENOMEM; |
| 356 | buffer[0] = 0; |
| 357 | goto done; |
| 358 | } |
| 359 | |
| 360 | if (ctx->cn_size && ctx->o_size) { |
| 361 | /* Consider combining O and CN, but use only the CN if it is |
| 362 | * prefixed by the O, or a significant portion thereof. |
| 363 | */ |
| 364 | namesize = ctx->cn_size; |
| 365 | name = data + ctx->cn_offset; |
| 366 | if (ctx->cn_size >= ctx->o_size && |
| 367 | memcmp(data + ctx->cn_offset, data + ctx->o_offset, |
| 368 | ctx->o_size) == 0) |
| 369 | goto single_component; |
| 370 | if (ctx->cn_size >= 7 && |
| 371 | ctx->o_size >= 7 && |
| 372 | memcmp(data + ctx->cn_offset, data + ctx->o_offset, 7) == 0) |
| 373 | goto single_component; |
| 374 | |
| 375 | buffer = kmalloc(ctx->o_size + 2 + ctx->cn_size + 1, |
| 376 | GFP_KERNEL); |
| 377 | if (!buffer) |
| 378 | return -ENOMEM; |
| 379 | |
| 380 | memcpy(buffer, |
| 381 | data + ctx->o_offset, ctx->o_size); |
| 382 | buffer[ctx->o_size + 0] = ':'; |
| 383 | buffer[ctx->o_size + 1] = ' '; |
| 384 | memcpy(buffer + ctx->o_size + 2, |
| 385 | data + ctx->cn_offset, ctx->cn_size); |
| 386 | buffer[ctx->o_size + 2 + ctx->cn_size] = 0; |
| 387 | goto done; |
| 388 | |
| 389 | } else if (ctx->cn_size) { |
| 390 | namesize = ctx->cn_size; |
| 391 | name = data + ctx->cn_offset; |
| 392 | } else if (ctx->o_size) { |
| 393 | namesize = ctx->o_size; |
| 394 | name = data + ctx->o_offset; |
| 395 | } else { |
| 396 | namesize = ctx->email_size; |
| 397 | name = data + ctx->email_offset; |
| 398 | } |
| 399 | |
| 400 | single_component: |
| 401 | buffer = kmalloc(namesize + 1, GFP_KERNEL); |
| 402 | if (!buffer) |
| 403 | return -ENOMEM; |
| 404 | memcpy(buffer, name, namesize); |
| 405 | buffer[namesize] = 0; |
| 406 | |
| 407 | done: |
| 408 | *_name = buffer; |
| 409 | ctx->cn_size = 0; |
| 410 | ctx->o_size = 0; |
| 411 | ctx->email_size = 0; |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | int x509_note_issuer(void *context, size_t hdrlen, |
| 416 | unsigned char tag, |
| 417 | const void *value, size_t vlen) |
| 418 | { |
| 419 | struct x509_parse_context *ctx = context; |
| 420 | ctx->cert->raw_issuer = value; |
| 421 | ctx->cert->raw_issuer_size = vlen; |
| 422 | return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->issuer, vlen); |
| 423 | } |
| 424 | |
| 425 | int x509_note_subject(void *context, size_t hdrlen, |
| 426 | unsigned char tag, |
| 427 | const void *value, size_t vlen) |
| 428 | { |
| 429 | struct x509_parse_context *ctx = context; |
| 430 | ctx->cert->raw_subject = value; |
| 431 | ctx->cert->raw_subject_size = vlen; |
| 432 | return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->subject, vlen); |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Extract the parameters for the public key |
| 437 | */ |
| 438 | int x509_note_params(void *context, size_t hdrlen, |
| 439 | unsigned char tag, |
| 440 | const void *value, size_t vlen) |
| 441 | { |
| 442 | struct x509_parse_context *ctx = context; |
| 443 | |
| 444 | /* |
| 445 | * AlgorithmIdentifier is used three times in the x509, we should skip |
| 446 | * first and ignore third, using second one which is after subject and |
| 447 | * before subjectPublicKey. |
| 448 | */ |
| 449 | if (!ctx->cert->raw_subject || ctx->key) |
| 450 | return 0; |
| 451 | ctx->params = value - hdrlen; |
| 452 | ctx->params_size = vlen + hdrlen; |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Extract the data for the public key algorithm |
| 458 | */ |
| 459 | int x509_extract_key_data(void *context, size_t hdrlen, |
| 460 | unsigned char tag, |
| 461 | const void *value, size_t vlen) |
| 462 | { |
| 463 | struct x509_parse_context *ctx = context; |
| 464 | |
| 465 | ctx->key_algo = ctx->last_oid; |
| 466 | if (ctx->last_oid == OID_rsaEncryption) |
| 467 | ctx->cert->pub->pkey_algo = "rsa"; |
| 468 | else if (ctx->last_oid == OID_gost2012PKey256 || |
| 469 | ctx->last_oid == OID_gost2012PKey512) |
| 470 | ctx->cert->pub->pkey_algo = "ecrdsa"; |
| 471 | else |
| 472 | return -ENOPKG; |
| 473 | |
| 474 | /* Discard the BIT STRING metadata */ |
| 475 | if (vlen < 1 || *(const u8 *)value != 0) |
| 476 | return -EBADMSG; |
| 477 | ctx->key = value + 1; |
| 478 | ctx->key_size = vlen - 1; |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | /* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */ |
| 483 | #define SEQ_TAG_KEYID (ASN1_CONT << 6) |
| 484 | |
| 485 | /* |
| 486 | * Process certificate extensions that are used to qualify the certificate. |
| 487 | */ |
| 488 | int x509_process_extension(void *context, size_t hdrlen, |
| 489 | unsigned char tag, |
| 490 | const void *value, size_t vlen) |
| 491 | { |
| 492 | struct x509_parse_context *ctx = context; |
| 493 | struct asymmetric_key_id *kid; |
| 494 | const unsigned char *v = value; |
| 495 | |
| 496 | pr_debug("Extension: %u\n", ctx->last_oid); |
| 497 | |
| 498 | if (ctx->last_oid == OID_subjectKeyIdentifier) { |
| 499 | /* Get hold of the key fingerprint */ |
| 500 | if (ctx->cert->skid || vlen < 3) |
| 501 | return -EBADMSG; |
| 502 | if (v[0] != ASN1_OTS || v[1] != vlen - 2) |
| 503 | return -EBADMSG; |
| 504 | v += 2; |
| 505 | vlen -= 2; |
| 506 | |
| 507 | ctx->cert->raw_skid_size = vlen; |
| 508 | ctx->cert->raw_skid = v; |
| 509 | kid = asymmetric_key_generate_id(v, vlen, "", 0); |
| 510 | if (IS_ERR(kid)) |
| 511 | return PTR_ERR(kid); |
| 512 | ctx->cert->skid = kid; |
| 513 | pr_debug("subjkeyid %*phN\n", kid->len, kid->data); |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | if (ctx->last_oid == OID_authorityKeyIdentifier) { |
| 518 | /* Get hold of the CA key fingerprint */ |
| 519 | ctx->raw_akid = v; |
| 520 | ctx->raw_akid_size = vlen; |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * x509_decode_time - Decode an X.509 time ASN.1 object |
| 529 | * @_t: The time to fill in |
| 530 | * @hdrlen: The length of the object header |
| 531 | * @tag: The object tag |
| 532 | * @value: The object value |
| 533 | * @vlen: The size of the object value |
| 534 | * |
| 535 | * Decode an ASN.1 universal time or generalised time field into a struct the |
| 536 | * kernel can handle and check it for validity. The time is decoded thus: |
| 537 | * |
| 538 | * [RFC5280 §4.1.2.5] |
| 539 | * CAs conforming to this profile MUST always encode certificate validity |
| 540 | * dates through the year 2049 as UTCTime; certificate validity dates in |
| 541 | * 2050 or later MUST be encoded as GeneralizedTime. Conforming |
| 542 | * applications MUST be able to process validity dates that are encoded in |
| 543 | * either UTCTime or GeneralizedTime. |
| 544 | */ |
| 545 | int x509_decode_time(time64_t *_t, size_t hdrlen, |
| 546 | unsigned char tag, |
| 547 | const unsigned char *value, size_t vlen) |
| 548 | { |
| 549 | static const unsigned char month_lengths[] = { 31, 28, 31, 30, 31, 30, |
| 550 | 31, 31, 30, 31, 30, 31 }; |
| 551 | const unsigned char *p = value; |
| 552 | unsigned year, mon, day, hour, min, sec, mon_len; |
| 553 | |
| 554 | #define dec2bin(X) ({ unsigned char x = (X) - '0'; if (x > 9) goto invalid_time; x; }) |
| 555 | #define DD2bin(P) ({ unsigned x = dec2bin(P[0]) * 10 + dec2bin(P[1]); P += 2; x; }) |
| 556 | |
| 557 | if (tag == ASN1_UNITIM) { |
| 558 | /* UTCTime: YYMMDDHHMMSSZ */ |
| 559 | if (vlen != 13) |
| 560 | goto unsupported_time; |
| 561 | year = DD2bin(p); |
| 562 | if (year >= 50) |
| 563 | year += 1900; |
| 564 | else |
| 565 | year += 2000; |
| 566 | } else if (tag == ASN1_GENTIM) { |
| 567 | /* GenTime: YYYYMMDDHHMMSSZ */ |
| 568 | if (vlen != 15) |
| 569 | goto unsupported_time; |
| 570 | year = DD2bin(p) * 100 + DD2bin(p); |
| 571 | if (year >= 1950 && year <= 2049) |
| 572 | goto invalid_time; |
| 573 | } else { |
| 574 | goto unsupported_time; |
| 575 | } |
| 576 | |
| 577 | mon = DD2bin(p); |
| 578 | day = DD2bin(p); |
| 579 | hour = DD2bin(p); |
| 580 | min = DD2bin(p); |
| 581 | sec = DD2bin(p); |
| 582 | |
| 583 | if (*p != 'Z') |
| 584 | goto unsupported_time; |
| 585 | |
| 586 | if (year < 1970 || |
| 587 | mon < 1 || mon > 12) |
| 588 | goto invalid_time; |
| 589 | |
| 590 | mon_len = month_lengths[mon - 1]; |
| 591 | if (mon == 2) { |
| 592 | if (year % 4 == 0) { |
| 593 | mon_len = 29; |
| 594 | if (year % 100 == 0) { |
| 595 | mon_len = 28; |
| 596 | if (year % 400 == 0) |
| 597 | mon_len = 29; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | if (day < 1 || day > mon_len || |
| 603 | hour > 24 || /* ISO 8601 permits 24:00:00 as midnight tomorrow */ |
| 604 | min > 59 || |
| 605 | sec > 60) /* ISO 8601 permits leap seconds [X.680 46.3] */ |
| 606 | goto invalid_time; |
| 607 | |
| 608 | *_t = mktime64(year, mon, day, hour, min, sec); |
| 609 | return 0; |
| 610 | |
| 611 | unsupported_time: |
| 612 | pr_debug("Got unsupported time [tag %02x]: '%*phN'\n", |
| 613 | tag, (int)vlen, value); |
| 614 | return -EBADMSG; |
| 615 | invalid_time: |
| 616 | pr_debug("Got invalid time [tag %02x]: '%*phN'\n", |
| 617 | tag, (int)vlen, value); |
| 618 | return -EBADMSG; |
| 619 | } |
| 620 | EXPORT_SYMBOL_GPL(x509_decode_time); |
| 621 | |
| 622 | int x509_note_not_before(void *context, size_t hdrlen, |
| 623 | unsigned char tag, |
| 624 | const void *value, size_t vlen) |
| 625 | { |
| 626 | struct x509_parse_context *ctx = context; |
| 627 | return x509_decode_time(&ctx->cert->valid_from, hdrlen, tag, value, vlen); |
| 628 | } |
| 629 | |
| 630 | int x509_note_not_after(void *context, size_t hdrlen, |
| 631 | unsigned char tag, |
| 632 | const void *value, size_t vlen) |
| 633 | { |
| 634 | struct x509_parse_context *ctx = context; |
| 635 | return x509_decode_time(&ctx->cert->valid_to, hdrlen, tag, value, vlen); |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Note a key identifier-based AuthorityKeyIdentifier |
| 640 | */ |
| 641 | int x509_akid_note_kid(void *context, size_t hdrlen, |
| 642 | unsigned char tag, |
| 643 | const void *value, size_t vlen) |
| 644 | { |
| 645 | struct x509_parse_context *ctx = context; |
| 646 | struct asymmetric_key_id *kid; |
| 647 | |
| 648 | pr_debug("AKID: keyid: %*phN\n", (int)vlen, value); |
| 649 | |
| 650 | if (ctx->cert->sig->auth_ids[1]) |
| 651 | return 0; |
| 652 | |
| 653 | kid = asymmetric_key_generate_id(value, vlen, "", 0); |
| 654 | if (IS_ERR(kid)) |
| 655 | return PTR_ERR(kid); |
| 656 | pr_debug("authkeyid %*phN\n", kid->len, kid->data); |
| 657 | ctx->cert->sig->auth_ids[1] = kid; |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * Note a directoryName in an AuthorityKeyIdentifier |
| 663 | */ |
| 664 | int x509_akid_note_name(void *context, size_t hdrlen, |
| 665 | unsigned char tag, |
| 666 | const void *value, size_t vlen) |
| 667 | { |
| 668 | struct x509_parse_context *ctx = context; |
| 669 | |
| 670 | pr_debug("AKID: name: %*phN\n", (int)vlen, value); |
| 671 | |
| 672 | ctx->akid_raw_issuer = value; |
| 673 | ctx->akid_raw_issuer_size = vlen; |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Note a serial number in an AuthorityKeyIdentifier |
| 679 | */ |
| 680 | int x509_akid_note_serial(void *context, size_t hdrlen, |
| 681 | unsigned char tag, |
| 682 | const void *value, size_t vlen) |
| 683 | { |
| 684 | struct x509_parse_context *ctx = context; |
| 685 | struct asymmetric_key_id *kid; |
| 686 | |
| 687 | pr_debug("AKID: serial: %*phN\n", (int)vlen, value); |
| 688 | |
| 689 | if (!ctx->akid_raw_issuer || ctx->cert->sig->auth_ids[0]) |
| 690 | return 0; |
| 691 | |
| 692 | kid = asymmetric_key_generate_id(value, |
| 693 | vlen, |
| 694 | ctx->akid_raw_issuer, |
| 695 | ctx->akid_raw_issuer_size); |
| 696 | if (IS_ERR(kid)) |
| 697 | return PTR_ERR(kid); |
| 698 | |
| 699 | pr_debug("authkeyid %*phN\n", kid->len, kid->data); |
| 700 | ctx->cert->sig->auth_ids[0] = kid; |
| 701 | return 0; |
| 702 | } |