Path: blob/master/crypto/asymmetric_keys/x509_cert_parser.c
51323 views
// SPDX-License-Identifier: GPL-2.0-or-later1/* X.509 certificate parser2*3* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.4* Written by David Howells ([email protected])5*/67#define pr_fmt(fmt) "X.509: "fmt8#include <linux/kernel.h>9#include <linux/export.h>10#include <linux/slab.h>11#include <linux/err.h>12#include <linux/oid_registry.h>13#include <crypto/public_key.h>14#include "x509_parser.h"15#include "x509.asn1.h"16#include "x509_akid.asn1.h"1718struct x509_parse_context {19struct x509_certificate *cert; /* Certificate being constructed */20unsigned long data; /* Start of data */21const void *key; /* Key data */22size_t key_size; /* Size of key data */23const void *params; /* Key parameters */24size_t params_size; /* Size of key parameters */25enum OID key_algo; /* Algorithm used by the cert's key */26enum OID last_oid; /* Last OID encountered */27enum OID sig_algo; /* Algorithm used to sign the cert */28u8 o_size; /* Size of organizationName (O) */29u8 cn_size; /* Size of commonName (CN) */30u8 email_size; /* Size of emailAddress */31u16 o_offset; /* Offset of organizationName (O) */32u16 cn_offset; /* Offset of commonName (CN) */33u16 email_offset; /* Offset of emailAddress */34unsigned raw_akid_size;35const void *raw_akid; /* Raw authorityKeyId in ASN.1 */36const void *akid_raw_issuer; /* Raw directoryName in authorityKeyId */37unsigned akid_raw_issuer_size;38};3940/*41* Free an X.509 certificate42*/43void x509_free_certificate(struct x509_certificate *cert)44{45if (cert) {46public_key_free(cert->pub);47public_key_signature_free(cert->sig);48kfree(cert->issuer);49kfree(cert->subject);50kfree(cert->id);51kfree(cert->skid);52kfree(cert);53}54}55EXPORT_SYMBOL_GPL(x509_free_certificate);5657/*58* Parse an X.509 certificate59*/60struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)61{62struct x509_certificate *cert __free(x509_free_certificate) = NULL;63struct x509_parse_context *ctx __free(kfree) = NULL;64struct asymmetric_key_id *kid;65long ret;6667cert = kzalloc(sizeof(struct x509_certificate), GFP_KERNEL);68if (!cert)69return ERR_PTR(-ENOMEM);70cert->pub = kzalloc(sizeof(struct public_key), GFP_KERNEL);71if (!cert->pub)72return ERR_PTR(-ENOMEM);73cert->sig = kzalloc(sizeof(struct public_key_signature), GFP_KERNEL);74if (!cert->sig)75return ERR_PTR(-ENOMEM);76ctx = kzalloc(sizeof(struct x509_parse_context), GFP_KERNEL);77if (!ctx)78return ERR_PTR(-ENOMEM);7980ctx->cert = cert;81ctx->data = (unsigned long)data;8283/* Attempt to decode the certificate */84ret = asn1_ber_decoder(&x509_decoder, ctx, data, datalen);85if (ret < 0)86return ERR_PTR(ret);8788/* Decode the AuthorityKeyIdentifier */89if (ctx->raw_akid) {90pr_devel("AKID: %u %*phN\n",91ctx->raw_akid_size, ctx->raw_akid_size, ctx->raw_akid);92ret = asn1_ber_decoder(&x509_akid_decoder, ctx,93ctx->raw_akid, ctx->raw_akid_size);94if (ret < 0) {95pr_warn("Couldn't decode AuthKeyIdentifier\n");96return ERR_PTR(ret);97}98}99100cert->pub->key = kmemdup(ctx->key, ctx->key_size, GFP_KERNEL);101if (!cert->pub->key)102return ERR_PTR(-ENOMEM);103104cert->pub->keylen = ctx->key_size;105106cert->pub->params = kmemdup(ctx->params, ctx->params_size, GFP_KERNEL);107if (!cert->pub->params)108return ERR_PTR(-ENOMEM);109110cert->pub->paramlen = ctx->params_size;111cert->pub->algo = ctx->key_algo;112113/* Grab the signature bits */114ret = x509_get_sig_params(cert);115if (ret < 0)116return ERR_PTR(ret);117118/* Generate cert issuer + serial number key ID */119kid = asymmetric_key_generate_id(cert->raw_serial,120cert->raw_serial_size,121cert->raw_issuer,122cert->raw_issuer_size);123if (IS_ERR(kid))124return ERR_CAST(kid);125cert->id = kid;126127/* Detect self-signed certificates */128ret = x509_check_for_self_signed(cert);129if (ret < 0)130return ERR_PTR(ret);131132return_ptr(cert);133}134EXPORT_SYMBOL_GPL(x509_cert_parse);135136/*137* Note an OID when we find one for later processing when we know how138* to interpret it.139*/140int x509_note_OID(void *context, size_t hdrlen,141unsigned char tag,142const void *value, size_t vlen)143{144struct x509_parse_context *ctx = context;145146ctx->last_oid = look_up_OID(value, vlen);147if (ctx->last_oid == OID__NR) {148char buffer[50];149sprint_oid(value, vlen, buffer, sizeof(buffer));150pr_debug("Unknown OID: [%lu] %s\n",151(unsigned long)value - ctx->data, buffer);152}153return 0;154}155156/*157* Save the position of the TBS data so that we can check the signature over it158* later.159*/160int x509_note_tbs_certificate(void *context, size_t hdrlen,161unsigned char tag,162const void *value, size_t vlen)163{164struct x509_parse_context *ctx = context;165166pr_debug("x509_note_tbs_certificate(,%zu,%02x,%ld,%zu)!\n",167hdrlen, tag, (unsigned long)value - ctx->data, vlen);168169ctx->cert->tbs = value - hdrlen;170ctx->cert->tbs_size = vlen + hdrlen;171return 0;172}173174/*175* Record the algorithm that was used to sign this certificate.176*/177int x509_note_sig_algo(void *context, size_t hdrlen, unsigned char tag,178const void *value, size_t vlen)179{180struct x509_parse_context *ctx = context;181182pr_debug("PubKey Algo: %u\n", ctx->last_oid);183184switch (ctx->last_oid) {185default:186return -ENOPKG; /* Unsupported combination */187188case OID_sha1WithRSAEncryption:189ctx->cert->sig->hash_algo = "sha1";190goto rsa_pkcs1;191192case OID_sha256WithRSAEncryption:193ctx->cert->sig->hash_algo = "sha256";194goto rsa_pkcs1;195196case OID_sha384WithRSAEncryption:197ctx->cert->sig->hash_algo = "sha384";198goto rsa_pkcs1;199200case OID_sha512WithRSAEncryption:201ctx->cert->sig->hash_algo = "sha512";202goto rsa_pkcs1;203204case OID_sha224WithRSAEncryption:205ctx->cert->sig->hash_algo = "sha224";206goto rsa_pkcs1;207208case OID_id_ecdsa_with_sha1:209ctx->cert->sig->hash_algo = "sha1";210goto ecdsa;211212case OID_id_rsassa_pkcs1_v1_5_with_sha3_256:213ctx->cert->sig->hash_algo = "sha3-256";214goto rsa_pkcs1;215216case OID_id_rsassa_pkcs1_v1_5_with_sha3_384:217ctx->cert->sig->hash_algo = "sha3-384";218goto rsa_pkcs1;219220case OID_id_rsassa_pkcs1_v1_5_with_sha3_512:221ctx->cert->sig->hash_algo = "sha3-512";222goto rsa_pkcs1;223224case OID_id_ecdsa_with_sha224:225ctx->cert->sig->hash_algo = "sha224";226goto ecdsa;227228case OID_id_ecdsa_with_sha256:229ctx->cert->sig->hash_algo = "sha256";230goto ecdsa;231232case OID_id_ecdsa_with_sha384:233ctx->cert->sig->hash_algo = "sha384";234goto ecdsa;235236case OID_id_ecdsa_with_sha512:237ctx->cert->sig->hash_algo = "sha512";238goto ecdsa;239240case OID_id_ecdsa_with_sha3_256:241ctx->cert->sig->hash_algo = "sha3-256";242goto ecdsa;243244case OID_id_ecdsa_with_sha3_384:245ctx->cert->sig->hash_algo = "sha3-384";246goto ecdsa;247248case OID_id_ecdsa_with_sha3_512:249ctx->cert->sig->hash_algo = "sha3-512";250goto ecdsa;251252case OID_gost2012Signature256:253ctx->cert->sig->hash_algo = "streebog256";254goto ecrdsa;255256case OID_gost2012Signature512:257ctx->cert->sig->hash_algo = "streebog512";258goto ecrdsa;259case OID_id_ml_dsa_44:260ctx->cert->sig->pkey_algo = "mldsa44";261goto ml_dsa;262case OID_id_ml_dsa_65:263ctx->cert->sig->pkey_algo = "mldsa65";264goto ml_dsa;265case OID_id_ml_dsa_87:266ctx->cert->sig->pkey_algo = "mldsa87";267goto ml_dsa;268}269270rsa_pkcs1:271ctx->cert->sig->pkey_algo = "rsa";272ctx->cert->sig->encoding = "pkcs1";273ctx->sig_algo = ctx->last_oid;274return 0;275ecrdsa:276ctx->cert->sig->pkey_algo = "ecrdsa";277ctx->cert->sig->encoding = "raw";278ctx->sig_algo = ctx->last_oid;279return 0;280ecdsa:281ctx->cert->sig->pkey_algo = "ecdsa";282ctx->cert->sig->encoding = "x962";283ctx->sig_algo = ctx->last_oid;284return 0;285ml_dsa:286ctx->cert->sig->algo_takes_data = true;287ctx->cert->sig->hash_algo = "none";288ctx->cert->sig->encoding = "raw";289ctx->sig_algo = ctx->last_oid;290return 0;291}292293/*294* Note the whereabouts and type of the signature.295*/296int x509_note_signature(void *context, size_t hdrlen,297unsigned char tag,298const void *value, size_t vlen)299{300struct x509_parse_context *ctx = context;301302pr_debug("Signature: alg=%u, size=%zu\n", ctx->last_oid, vlen);303304/*305* In X.509 certificates, the signature's algorithm is stored in two306* places: inside the TBSCertificate (the data that is signed), and307* alongside the signature. These *must* match.308*/309if (ctx->last_oid != ctx->sig_algo) {310pr_warn("signatureAlgorithm (%u) differs from tbsCertificate.signature (%u)\n",311ctx->last_oid, ctx->sig_algo);312return -EINVAL;313}314315if (strcmp(ctx->cert->sig->pkey_algo, "rsa") == 0 ||316strcmp(ctx->cert->sig->pkey_algo, "ecrdsa") == 0 ||317strcmp(ctx->cert->sig->pkey_algo, "ecdsa") == 0 ||318strncmp(ctx->cert->sig->pkey_algo, "mldsa", 5) == 0) {319/* Discard the BIT STRING metadata */320if (vlen < 1 || *(const u8 *)value != 0)321return -EBADMSG;322323value++;324vlen--;325}326327ctx->cert->raw_sig = value;328ctx->cert->raw_sig_size = vlen;329return 0;330}331332/*333* Note the certificate serial number334*/335int x509_note_serial(void *context, size_t hdrlen,336unsigned char tag,337const void *value, size_t vlen)338{339struct x509_parse_context *ctx = context;340ctx->cert->raw_serial = value;341ctx->cert->raw_serial_size = vlen;342return 0;343}344345/*346* Note some of the name segments from which we'll fabricate a name.347*/348int x509_extract_name_segment(void *context, size_t hdrlen,349unsigned char tag,350const void *value, size_t vlen)351{352struct x509_parse_context *ctx = context;353354switch (ctx->last_oid) {355case OID_commonName:356ctx->cn_size = vlen;357ctx->cn_offset = (unsigned long)value - ctx->data;358break;359case OID_organizationName:360ctx->o_size = vlen;361ctx->o_offset = (unsigned long)value - ctx->data;362break;363case OID_email_address:364ctx->email_size = vlen;365ctx->email_offset = (unsigned long)value - ctx->data;366break;367default:368break;369}370371return 0;372}373374/*375* Fabricate and save the issuer and subject names376*/377static int x509_fabricate_name(struct x509_parse_context *ctx, size_t hdrlen,378unsigned char tag,379char **_name, size_t vlen)380{381const void *name, *data = (const void *)ctx->data;382size_t namesize;383char *buffer;384385if (*_name)386return -EINVAL;387388/* Empty name string if no material */389if (!ctx->cn_size && !ctx->o_size && !ctx->email_size) {390buffer = kzalloc(1, GFP_KERNEL);391if (!buffer)392return -ENOMEM;393goto done;394}395396if (ctx->cn_size && ctx->o_size) {397/* Consider combining O and CN, but use only the CN if it is398* prefixed by the O, or a significant portion thereof.399*/400namesize = ctx->cn_size;401name = data + ctx->cn_offset;402if (ctx->cn_size >= ctx->o_size &&403memcmp(data + ctx->cn_offset, data + ctx->o_offset,404ctx->o_size) == 0)405goto single_component;406if (ctx->cn_size >= 7 &&407ctx->o_size >= 7 &&408memcmp(data + ctx->cn_offset, data + ctx->o_offset, 7) == 0)409goto single_component;410411buffer = kmalloc(ctx->o_size + 2 + ctx->cn_size + 1,412GFP_KERNEL);413if (!buffer)414return -ENOMEM;415416memcpy(buffer,417data + ctx->o_offset, ctx->o_size);418buffer[ctx->o_size + 0] = ':';419buffer[ctx->o_size + 1] = ' ';420memcpy(buffer + ctx->o_size + 2,421data + ctx->cn_offset, ctx->cn_size);422buffer[ctx->o_size + 2 + ctx->cn_size] = 0;423goto done;424425} else if (ctx->cn_size) {426namesize = ctx->cn_size;427name = data + ctx->cn_offset;428} else if (ctx->o_size) {429namesize = ctx->o_size;430name = data + ctx->o_offset;431} else {432namesize = ctx->email_size;433name = data + ctx->email_offset;434}435436single_component:437buffer = kmalloc(namesize + 1, GFP_KERNEL);438if (!buffer)439return -ENOMEM;440memcpy(buffer, name, namesize);441buffer[namesize] = 0;442443done:444*_name = buffer;445ctx->cn_size = 0;446ctx->o_size = 0;447ctx->email_size = 0;448return 0;449}450451int x509_note_issuer(void *context, size_t hdrlen,452unsigned char tag,453const void *value, size_t vlen)454{455struct x509_parse_context *ctx = context;456struct asymmetric_key_id *kid;457458ctx->cert->raw_issuer = value;459ctx->cert->raw_issuer_size = vlen;460461if (!ctx->cert->sig->auth_ids[2]) {462kid = asymmetric_key_generate_id(value, vlen, "", 0);463if (IS_ERR(kid))464return PTR_ERR(kid);465ctx->cert->sig->auth_ids[2] = kid;466}467468return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->issuer, vlen);469}470471int x509_note_subject(void *context, size_t hdrlen,472unsigned char tag,473const void *value, size_t vlen)474{475struct x509_parse_context *ctx = context;476ctx->cert->raw_subject = value;477ctx->cert->raw_subject_size = vlen;478return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->subject, vlen);479}480481/*482* Extract the parameters for the public key483*/484int x509_note_params(void *context, size_t hdrlen,485unsigned char tag,486const void *value, size_t vlen)487{488struct x509_parse_context *ctx = context;489490/*491* AlgorithmIdentifier is used three times in the x509, we should skip492* first and ignore third, using second one which is after subject and493* before subjectPublicKey.494*/495if (!ctx->cert->raw_subject || ctx->key)496return 0;497ctx->params = value - hdrlen;498ctx->params_size = vlen + hdrlen;499return 0;500}501502/*503* Extract the data for the public key algorithm504*/505int x509_extract_key_data(void *context, size_t hdrlen,506unsigned char tag,507const void *value, size_t vlen)508{509struct x509_parse_context *ctx = context;510enum OID oid;511512ctx->key_algo = ctx->last_oid;513switch (ctx->last_oid) {514case OID_rsaEncryption:515ctx->cert->pub->pkey_algo = "rsa";516break;517case OID_gost2012PKey256:518case OID_gost2012PKey512:519ctx->cert->pub->pkey_algo = "ecrdsa";520break;521case OID_id_ecPublicKey:522if (parse_OID(ctx->params, ctx->params_size, &oid) != 0)523return -EBADMSG;524525switch (oid) {526case OID_id_prime192v1:527ctx->cert->pub->pkey_algo = "ecdsa-nist-p192";528break;529case OID_id_prime256v1:530ctx->cert->pub->pkey_algo = "ecdsa-nist-p256";531break;532case OID_id_ansip384r1:533ctx->cert->pub->pkey_algo = "ecdsa-nist-p384";534break;535case OID_id_ansip521r1:536ctx->cert->pub->pkey_algo = "ecdsa-nist-p521";537break;538default:539return -ENOPKG;540}541break;542case OID_id_ml_dsa_44:543ctx->cert->pub->pkey_algo = "mldsa44";544break;545case OID_id_ml_dsa_65:546ctx->cert->pub->pkey_algo = "mldsa65";547break;548case OID_id_ml_dsa_87:549ctx->cert->pub->pkey_algo = "mldsa87";550break;551default:552return -ENOPKG;553}554555/* Discard the BIT STRING metadata */556if (vlen < 1 || *(const u8 *)value != 0)557return -EBADMSG;558ctx->key = value + 1;559ctx->key_size = vlen - 1;560return 0;561}562563/* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */564#define SEQ_TAG_KEYID (ASN1_CONT << 6)565566/*567* Process certificate extensions that are used to qualify the certificate.568*/569int x509_process_extension(void *context, size_t hdrlen,570unsigned char tag,571const void *value, size_t vlen)572{573struct x509_parse_context *ctx = context;574struct asymmetric_key_id *kid;575const unsigned char *v = value;576577pr_debug("Extension: %u\n", ctx->last_oid);578579if (ctx->last_oid == OID_subjectKeyIdentifier) {580/* Get hold of the key fingerprint */581if (ctx->cert->skid || vlen < 3)582return -EBADMSG;583if (v[0] != ASN1_OTS || v[1] != vlen - 2)584return -EBADMSG;585v += 2;586vlen -= 2;587588ctx->cert->raw_skid_size = vlen;589ctx->cert->raw_skid = v;590kid = asymmetric_key_generate_id(v, vlen, "", 0);591if (IS_ERR(kid))592return PTR_ERR(kid);593ctx->cert->skid = kid;594pr_debug("subjkeyid %*phN\n", kid->len, kid->data);595return 0;596}597598if (ctx->last_oid == OID_keyUsage) {599/*600* Get hold of the keyUsage bit string601* v[1] is the encoding size602* (Expect either 0x02 or 0x03, making it 1 or 2 bytes)603* v[2] is the number of unused bits in the bit string604* (If >= 3 keyCertSign is missing when v[1] = 0x02)605* v[3] and possibly v[4] contain the bit string606*607* From RFC 5280 4.2.1.3:608* 0x04 is where keyCertSign lands in this bit string609* 0x80 is where digitalSignature lands in this bit string610*/611if (v[0] != ASN1_BTS)612return -EBADMSG;613if (vlen < 4)614return -EBADMSG;615if (v[2] >= 8)616return -EBADMSG;617if (v[3] & 0x80)618ctx->cert->pub->key_eflags |= 1 << KEY_EFLAG_DIGITALSIG;619if (v[1] == 0x02 && v[2] <= 2 && (v[3] & 0x04))620ctx->cert->pub->key_eflags |= 1 << KEY_EFLAG_KEYCERTSIGN;621else if (vlen > 4 && v[1] == 0x03 && (v[3] & 0x04))622ctx->cert->pub->key_eflags |= 1 << KEY_EFLAG_KEYCERTSIGN;623return 0;624}625626if (ctx->last_oid == OID_authorityKeyIdentifier) {627/* Get hold of the CA key fingerprint */628ctx->raw_akid = v;629ctx->raw_akid_size = vlen;630return 0;631}632633if (ctx->last_oid == OID_basicConstraints) {634/*635* Get hold of the basicConstraints636* v[1] is the encoding size637* (Expect 0x00 for empty SEQUENCE with CA:FALSE, or638* 0x03 or greater for non-empty SEQUENCE)639* v[2] is the encoding type640* (Expect an ASN1_BOOL for the CA)641* v[3] is the length of the ASN1_BOOL642* (Expect 1 for a single byte boolean)643* v[4] is the contents of the ASN1_BOOL644* (Expect 0xFF if the CA is TRUE)645* vlen should match the entire extension size646*/647if (v[0] != (ASN1_CONS_BIT | ASN1_SEQ))648return -EBADMSG;649if (vlen < 2)650return -EBADMSG;651if (v[1] != vlen - 2)652return -EBADMSG;653/* Empty SEQUENCE means CA:FALSE (default value omitted per DER) */654if (v[1] == 0)655return 0;656if (vlen >= 5 && v[2] == ASN1_BOOL && v[3] == 1 && v[4] == 0xFF)657ctx->cert->pub->key_eflags |= 1 << KEY_EFLAG_CA;658else659return -EBADMSG;660return 0;661}662663return 0;664}665666/**667* x509_decode_time - Decode an X.509 time ASN.1 object668* @_t: The time to fill in669* @hdrlen: The length of the object header670* @tag: The object tag671* @value: The object value672* @vlen: The size of the object value673*674* Decode an ASN.1 universal time or generalised time field into a struct the675* kernel can handle and check it for validity. The time is decoded thus:676*677* [RFC5280 ยง4.1.2.5]678* CAs conforming to this profile MUST always encode certificate validity679* dates through the year 2049 as UTCTime; certificate validity dates in680* 2050 or later MUST be encoded as GeneralizedTime. Conforming681* applications MUST be able to process validity dates that are encoded in682* either UTCTime or GeneralizedTime.683*/684int x509_decode_time(time64_t *_t, size_t hdrlen,685unsigned char tag,686const unsigned char *value, size_t vlen)687{688static const unsigned char month_lengths[] = { 31, 28, 31, 30, 31, 30,68931, 31, 30, 31, 30, 31 };690const unsigned char *p = value;691unsigned year, mon, day, hour, min, sec, mon_len;692693#define dec2bin(X) ({ unsigned char x = (X) - '0'; if (x > 9) goto invalid_time; x; })694#define DD2bin(P) ({ unsigned x = dec2bin(P[0]) * 10 + dec2bin(P[1]); P += 2; x; })695696if (tag == ASN1_UNITIM) {697/* UTCTime: YYMMDDHHMMSSZ */698if (vlen != 13)699goto unsupported_time;700year = DD2bin(p);701if (year >= 50)702year += 1900;703else704year += 2000;705} else if (tag == ASN1_GENTIM) {706/* GenTime: YYYYMMDDHHMMSSZ */707if (vlen != 15)708goto unsupported_time;709year = DD2bin(p) * 100 + DD2bin(p);710if (year >= 1950 && year <= 2049)711goto invalid_time;712} else {713goto unsupported_time;714}715716mon = DD2bin(p);717day = DD2bin(p);718hour = DD2bin(p);719min = DD2bin(p);720sec = DD2bin(p);721722if (*p != 'Z')723goto unsupported_time;724725if (year < 1970 ||726mon < 1 || mon > 12)727goto invalid_time;728729mon_len = month_lengths[mon - 1];730if (mon == 2) {731if (year % 4 == 0) {732mon_len = 29;733if (year % 100 == 0) {734mon_len = 28;735if (year % 400 == 0)736mon_len = 29;737}738}739}740741if (day < 1 || day > mon_len ||742hour > 24 || /* ISO 8601 permits 24:00:00 as midnight tomorrow */743min > 59 ||744sec > 60) /* ISO 8601 permits leap seconds [X.680 46.3] */745goto invalid_time;746747*_t = mktime64(year, mon, day, hour, min, sec);748return 0;749750unsupported_time:751pr_debug("Got unsupported time [tag %02x]: '%*phN'\n",752tag, (int)vlen, value);753return -EBADMSG;754invalid_time:755pr_debug("Got invalid time [tag %02x]: '%*phN'\n",756tag, (int)vlen, value);757return -EBADMSG;758}759EXPORT_SYMBOL_GPL(x509_decode_time);760761int x509_note_not_before(void *context, size_t hdrlen,762unsigned char tag,763const void *value, size_t vlen)764{765struct x509_parse_context *ctx = context;766return x509_decode_time(&ctx->cert->valid_from, hdrlen, tag, value, vlen);767}768769int x509_note_not_after(void *context, size_t hdrlen,770unsigned char tag,771const void *value, size_t vlen)772{773struct x509_parse_context *ctx = context;774return x509_decode_time(&ctx->cert->valid_to, hdrlen, tag, value, vlen);775}776777/*778* Note a key identifier-based AuthorityKeyIdentifier779*/780int x509_akid_note_kid(void *context, size_t hdrlen,781unsigned char tag,782const void *value, size_t vlen)783{784struct x509_parse_context *ctx = context;785struct asymmetric_key_id *kid;786787pr_debug("AKID: keyid: %*phN\n", (int)vlen, value);788789if (ctx->cert->sig->auth_ids[1])790return 0;791792kid = asymmetric_key_generate_id(value, vlen, "", 0);793if (IS_ERR(kid))794return PTR_ERR(kid);795pr_debug("authkeyid %*phN\n", kid->len, kid->data);796ctx->cert->sig->auth_ids[1] = kid;797return 0;798}799800/*801* Note a directoryName in an AuthorityKeyIdentifier802*/803int x509_akid_note_name(void *context, size_t hdrlen,804unsigned char tag,805const void *value, size_t vlen)806{807struct x509_parse_context *ctx = context;808809pr_debug("AKID: name: %*phN\n", (int)vlen, value);810811ctx->akid_raw_issuer = value;812ctx->akid_raw_issuer_size = vlen;813return 0;814}815816/*817* Note a serial number in an AuthorityKeyIdentifier818*/819int x509_akid_note_serial(void *context, size_t hdrlen,820unsigned char tag,821const void *value, size_t vlen)822{823struct x509_parse_context *ctx = context;824struct asymmetric_key_id *kid;825826pr_debug("AKID: serial: %*phN\n", (int)vlen, value);827828if (!ctx->akid_raw_issuer || ctx->cert->sig->auth_ids[0])829return 0;830831kid = asymmetric_key_generate_id(value,832vlen,833ctx->akid_raw_issuer,834ctx->akid_raw_issuer_size);835if (IS_ERR(kid))836return PTR_ERR(kid);837838pr_debug("authkeyid %*phN\n", kid->len, kid->data);839ctx->cert->sig->auth_ids[0] = kid;840return 0;841}842843844