/*
 * emudhra-blocklist.c: reject eMudhra's compromised G3 keys, and the RSA G2B/G2C
 * keys from the same ceremony, in Mbed TLS.
 *
 * For devices that cannot replace their compiled-in CA store quickly. Pass
 * emudhra_blocklist_vrfy as the f_vrfy callback of mbedtls_x509_crt_verify(),
 * or install it with mbedtls_ssl_conf_verify(). Any certificate in the chain
 * whose public key matches one of these keys is flagged as revoked, so the
 * handshake fails. Matching on the key (SPKI) rather than the certificate
 * also catches the Sectigo cross-signed copies and any re-issued variant.
 *
 * Tested with Mbed TLS 3.6.6. It uses only crt->pk_raw and psa_hash_compute(),
 * which Mbed TLS 4.x / TF-PSA-Crypto also provide, but it is untested there.
 * psa_crypto_init() must have been called, which Mbed TLS TLS setup needs anyway.
 *
 * Build the self-test against an Mbed TLS build tree:
 *   cc -DEMUDHRA_BLOCKLIST_TEST emudhra-blocklist.c -I<mbedtls>/include \
 *      -L<build>/library -lmbedx509 -lmbedcrypto -o blocklist-test
 *   ./blocklist-test chain.pem ca-bundle.pem
 *
 * SPDX-License-Identifier: Apache-2.0
 * Dotstar Systems, https://dotstarsystems.com/blog/emudhra-three-weeks-later/
 */
#include <string.h>
#include <psa/crypto.h>
#include <mbedtls/x509_crt.h>

