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 internal definitions |
| 3 | * |
| 4 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
| 6 | */ |
| 7 | |
AKASHI Takahiro | 5180e1a | 2020-04-21 09:37:52 +0900 | [diff] [blame] | 8 | #ifndef _X509_PARSER_H |
| 9 | #define _X509_PARSER_H |
| 10 | |
AKASHI Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 11 | #include <linux/time.h> |
| 12 | #include <crypto/public_key.h> |
| 13 | #include <keys/asymmetric-type.h> |
Raymond Mao | f7b522f | 2024-10-03 14:50:31 -0700 | [diff] [blame] | 14 | #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509) |
| 15 | #include <image.h> |
| 16 | #include <mbedtls/error.h> |
| 17 | #include <mbedtls/asn1.h> |
| 18 | #endif |
AKASHI Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 19 | |
Raymond Mao | f7b522f | 2024-10-03 14:50:31 -0700 | [diff] [blame] | 20 | #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509) |
| 21 | struct 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 Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 39 | struct x509_certificate { |
Raymond Mao | f7b522f | 2024-10-03 14:50:31 -0700 | [diff] [blame] | 40 | #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509) |
| 41 | struct x509_cert_mbedtls_ctx *mbedtls_ctx; |
| 42 | #endif |
AKASHI Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 43 | 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 | */ |
| 77 | extern void x509_free_certificate(struct x509_certificate *cert); |
Raymond Mao | f7b522f | 2024-10-03 14:50:31 -0700 | [diff] [blame] | 78 | #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 | */ |
| 86 | int 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 | */ |
| 94 | int 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 | */ |
| 102 | time64_t x509_get_timestamp(const mbedtls_x509_time *x509_time); |
| 103 | #endif |
AKASHI Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 104 | extern struct x509_certificate *x509_cert_parse(const void *data, size_t datalen); |
| 105 | extern 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 Mao | f7b522f | 2024-10-03 14:50:31 -0700 | [diff] [blame] | 112 | #if !CONFIG_IS_ENABLED(MBEDTLS_LIB_X509) |
AKASHI Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 113 | extern int x509_get_sig_params(struct x509_certificate *cert); |
Raymond Mao | f7b522f | 2024-10-03 14:50:31 -0700 | [diff] [blame] | 114 | #endif |
AKASHI Takahiro | 591535c | 2019-11-13 09:45:00 +0900 | [diff] [blame] | 115 | extern int x509_check_for_self_signed(struct x509_certificate *cert); |
AKASHI Takahiro | 5180e1a | 2020-04-21 09:37:52 +0900 | [diff] [blame] | 116 | #endif /* _X509_PARSER_H */ |