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