/* SHA-256 of the DER SubjectPublicKeyInfo of each key. */
static const unsigned char emudhra_g3_spki_sha256[][32] = {
    /* emSign ECC Root CA - G3 (trust anchor, removed from root stores) */
    {0x8d, 0x41, 0x7d, 0xb2, 0xdd, 0x8b, 0xf5, 0xe3, 0x08, 0x4d, 0x1e, 0x3f, 0x19, 0x6d, 0x58, 0x38,
     0x49, 0xd8, 0x1b, 0xdd, 0x4c, 0x00, 0xc7, 0x0b, 0x9d, 0x39, 0x36, 0x9e, 0x96, 0xb8, 0xc7, 0x82},
    /* emSign Root TLS CA - G3 */
    {0xc6, 0x19, 0xe4, 0x78, 0x0e, 0xd8, 0xe0, 0xab, 0xed, 0xa2, 0x4a, 0xd7, 0x32, 0x56, 0x7d, 0xe4,
     0x38, 0x3a, 0x7e, 0xa9, 0x41, 0x33, 0x8f, 0x6d, 0x1a, 0x7d, 0x1b, 0x49, 0xac, 0x79, 0x2f, 0xe9},
    /* EM DV TLS CA - G3B (d = 156) */
    {0xeb, 0xed, 0x64, 0x75, 0xc2, 0xc2, 0xd8, 0x0f, 0xcb, 0x19, 0x9f, 0x3f, 0xe8, 0xdd, 0x6e, 0x6b,
     0x3c, 0xe6, 0x0b, 0x77, 0xeb, 0x15, 0xd4, 0xe4, 0x2c, 0x3d, 0xec, 0x0c, 0x6f, 0x5b, 0x51, 0x05},
    /* EM DV TLS CA - G3C (d = 230) */
    {0x49, 0xdd, 0x35, 0x3d, 0xc2, 0xb9, 0x19, 0xa3, 0x77, 0x06, 0x45, 0x11, 0x7d, 0x0e, 0x60, 0xc1,
     0x61, 0x15, 0xe7, 0xd1, 0xbc, 0x25, 0x9e, 0xef, 0xe7, 0x34, 0xb0, 0x0e, 0xa4, 0x34, 0xfe, 0x5f},
    /* EM EV TLS CA - G3B (d = 115) */
    {0xe0, 0x25, 0x4c, 0x7f, 0xcd, 0xc2, 0x65, 0xe2, 0xf0, 0xf8, 0xa4, 0x28, 0xda, 0xbb, 0x75, 0xab,
     0x4a, 0xe0, 0x39, 0xb4, 0x87, 0x2d, 0x62, 0xf5, 0xee, 0x57, 0xf0, 0x87, 0x7d, 0x5a, 0x8a, 0x41},
    /* EM EV TLS CA - G3C (d = 56) */
    {0x5c, 0x06, 0x60, 0xa2, 0xb1, 0x64, 0x91, 0x0d, 0x8f, 0xf5, 0x89, 0xc1, 0x8e, 0x14, 0x55, 0x2e,
     0x9e, 0x0e, 0xc2, 0x37, 0xdc, 0xef, 0xe2, 0xcb, 0x73, 0x6d, 0x41, 0xa4, 0x81, 0x1f, 0x81, 0xa0},
    /* EM OV CS CA - G3B (d = 66) */
    {0xec, 0x4f, 0xfa, 0x1a, 0xd0, 0xac, 0x88, 0x08, 0x13, 0x14, 0xb7, 0xaa, 0xff, 0xef, 0x6a, 0x91,
     0xf9, 0x7c, 0xd5, 0x72, 0x91, 0xbb, 0x5b, 0xa6, 0x86, 0xc5, 0x41, 0x5f, 0x5e, 0xb4, 0xf6, 0xb6},
    /* EM OV CS CA - G3C (d = 237) */
    {0x92, 0xc0, 0x63, 0xd6, 0x7e, 0x8f, 0x14, 0xdb, 0x71, 0xd2, 0x88, 0xe1, 0x12, 0x66, 0xff, 0xda,
     0xf6, 0x9c, 0xbf, 0x11, 0x5f, 0x7b, 0xab, 0x14, 0xf3, 0xbc, 0x2f, 0x68, 0xd2, 0x5e, 0x0e, 0x30},
    /* EM OV TLS CA - G3B (d = 155) */
    {0x43, 0xbf, 0xfa, 0x19, 0xd6, 0xef, 0x65, 0x14, 0x51, 0xf6, 0x84, 0x1e, 0x01, 0x99, 0x31, 0xe9,
     0x6f, 0x6f, 0x2e, 0xae, 0xff, 0x28, 0xab, 0x4b, 0x6f, 0x1c, 0xe4, 0x57, 0x6d, 0xe2, 0x71, 0x7b},
    /* EM OV TLS CA - G3C (d = 4) */
    {0x63, 0xec, 0xef, 0xee, 0x43, 0x1d, 0x49, 0x52, 0x3b, 0xc1, 0x13, 0x6c, 0x4e, 0x72, 0xd0, 0x20,
     0x6a, 0x47, 0x70, 0x86, 0xca, 0xf6, 0x06, 0xcd, 0x5c, 0xf0, 0x25, 0x68, 0x6c, 0x10, 0x9c, 0xf8},
    /*
     * RSA-4096 G2B/G2C intermediates (added 11 November 2026). Not shown to
     * be compromised, but their keys passed through host memory in the same
     * ceremony and eMudhra cannot show they stayed confidential. Revoked 7
     * November 2026 (cessationOfOperation). The TLS ones chain to
     * emSign Root CA - G1, which is still in Mozilla's store.
     */
    /* EM DV TLS CA - G2B */
    {0x04, 0xdb, 0x3b, 0x7d, 0xdf, 0x68, 0x3b, 0x1d, 0x4b, 0x5b, 0x64, 0xfe, 0x74, 0x3a, 0x3a, 0x8d,
     0x8c, 0x3d, 0x71, 0xf0, 0xc3, 0x39, 0xd7, 0xdc, 0x25, 0xb3, 0x43, 0xe6, 0x65, 0xa9, 0xeb, 0x5b},
    /* EM DV TLS CA - G2C */
    {0x34, 0x55, 0x68, 0x38, 0x28, 0x16, 0x88, 0xd1, 0xc9, 0x67, 0xd4, 0x3d, 0x13, 0x72, 0xa1, 0xbb,
     0xaf, 0x38, 0x92, 0x77, 0x22, 0x2f, 0x03, 0xe1, 0x01, 0x7b, 0x18, 0xb3, 0x48, 0xa1, 0xf0, 0x0e},
    /* EM OV TLS CA - G2B */
    {0x84, 0x29, 0x35, 0x10, 0x35, 0xba, 0x90, 0xba, 0x14, 0xef, 0x3f, 0x7a, 0xe3, 0x8c, 0xa3, 0x54,
     0x80, 0x60, 0x5a, 0x02, 0x96, 0xaa, 0x6c, 0x26, 0x3c, 0x6a, 0x9b, 0x26, 0xbb, 0x9a, 0x48, 0xe1},
    /* EM OV TLS CA - G2C */
    {0xaa, 0x42, 0x16, 0xdc, 0x80, 0xa7, 0x29, 0x9a, 0xe6, 0xde, 0x08, 0x44, 0x4b, 0xea, 0x2c, 0x58,
     0x8d, 0xc8, 0x73, 0x63, 0x1e, 0xbc, 0x56, 0x3c, 0x07, 0x76, 0xd8, 0x3a, 0x1b, 0x61, 0xe3, 0xce},
    /* EM EV TLS CA - G2B */
    {0xc2, 0x69, 0x0b, 0x6b, 0xf3, 0x5a, 0x93, 0x64, 0xe6, 0x97, 0x73, 0x5e, 0xc6, 0x30, 0xbf, 0xd9,
     0xd2, 0xcd, 0x59, 0x04, 0x0e, 0x39, 0x08, 0xa6, 0x67, 0x65, 0x16, 0x8e, 0x32, 0x82, 0xb0, 0x4e},
    /* EM EV TLS CA - G2C */
    {0x1a, 0x94, 0x2e, 0x39, 0x25, 0xb8, 0x09, 0x89, 0x6b, 0x4b, 0x29, 0x1a, 0x8c, 0xd2, 0xe7, 0xbc,
     0x5f, 0x1d, 0x53, 0x3a, 0xa5, 0x23, 0xb8, 0x35, 0x3b, 0x2d, 0xf3, 0x77, 0xb0, 0xf0, 0xad, 0xbe},
    /* EM OV CS CA - G2B */
    {0x01, 0x69, 0x61, 0x35, 0x48, 0x12, 0x3d, 0xab, 0x14, 0xaf, 0xff, 0xcb, 0x5a, 0x3f, 0x0e, 0x80,
     0x4c, 0x70, 0xac, 0x90, 0x3f, 0x29, 0xec, 0xad, 0x55, 0xed, 0x43, 0xdc, 0x75, 0xf1, 0x38, 0x81},
    /* EM OV CS CA - G2C */
    {0xc0, 0x73, 0xef, 0xb3, 0x14, 0x22, 0xff, 0x4d, 0xb9, 0x9d, 0xc2, 0x4d, 0x74, 0x42, 0x43, 0x8e,
     0x94, 0x2c, 0x73, 0xcd, 0x3c, 0x61, 0xb5, 0x29, 0xa8, 0xba, 0x32, 0x54, 0x2e, 0x94, 0x6b, 0x86},
};

