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_setup.h"
28 #include "ares.h"
29 #include "ares_data.h"
30 #include "ares_private.h"
31
ares_parse_caa_reply(const unsigned char * abuf,int alen_int,struct ares_caa_reply ** caa_out)32 int ares_parse_caa_reply(const unsigned char *abuf, int alen_int,
33 struct ares_caa_reply **caa_out)
34 {
35 ares_status_t status;
36 size_t alen;
37 struct ares_caa_reply *caa_head = NULL;
38 struct ares_caa_reply *caa_last = NULL;
39 struct ares_caa_reply *caa_curr;
40 ares_dns_record_t *dnsrec = NULL;
41 size_t i;
42
43 *caa_out = NULL;
44
45 if (alen_int < 0) {
46 return ARES_EBADRESP;
47 }
48
49 alen = (size_t)alen_int;
50
51 status = ares_dns_parse(abuf, alen, 0, &dnsrec);
52 if (status != ARES_SUCCESS) {
53 goto done;
54 }
55
56 if (ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER) == 0) {
57 status = ARES_ENODATA;
58 goto done;
59 }
60
61 for (i = 0; i < ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); i++) {
62 const unsigned char *ptr;
63 size_t ptr_len;
64 const ares_dns_rr_t *rr =
65 ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i);
66
67 if (rr == NULL) {
68 /* Shouldn't be possible */
69 status = ARES_EBADRESP;
70 goto done;
71 }
72
73 /* XXX: Why do we allow Chaos class? */
74 if (ares_dns_rr_get_class(rr) != ARES_CLASS_IN &&
75 ares_dns_rr_get_class(rr) != ARES_CLASS_CHAOS) {
76 continue;
77 }
78
79 /* Only looking for CAA records */
80 if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_CAA) {
81 continue;
82 }
83
84 /* Allocate storage for this CAA answer appending it to the list */
85 caa_curr = ares_malloc_data(ARES_DATATYPE_CAA_REPLY);
86 if (caa_curr == NULL) {
87 status = ARES_ENOMEM;
88 goto done;
89 }
90
91 /* Link in the record */
92 if (caa_last) {
93 caa_last->next = caa_curr;
94 } else {
95 caa_head = caa_curr;
96 }
97 caa_last = caa_curr;
98
99 caa_curr->critical = ares_dns_rr_get_u8(rr, ARES_RR_CAA_CRITICAL);
100 caa_curr->property =
101 (unsigned char *)ares_strdup(ares_dns_rr_get_str(rr, ARES_RR_CAA_TAG));
102 if (caa_curr->property == NULL) {
103 status = ARES_ENOMEM;
104 break;
105 }
106 /* RFC6844 says this can only be ascii, so not sure why we're recording a
107 * length */
108 caa_curr->plength = ares_strlen((const char *)caa_curr->property);
109
110 ptr = ares_dns_rr_get_bin(rr, ARES_RR_CAA_VALUE, &ptr_len);
111 if (ptr == NULL) {
112 status = ARES_EBADRESP;
113 goto done;
114 }
115
116 /* Wants NULL termination for some reason */
117 caa_curr->value = ares_malloc(ptr_len + 1);
118 if (caa_curr->value == NULL) {
119 status = ARES_ENOMEM;
120 goto done;
121 }
122 memcpy(caa_curr->value, ptr, ptr_len);
123 caa_curr->value[ptr_len] = 0;
124 caa_curr->length = ptr_len;
125 }
126
127 done:
128 /* clean up on error */
129 if (status != ARES_SUCCESS) {
130 if (caa_head) {
131 ares_free_data(caa_head);
132 }
133 } else {
134 /* everything looks fine, return the data */
135 *caa_out = caa_head;
136 }
137 ares_dns_record_destroy(dnsrec);
138 return (int)status;
139 }
140