quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

ares_parse_caa_reply.c (4512B)


      1 /* MIT License
      2  *
      3  * Copyright (c) 2023 Brad House
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a copy
      6  * of this software and associated documentation files (the "Software"), to deal
      7  * in the Software without restriction, including without limitation the rights
      8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      9  * copies of the Software, and to permit persons to whom the Software is
     10  * furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice (including the next
     13  * paragraph) shall be included in all copies or substantial portions of the
     14  * Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     22  * SOFTWARE.
     23  *
     24  * SPDX-License-Identifier: MIT
     25  */
     26 
     27 #include "ares_private.h"
     28 #include "ares_data.h"
     29 
     30 int ares_parse_caa_reply(const unsigned char *abuf, int alen_int,
     31                          struct ares_caa_reply **caa_out)
     32 {
     33   ares_status_t          status;
     34   size_t                 alen;
     35   struct ares_caa_reply *caa_head = NULL;
     36   struct ares_caa_reply *caa_last = NULL;
     37   struct ares_caa_reply *caa_curr;
     38   ares_dns_record_t     *dnsrec = NULL;
     39   size_t                 i;
     40 
     41   *caa_out = NULL;
     42 
     43   if (alen_int < 0) {
     44     return ARES_EBADRESP;
     45   }
     46 
     47   alen = (size_t)alen_int;
     48 
     49   status = ares_dns_parse(abuf, alen, 0, &dnsrec);
     50   if (status != ARES_SUCCESS) {
     51     goto done;
     52   }
     53 
     54   if (ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER) == 0) {
     55     status = ARES_ENODATA;
     56     goto done;
     57   }
     58 
     59   for (i = 0; i < ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); i++) {
     60     const unsigned char *ptr;
     61     size_t               ptr_len;
     62     const ares_dns_rr_t *rr =
     63       ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i);
     64 
     65     if (rr == NULL) {
     66       /* Shouldn't be possible */
     67       status = ARES_EBADRESP; /* LCOV_EXCL_LINE: DefensiveCoding */
     68       goto done;              /* LCOV_EXCL_LINE: DefensiveCoding */
     69     }
     70 
     71     /* XXX: Why do we allow Chaos class? */
     72     if (ares_dns_rr_get_class(rr) != ARES_CLASS_IN &&
     73         ares_dns_rr_get_class(rr) != ARES_CLASS_CHAOS) {
     74       continue;
     75     }
     76 
     77     /* Only looking for CAA records */
     78     if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_CAA) {
     79       continue;
     80     }
     81 
     82     /* Allocate storage for this CAA answer appending it to the list */
     83     caa_curr = ares_malloc_data(ARES_DATATYPE_CAA_REPLY);
     84     if (caa_curr == NULL) {
     85       status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
     86       goto done;            /* LCOV_EXCL_LINE: OutOfMemory */
     87     }
     88 
     89     /* Link in the record */
     90     if (caa_last) {
     91       caa_last->next = caa_curr;
     92     } else {
     93       caa_head = caa_curr;
     94     }
     95     caa_last = caa_curr;
     96 
     97     caa_curr->critical = ares_dns_rr_get_u8(rr, ARES_RR_CAA_CRITICAL);
     98     caa_curr->property =
     99       (unsigned char *)ares_strdup(ares_dns_rr_get_str(rr, ARES_RR_CAA_TAG));
    100     if (caa_curr->property == NULL) {
    101       status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
    102       break;                /* LCOV_EXCL_LINE: OutOfMemory */
    103     }
    104     /* RFC6844 says this can only be ascii, so not sure why we're recording a
    105      * length */
    106     caa_curr->plength = ares_strlen((const char *)caa_curr->property);
    107 
    108     ptr = ares_dns_rr_get_bin(rr, ARES_RR_CAA_VALUE, &ptr_len);
    109     if (ptr == NULL) {
    110       status = ARES_EBADRESP; /* LCOV_EXCL_LINE: DefensiveCoding */
    111       goto done;              /* LCOV_EXCL_LINE: DefensiveCoding */
    112     }
    113 
    114     /* Wants NULL termination for some reason */
    115     caa_curr->value = ares_malloc(ptr_len + 1);
    116     if (caa_curr->value == NULL) {
    117       status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
    118       goto done;            /* LCOV_EXCL_LINE: OutOfMemory */
    119     }
    120     memcpy(caa_curr->value, ptr, ptr_len);
    121     caa_curr->value[ptr_len] = 0;
    122     caa_curr->length         = ptr_len;
    123   }
    124 
    125 done:
    126   /* clean up on error */
    127   if (status != ARES_SUCCESS) {
    128     if (caa_head) {
    129       ares_free_data(caa_head);
    130     }
    131   } else {
    132     /* everything looks fine, return the data */
    133     *caa_out = caa_head;
    134   }
    135   ares_dns_record_destroy(dnsrec);
    136   return (int)status;
    137 }