int emudhra_blocklist_vrfy(void *ctx, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
{
    unsigned char h[32];
    size_t hlen;

    (void) ctx;
    (void) depth;
    if (psa_hash_compute(PSA_ALG_SHA_256, crt->pk_raw.p, crt->pk_raw.len,
                         h, sizeof(h), &hlen) != PSA_SUCCESS) {
        *flags |= MBEDTLS_X509_BADCERT_OTHER; /* fail closed */
        return 0;
    }
    for (size_t i = 0; i < sizeof(emudhra_g3_spki_sha256) / 32; i++) {
        if (memcmp(h, emudhra_g3_spki_sha256[i], 32) == 0) {
            *flags |= MBEDTLS_X509_BADCERT_REVOKED;
        }
    }
    return 0;
}

#ifdef EMUDHRA_BLOCKLIST_TEST
#include <stdio.h>

int main(int argc, char **argv)
{
    mbedtls_x509_crt chain, ca;
    uint32_t plain = 0, blocked = 0;
    char buf[512];

    if (argc != 3) {
        fprintf(stderr, "usage: %s chain.pem ca-bundle.pem\n", argv[0]);
        return 2;
    }
    psa_crypto_init();
    mbedtls_x509_crt_init(&chain);
    mbedtls_x509_crt_init(&ca);
    if (mbedtls_x509_crt_parse_file(&chain, argv[1]) != 0 ||
        mbedtls_x509_crt_parse_file(&ca, argv[2]) < 0) {
        fprintf(stderr, "cannot parse input\n");
        return 2;
    }
    mbedtls_x509_crt_verify(&chain, &ca, NULL, NULL, &plain, NULL, NULL);
    mbedtls_x509_crt_verify(&chain, &ca, NULL, NULL, &blocked,
                            emudhra_blocklist_vrfy, NULL);
    printf("without blocklist: %s\n", plain ? "rejected" : "accepted");
    printf("with blocklist:    %s\n", blocked ? "rejected" : "accepted");
    if (blocked) {
        mbedtls_x509_crt_verify_info(buf, sizeof(buf), "  ", blocked);
        fputs(buf, stdout);
    }
    return blocked ? 1 : 0;
}
#endif
