1 // SPDX-License-Identifier: GPL-2.0+
3 * X509 cert parser using MbedTLS X509 library
5 * Copyright (c) 2024 Linaro Limited
10 #include <crypto/public_key.h>
11 #include <crypto/x509_parser.h>
13 static void x509_free_mbedtls_ctx(struct x509_cert_mbedtls_ctx *ctx)
19 kfree(ctx->raw_serial);
20 kfree(ctx->raw_issuer);
21 kfree(ctx->raw_subject);
26 static int x509_set_cert_flags(struct x509_certificate *cert)
28 struct public_key_signature *sig = cert->sig;
30 if (!sig || !cert->pub) {
31 pr_err("Signature or public key is not initialized\n");
35 if (!cert->pub->pkey_algo)
36 cert->unsupported_key = true;
39 cert->unsupported_sig = true;
42 cert->unsupported_sig = true;
44 /* TODO: is_hash_blacklisted()? */
46 /* Detect self-signed certificates and set self_signed flag */
47 return x509_check_for_self_signed(cert);
50 time64_t x509_get_timestamp(const mbedtls_x509_time *x509_time)
52 unsigned int year, mon, day, hour, min, sec;
54 /* Adjust for year since 1900 */
55 year = x509_time->year - 1900;
56 /* Adjust for 0-based month */
57 mon = x509_time->mon - 1;
59 hour = x509_time->hour;
63 return (time64_t)mktime64(year, mon, day, hour, min, sec);
66 static char *x509_populate_dn_name_string(const mbedtls_x509_name *name)
73 name_str = kzalloc(len, GFP_KERNEL);
77 wb = mbedtls_x509_dn_gets(name_str, len, name);
79 pr_err("Get DN string failed, ret:-0x%04x\n",
82 len = len * 2; /* Try with a bigger buffer */
86 name_str[wb] = '\0'; /* add the terminator */
91 static int x509_populate_signature_params(const mbedtls_x509_crt *cert,
92 struct public_key_signature **sig)
94 struct public_key_signature *s;
95 struct image_region region;
97 unsigned char *akid_data;
100 /* Check if signed data exist */
101 if (!cert->tbs.p || !cert->tbs.len)
104 region.data = cert->tbs.p;
105 region.size = cert->tbs.len;
107 s = kzalloc(sizeof(*s), GFP_KERNEL);
112 * Get the public key algorithm.
114 * ECRDSA (Elliptic Curve Russian Digital Signature Algorithm) is not
115 * supported by MbedTLS.
117 switch (cert->sig_pk) {
119 s->pkey_algo = "rsa";
126 /* Get the hash algorithm */
127 switch (cert->sig_md) {
128 case MBEDTLS_MD_SHA1:
129 s->hash_algo = "sha1";
130 s->digest_size = SHA1_SUM_LEN;
132 case MBEDTLS_MD_SHA256:
133 s->hash_algo = "sha256";
134 s->digest_size = SHA256_SUM_LEN;
136 case MBEDTLS_MD_SHA384:
137 s->hash_algo = "sha384";
138 s->digest_size = SHA384_SUM_LEN;
140 case MBEDTLS_MD_SHA512:
141 s->hash_algo = "sha512";
142 s->digest_size = SHA512_SUM_LEN;
144 /* Unsupported algo */
146 case MBEDTLS_MD_SHA224:
153 * Optional attributes:
154 * auth_ids holds AuthorityKeyIdentifier (information of issuer),
155 * aka akid, which is used to match with a cert's id or skid to
156 * indicate that is the issuer when we lookup a cert chain.
159 * [PKCS#7 or CMS ver 1] - generated from "Issuer + Serial number"
160 * [CMS ver 3] - generated from skid (subjectKeyId)
161 * auth_ids[1]: generated from skid (subjectKeyId)
163 * Assume that we are using PKCS#7 (msg->version=1),
164 * not CMS ver 3 (msg->version=3).
166 akid_len = cert->authority_key_id.authorityCertSerialNumber.len;
167 akid_data = cert->authority_key_id.authorityCertSerialNumber.p;
169 /* Check if serial number exists */
170 if (akid_len && akid_data) {
171 s->auth_ids[0] = asymmetric_key_generate_id(akid_data,
174 cert->issuer_raw.len);
175 if (!s->auth_ids[0]) {
181 akid_len = cert->authority_key_id.keyIdentifier.len;
182 akid_data = cert->authority_key_id.keyIdentifier.p;
184 /* Check if subjectKeyId exists */
185 if (akid_len && akid_data) {
186 s->auth_ids[1] = asymmetric_key_generate_id(akid_data,
189 if (!s->auth_ids[1]) {
196 * Encoding can be pkcs1 or raw, but only pkcs1 is supported.
197 * Set the encoding explicitly to pkcs1.
199 s->encoding = "pkcs1";
201 /* Copy the signature data */
202 s->s = kmemdup(cert->sig.p, cert->sig.len, GFP_KERNEL);
207 s->s_size = cert->sig.len;
209 /* Calculate the digest of signed data (tbs) */
210 s->digest = kzalloc(s->digest_size, GFP_KERNEL);
216 ret = hash_calculate(s->hash_algo, ®ion, 1, s->digest);
223 public_key_signature_free(s);
227 static int x509_save_mbedtls_ctx(const mbedtls_x509_crt *cert,
228 struct x509_cert_mbedtls_ctx **pctx)
230 struct x509_cert_mbedtls_ctx *ctx;
232 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
236 /* Signed data (tbs - The part that is To Be Signed)*/
237 ctx->tbs = kmemdup(cert->tbs.p, cert->tbs.len,
242 /* Raw serial number */
243 ctx->raw_serial = kmemdup(cert->serial.p,
244 cert->serial.len, GFP_KERNEL);
245 if (!ctx->raw_serial)
249 ctx->raw_issuer = kmemdup(cert->issuer_raw.p,
250 cert->issuer_raw.len, GFP_KERNEL);
251 if (!ctx->raw_issuer)
255 ctx->raw_subject = kmemdup(cert->subject_raw.p,
256 cert->subject_raw.len, GFP_KERNEL);
257 if (!ctx->raw_subject)
260 /* Raw subjectKeyId */
261 ctx->raw_skid = kmemdup(cert->subject_key_id.p,
262 cert->subject_key_id.len, GFP_KERNEL);
271 x509_free_mbedtls_ctx(ctx);
276 * Free an X.509 certificate
278 void x509_free_certificate(struct x509_certificate *cert)
281 public_key_free(cert->pub);
282 public_key_signature_free(cert->sig);
284 kfree(cert->subject);
287 x509_free_mbedtls_ctx(cert->mbedtls_ctx);
292 int x509_populate_pubkey(mbedtls_x509_crt *cert, struct public_key **pub_key)
294 struct public_key *pk;
296 pk = kzalloc(sizeof(*pk), GFP_KERNEL);
300 pk->key = kzalloc(cert->pk_raw.len, GFP_KERNEL);
305 memcpy(pk->key, cert->pk_raw.p, cert->pk_raw.len);
306 pk->keylen = cert->pk_raw.len;
309 * For ECC keys, params field might include information about the curve used,
310 * the generator point, or other algorithm-specific parameters.
311 * For RSA keys, it's common for the params field to be NULL.
312 * FIXME: Assume that we just support RSA keys with id_type X509.
317 pk->key_is_private = false;
318 pk->id_type = "X509";
319 pk->pkey_algo = "rsa";
320 pk->algo = OID_rsaEncryption;
327 int x509_populate_cert(mbedtls_x509_crt *mbedtls_cert,
328 struct x509_certificate **pcert)
330 struct x509_certificate *cert;
331 struct asymmetric_key_id *kid;
332 struct asymmetric_key_id *skid;
335 cert = kzalloc(sizeof(*cert), GFP_KERNEL);
339 /* Public key details */
340 ret = x509_populate_pubkey(mbedtls_cert, &cert->pub);
344 /* Signature parameters */
345 ret = x509_populate_signature_params(mbedtls_cert, &cert->sig);
351 /* Name of certificate issuer */
352 cert->issuer = x509_populate_dn_name_string(&mbedtls_cert->issuer);
356 /* Name of certificate subject */
357 cert->subject = x509_populate_dn_name_string(&mbedtls_cert->subject);
361 /* Certificate validity */
362 cert->valid_from = x509_get_timestamp(&mbedtls_cert->valid_from);
363 cert->valid_to = x509_get_timestamp(&mbedtls_cert->valid_to);
365 /* Save mbedtls context we need */
366 ret = x509_save_mbedtls_ctx(mbedtls_cert, &cert->mbedtls_ctx);
370 /* Signed data (tbs - The part that is To Be Signed)*/
371 cert->tbs = cert->mbedtls_ctx->tbs;
372 cert->tbs_size = mbedtls_cert->tbs.len;
374 /* Raw serial number */
375 cert->raw_serial = cert->mbedtls_ctx->raw_serial;
376 cert->raw_serial_size = mbedtls_cert->serial.len;
379 cert->raw_issuer = cert->mbedtls_ctx->raw_issuer;
380 cert->raw_issuer_size = mbedtls_cert->issuer_raw.len;
383 cert->raw_subject = cert->mbedtls_ctx->raw_subject;
384 cert->raw_subject_size = mbedtls_cert->subject_raw.len;
386 /* Raw subjectKeyId */
387 cert->raw_skid = cert->mbedtls_ctx->raw_skid;
388 cert->raw_skid_size = mbedtls_cert->subject_key_id.len;
390 /* Generate cert issuer + serial number key ID */
391 kid = asymmetric_key_generate_id(cert->raw_serial,
392 cert->raw_serial_size,
394 cert->raw_issuer_size);
401 /* Generate subject + subjectKeyId */
402 skid = asymmetric_key_generate_id(cert->raw_skid, cert->raw_skid_size, "", 0);
410 * Set the certificate flags:
411 * self_signed, unsupported_key, unsupported_sig, blacklisted
413 ret = x509_set_cert_flags(cert);
420 x509_free_certificate(cert);
424 struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)
426 mbedtls_x509_crt mbedtls_cert;
427 struct x509_certificate *cert = NULL;
430 /* Parse DER encoded certificate */
431 mbedtls_x509_crt_init(&mbedtls_cert);
432 ret = mbedtls_x509_crt_parse_der(&mbedtls_cert, data, datalen);
436 /* Populate x509_certificate from mbedtls_x509_crt */
437 ret = x509_populate_cert(&mbedtls_cert, &cert);
442 mbedtls_x509_crt_free(&mbedtls_cert);