blob: 0e22e33f66b5a534845cad9773b3a19b8c81e277 [file] [log] [blame]
AKASHI Takahiro591535c2019-11-13 09:45:00 +09001/* SPDX-License-Identifier: GPL-2.0-or-later */
2/* X.509 certificate parser internal definitions
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
AKASHI Takahiro5180e1a2020-04-21 09:37:52 +09008#ifndef _X509_PARSER_H
9#define _X509_PARSER_H
10
AKASHI Takahiro591535c2019-11-13 09:45:00 +090011#include <linux/time.h>
12#include <crypto/public_key.h>
13#include <keys/asymmetric-type.h>
Raymond Maof7b522f2024-10-03 14:50:31 -070014#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
15#include <image.h>
16#include <mbedtls/error.h>
17#include <mbedtls/asn1.h>
18#endif
AKASHI Takahiro591535c2019-11-13 09:45:00 +090019
Raymond Maof7b522f2024-10-03 14:50:31 -070020#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
21struct x509_cert_mbedtls_ctx {
22 void *tbs; /* Signed data */
23 void *raw_serial; /* Raw serial number in ASN.1 */
24 void *raw_issuer; /* Raw issuer name in ASN.1 */
25 void *raw_subject; /* Raw subject name in ASN.1 */
26 void *raw_skid; /* Raw subjectKeyId in ASN.1 */
27};
28#endif
29
30/*
31 * MbedTLS integration Notes:
32 *
33 * Fields we don't need to populate from MbedTLS context:
34 * 'raw_sig' and 'raw_sig_size' are buffer for x509_parse_context,
35 * not needed for MbedTLS.
36 * 'signer' and 'seen' are used internally by pkcs7_verify.
37 * 'verified' is not in use.
38 */
AKASHI Takahiro591535c2019-11-13 09:45:00 +090039struct x509_certificate {
Raymond Maof7b522f2024-10-03 14:50:31 -070040#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
41 struct x509_cert_mbedtls_ctx *mbedtls_ctx;
42#endif
AKASHI Takahiro591535c2019-11-13 09:45:00 +090043 struct x509_certificate *next;
44 struct x509_certificate *signer; /* Certificate that signed this one */
45 struct public_key *pub; /* Public key details */
46 struct public_key_signature *sig; /* Signature parameters */
47 char *issuer; /* Name of certificate issuer */
48 char *subject; /* Name of certificate subject */
49 struct asymmetric_key_id *id; /* Issuer + Serial number */
50 struct asymmetric_key_id *skid; /* Subject + subjectKeyId (optional) */
51 time64_t valid_from;
52 time64_t valid_to;
53 const void *tbs; /* Signed data */
54 unsigned tbs_size; /* Size of signed data */
55 unsigned raw_sig_size; /* Size of sigature */
56 const void *raw_sig; /* Signature data */
57 const void *raw_serial; /* Raw serial number in ASN.1 */
58 unsigned raw_serial_size;
59 unsigned raw_issuer_size;
60 const void *raw_issuer; /* Raw issuer name in ASN.1 */
61 const void *raw_subject; /* Raw subject name in ASN.1 */
62 unsigned raw_subject_size;
63 unsigned raw_skid_size;
64 const void *raw_skid; /* Raw subjectKeyId in ASN.1 */
65 unsigned index;
66 bool seen; /* Infinite recursion prevention */
67 bool verified;
68 bool self_signed; /* T if self-signed (check unsupported_sig too) */
69 bool unsupported_key; /* T if key uses unsupported crypto */
70 bool unsupported_sig; /* T if signature uses unsupported crypto */
71 bool blacklisted;
72};
73
74/*
75 * x509_cert_parser.c
76 */
77extern void x509_free_certificate(struct x509_certificate *cert);
Raymond Maof7b522f2024-10-03 14:50:31 -070078#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
79/**
80 * x509_populate_pubkey() - Populate public key from MbedTLS context
81 *
82 * @cert: Pointer to MbedTLS X509 cert
83 * @pub_key: Pointer to the populated public key handle
84 * Return: 0 on succcess, error code on failure
85 */
86int x509_populate_pubkey(mbedtls_x509_crt *cert, struct public_key **pub_key);
87/**
88 * x509_populate_cert() - Populate X509 cert from MbedTLS context
89 *
90 * @mbedtls_cert: Pointer to MbedTLS X509 cert
91 * @pcert: Pointer to the populated X509 cert handle
92 * Return: 0 on succcess, error code on failure
93 */
94int x509_populate_cert(mbedtls_x509_crt *mbedtls_cert,
95 struct x509_certificate **pcert);
96/**
97 * x509_get_timestamp() - Translate timestamp from MbedTLS context
98 *
99 * @x509_time: Pointer to MbedTLS time
100 * Return: Time in time64_t format
101 */
102time64_t x509_get_timestamp(const mbedtls_x509_time *x509_time);
103#endif
AKASHI Takahiro591535c2019-11-13 09:45:00 +0900104extern struct x509_certificate *x509_cert_parse(const void *data, size_t datalen);
105extern int x509_decode_time(time64_t *_t, size_t hdrlen,
106 unsigned char tag,
107 const unsigned char *value, size_t vlen);
108
109/*
110 * x509_public_key.c
111 */
Raymond Maof7b522f2024-10-03 14:50:31 -0700112#if !CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
AKASHI Takahiro591535c2019-11-13 09:45:00 +0900113extern int x509_get_sig_params(struct x509_certificate *cert);
Raymond Maof7b522f2024-10-03 14:50:31 -0700114#endif
AKASHI Takahiro591535c2019-11-13 09:45:00 +0900115extern int x509_check_for_self_signed(struct x509_certificate *cert);
AKASHI Takahiro5180e1a2020-04-21 09:37:52 +0900116#endif /* _X509_PARSER_H */