libcoap 4.3.5-develop-5347a62
Loading...
Searching...
No Matches
coap_asn1.c
Go to the documentation of this file.
1/* coap_asn1.c -- ASN.1 handling functions
2 *
3 * Copyright (C) 2020-2026 Jon Shallow <supjps-libcoap@jpshallow.com>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * This file is part of the CoAP library libcoap. Please see
8 * README for terms of use.
9 */
10
15
17
18size_t
19asn1_len(const uint8_t **ptr, size_t *plen) {
20 size_t len = 0;
21
22 if (*plen == 0)
23 return 0;
24 if ((**ptr) & 0x80) {
25 size_t octets = (**ptr) & 0x7f;
26 (*plen)--;
27 (*ptr)++;
28 while (octets) {
29 if (*plen == 0)
30 return 0;
31 len = (len << 8) + (**ptr);
32 (*plen)--;
33 (*ptr)++;
34 octets--;
35 }
36 } else {
37 if (*plen == 0)
38 return 0;
39 len = (**ptr) & 0x7f;
40 (*plen)--;
41 (*ptr)++;
42 }
43 if (len > *plen)
44 return *plen;
45 return len;
46}
47
49asn1_tag_c(const uint8_t **ptr, size_t *plen, int *constructed, int *cls) {
50 coap_asn1_tag_t tag = 0;
51 uint8_t byte;
52
53 if (*plen == 0) {
54 *constructed = 0;
55 *cls = 0;
56 return COAP_ASN1_FAIL;
57 }
58 byte = (**ptr);
59 *constructed = (byte & 0x20) ? 1 : 0;
60 *cls = byte >> 6;
61 tag = byte & 0x1F;
62 (*plen)--;
63 (*ptr)++;
64 if (tag < 0x1F)
65 return tag;
66
67 /* Tag can be one byte or more based on B8 */
68 if (*plen == 0)
69 return COAP_ASN1_FAIL;
70 byte = (**ptr);
71 while (byte & 0x80) {
72 tag = (tag << 7) + (byte & 0x7F);
73 (*plen)--;
74 (*ptr)++;
75 if (*plen == 0 || (uint32_t)tag > (INT_MAX >> 7))
76 return COAP_ASN1_FAIL;
77 byte = (**ptr);
78 }
79 /* Do the final one */
80 tag = (tag << 7) + (byte & 0x7F);
81 (*plen)--;
82 (*ptr)++;
83 return tag;
84}
85
86static coap_binary_t *
87get_asn1_tag_internal(coap_asn1_tag_t ltag, const uint8_t *ptr, size_t tlen,
88 asn1_validate validate, uint32_t recursive_check) {
89 int constructed;
90 int class;
91 coap_asn1_tag_t tag = asn1_tag_c(&ptr, &tlen, &constructed, &class);
92 size_t len;
93 coap_binary_t *tag_data;
94
95 if (tag == COAP_ASN1_FAIL)
96 return NULL;
97 len = asn1_len(&ptr, &tlen);
98
99 while (tlen > 0 && len <= tlen) {
100 if (class == 2 && constructed == 1) {
101 /* Skip over element description */
102 tag = asn1_tag_c(&ptr, &tlen, &constructed, &class);
103 if (tag == COAP_ASN1_FAIL)
104 return NULL;
105 len = asn1_len(&ptr, &tlen);
106 }
107 if (tag == ltag) {
108 if (!validate || validate(ptr, len)) {
109 tag_data = coap_new_binary(len);
110 if (tag_data == NULL)
111 return NULL;
112 tag_data->length = len;
113 memcpy(tag_data->s, ptr, len);
114 return tag_data;
115 }
116 }
117 if (tag == 0x10 && constructed == 1) {
118 /* SEQUENCE or SEQUENCE OF */
119 if (recursive_check > 100)
120 return NULL;
121 tag_data = get_asn1_tag_internal(ltag, ptr, len, validate, recursive_check + 1);
122 if (tag_data)
123 return tag_data;
124 }
125 /* Skip over non matching tag */
126 ptr += len;
127 tlen -= len;
128 tag = asn1_tag_c(&ptr, &tlen, &constructed, &class);
129 if (tag == COAP_ASN1_FAIL)
130 return NULL;
131 len = asn1_len(&ptr, &tlen);
132 }
133 return NULL;
134}
135
136/* caller must free off returned coap_binary_t* */
138get_asn1_tag(coap_asn1_tag_t ltag, const uint8_t *ptr, size_t tlen,
139 asn1_validate validate) {
140 return get_asn1_tag_internal(ltag, ptr, tlen, validate, 0);
141}
142
143/* first part of Raw public key, this is the start of the Subject Public Key */
144static const unsigned char cert_asn1_header1[] = {
145 0x30, 0x59, /* SEQUENCE, length 89 bytes */
146 0x30, 0x13, /* SEQUENCE, length 19 bytes */
147 0x06, 0x07, /* OBJECT IDENTIFIER ecPublicKey (1 2 840 10045 2 1) */
148 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01,
149};
150/* PrimeX will get inserted */
151#if 0
1520x06, 0x08, /* OBJECT IDENTIFIER prime256v1 (1 2 840 10045 3 1 7) */
153 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07,
154#endif
155static const unsigned char cert_asn1_header2[] = {
156 0x03, 0x42, /* BIT STRING, length 66 bytes */
157 /* Note: 0 bits (0x00) and no compression (0x04) are already in the certificate */
158};
159
161get_asn1_spki(const uint8_t *data, size_t size) {
162 coap_binary_t *pub_key = get_asn1_tag(COAP_ASN1_BITSTRING, data, size, NULL);
164 coap_binary_t *spki = NULL;
165
166 if (pub_key && prime) {
167 size_t header_size = sizeof(cert_asn1_header1) +
168 2 +
169 prime->length +
170 sizeof(cert_asn1_header2);
171 spki = coap_new_binary(header_size + pub_key->length);
172 if (spki) {
173 memcpy(&spki->s[header_size], pub_key->s, pub_key->length);
174 memcpy(spki->s, cert_asn1_header1, sizeof(cert_asn1_header1));
176 spki->s[sizeof(cert_asn1_header1)+1] = (uint8_t)prime->length;
177 memcpy(&spki->s[sizeof(cert_asn1_header1)+2],
178 prime->s, prime->length);
179 memcpy(&spki->s[sizeof(cert_asn1_header1)+2+prime->length],
181 spki->length = header_size + pub_key->length;
182 }
183 }
184 if (pub_key)
185 coap_delete_binary(pub_key);
186 if (prime)
187 coap_delete_binary(prime);
188 return spki;
189}
190
193 int constructed;
194 int class;
195 const uint8_t *acp = asn1->s;
196 coap_asn1_tag_t tag;
197 size_t len;
198 coap_binary_t *sign;
199
200 if (asn1->s[0] != 0x30)
201 return NULL;
202
203 tag = asn1_tag_c(&acp, &size, &constructed, &class);
204 if (tag == COAP_ASN1_FAIL)
205 return NULL;
206 len = asn1_len(&acp, &size);
207 if (len == 0)
208 return NULL;
209
210 tag = asn1_tag_c(&acp, &size, &constructed, &class);
211 if (tag == COAP_ASN1_FAIL)
212 return NULL;
213 len = asn1_len(&acp, &size);
214 if (tag != COAP_ASN1_INTEGER)
215 return NULL;
216 sign = coap_new_binary(size);
217 if (sign == NULL)
218 return NULL;
219 if (len < size/2) {
220 /* pad with leading 0s */
221 memset(&sign->s[0], 0, size/2 - len);
222 memcpy(&sign->s[size/2 - len], acp, len);
223 } else {
224 /* drop leading 0s if needed */
225 memcpy(&sign->s[0], acp + len - size/2, len);
226 }
227
228 acp += len;
229 tag = asn1_tag_c(&acp, &size, &constructed, &class);
230 if (tag == COAP_ASN1_FAIL) {
231 coap_delete_binary(sign);
232 return NULL;
233 }
234 len = asn1_len(&acp, &size);
235 if (tag != COAP_ASN1_INTEGER) {
236 coap_delete_binary(sign);
237 return NULL;
238 }
239 if (len < size/2) {
240 /* pad with leading 0s */
241 memset(&sign->s[size/2], 0, size/2 - len);
242 memcpy(&sign->s[size/2 + size/2 - len], acp, len);
243 } else {
244 /* drop leading 0s if needed */
245 memcpy(&sign->s[size/2], acp + len - size/2, len);
246 }
247 return sign;
248}
249
250static void
251asn1_add_integer(uint8_t **cp, uint8_t *integer, size_t int_len) {
252 size_t i;
253
254 *((*cp)++) = (uint8_t)COAP_ASN1_INTEGER;
255 if (integer[0] & 0x80) {
256 *((*cp)++) = (uint8_t)(int_len + 1);
257 *((*cp)++) = 0x00;
258 i = 0;
259 } else {
260 /* drop leading 0s if needed */
261 for (i = 0; i < int_len - 1; i++) {
262 if (integer[i] != 0)
263 break;
264 }
265 *((*cp)++) = (uint8_t)(int_len - i);
266 }
267 memcpy(*cp, &integer[i], int_len - i);
268 *cp += int_len - i;
269}
270
273 coap_binary_t *sign = coap_new_binary(r_s->length + 8);
274 uint8_t *cp;
275
276 if (sign == NULL)
277 return NULL;
278
279 cp = sign->s;
280 *(cp++) = 0x30; /* SEQUENCE */
281 *(cp++) = 0x00; /* Length - to be filled in later */
282
283 asn1_add_integer(&cp, r_s->s, r_s->length/2);
284
285 asn1_add_integer(&cp, &r_s->s[r_s->length/2], r_s->length/2);
286
287 sign->s[1] = (uint8_t)(cp - sign->s - 2);
288 sign->length = cp - sign->s;
289 return sign;
290}
291
292#if COAP_OSCORE_GROUP_SUPPORT
293static void
294asn1_add_seq_bin_oid(uint8_t **cp, const uint8_t *oid, size_t oid_len) {
295 uint8_t *keep_cp = *cp;
296
297 *((*cp)++) = 0x30; /* SEQUENCE */
298 *((*cp)++) = 0x00; /* Length - to be filled in later */
299
300 *((*cp)++) = COAP_ASN1_IDENTIFIER;
301 *((*cp)++) = oid_len;
302 memcpy(*cp, oid, oid_len);
303 *cp += oid_len;
304 keep_cp[1] = *cp - keep_cp - 2;
305}
306
307static void
308asn1_add_octet_string(uint8_t **cp, const uint8_t *octet, size_t octet_len) {
309 *((*cp)++) = COAP_ASN1_OCTETSTRING;
310 *((*cp)++) = octet_len;
311 memcpy(*cp, octet, octet_len);
312 *cp += octet_len;
313}
314
315
317coap_asn1_pri_key(cose_curve_t curve, coap_bin_const_t *raw) {
319 coap_binary_t *pri_der;
320 uint8_t *cp;
321 uint8_t val0 = 0;
322
323 if (oid == NULL)
324 return NULL;
325
326 pri_der = coap_new_binary(13 + oid->length + raw->length);
327 if (pri_der == NULL)
328 return NULL;
329
330 cp = pri_der->s;
331 *(cp++) = 0x30; /* SEQUENCE */
332 *(cp++) = 0x00; /* Length - to be filled in later */
333
334 asn1_add_integer(&cp, &val0, 1);
335
336 asn1_add_seq_bin_oid(&cp, oid->s, oid->length);
337 *(cp++) = COAP_ASN1_OCTETSTRING;
338 *(cp++) = raw->length + 2;
339 asn1_add_octet_string(&cp, raw->s, raw->length);
340
341 pri_der->s[1] = cp - pri_der->s - 2;
342 assert(pri_der->length >= (size_t)(cp - pri_der->s));
343 pri_der->length = cp - pri_der->s;
344 return pri_der;
345}
346#endif /* COAP_OSCORE_GROUP_SUPPORT */
static const unsigned char cert_asn1_header2[]
Definition coap_asn1.c:155
static coap_binary_t * get_asn1_tag_internal(coap_asn1_tag_t ltag, const uint8_t *ptr, size_t tlen, asn1_validate validate, uint32_t recursive_check)
Definition coap_asn1.c:87
static const unsigned char cert_asn1_header1[]
Definition coap_asn1.c:144
static void asn1_add_integer(uint8_t **cp, uint8_t *integer, size_t int_len)
Definition coap_asn1.c:251
Library specific build wrapper for coap_internal.h.
#define NULL
Definition coap_option.h:30
size_t asn1_len(const uint8_t **ptr, size_t *plen)
Get the asn1 length from the current ptr.
Definition coap_asn1.c:19
coap_binary_t * get_asn1_tag(coap_asn1_tag_t ltag, const uint8_t *ptr, size_t tlen, asn1_validate validate)
Get the asn1 tag and data from the current ptr.
Definition coap_asn1.c:138
coap_binary_t * coap_asn1_split_r_s(coap_binary_t *asn1, size_t size)
Split the ASN.1 signature into the r and s components.
Definition coap_asn1.c:192
coap_asn1_tag_t
coap_binary_t * coap_asn1_r_s_join(coap_binary_t *r_s)
Join the r and s components into ASN1.
Definition coap_asn1.c:272
coap_binary_t * get_asn1_spki(const uint8_t *data, size_t size)
Abstract SPKI public key from the ASN1.
Definition coap_asn1.c:161
int(* asn1_validate)(const uint8_t *data, size_t size)
Callback to validate the asn1 tag and data.
coap_asn1_tag_t asn1_tag_c(const uint8_t **ptr, size_t *plen, int *constructed, int *cls)
Get the asn1 tag from the current ptr.
Definition coap_asn1.c:49
@ COAP_ASN1_OCTETSTRING
@ COAP_ASN1_INTEGER
@ COAP_ASN1_BITSTRING
@ COAP_ASN1_FAIL
@ COAP_ASN1_IDENTIFIER
coap_bin_const_t * cose_get_curve_bin_oid(cose_curve_t curve)
cose_curve_t
Definition oscore_cose.h:62
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition coap_str.c:81
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:114
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67
CoAP binary data definition.
Definition coap_str.h:57
size_t length
length of binary data
Definition coap_str.h:58
uint8_t * s
binary data
Definition coap_str.h:59