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