blob: 2c6a6f769b92c34f9e5809d2d38bd1f3f40f69b3 [file] [log] [blame]
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <asn_internal.h>
6#include <errno.h>
7
8static ssize_t der_write_TL(ber_tlv_tag_t tag, ber_tlv_len_t len,
9 asn_app_consume_bytes_f *cb, void *app_key, int constructed);
10
11/*
12 * The DER encoder of any type.
13 */
14asn_enc_rval_t
15der_encode(const asn_TYPE_descriptor_t *type_descriptor, const void *struct_ptr,
16 asn_app_consume_bytes_f *consume_bytes, void *app_key) {
17 ASN_DEBUG("DER encoder invoked for %s",
18 type_descriptor->name);
19
20 /*
21 * Invoke type-specific encoder.
22 */
23 return type_descriptor->op->der_encoder(
24 type_descriptor, struct_ptr, /* Pointer to the destination structure */
25 0, 0, consume_bytes, app_key);
26}
27
28/*
29 * Argument type and callback necessary for der_encode_to_buffer().
30 */
31typedef struct enc_to_buf_arg {
32 void *buffer;
33 size_t left;
34} enc_to_buf_arg;
35static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
36 enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
37
38 if(arg->left < size)
39 return -1; /* Data exceeds the available buffer size */
40
41 memcpy(arg->buffer, buffer, size);
42 arg->buffer = ((char *)arg->buffer) + size;
43 arg->left -= size;
44
45 return 0;
46}
47
48/*
49 * A variant of the der_encode() which encodes the data into the provided buffer
50 */
51asn_enc_rval_t
52der_encode_to_buffer(const asn_TYPE_descriptor_t *type_descriptor,
53 const void *struct_ptr, void *buffer, size_t buffer_size) {
54 enc_to_buf_arg arg;
55 asn_enc_rval_t ec;
56
57 arg.buffer = buffer;
58 arg.left = buffer_size;
59
60 ec = type_descriptor->op->der_encoder(type_descriptor,
61 struct_ptr, /* Pointer to the destination structure */
62 0, 0, encode_to_buffer_cb, &arg);
63 if(ec.encoded != -1) {
64 assert(ec.encoded == (ssize_t)(buffer_size - arg.left));
65 /* Return the encoded contents size */
66 }
67 return ec;
68}
69
70
71/*
72 * Write out leading TL[v] sequence according to the type definition.
73 */
74ssize_t
75der_write_tags(const asn_TYPE_descriptor_t *sd, size_t struct_length,
76 int tag_mode, int last_tag_form,
77 ber_tlv_tag_t tag, /* EXPLICIT or IMPLICIT tag */
78 asn_app_consume_bytes_f *cb, void *app_key) {
79#define ASN1_DER_MAX_TAGS_COUNT 4
80 ber_tlv_tag_t
81 tags_buf_scratch[ASN1_DER_MAX_TAGS_COUNT * sizeof(ber_tlv_tag_t)];
82 ssize_t lens[ASN1_DER_MAX_TAGS_COUNT * sizeof(ssize_t)];
83 const ber_tlv_tag_t *tags; /* Copy of tags stream */
84 int tags_count; /* Number of tags */
85 size_t overall_length;
86 int i;
87
88 ASN_DEBUG("Writing tags (%s, tm=%d, tc=%d, tag=%s, mtc=%d)",
89 sd->name, tag_mode, sd->tags_count,
90 ber_tlv_tag_string(tag),
91 tag_mode
92 ?(sd->tags_count+1
93 -((tag_mode == -1) && sd->tags_count))
94 :sd->tags_count
95 );
96
97 if(sd->tags_count + 1 > ASN1_DER_MAX_TAGS_COUNT) {
98 ASN_DEBUG("System limit %d on tags count", ASN1_DER_MAX_TAGS_COUNT);
99 return -1;
100 }
101
102 if(tag_mode) {
103 /*
104 * Instead of doing shaman dance like we do in ber_check_tags(),
105 * allocate a small array on the stack
106 * and initialize it appropriately.
107 */
108 int stag_offset;
109 ber_tlv_tag_t *tags_buf = tags_buf_scratch;
110 tags_count = sd->tags_count
111 + 1 /* EXPLICIT or IMPLICIT tag is given */
112 - ((tag_mode == -1) && sd->tags_count);
113 /* Copy tags over */
114 tags_buf[0] = tag;
115 stag_offset = -1 + ((tag_mode == -1) && sd->tags_count);
116 for(i = 1; i < tags_count; i++)
117 tags_buf[i] = sd->tags[i + stag_offset];
118 tags = tags_buf;
119 } else {
120 tags = sd->tags;
121 tags_count = sd->tags_count;
122 }
123
124 /* No tags to write */
125 if(tags_count == 0)
126 return 0;
127
128 /*
129 * Array of tags is initialized.
130 * Now, compute the size of the TLV pairs, from right to left.
131 */
132 overall_length = struct_length;
133 for(i = tags_count - 1; i >= 0; --i) {
134 lens[i] = der_write_TL(tags[i], overall_length, 0, 0, 0);
135 if(lens[i] == -1) return -1;
136 overall_length += lens[i];
137 lens[i] = overall_length - lens[i];
138 }
139
140 if(!cb) return overall_length - struct_length;
141
142 ASN_DEBUG("Encoding %s TL sequence (%d elements)", sd->name,
143 tags_count);
144
145 /*
146 * Encode the TL sequence for real.
147 */
148 for(i = 0; i < tags_count; i++) {
149 ssize_t len;
150 int _constr;
151
152 /* Check if this tag happens to be constructed */
153 _constr = (last_tag_form || i < (tags_count - 1));
154
155 len = der_write_TL(tags[i], lens[i], cb, app_key, _constr);
156 if(len == -1) return -1;
157 }
158
159 return overall_length - struct_length;
160}
161
162static ssize_t
163der_write_TL(ber_tlv_tag_t tag, ber_tlv_len_t len,
164 asn_app_consume_bytes_f *cb, void *app_key,
165 int constructed) {
166 uint8_t buf[32];
167 size_t size = 0;
168 int buf_size = cb?sizeof(buf):0;
169 ssize_t tmp;
170
171 /* Serialize tag (T from TLV) into possibly zero-length buffer */
172 tmp = ber_tlv_tag_serialize(tag, buf, buf_size);
173 if(tmp == -1 || tmp > (ssize_t)sizeof(buf)) return -1;
174 size += tmp;
175
176 /* Serialize length (L from TLV) into possibly zero-length buffer */
177 tmp = der_tlv_length_serialize(len, buf+size, buf_size?buf_size-size:0);
178 if(tmp == -1) return -1;
179 size += tmp;
180
181 if(size > sizeof(buf))
182 return -1;
183
184 /*
185 * If callback is specified, invoke it, and check its return value.
186 */
187 if(cb) {
188 if(constructed) *buf |= 0x20;
189 if(cb(buf, size, app_key) < 0)
190 return -1;
191 }
192
193 return size;
194}