Skip to content

Wrong certificate obtained with atca_mbedtls_cert_add() after upgrading to ~v3.7.7 #410

@AdityaHPatwardhan

Description

@AdityaHPatwardhan

Describe the bug

I am not exactly sure when this error was introduced, but now the certificate added through atca_mbedtls_cert_add() does not work correctly.

When the certificate is added through this API, The following certificate gets added

Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            5f:72:87:ca:61:15:ae:16:b8:d0:cd:9c:fa:f4:61:dc
        Signature Algorithm: ecdsa-with-SHA256
        Issuer: O=Microchip Technology Inc, CN=Crypto Authentication Signer 2D20
        Validity
            Not Before: Mar  1 02:00:00 2019 GMT
            Not After : Mar  1 02:00:00 2047 GMT
        Subject: O=Microchip Technology Inc, CN=012386E1761428FF01 ATECC
        Subject Public Key Info:
            Public Key Algorithm: id-ecPublicKey
                Public-Key: (256 bit)
                pub:
                    04:4f:cc:2d:ee:db:a2:eb:05:15:c0:01:70:1f:94:
                    7d:9d:db:1c:04:03:ca:a9:36:22:ea:43:81:03:68:
                    a4:e6:c5:57:32:f4:cb:71:04:e7:54:ff:b1:23:ae:
                    e3:3c:17:4c:71:a2:d9:87:c7:1d:6b:dd:cf:b0:18:
                    fe:b6:bc:36:db
                ASN1 OID: prime256v1
                NIST CURVE: P-256
        X509v3 extensions:
            X509v3 Basic Constraints: critical
                CA:FALSE
            X509v3 Key Usage: critical
                Digital Signature, Key Agreement
            X509v3 Subject Key Identifier:
                F4:4F:2B:86:5D:9D:99:A3:A1:41:09:90:DC:19:06:76:DC:84:13:77
            X509v3 Authority Key Identifier:
                00:B5:E1:B7:3F:4B:60:66:DD:1F:C3:3C:D2:AA:CA:9D:09:52:48:BA
    Signature Algorithm: ecdsa-with-SHA256
    Signature Value:
        30:45:02:21:00:97:88:b3:5d:a0:e2:63:1e:2c:ed:08:04:44:
        58:2e:89:a6:a5:09:1e:c8:c3:f5:1e:80:d5:d6:7b:d5:0f:27:
        59:02:20:01:f8:02:4e:62:18:48:35:89:57:ad:64:b7:01:da:
        60:4d:ff:d5:ed:f9:45:c3:a8:39:4e:be:44:7d:5e:a1:40

whereas the actual certificate is (obtained through tng_atcacert_read_device_cert() API)

Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            5f:72:87:ca:61:15:ae:16:b8:d0:cd:9c:fa:f4:61:dc
        Signature Algorithm: ecdsa-with-SHA256
        Issuer: O=Microchip Technology Inc, CN=Crypto Authentication Signer 2D20
        Validity
            Not Before: Mar  1 02:00:00 2019 GMT
            Not After : Mar  1 02:00:00 2047 GMT
        Subject: O=Microchip Technology Inc, CN=012386E1761428FF01 ATECC
        Subject Public Key Info:
            Public Key Algorithm: id-ecPublicKey
                Public-Key: (256 bit)
                pub:
                    04:4f:cc:2d:ee:db:a2:eb:05:15:c0:01:70:1f:94:
                    7d:9d:db:1c:04:03:ca:a9:36:22:ea:43:81:03:68:
                    a4:e6:c5:57:32:f4:cb:71:04:e7:54:ff:b1:23:ae:
                    e3:3c:17:4c:71:a2:d9:87:c7:1d:6b:dd:cf:b0:18:
                    fe:b6:bc:36:db
                ASN1 OID: prime256v1
                NIST CURVE: P-256
        X509v3 extensions:
            X509v3 Basic Constraints: critical
                CA:FALSE
            X509v3 Key Usage: critical
                Digital Signature, Key Agreement
            X509v3 Subject Key Identifier:
                F4:4F:2B:86:5D:9D:99:A3:A1:41:09:90:DC:19:06:76:DC:84:13:77
            X509v3 Authority Key Identifier:
                14:BE:A9:F9:FE:D6:2F:91:42:51:87:4E:FC:0A:1C:F0:EF:44:F1:94
    Signature Algorithm: ecdsa-with-SHA256
    Signature Value:
        30:45:02:21:00:97:88:b3:5d:a0:e2:63:1e:2c:ed:08:04:44:
        58:2e:89:a6:a5:09:1e:c8:c3:f5:1e:80:d5:d6:7b:d5:0f:27:
        59:02:20:01:f8:02:4e:62:18:48:35:89:57:ad:64:b7:01:da:
        60:4d:ff:d5:ed:f9:45:c3:a8:39:4e:be:44:7d:5e:a1:40

