LCOV - code coverage report
Current view: top level - src/edhoc - retrieve_cred.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 69.9 % 83 58
Test Date: 2026-11-06 14:43:19 Functions: 100.0 % 4 4
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 51.8 % 85 44

             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 <string.h>
      13                 :             : 
      14                 :             : #include "edhoc/buffer_sizes.h"
      15                 :             : #include "edhoc/cert.h"
      16                 :             : #include "edhoc/bstr_encode_decode.h"
      17                 :             : #include "edhoc/int_encode_decode.h"
      18                 :             : #include "edhoc/retrieve_cred.h"
      19                 :             : 
      20                 :             : #include "common/crypto_wrapper.h"
      21                 :             : #include "common/oscore_edhoc_error.h"
      22                 :             : #include "common/print_util.h"
      23                 :             : #include "common/memcpy_s.h"
      24                 :             : 
      25                 :             : #include "cbor/edhoc_decode_id_cred_x.h"
      26                 :             : 
      27                 :             : /**
      28                 :             :  * @brief                       Verifies a certificate and copies it to the cred 
      29                 :             :  *                              buffer. It also extracts the public key
      30                 :             :  *                              contained in the certificate. 
      31                 :             :  * 
      32                 :             :  * @param static_dh_auth        Type of the key contained in the certificate -- 
      33                 :             :  *                              signature key or static DH key.
      34                 :             :  * @param cred_array            array containing credentials.
      35                 :             :  * @param label                 CBOR map label of id_cred_x.
      36                 :             :  * @param[in] cert              The certificate.
      37                 :             :  * @param[in] cred              Cred buffer.
      38                 :             :  * @param[in] pk                Public key.
      39                 :             :  * @param[in] g                 Static DH public key.
      40                 :             :  * @return                      Ok or error code
      41                 :             :  */
      42                 :             : static inline enum err
      43                 :           2 : verify_cert2cred(bool static_dh_auth, struct cred_array *cred_array,
      44                 :             :                  enum id_cred_x_label label, struct const_byte_array *cert,
      45                 :             :                  struct byte_array *cred, struct byte_array *pk,
      46                 :             :                  struct byte_array *g)
      47                 :             : {
      48                 :           2 :         PRINT_ARRAY("ID_CRED_x contains a certificate", cert->ptr, cert->len);
      49         [ -  + ]:           2 :         TRY(encode_bstr((struct byte_array *)cert, cred));
      50                 :             : 
      51                 :           2 :         bool verified = false;
      52      [ +  -  - ]:           2 :         switch (label) {
      53                 :             :         /* for now we transfer a single certificate, therefore bag and chain are the same */
      54                 :           2 :         case x5bag:
      55                 :             :         case x5chain:
      56         [ -  + ]:           2 :                 if (static_dh_auth) {
      57                 :           0 :                         pk->len = 0;
      58         [ #  # ]:           0 :                         TRY(cert_x509_verify(cert, cred_array, g, &verified));
      59                 :             :                 } else {
      60                 :           2 :                         g->len = 0;
      61         [ -  + ]:           2 :                         TRY(cert_x509_verify(cert, cred_array, pk, &verified));
      62                 :             :                 }
      63                 :           2 :                 break;
      64                 :           0 :         case c5b:
      65                 :             :         case c5c:
      66         [ #  # ]:           0 :                 if (static_dh_auth) {
      67                 :           0 :                         pk->len = 0;
      68         [ #  # ]:           0 :                         TRY(cert_c509_verify(cert, cred_array, g, &verified));
      69                 :             :                 } else {
      70                 :           0 :                         g->len = 0;
      71         [ #  # ]:           0 :                         TRY(cert_c509_verify(cert, cred_array, pk, &verified));
      72                 :             :                 }
      73                 :           0 :                 break;
      74                 :             :                 break;
      75                 :             : 
      76                 :           0 :         default:
      77                 :           0 :                 break;
      78                 :             :         }
      79                 :             : 
      80         [ +  - ]:           2 :         if (verified) {
      81                 :           2 :                 PRINT_MSG("Certificate verification successful!\n");
      82                 :           2 :                 return ok;
      83                 :             :         } else {
      84                 :           0 :                 return certificate_authentication_failed;
      85                 :             :         }
      86                 :             :         return ok;
      87                 :             : }
      88                 :             : 
      89                 :             : /**
      90                 :             :  * @brief                       Finds the credential with a given ID_CRED_x.
      91                 :             :  *
      92                 :             :  * @param[in] cred_array        An array of trust anchors
      93                 :             :  * @param[in] id_cred           The encoded ID_CRED_x to look for.
      94                 :             :  * @param id_cred_len           Length of id_cred.
      95                 :             :  * @return                      The index of the credential or -1.
      96                 :             :  */
      97                 :          15 : static int32_t find_cred(const struct cred_array *cred_array,
      98                 :             :                          const uint8_t *id_cred, uint32_t id_cred_len)
      99                 :             : {
     100         [ +  + ]:          19 :         for (uint32_t i = 0; i < cred_array->len; i++) {
     101         [ +  + ]:          15 :                 if ((cred_array->ptr[i].id_cred.len == id_cred_len) &&
     102         [ +  + ]:          13 :                     (0 == memcmp(cred_array->ptr[i].id_cred.ptr, id_cred,
     103                 :             :                                  id_cred_len))) {
     104                 :          11 :                         return (int32_t)i;
     105                 :             :                 }
     106                 :             :         }
     107                 :           4 :         return -1;
     108                 :             : }
     109                 :             : 
     110                 :             : /**
     111                 :             :  * @brief                       Get the local cred object.
     112                 :             :  * 
     113                 :             :  * @param static_dh_auth        True if static DH is used for authentication.
     114                 :             :  * @param[in] cred_array        An array of trust anchors
     115                 :             :  * @param[in,out] ID_cred       The ID of the actual credential. Replaced by
     116                 :             :  *                              the configured form if it matched in the
     117                 :             :  *                              int form of drafts before RFC 9528.
     118                 :             :  * @param[out] cred             The retrievd credentials        
     119                 :             :  * @param[out] pk               The retrievd signature authentication public key
     120                 :             :  * @param[out] g                The retrievd static DH authentication public key
     121                 :             :  * @return                      Ok or error code
     122                 :             :  */
     123                 :          13 : static enum err get_local_cred(bool static_dh_auth,
     124                 :             :                                struct cred_array *cred_array,
     125                 :             :                                struct byte_array *ID_cred,
     126                 :             :                                struct byte_array *cred, struct byte_array *pk,
     127                 :             :                                struct byte_array *g)
     128                 :             : {
     129                 :          13 :         int32_t i = find_cred(cred_array, ID_cred->ptr, ID_cred->len);
     130                 :             : 
     131                 :             :         /* A kid conveyed as a CBOR int is reconstructed as the byte string
     132                 :             :          * holding its encoding, e.g., 0x32 as { 4 : h'32' } = a1 04 41 32
     133                 :             :          * (RFC 9528 Section 3.3.2). Credentials configured with the int form
     134                 :             :          * of drafts before RFC 9528, { 4 : -19 } = a1 04 32, still match; the
     135                 :             :          * peer then computed its MAC over that form, so continue with it. */
     136   [ +  +  +  -  :          13 :         if (i < 0 && ID_cred->len == 4 && ID_cred->ptr[0] == 0xa1 &&
                   +  - ]
     137   [ +  -  +  - ]:           3 :             ID_cred->ptr[1] == 0x04 && ID_cred->ptr[2] == 0x41) {
     138                 :           3 :                 const struct byte_array kid_bstr = { .ptr = &ID_cred->ptr[3],
     139                 :             :                                                      .len = 1 };
     140                 :           3 :                 const uint8_t legacy[] = { 0xa1, 0x04, ID_cred->ptr[3] };
     141                 :             : 
     142         [ +  + ]:           3 :                 if (c_x_is_encoded_int(&kid_bstr)) {
     143                 :           2 :                         i = find_cred(cred_array, legacy, sizeof(legacy));
     144         [ +  + ]:           2 :                         if (i >= 0) {
     145         [ -  + ]:           1 :                                 TRY(_memcpy_s(ID_cred->ptr, ID_cred->len,
     146                 :             :                                               legacy, sizeof(legacy)));
     147                 :           1 :                                 ID_cred->len = sizeof(legacy);
     148                 :             :                         }
     149                 :             :                 }
     150                 :             :         }
     151                 :             : 
     152         [ +  + ]:          13 :         if (i < 0) {
     153                 :           2 :                 return credential_not_found;
     154                 :             :         }
     155                 :             : 
     156                 :             :         /*retrieve CRED_x*/
     157         [ -  + ]:          11 :         TRY(_memcpy_s(cred->ptr, cred->len, cred_array->ptr[i].cred.ptr,
     158                 :             :                       cred_array->ptr[i].cred.len));
     159                 :          11 :         cred->len = cred_array->ptr[i].cred.len;
     160                 :             : 
     161                 :             :         /*retrieve PK*/
     162         [ +  + ]:          11 :         if (static_dh_auth) {
     163                 :           7 :                 pk->len = 0;
     164         [ +  + ]:           7 :                 if (cred_array->ptr[i].g.len ==
     165                 :             :                     P_256_PUB_KEY_UNCOMPRESSED_SIZE) {
     166                 :             :                         /*uncompressed P256 DH pk: keep the x-coordinate, the
     167                 :             :                         form shared_secret_derive() takes*/
     168         [ -  + ]:           1 :                         TRY(_memcpy_s(g->ptr, g->len,
     169                 :             :                                       &cred_array->ptr[i].g.ptr[1],
     170                 :             :                                       P_256_PUB_KEY_X_CORD_SIZE));
     171                 :           1 :                         g->len = P_256_PUB_KEY_X_CORD_SIZE;
     172                 :             : 
     173                 :             :                 } else {
     174         [ -  + ]:           6 :                         TRY(_memcpy_s(g->ptr, g->len, cred_array->ptr[i].g.ptr,
     175                 :             :                                       cred_array->ptr[i].g.len));
     176                 :           6 :                         g->len = cred_array->ptr[i].g.len;
     177                 :             :                 }
     178                 :             : 
     179                 :             :         } else {
     180                 :           4 :                 g->len = 0;
     181         [ -  + ]:           4 :                 TRY(_memcpy_s(pk->ptr, pk->len, cred_array->ptr[i].pk.ptr,
     182                 :             :                               cred_array->ptr[i].pk.len));
     183                 :           4 :                 pk->len = cred_array->ptr[i].pk.len;
     184                 :             :         }
     185                 :          11 :         return ok;
     186                 :             : }
     187                 :             : 
     188                 :          15 : enum err retrieve_cred(bool static_dh_auth, struct cred_array *cred_array,
     189                 :             :                        struct byte_array *id_cred, struct byte_array *cred,
     190                 :             :                        struct byte_array *pk, struct byte_array *g)
     191                 :             : {
     192                 :          15 :         size_t decode_len = 0;
     193                 :          15 :         struct id_cred_x_map map = { 0 };
     194                 :             : 
     195         [ -  + ]:          15 :         TRY_EXPECT(cbor_decode_id_cred_x_map(id_cred->ptr, id_cred->len, &map,
     196                 :             :                                              &decode_len),
     197                 :             :                    0);
     198                 :             :         /*the cred should be locally available on the device if 
     199                 :             :         kid, x5u, x5t, c5u, c5t is used*/
     200   [ +  +  +  - ]:          15 :         if (map.id_cred_x_map_kid_present || map.id_cred_x_map_x5u_present ||
     201   [ +  +  +  - ]:           6 :             map.id_cred_x_map_x5t_present || map.id_cred_x_map_c5u_present ||
     202         [ -  + ]:           2 :             map.id_cred_x_map_c5t_present) {
     203         [ +  + ]:          13 :                 TRY(get_local_cred(static_dh_auth, cred_array, id_cred, cred,
     204                 :             :                                    pk, g));
     205                 :          11 :                 return ok;
     206                 :             :         }
     207                 :             :         /*x5chain*/
     208         [ +  - ]:           2 :         else if (map.id_cred_x_map_x5chain_present) {
     209                 :           2 :                 struct const_byte_array cert = BYTE_ARRAY_INIT(
     210                 :             :                         map.id_cred_x_map_x5chain.id_cred_x_map_x5chain.value,
     211                 :             :                         (uint32_t)map.id_cred_x_map_x5chain
     212                 :             :                                 .id_cred_x_map_x5chain.len);
     213                 :             : 
     214         [ -  + ]:           2 :                 TRY(verify_cert2cred(static_dh_auth, cred_array, x5chain, &cert,
     215                 :             :                                      cred, pk, g));
     216                 :           2 :                 return ok;
     217                 :             :         }
     218                 :             :         /*x5bag*/
     219         [ #  # ]:           0 :         else if (map.id_cred_x_map_x5bag_present) {
     220                 :           0 :                 struct const_byte_array cert = BYTE_ARRAY_INIT(
     221                 :             :                         map.id_cred_x_map_x5bag.id_cred_x_map_x5bag.value,
     222                 :             :                         (uint32_t)map.id_cred_x_map_x5bag.id_cred_x_map_x5bag
     223                 :             :                                 .len);
     224         [ #  # ]:           0 :                 TRY(verify_cert2cred(static_dh_auth, cred_array, x5bag, &cert,
     225                 :             :                                      cred, pk, g));
     226                 :           0 :                 return ok;
     227                 :             :         }
     228                 :             :         /*c5c*/
     229         [ #  # ]:           0 :         else if (map.id_cred_x_map_c5c_present) {
     230                 :           0 :                 struct const_byte_array cert = BYTE_ARRAY_INIT(
     231                 :             :                         map.id_cred_x_map_c5c.id_cred_x_map_c5c.value,
     232                 :             :                         (uint32_t)map.id_cred_x_map_c5c.id_cred_x_map_c5c.len);
     233         [ #  # ]:           0 :                 TRY(verify_cert2cred(static_dh_auth, cred_array, c5c, &cert,
     234                 :             :                                      cred, pk, g));
     235                 :           0 :                 return ok;
     236                 :             :         }
     237                 :             :         /*c5b*/
     238         [ #  # ]:           0 :         else if (map.id_cred_x_map_c5b_present) {
     239                 :           0 :                 struct const_byte_array cert = BYTE_ARRAY_INIT(
     240                 :             :                         map.id_cred_x_map_c5b.id_cred_x_map_c5b.value,
     241                 :             :                         (uint32_t)map.id_cred_x_map_c5b.id_cred_x_map_c5b.len);
     242         [ #  # ]:           0 :                 TRY(verify_cert2cred(static_dh_auth, cred_array, c5b, &cert,
     243                 :             :                                      cred, pk, g));
     244                 :           0 :                 return ok;
     245                 :             :         }
     246                 :             : 
     247                 :           0 :         return credential_not_found;
     248                 :             : }
        

Generated by: LCOV version 2.0-1