LCOV - code coverage report
Current view: top level - src/edhoc - cert.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 71.3 % 122 87
Test Date: 2026-11-06 14:43:19 Functions: 83.3 % 6 5
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 45.8 % 72 33

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :    Copyright (c) 2021 Fraunhofer AISEC. See the COPYRIGHT
       3                 :             :    file at the top-level directory of this distribution.
       4                 :             : 
       5                 :             :    Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
       6                 :             :    http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
       7                 :             :    <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
       8                 :             :    option. This file may not be copied, modified, or distributed
       9                 :             :    except according to those terms.
      10                 :             : */
      11                 :             : 
      12                 :             : #include <stdbool.h>
      13                 :             : #include <stdint.h>
      14                 :             : 
      15                 :             : #include "edhoc/buffer_sizes.h"
      16                 :             : 
      17                 :             : #include "edhoc/cert.h"
      18                 :             : 
      19                 :             : #include "common/memcpy_s.h"
      20                 :             : #include "common/oscore_edhoc_error.h"
      21                 :             : #include "common/crypto_wrapper.h"
      22                 :             : #include "common/print_util.h"
      23                 :             : 
      24                 :             : #include "cbor/edhoc_decode_cert.h"
      25                 :             : 
      26                 :             : #ifdef MBEDTLS
      27                 :             : #define MBEDTLS_ALLOW_PRIVATE_ACCESS
      28                 :             : 
      29                 :             : #include <psa/crypto.h>
      30                 :             : #include <mbedtls/asn1.h>
      31                 :             : #include <mbedtls/error.h>
      32                 :             : #include <mbedtls/md.h>
      33                 :             : #include <mbedtls/oid.h>
      34                 :             : #include <mbedtls/x509.h>
      35                 :             : #include <mbedtls/x509_crt.h>
      36                 :             : 
      37                 :             : struct deser_sign_ctx_s {
      38                 :             :         uint8_t *seek;
      39                 :             :         uint8_t *end;
      40                 :             :         int unit_size;
      41                 :             : };
      42                 :             : 
      43                 :           2 : static void deser_sign_ctx_init(struct deser_sign_ctx_s *ctx, uint8_t *seek,
      44                 :             :                                 uint8_t *end, int unit_size)
      45                 :             : {
      46                 :           2 :         ctx->seek = seek;
      47                 :           2 :         ctx->end = end;
      48                 :           2 :         ctx->unit_size = unit_size;
      49                 :           2 : }
      50                 :             : 
      51                 :           4 : static int deser_sign_cb(void *void_ctx, int tag, unsigned char *start,
      52                 :             :                          size_t len)
      53                 :             : {
      54         [ +  - ]:           4 :         if (tag == MBEDTLS_ASN1_INTEGER) {
      55                 :           4 :                 struct deser_sign_ctx_s *ctx = void_ctx;
      56                 :           4 :                 uint8_t *unit_end = ctx->seek + ctx->unit_size;
      57         [ +  - ]:           4 :                 if (unit_end <= ctx->end) {
      58                 :           4 :                         memset(ctx->seek, 0, (size_t)ctx->unit_size);
      59         [ +  - ]:           4 :                         if (len >= (size_t)ctx->unit_size) {
      60                 :           4 :                                 memcpy(ctx->seek,
      61                 :           4 :                                        start + len - (size_t)ctx->unit_size,
      62                 :           4 :                                        (size_t)ctx->unit_size);
      63                 :             :                         } else {
      64                 :           0 :                                 memcpy(ctx->seek + (ctx->unit_size - (int)len),
      65                 :             :                                        start, len);
      66                 :             :                         }
      67                 :           4 :                         ctx->seek = unit_end;
      68                 :             :                 }
      69                 :             :         }
      70                 :           4 :         return 0;
      71                 :             : }
      72                 :             : 
      73                 :           4 : static int find_pk_cb(void *void_ppk, int tag, unsigned char *start, size_t len)
      74                 :             : {
      75                 :             :         (void)len;
      76                 :             : 
      77         [ +  + ]:           4 :         if (tag == MBEDTLS_ASN1_BIT_STRING) {
      78                 :           2 :                 uint8_t **pk = void_ppk;
      79                 :           2 :                 *pk = start;
      80                 :             :         }
      81                 :           4 :         return 0;
      82                 :             : }
      83                 :             : 
      84                 :             : #define PSA_KEY_ALL_USAGES                                                     \
      85                 :             :         (PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY | PSA_KEY_USAGE_ENCRYPT |   \
      86                 :             :          PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_SIGN_MESSAGE |                  \
      87                 :             :          PSA_KEY_USAGE_VERIFY_MESSAGE | PSA_KEY_USAGE_SIGN_HASH |              \
      88                 :             :          PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_DERIVE)
      89                 :             : 
      90                 :             : #else /* MBEDTLS */
      91                 :             : 
      92                 :             : #define ISSUER_CN_OID "\x55\x04\x03"
      93                 :             : 
      94                 :             : #define EXPECTO_TAG(tag, cursor, len)                                          \
      95                 :             :         if (cursor >= end || *cursor != tag) {                                 \
      96                 :             :                 rv = wrong_parameter;                                          \
      97                 :             :                 PRINTF(RED                                                     \
      98                 :             :                        "Runtime error: expected %s tag at %s:%d\n\n" RESET,    \
      99                 :             :                        #tag, __FILE__, __LINE__);                              \
     100                 :             :                 break;                                                         \
     101                 :             :         } else {                                                               \
     102                 :             :                 cursor++;                                                      \
     103                 :             :                 if (0 != mbedtls_asn1_get_len(&cursor, end, &len)) {           \
     104                 :             :                         rv = wrong_parameter;                                  \
     105                 :             :                         break;                                                 \
     106                 :             :                 }                                                              \
     107                 :             :                 if (0 < len && 0 == *cursor) {                                 \
     108                 :             :                         cursor++;                                              \
     109                 :             :                         len--;                                                 \
     110                 :             :                 }                                                              \
     111                 :             :         }
     112                 :             : 
     113                 :             : enum tag_map_enum {
     114                 :             :         ASN1_INTEGER = 0x02,
     115                 :             :         ASN1_BIT_STRING = 0x03,
     116                 :             :         ASN1_SEQUENCE = 0x30
     117                 :             : };
     118                 :             : 
     119                 :             : /* Extracted from mbedtls library; asn1_parse.c */
     120                 :             : /* License for file: Apache-2.0 */
     121                 :             : /* ASN.1 DER encoding is described in ITU-T X.690 standard. */
     122                 :             : /* First bit of length byte contains information, if length
     123                 :             :    value shall be concatenated with following byte length. */
     124                 :             : static int mbedtls_asn1_get_len(const unsigned char **p,
     125                 :             :                                 const unsigned char *end, uint32_t *len)
     126                 :             : {
     127                 :             :         if ((end - *p) < 1)
     128                 :             :                 return (buffer_to_small);
     129                 :             : 
     130                 :             :         if ((**p & 0x80) == 0)
     131                 :             :                 *len = *(*p)++;
     132                 :             :         else {
     133                 :             :                 switch (**p & 0x7F) {
     134                 :             :                 case 1:
     135                 :             :                         if ((end - *p) < 2)
     136                 :             :                                 return (buffer_to_small);
     137                 :             : 
     138                 :             :                         *len = (*p)[1];
     139                 :             :                         (*p) += 2;
     140                 :             :                         break;
     141                 :             : 
     142                 :             :                 case 2:
     143                 :             :                         if ((end - *p) < 3)
     144                 :             :                                 return (buffer_to_small);
     145                 :             : 
     146                 :             :                         *len = ((uint32_t)(*p)[1] << 8) | (*p)[2];
     147                 :             :                         (*p) += 3;
     148                 :             :                         break;
     149                 :             : 
     150                 :             :                 case 3:
     151                 :             :                         if ((end - *p) < 4)
     152                 :             :                                 return (buffer_to_small);
     153                 :             : 
     154                 :             :                         *len = ((uint32_t)(*p)[1] << 16) |
     155                 :             :                                ((uint32_t)(*p)[2] << 8) | (*p)[3];
     156                 :             :                         (*p) += 4;
     157                 :             :                         break;
     158                 :             : 
     159                 :             :                 case 4:
     160                 :             :                         if ((end - *p) < 5)
     161                 :             :                                 return (buffer_to_small);
     162                 :             : 
     163                 :             :                         *len = ((uint32_t)(*p)[1] << 24) |
     164                 :             :                                ((uint32_t)(*p)[2] << 16) |
     165                 :             :                                ((uint32_t)(*p)[3] << 8) | (*p)[4];
     166                 :             :                         (*p) += 5;
     167                 :             :                         break;
     168                 :             : 
     169                 :             :                 default:
     170                 :             :                         return (buffer_to_small);
     171                 :             :                 }
     172                 :             :         }
     173                 :             : 
     174                 :             :         if (*len > (uint32_t)(end - *p))
     175                 :             :                 return (buffer_to_small);
     176                 :             : 
     177                 :             :         return (0);
     178                 :             : }
     179                 :             : 
     180                 :             : #endif /* MBEDTLS */
     181                 :             : 
     182                 :             : /**
     183                 :             :  * @brief retrieves the public key of the CA from CRED_ARRAY.
     184                 :             :  * 
     185                 :             :  * 
     186                 :             :  * @param[in] cred_array contains the public key of the root CA
     187                 :             :  * @param[in] issuer the issuer name, i.e. the name of the CA
     188                 :             :  * @param[out] root_pk the root public key
     189                 :             :  * @return error code
     190                 :             :  */
     191                 :           2 : static enum err ca_pk_get(const struct cred_array *cred_array,
     192                 :             :                           const uint8_t *issuer, struct byte_array *root_pk)
     193                 :             : {
     194                 :             :         /* when single credential without certificate is stored, return stored ca_pk if available */
     195         [ +  - ]:           2 :         if (1 == cred_array->len
     196                 :             : #ifdef MBEDTLS
     197                 :             :             /* In case no MBEDTLS is enabled, issuer identification is not extracted from certificate */
     198         [ +  - ]:           2 :             && (0 == cred_array->ptr[0].ca.len ||
     199         [ -  + ]:           2 :                 NULL == cred_array->ptr[0].ca.ptr)
     200                 :             : #endif
     201                 :             :         ) {
     202         [ #  # ]:           0 :                 if (NULL == cred_array->ptr[0].ca_pk.ptr ||
     203         [ #  # ]:           0 :                     0 == cred_array->ptr[0].ca_pk.len) {
     204                 :           0 :                         return no_such_ca;
     205                 :             :                 }
     206                 :             : 
     207                 :           0 :                 root_pk->ptr = cred_array->ptr[0].ca_pk.ptr;
     208                 :           0 :                 root_pk->len = cred_array->ptr[0].ca_pk.len;
     209                 :           0 :                 return ok;
     210                 :             :         }
     211                 :             : 
     212                 :             : #ifdef MBEDTLS
     213                 :             :         /* Accept only certificate based search if multiple credentials available*/
     214         [ +  - ]:           2 :         for (uint16_t i = 0; i < cred_array->len; i++) {
     215         [ +  - ]:           2 :                 if (NULL == cred_array->ptr[i].ca.ptr ||
     216         [ -  + ]:           2 :                     0 == cred_array->ptr[i].ca.len) {
     217                 :           0 :                         continue;
     218                 :             :                 }
     219                 :             : 
     220                 :           2 :                 PRINT_ARRAY("cred_array[i].ca.ptr", cred_array->ptr[i].ca.ptr,
     221                 :             :                             cred_array->ptr[i].ca.len);
     222                 :             : 
     223                 :             :                 mbedtls_x509_crt m_cert;
     224                 :           2 :                 mbedtls_x509_crt_init(&m_cert);
     225                 :             : 
     226                 :             :                 /* parse the certificate */
     227         [ -  + ]:           4 :                 TRY_EXPECT(mbedtls_x509_crt_parse_der_nocopy(
     228                 :             :                                    &m_cert, cred_array->ptr[i].ca.ptr,
     229                 :             :                                    cred_array->ptr[i].ca.len),
     230                 :             :                            0);
     231                 :             : 
     232                 :           2 :                 const mbedtls_x509_name *p = &m_cert.subject;
     233                 :           2 :                 const mbedtls_asn1_buf *subject_id = NULL;
     234         [ +  + ]:           4 :                 while (p) {
     235   [ +  -  -  +  :           2 :                         if (0 == MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &p->oid)) {
                   +  - ]
     236                 :           2 :                                 subject_id = &p->val;
     237                 :             :                         }
     238                 :           2 :                         p = p->next;
     239                 :             :                 };
     240                 :             : 
     241         [ +  - ]:           2 :                 if (0 == memcmp(subject_id->p, issuer, subject_id->len)) {
     242                 :           2 :                         root_pk->ptr = cred_array->ptr[i].ca_pk.ptr;
     243                 :           2 :                         root_pk->len = cred_array->ptr[i].ca_pk.len;
     244                 :           2 :                         PRINT_ARRAY("Root PK of the CA", root_pk->ptr,
     245                 :             :                                     root_pk->len);
     246                 :           2 :                         mbedtls_x509_crt_free(&m_cert);
     247                 :           2 :                         return ok;
     248                 :             :                 } else {
     249                 :           0 :                         mbedtls_x509_crt_free(&m_cert);
     250                 :             :                 }
     251                 :             :         }
     252                 :             : #endif /* MBEDTLS */
     253                 :             : 
     254                 :           0 :         return no_such_ca;
     255                 :             : }
     256                 :             : 
     257                 :           0 : enum err cert_c509_verify(struct const_byte_array *cert,
     258                 :             :                           const struct cred_array *cred_array,
     259                 :             :                           struct byte_array *pk, bool *verified)
     260                 :             : {
     261                 :           0 :         size_t decode_len = 0;
     262                 :             :         struct cert c;
     263                 :             : 
     264         [ #  # ]:           0 :         TRY_EXPECT(cbor_decode_cert(cert->ptr, cert->len, &c, &decode_len), 0);
     265                 :             : 
     266                 :           0 :         PRINT_MSG("CBOR certificate parsed.\n");
     267                 :           0 :         PRINTF("Certificate type: %d\n", c.cert_type);
     268                 :           0 :         PRINT_ARRAY("issuer", c.cert_issuer.value, (uint32_t)c.cert_issuer.len);
     269                 :           0 :         PRINTF("validity_not_before: %d\n", c.cert_validity_not_before);
     270                 :           0 :         PRINTF("validity_not_after: %d\n", c.cert_validity_not_after);
     271                 :           0 :         PRINT_ARRAY("subject", c.cert_subject.value,
     272                 :             :                     (uint32_t)c.cert_subject.len);
     273                 :           0 :         PRINT_ARRAY("PK", c.cert_pk.value, (uint32_t)c.cert_pk.len);
     274                 :           0 :         PRINTF("extensions: %d\n", c.cert_extensions);
     275                 :           0 :         PRINTF("issuer_signature_algorithm: %d\n",
     276                 :             :                c.cert_issuer_signature_algorithm);
     277                 :           0 :         PRINT_ARRAY("Signature", c.cert_signature.value,
     278                 :             :                     (uint32_t)c.cert_signature.len);
     279                 :             : 
     280                 :             :         /*get the CA's public key*/
     281                 :             :         struct byte_array root_pk;
     282         [ #  # ]:           0 :         TRY(ca_pk_get(cred_array, c.cert_issuer.value, &root_pk));
     283                 :             : 
     284                 :             :         /*verify the certificates signature*/
     285                 :           0 :         struct const_byte_array m = BYTE_ARRAY_INIT(
     286                 :             :                 cert->ptr, cert->len - 2 - (uint32_t)c.cert_signature.len);
     287                 :           0 :         struct const_byte_array sgn = BYTE_ARRAY_INIT(
     288                 :             :                 c.cert_signature.value, (uint32_t)c.cert_signature.len);
     289                 :             : 
     290         [ #  # ]:           0 :         TRY(verify((enum sign_alg)c.cert_issuer_signature_algorithm, &root_pk,
     291                 :             :                    &m, &sgn, verified));
     292                 :             : 
     293         [ #  # ]:           0 :         TRY(_memcpy_s(pk->ptr, pk->len, c.cert_pk.value,
     294                 :             :                       (uint32_t)c.cert_pk.len));
     295                 :           0 :         pk->len = (uint32_t)c.cert_pk.len;
     296                 :             : 
     297                 :           0 :         return ok;
     298                 :             : }
     299                 :             : 
     300                 :           2 : enum err cert_x509_verify(struct const_byte_array *cert,
     301                 :             :                           const struct cred_array *cred_array,
     302                 :             :                           struct byte_array *pk, bool *verified)
     303                 :             : {
     304                 :             : #ifdef MBEDTLS
     305                 :             : 
     306                 :           2 :         PRINT_MSG("Start parsing an ASN.1 certificate\n");
     307                 :             : 
     308                 :             :         mbedtls_x509_crt m_cert;
     309                 :           2 :         mbedtls_x509_crt_init(&m_cert);
     310                 :             : 
     311                 :             :         /* parse the certificate */
     312         [ -  + ]:           2 :         TRY_EXPECT(mbedtls_x509_crt_parse_der_nocopy(&m_cert, cert->ptr,
     313                 :             :                                                      cert->len),
     314                 :             :                    0);
     315                 :             : 
     316                 :             :         /* some raw data from certificate */
     317                 :           2 :         PRINT_ARRAY("cert.serial", m_cert.serial.p,
     318                 :             :                     (uint32_t)m_cert.serial.len);
     319                 :           2 :         PRINT_ARRAY("cert.issuer_raw", m_cert.issuer_raw.p,
     320                 :             :                     (uint32_t)m_cert.issuer_raw.len);
     321                 :             : 
     322                 :             :         /* write details about the issuer */
     323                 :             :         /* and find CN (Common Name), further referred to as "issuer_id" */
     324                 :           2 :         const mbedtls_x509_name *p = &m_cert.issuer;
     325                 :             : 
     326                 :             : #ifdef DEBUG_PRINT
     327                 :             :         const char *short_name;
     328                 :             : #endif
     329                 :           2 :         const mbedtls_asn1_buf *issuer_id = NULL;
     330         [ +  + ]:           4 :         while (p) {
     331                 :             : #ifdef DEBUG_PRINT
     332                 :           2 :                 mbedtls_oid_get_attr_short_name(&p->oid, &short_name);
     333                 :           2 :                 PRINTF("        %s: %.*s\n", short_name, (int)p->val.len,
     334                 :             :                        p->val.p);
     335                 :             : #endif
     336   [ +  -  -  +  :           2 :                 if (0 == MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &p->oid)) {
                   +  - ]
     337                 :           2 :                         issuer_id = &p->val;
     338                 :             :                 }
     339                 :           2 :                 p = p->next;
     340                 :             :         };
     341                 :             : 
     342                 :           2 :         PRINT_ARRAY("cert issuer_id", issuer_id->p, (uint32_t)issuer_id->len);
     343                 :             : 
     344                 :             :         enum sign_alg sign_alg;
     345                 :             : 
     346                 :             :         /* make sure it is ECDSA */
     347         [ +  - ]:           2 :         if (MBEDTLS_PK_ECDSA == m_cert.sig_pk) {
     348                 :           2 :                 sign_alg = ES256;
     349                 :             :         } else {
     350                 :           0 :                 mbedtls_x509_crt_free(&m_cert);
     351                 :           0 :                 return unsupported_signature_algorithm;
     352                 :             :         }
     353                 :             : 
     354                 :             :         /* check hash algorithm and init signature buffer */
     355                 :             :         const mbedtls_md_info_t *md_info =
     356                 :           2 :                 mbedtls_md_info_from_type(m_cert.sig_md);
     357         [ -  + ]:           2 :         if (NULL == md_info) {
     358                 :           0 :                 PRINTF("mbedtls_md_info_from_type(%d) : not found\n",
     359                 :             :                        m_cert.sig_md);
     360                 :           0 :                 mbedtls_x509_crt_free(&m_cert);
     361                 :           0 :                 return unsupported_signature_algorithm;
     362                 :             :         }
     363                 :           2 :         int hash_len = mbedtls_md_get_size(md_info);
     364                 :             : 
     365   [ -  +  -  + ]:           2 :         BYTE_ARRAY_NEW(sig, SIGNATURE_SIZE, get_signature_len(sign_alg));
     366                 :             : 
     367                 :             :         /* get the public key of the CA */
     368                 :             :         struct byte_array root_pk;
     369         [ -  + ]:           2 :         TRY(ca_pk_get(cred_array, issuer_id->p, &root_pk));
     370                 :             : 
     371                 :             :         /* deserialize signature from ASN.1 to raw concatenation of (R, S) */
     372                 :             :         {
     373                 :           2 :                 uint8_t *pp = m_cert.sig.p;
     374                 :             :                 struct deser_sign_ctx_s deser_sign_ctx;
     375                 :           2 :                 deser_sign_ctx_init(&deser_sign_ctx, sig.ptr, sig.ptr + sig.len,
     376                 :             :                                     hash_len);
     377                 :           2 :                 mbedtls_asn1_traverse_sequence_of(&pp, pp + m_cert.sig.len, 0,
     378                 :             :                                                   0, 0, 0, deser_sign_cb,
     379                 :             :                                                   &deser_sign_ctx);
     380                 :           2 :                 PRINT_ARRAY("Certificate signature", sig.ptr, sig.len);
     381                 :             :         }
     382                 :             : 
     383                 :             :         /*verify the certificates signature*/
     384                 :           2 :         struct const_byte_array m =
     385                 :           2 :                 BYTE_ARRAY_INIT(m_cert.tbs.p, (uint32_t)m_cert.tbs.len);
     386         [ -  + ]:           2 :         TRY(verify(sign_alg, &root_pk, &m, (struct const_byte_array *)&sig,
     387                 :             :                    verified));
     388                 :             : 
     389                 :             :         /* export the public key from certificate */
     390                 :             :         {
     391                 :           2 :                 uint8_t *cpk = NULL;
     392                 :           2 :                 uint32_t cpk_len = 0;
     393                 :           2 :                 uint8_t *pp = m_cert.pk_raw.p;
     394                 :           2 :                 mbedtls_asn1_traverse_sequence_of(&pp, pp + m_cert.pk_raw.len,
     395                 :             :                                                   0, 0, 0, 0, find_pk_cb, &cpk);
     396         [ +  - ]:           2 :                 if (cpk) {
     397         [ +  - ]:           2 :                         if (*cpk == 0) {
     398                 :           2 :                                 ++cpk;
     399                 :             :                         }
     400                 :           2 :                         cpk_len = (uint32_t)m_cert.pk_raw.len -
     401                 :           2 :                                   (uint32_t)(cpk - m_cert.pk_raw.p);
     402                 :             :                 }
     403         [ -  + ]:           2 :                 TRY(_memcpy_s(pk->ptr, pk->len, cpk, (uint32_t)cpk_len));
     404                 :           2 :                 pk->len = (uint32_t)cpk_len;
     405                 :           2 :                 PRINT_ARRAY("pk from cert", pk->ptr, pk->len);
     406                 :             :         }
     407                 :             : 
     408                 :             :         /* cleanup */
     409                 :           2 :         mbedtls_x509_crt_free(&m_cert);
     410                 :             : 
     411                 :           2 :         return ok;
     412                 :             : 
     413                 :             : #else /* MBEDTLS */
     414                 :             : 
     415                 :             :         const uint8_t *tbs_start = cert->ptr;
     416                 :             :         const uint8_t *tbs_end = &cert->ptr[cert->len];
     417                 :             :         BYTE_ARRAY_NEW(sig, SIGNATURE_SIZE, get_signature_len(ES256));
     418                 :             : 
     419                 :             :         enum err rv = certificate_authentication_failed;
     420                 :             : 
     421                 :             :         /* Crude way to get TBSCertificate address, public key and signature */
     422                 :             :         do {
     423                 :             :                 const uint8_t *cursor = cert->ptr;
     424                 :             :                 const uint8_t *end = &cert->ptr[cert->len];
     425                 :             :                 uint32_t len = 0;
     426                 :             : 
     427                 :             :                 /* Get first tag, which should be first sequence */
     428                 :             :                 EXPECTO_TAG(ASN1_SEQUENCE, cursor, len);
     429                 :             : 
     430                 :             :                 /* Get second, inner tag, which should be TBSCertificate */
     431                 :             :                 tbs_start = cursor;
     432                 :             :                 EXPECTO_TAG(ASN1_SEQUENCE, cursor, len);
     433                 :             :                 tbs_end = cursor + len;
     434                 :             : 
     435                 :             :                 /* Iterate over 6 elements to get to public key according to X.509 schema */
     436                 :             :                 for (uint32_t iter = 0; iter < 6; iter++) {
     437                 :             :                         /* TAG shall not be parsed as it is not used */
     438                 :             :                         if (cursor >= end) {
     439                 :             :                                 rv = wrong_parameter;
     440                 :             :                                 break;
     441                 :             :                         }
     442                 :             :                         cursor++;
     443                 :             :                         /* Get section length */
     444                 :             :                         if (0 != mbedtls_asn1_get_len(&cursor, end, &len)) {
     445                 :             :                                 rv = wrong_parameter;
     446                 :             :                                 break;
     447                 :             :                         }
     448                 :             :                         cursor += len;
     449                 :             :                 }
     450                 :             :                 if (wrong_parameter == rv) {
     451                 :             :                         break;
     452                 :             :                 }
     453                 :             : 
     454                 :             :                 /* Here should be 7th element with information about key */
     455                 :             :                 EXPECTO_TAG(ASN1_SEQUENCE, cursor, len);
     456                 :             : 
     457                 :             :                 /* Here should be element with information about type of key */
     458                 :             :                 EXPECTO_TAG(ASN1_SEQUENCE, cursor, len);
     459                 :             :                 /* This section is being skippped */
     460                 :             :                 cursor += len;
     461                 :             : 
     462                 :             :                 /* Expected BIT STRING containing public key */
     463                 :             :                 EXPECTO_TAG(ASN1_BIT_STRING, cursor, len);
     464                 :             : 
     465                 :             :                 /* Now cursor points to public key */
     466                 :             :                 TRY(_memcpy_s(pk->ptr, pk->len, cursor, (uint32_t)len));
     467                 :             :                 pk->len = (uint32_t)len;
     468                 :             :                 PRINT_ARRAY("pk from cert", pk->ptr, pk->len);
     469                 :             : 
     470                 :             :                 /* We can skip whole TBSCertificate */
     471                 :             :                 cursor = tbs_end;
     472                 :             : 
     473                 :             :                 /* Here should be information about signature algorithm */
     474                 :             :                 EXPECTO_TAG(ASN1_SEQUENCE, cursor, len);
     475                 :             :                 /* We can skip algorithm info length */
     476                 :             :                 cursor += len;
     477                 :             : 
     478                 :             :                 /* Expected BIT STRING containing signature */
     479                 :             :                 EXPECTO_TAG(ASN1_BIT_STRING, cursor, len);
     480                 :             :                 // cursor++;
     481                 :             : 
     482                 :             :                 EXPECTO_TAG(ASN1_SEQUENCE, cursor, len);
     483                 :             : 
     484                 :             :                 EXPECTO_TAG(ASN1_INTEGER, cursor, len);
     485                 :             : 
     486                 :             :                 TRY_EXPECT((cursor + len) <= end, 1);
     487                 :             :                 TRY(_memcpy_s(sig.ptr, SIGNATURE_SIZE, cursor, (uint32_t)len));
     488                 :             :                 sig.len = len;
     489                 :             :                 cursor += len;
     490                 :             :                 PRINT_ARRAY("Certificate signature - part1", sig.ptr,
     491                 :             :                             (uint32_t)sig.len);
     492                 :             : 
     493                 :             :                 EXPECTO_TAG(ASN1_INTEGER, cursor, len);
     494                 :             :                 TRY_EXPECT((cursor + len) <= end, 1);
     495                 :             :                 TRY(_memcpy_s(sig.ptr + sig.len,
     496                 :             :                               (uint32_t)(SIGNATURE_SIZE - sig.len), cursor,
     497                 :             :                               (uint32_t)len));
     498                 :             :                 sig.len += len;
     499                 :             :                 rv = ok;
     500                 :             :                 PRINT_ARRAY("Certificate signature", sig.ptr,
     501                 :             :                             (uint32_t)sig.len);
     502                 :             : 
     503                 :             :         } while (0);
     504                 :             : 
     505                 :             :         struct byte_array root_pk;
     506                 :             :         struct const_byte_array m =
     507                 :             :                 BYTE_ARRAY_INIT(tbs_start, (uint32_t)(tbs_end - tbs_start));
     508                 :             : 
     509                 :             :         if (ok == rv) {
     510                 :             :                 for (uint32_t iter = 0; iter < cred_array->len; iter++) {
     511                 :             :                         /* Issuer will not be read from certificate, so no identification is possible within public keys array. */
     512                 :             :                         TRY(ca_pk_get(&cred_array[iter], NULL, &root_pk));
     513                 :             :                         PRINT_ARRAY("pk from cred_list", root_pk.ptr,
     514                 :             :                                     root_pk.len);
     515                 :             :                         struct const_byte_array s = { .ptr = sig.ptr,
     516                 :             :                                                       .len = sig.len };
     517                 :             :                         rv = verify(ES256, &root_pk, &m, &s, verified);
     518                 :             :                 }
     519                 :             :         }
     520                 :             :         return rv;
     521                 :             : 
     522                 :             : #endif /* MBEDTLS */
     523                 :             : }
        

Generated by: LCOV version 2.0-1