The only difference here is in the X509v3 Authority Key Identifier.

Here is the signer obtained through the tng_atcacert_read_signer_cert(cert_buf, cert_len) for reference

Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            71:0a:49:b4:f9:df:04:25:69:06:c7:53:df:98:2c:d0
        Signature Algorithm: ecdsa-with-SHA256
        Issuer: O=Microchip Technology Inc, CN=Crypto Authentication Root CA 002
        Validity
            Not Before: Dec 14 20:00:00 2018 GMT
            Not After : Dec 14 20:00:00 2049 GMT
        Subject: O=Microchip Technology Inc, CN=Crypto Authentication Signer 2D20
        Subject Public Key Info:
            Public Key Algorithm: id-ecPublicKey
                Public-Key: (256 bit)
                pub:
                    04:e6:0f:d3:86:83:2a:a1:52:20:b4:ba:4c:63:6f:
                    b4:0c:52:08:c4:10:2a:8e:5f:1b:4a:85:aa:f1:ee:
                    c5:aa:7a:c7:e6:36:ca:c6:78:1a:8e:9d:74:e4:77:
                    04:8f:ee:5b:35:9e:a1:39:13:26:8b:ad:21:3d:39:
                    ed:fe:ae:41:1d
                ASN1 OID: prime256v1
                NIST CURVE: P-256
        X509v3 extensions:
            X509v3 Key Usage: critical
                Digital Signature, Certificate Sign, CRL Sign
            X509v3 Basic Constraints: critical
                CA:TRUE, pathlen:0
            X509v3 Subject Key Identifier:
                14:BE:A9:F9:FE:D6:2F:91:42:51:87:4E:FC:0A:1C:F0:EF:44:F1:94
            X509v3 Authority Key Identifier:
                7A:ED:7D:6D:C6:B7:78:9D:B2:38:01:A5:E8:4A:8C:B0:A4:0E:2A:8C
    Signature Algorithm: ecdsa-with-SHA256
    Signature Value:
        30:45:02:20:00:f5:11:7a:3f:d2:87:c9:3a:07:f8:9a:83:15:
        00:e2:ac:0a:2d:1c:e4:4a:54:0a:7e:02:44:d3:e5:20:be:dd:
        02:21:00:e6:27:5e:61:7c:59:7b:30:12:7b:25:c5:75:b7:5a:
        8e:e6:90:16:38:93:63:79:7c:7d:93:c4:3a:7c:92:cc:21

To Reproduce

  1. Use a TNG type of ATECC608 device,

Use atca_mbedtls_cert_add() to import the tng certificate to the mbedtls certificate structure and print it on console as follows:


    mbedtls_x509_crt_init(&tls->clientcert);
    ret = tng_get_device_cert_def(&cert_def);
    if (ret != 0) {
        ESP_LOGE(TAG, "Failed to get device cert def");
        return ESP_ERR_ESP_TLS_SE_FAILED;
    }
    ret = atca_mbedtls_cert_add(&tls->clientcert, cert_def);
    if (ret != 0) {
        ESP_LOGE(TAG, "Failed to parse cert from device, return 0x%04X", ret);
        mbedtls_print_error_msg(ret);
        return ESP_ERR_ESP_TLS_SE_FAILED;
    }

    /* Print the certificate: DER format (mbedTLS v3.x only stores DER, not PEM) */
    if (tls->clientcert.raw.p != NULL && tls->clientcert.raw.len > 0) {
        /* Print DER certificate in hex format */
        const unsigned char *cert_data = tls->clientcert.raw.p;
        size_t cert_len = tls->clientcert.raw.len;
        for (size_t i = 0; i < cert_len; i++) {
            printf("%02X", cert_data[i]);
        }
        printf("\n");
    } else {
        printf("No client certificate available to print.\n");
    }

Expected behavior
The certificate here must match with the certificate provided by tng_atcacert_read_device_cert API

Additional context
The problem seems to be in the loc (location) value of the auth key identifier for the device cert.
Somehow that is being messed up.

Note, I am using ATECC608A device with serial no - 012386e1761428ff01

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions