1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* include/k5-der.h - Distinguished Encoding Rules (DER) declarations */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright (C) 2023 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All rights reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert * are met:
10*7f2fe78bSCy Schubert *
11*7f2fe78bSCy Schubert * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert *
14*7f2fe78bSCy Schubert * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert * the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert * distribution.
18*7f2fe78bSCy Schubert *
19*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert */
32*7f2fe78bSCy Schubert
33*7f2fe78bSCy Schubert /*
34*7f2fe78bSCy Schubert * Most ASN.1 encoding and decoding is done using the table-driven framework in
35*7f2fe78bSCy Schubert * libkrb5. When that is not an option, these helpers can be used to encode
36*7f2fe78bSCy Schubert * and decode simple types.
37*7f2fe78bSCy Schubert */
38*7f2fe78bSCy Schubert
39*7f2fe78bSCy Schubert #ifndef K5_DER_H
40*7f2fe78bSCy Schubert #define K5_DER_H
41*7f2fe78bSCy Schubert
42*7f2fe78bSCy Schubert #include <stdint.h>
43*7f2fe78bSCy Schubert #include <stdbool.h>
44*7f2fe78bSCy Schubert #include "k5-buf.h"
45*7f2fe78bSCy Schubert #include "k5-input.h"
46*7f2fe78bSCy Schubert
47*7f2fe78bSCy Schubert /* Return the number of bytes needed to encode len as a DER encoding length. */
48*7f2fe78bSCy Schubert static inline size_t
k5_der_len_len(size_t len)49*7f2fe78bSCy Schubert k5_der_len_len(size_t len)
50*7f2fe78bSCy Schubert {
51*7f2fe78bSCy Schubert size_t llen;
52*7f2fe78bSCy Schubert
53*7f2fe78bSCy Schubert if (len < 128)
54*7f2fe78bSCy Schubert return 1;
55*7f2fe78bSCy Schubert llen = 1;
56*7f2fe78bSCy Schubert while (len > 0) {
57*7f2fe78bSCy Schubert len >>= 8;
58*7f2fe78bSCy Schubert llen++;
59*7f2fe78bSCy Schubert }
60*7f2fe78bSCy Schubert return llen;
61*7f2fe78bSCy Schubert }
62*7f2fe78bSCy Schubert
63*7f2fe78bSCy Schubert /* Return the number of bytes needed to encode a DER value (with identifier
64*7f2fe78bSCy Schubert * byte and length) for a given contents length. */
65*7f2fe78bSCy Schubert static inline size_t
k5_der_value_len(size_t contents_len)66*7f2fe78bSCy Schubert k5_der_value_len(size_t contents_len)
67*7f2fe78bSCy Schubert {
68*7f2fe78bSCy Schubert return 1 + k5_der_len_len(contents_len) + contents_len;
69*7f2fe78bSCy Schubert }
70*7f2fe78bSCy Schubert
71*7f2fe78bSCy Schubert /* Add a DER identifier byte (composed by the caller, including the ASN.1
72*7f2fe78bSCy Schubert * class, tag, and constructed bit) and length. */
73*7f2fe78bSCy Schubert static inline void
k5_der_add_taglen(struct k5buf * buf,uint8_t idbyte,size_t len)74*7f2fe78bSCy Schubert k5_der_add_taglen(struct k5buf *buf, uint8_t idbyte, size_t len)
75*7f2fe78bSCy Schubert {
76*7f2fe78bSCy Schubert uint8_t *p;
77*7f2fe78bSCy Schubert size_t llen = k5_der_len_len(len);
78*7f2fe78bSCy Schubert
79*7f2fe78bSCy Schubert p = k5_buf_get_space(buf, 1 + llen);
80*7f2fe78bSCy Schubert if (p == NULL)
81*7f2fe78bSCy Schubert return;
82*7f2fe78bSCy Schubert *p++ = idbyte;
83*7f2fe78bSCy Schubert if (len < 128) {
84*7f2fe78bSCy Schubert *p = len;
85*7f2fe78bSCy Schubert } else {
86*7f2fe78bSCy Schubert *p = 0x80 | (llen - 1);
87*7f2fe78bSCy Schubert /* Encode the length bytes backwards so the most significant byte is
88*7f2fe78bSCy Schubert * first. */
89*7f2fe78bSCy Schubert p += llen;
90*7f2fe78bSCy Schubert while (len > 0) {
91*7f2fe78bSCy Schubert *--p = len & 0xFF;
92*7f2fe78bSCy Schubert len >>= 8;
93*7f2fe78bSCy Schubert }
94*7f2fe78bSCy Schubert }
95*7f2fe78bSCy Schubert }
96*7f2fe78bSCy Schubert
97*7f2fe78bSCy Schubert /* Add a DER value (identifier byte, length, and contents). */
98*7f2fe78bSCy Schubert static inline void
k5_der_add_value(struct k5buf * buf,uint8_t idbyte,const void * contents,size_t len)99*7f2fe78bSCy Schubert k5_der_add_value(struct k5buf *buf, uint8_t idbyte, const void *contents,
100*7f2fe78bSCy Schubert size_t len)
101*7f2fe78bSCy Schubert {
102*7f2fe78bSCy Schubert k5_der_add_taglen(buf, idbyte, len);
103*7f2fe78bSCy Schubert k5_buf_add_len(buf, contents, len);
104*7f2fe78bSCy Schubert }
105*7f2fe78bSCy Schubert
106*7f2fe78bSCy Schubert /*
107*7f2fe78bSCy Schubert * If the next byte in in matches idbyte and the subsequent DER length is
108*7f2fe78bSCy Schubert * valid, advance in past the value, set *contents_out to the value contents,
109*7f2fe78bSCy Schubert * and return true. Otherwise return false. Only set an error on in if the
110*7f2fe78bSCy Schubert * next bytes matches idbyte but the ensuing length is invalid. contents_out
111*7f2fe78bSCy Schubert * may be aliased to in; it will only be written to on successful decoding of a
112*7f2fe78bSCy Schubert * value.
113*7f2fe78bSCy Schubert */
114*7f2fe78bSCy Schubert static inline bool
k5_der_get_value(struct k5input * in,uint8_t idbyte,struct k5input * contents_out)115*7f2fe78bSCy Schubert k5_der_get_value(struct k5input *in, uint8_t idbyte,
116*7f2fe78bSCy Schubert struct k5input *contents_out)
117*7f2fe78bSCy Schubert {
118*7f2fe78bSCy Schubert uint8_t lenbyte, i;
119*7f2fe78bSCy Schubert size_t len;
120*7f2fe78bSCy Schubert const void *bytes;
121*7f2fe78bSCy Schubert
122*7f2fe78bSCy Schubert /* Do nothing if in is empty or the next byte doesn't match idbyte. */
123*7f2fe78bSCy Schubert if (in->status || in->len == 0 || *in->ptr != idbyte)
124*7f2fe78bSCy Schubert return false;
125*7f2fe78bSCy Schubert
126*7f2fe78bSCy Schubert /* Advance past the identifier byte and decode the length. */
127*7f2fe78bSCy Schubert (void)k5_input_get_byte(in);
128*7f2fe78bSCy Schubert lenbyte = k5_input_get_byte(in);
129*7f2fe78bSCy Schubert if (lenbyte < 128) {
130*7f2fe78bSCy Schubert len = lenbyte;
131*7f2fe78bSCy Schubert } else {
132*7f2fe78bSCy Schubert len = 0;
133*7f2fe78bSCy Schubert for (i = 0; i < (lenbyte & 0x7F); i++) {
134*7f2fe78bSCy Schubert if (len > (SIZE_MAX >> 8)) {
135*7f2fe78bSCy Schubert k5_input_set_status(in, EOVERFLOW);
136*7f2fe78bSCy Schubert return false;
137*7f2fe78bSCy Schubert }
138*7f2fe78bSCy Schubert len = (len << 8) | k5_input_get_byte(in);
139*7f2fe78bSCy Schubert }
140*7f2fe78bSCy Schubert }
141*7f2fe78bSCy Schubert
142*7f2fe78bSCy Schubert bytes = k5_input_get_bytes(in, len);
143*7f2fe78bSCy Schubert if (bytes == NULL)
144*7f2fe78bSCy Schubert return false;
145*7f2fe78bSCy Schubert k5_input_init(contents_out, bytes, len);
146*7f2fe78bSCy Schubert return true;
147*7f2fe78bSCy Schubert }
148*7f2fe78bSCy Schubert
149*7f2fe78bSCy Schubert #endif /* K5_DER_H */
150