[599] | 1 | /*
|
---|
| 2 | Unix SMB/CIFS implementation.
|
---|
| 3 | simple ASN1 routines
|
---|
| 4 | Copyright (C) Andrew Tridgell 2001
|
---|
| 5 |
|
---|
| 6 | This program is free software; you can redistribute it and/or modify
|
---|
| 7 | it under the terms of the GNU General Public License as published by
|
---|
| 8 | the Free Software Foundation; either version 3 of the License, or
|
---|
| 9 | (at your option) any later version.
|
---|
| 10 |
|
---|
| 11 | This program is distributed in the hope that it will be useful,
|
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | GNU General Public License for more details.
|
---|
| 15 |
|
---|
| 16 | You should have received a copy of the GNU General Public License
|
---|
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | #include "includes.h"
|
---|
| 21 | #include "../lib/util/asn1.h"
|
---|
| 22 |
|
---|
| 23 | /* allocate an asn1 structure */
|
---|
| 24 | struct asn1_data *asn1_init(TALLOC_CTX *mem_ctx)
|
---|
| 25 | {
|
---|
| 26 | struct asn1_data *ret = talloc_zero(mem_ctx, struct asn1_data);
|
---|
| 27 | if (ret == NULL) {
|
---|
| 28 | DEBUG(0,("asn1_init failed! out of memory\n"));
|
---|
| 29 | }
|
---|
| 30 | return ret;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | /* free an asn1 structure */
|
---|
| 34 | void asn1_free(struct asn1_data *data)
|
---|
| 35 | {
|
---|
| 36 | talloc_free(data);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | /* write to the ASN1 buffer, advancing the buffer pointer */
|
---|
| 40 | bool asn1_write(struct asn1_data *data, const void *p, int len)
|
---|
| 41 | {
|
---|
| 42 | if (data->has_error) return false;
|
---|
| 43 | if (data->length < data->ofs+len) {
|
---|
| 44 | uint8_t *newp;
|
---|
| 45 | newp = talloc_realloc(data, data->data, uint8_t, data->ofs+len);
|
---|
| 46 | if (!newp) {
|
---|
| 47 | asn1_free(data);
|
---|
| 48 | data->has_error = true;
|
---|
| 49 | return false;
|
---|
| 50 | }
|
---|
| 51 | data->data = newp;
|
---|
| 52 | data->length = data->ofs+len;
|
---|
| 53 | }
|
---|
| 54 | memcpy(data->data + data->ofs, p, len);
|
---|
| 55 | data->ofs += len;
|
---|
| 56 | return true;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /* useful fn for writing a uint8_t */
|
---|
| 60 | bool asn1_write_uint8(struct asn1_data *data, uint8_t v)
|
---|
| 61 | {
|
---|
| 62 | return asn1_write(data, &v, 1);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /* push a tag onto the asn1 data buffer. Used for nested structures */
|
---|
| 66 | bool asn1_push_tag(struct asn1_data *data, uint8_t tag)
|
---|
| 67 | {
|
---|
| 68 | struct nesting *nesting;
|
---|
| 69 |
|
---|
| 70 | asn1_write_uint8(data, tag);
|
---|
| 71 | nesting = talloc(data, struct nesting);
|
---|
| 72 | if (!nesting) {
|
---|
| 73 | data->has_error = true;
|
---|
| 74 | return false;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | nesting->start = data->ofs;
|
---|
| 78 | nesting->next = data->nesting;
|
---|
| 79 | data->nesting = nesting;
|
---|
| 80 | return asn1_write_uint8(data, 0xff);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /* pop a tag */
|
---|
| 84 | bool asn1_pop_tag(struct asn1_data *data)
|
---|
| 85 | {
|
---|
| 86 | struct nesting *nesting;
|
---|
| 87 | size_t len;
|
---|
| 88 |
|
---|
| 89 | nesting = data->nesting;
|
---|
| 90 |
|
---|
| 91 | if (!nesting) {
|
---|
| 92 | data->has_error = true;
|
---|
| 93 | return false;
|
---|
| 94 | }
|
---|
| 95 | len = data->ofs - (nesting->start+1);
|
---|
| 96 | /* yes, this is ugly. We don't know in advance how many bytes the length
|
---|
| 97 | of a tag will take, so we assumed 1 byte. If we were wrong then we
|
---|
| 98 | need to correct our mistake */
|
---|
| 99 | if (len > 0xFFFFFF) {
|
---|
| 100 | data->data[nesting->start] = 0x84;
|
---|
| 101 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 102 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 103 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 104 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 105 | memmove(data->data+nesting->start+5, data->data+nesting->start+1, len);
|
---|
| 106 | data->data[nesting->start+1] = (len>>24) & 0xFF;
|
---|
| 107 | data->data[nesting->start+2] = (len>>16) & 0xFF;
|
---|
| 108 | data->data[nesting->start+3] = (len>>8) & 0xFF;
|
---|
| 109 | data->data[nesting->start+4] = len&0xff;
|
---|
| 110 | } else if (len > 0xFFFF) {
|
---|
| 111 | data->data[nesting->start] = 0x83;
|
---|
| 112 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 113 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 114 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 115 | memmove(data->data+nesting->start+4, data->data+nesting->start+1, len);
|
---|
| 116 | data->data[nesting->start+1] = (len>>16) & 0xFF;
|
---|
| 117 | data->data[nesting->start+2] = (len>>8) & 0xFF;
|
---|
| 118 | data->data[nesting->start+3] = len&0xff;
|
---|
| 119 | } else if (len > 255) {
|
---|
| 120 | data->data[nesting->start] = 0x82;
|
---|
| 121 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 122 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 123 | memmove(data->data+nesting->start+3, data->data+nesting->start+1, len);
|
---|
| 124 | data->data[nesting->start+1] = len>>8;
|
---|
| 125 | data->data[nesting->start+2] = len&0xff;
|
---|
| 126 | } else if (len > 127) {
|
---|
| 127 | data->data[nesting->start] = 0x81;
|
---|
| 128 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 129 | memmove(data->data+nesting->start+2, data->data+nesting->start+1, len);
|
---|
| 130 | data->data[nesting->start+1] = len;
|
---|
| 131 | } else {
|
---|
| 132 | data->data[nesting->start] = len;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | data->nesting = nesting->next;
|
---|
| 136 | talloc_free(nesting);
|
---|
| 137 | return true;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /* "i" is the one's complement representation, as is the normal result of an
|
---|
| 141 | * implicit signed->unsigned conversion */
|
---|
| 142 |
|
---|
| 143 | static bool push_int_bigendian(struct asn1_data *data, unsigned int i, bool negative)
|
---|
| 144 | {
|
---|
| 145 | uint8_t lowest = i & 0xFF;
|
---|
| 146 |
|
---|
| 147 | i = i >> 8;
|
---|
| 148 | if (i != 0)
|
---|
| 149 | if (!push_int_bigendian(data, i, negative))
|
---|
| 150 | return false;
|
---|
| 151 |
|
---|
| 152 | if (data->nesting->start+1 == data->ofs) {
|
---|
| 153 |
|
---|
| 154 | /* We did not write anything yet, looking at the highest
|
---|
| 155 | * valued byte */
|
---|
| 156 |
|
---|
| 157 | if (negative) {
|
---|
| 158 | /* Don't write leading 0xff's */
|
---|
| 159 | if (lowest == 0xFF)
|
---|
| 160 | return true;
|
---|
| 161 |
|
---|
| 162 | if ((lowest & 0x80) == 0) {
|
---|
| 163 | /* The only exception for a leading 0xff is if
|
---|
| 164 | * the highest bit is 0, which would indicate
|
---|
| 165 | * a positive value */
|
---|
| 166 | if (!asn1_write_uint8(data, 0xff))
|
---|
| 167 | return false;
|
---|
| 168 | }
|
---|
| 169 | } else {
|
---|
| 170 | if (lowest & 0x80) {
|
---|
| 171 | /* The highest bit of a positive integer is 1,
|
---|
| 172 | * this would indicate a negative number. Push
|
---|
| 173 | * a 0 to indicate a positive one */
|
---|
| 174 | if (!asn1_write_uint8(data, 0))
|
---|
| 175 | return false;
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | return asn1_write_uint8(data, lowest);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /* write an Integer without the tag framing. Needed for example for the LDAP
|
---|
| 184 | * Abandon Operation */
|
---|
| 185 |
|
---|
| 186 | bool asn1_write_implicit_Integer(struct asn1_data *data, int i)
|
---|
| 187 | {
|
---|
| 188 | if (i == -1) {
|
---|
| 189 | /* -1 is special as it consists of all-0xff bytes. In
|
---|
| 190 | push_int_bigendian this is the only case that is not
|
---|
| 191 | properly handled, as all 0xff bytes would be handled as
|
---|
| 192 | leading ones to be ignored. */
|
---|
| 193 | return asn1_write_uint8(data, 0xff);
|
---|
| 194 | } else {
|
---|
| 195 | return push_int_bigendian(data, i, i<0);
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 | /* write an integer */
|
---|
| 201 | bool asn1_write_Integer(struct asn1_data *data, int i)
|
---|
| 202 | {
|
---|
| 203 | if (!asn1_push_tag(data, ASN1_INTEGER)) return false;
|
---|
| 204 | if (!asn1_write_implicit_Integer(data, i)) return false;
|
---|
| 205 | return asn1_pop_tag(data);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /* write a BIT STRING */
|
---|
| 209 | bool asn1_write_BitString(struct asn1_data *data, const void *p, size_t length, uint8_t padding)
|
---|
| 210 | {
|
---|
| 211 | if (!asn1_push_tag(data, ASN1_BIT_STRING)) return false;
|
---|
| 212 | if (!asn1_write_uint8(data, padding)) return false;
|
---|
| 213 | if (!asn1_write(data, p, length)) return false;
|
---|
| 214 | return asn1_pop_tag(data);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[745] | 217 | bool ber_write_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *OID)
|
---|
[599] | 218 | {
|
---|
[745] | 219 | unsigned int v, v2;
|
---|
[599] | 220 | const char *p = (const char *)OID;
|
---|
| 221 | char *newp;
|
---|
| 222 | int i;
|
---|
| 223 |
|
---|
[745] | 224 | if (!isdigit(*p)) return false;
|
---|
[599] | 225 | v = strtoul(p, &newp, 10);
|
---|
| 226 | if (newp[0] != '.') return false;
|
---|
| 227 | p = newp + 1;
|
---|
| 228 |
|
---|
[745] | 229 | if (!isdigit(*p)) return false;
|
---|
[599] | 230 | v2 = strtoul(p, &newp, 10);
|
---|
| 231 | if (newp[0] != '.') return false;
|
---|
| 232 | p = newp + 1;
|
---|
| 233 |
|
---|
| 234 | /*the ber representation can't use more space then the string one */
|
---|
[745] | 235 | *blob = data_blob_talloc(mem_ctx, NULL, strlen(OID));
|
---|
[599] | 236 | if (!blob->data) return false;
|
---|
| 237 |
|
---|
| 238 | blob->data[0] = 40*v + v2;
|
---|
| 239 |
|
---|
| 240 | i = 1;
|
---|
| 241 | while (*p) {
|
---|
[745] | 242 | if (!isdigit(*p)) return false;
|
---|
[599] | 243 | v = strtoul(p, &newp, 10);
|
---|
| 244 | if (newp[0] == '.') {
|
---|
| 245 | p = newp + 1;
|
---|
[745] | 246 | /* check for empty last component */
|
---|
| 247 | if (!*p) return false;
|
---|
[599] | 248 | } else if (newp[0] == '\0') {
|
---|
| 249 | p = newp;
|
---|
| 250 | } else {
|
---|
| 251 | data_blob_free(blob);
|
---|
| 252 | return false;
|
---|
| 253 | }
|
---|
| 254 | if (v >= (1<<28)) blob->data[i++] = (0x80 | ((v>>28)&0x7f));
|
---|
| 255 | if (v >= (1<<21)) blob->data[i++] = (0x80 | ((v>>21)&0x7f));
|
---|
| 256 | if (v >= (1<<14)) blob->data[i++] = (0x80 | ((v>>14)&0x7f));
|
---|
| 257 | if (v >= (1<<7)) blob->data[i++] = (0x80 | ((v>>7)&0x7f));
|
---|
| 258 | blob->data[i++] = (v&0x7f);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | blob->length = i;
|
---|
| 262 |
|
---|
| 263 | return true;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[745] | 266 | /**
|
---|
| 267 | * Serialize partial OID string.
|
---|
| 268 | * Partial OIDs are in the form:
|
---|
| 269 | * 1:2.5.6:0x81
|
---|
| 270 | * 1:2.5.6:0x8182
|
---|
| 271 | */
|
---|
| 272 | bool ber_write_partial_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *partial_oid)
|
---|
| 273 | {
|
---|
| 274 | TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
|
---|
| 275 | char *oid = talloc_strdup(tmp_ctx, partial_oid);
|
---|
| 276 | char *p;
|
---|
| 277 |
|
---|
| 278 | /* truncate partial part so ber_write_OID_String() works */
|
---|
| 279 | p = strchr(oid, ':');
|
---|
| 280 | if (p) {
|
---|
| 281 | *p = '\0';
|
---|
| 282 | p++;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | if (!ber_write_OID_String(mem_ctx, blob, oid)) {
|
---|
| 286 | talloc_free(tmp_ctx);
|
---|
| 287 | return false;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /* Add partially encoded sub-identifier */
|
---|
| 291 | if (p) {
|
---|
| 292 | DATA_BLOB tmp_blob = strhex_to_data_blob(tmp_ctx, p);
|
---|
| 293 | if (!data_blob_append(mem_ctx, blob, tmp_blob.data,
|
---|
| 294 | tmp_blob.length)) {
|
---|
| 295 | talloc_free(tmp_ctx);
|
---|
| 296 | return false;
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | talloc_free(tmp_ctx);
|
---|
| 301 |
|
---|
| 302 | return true;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[599] | 305 | /* write an object ID to a ASN1 buffer */
|
---|
| 306 | bool asn1_write_OID(struct asn1_data *data, const char *OID)
|
---|
| 307 | {
|
---|
| 308 | DATA_BLOB blob;
|
---|
| 309 |
|
---|
| 310 | if (!asn1_push_tag(data, ASN1_OID)) return false;
|
---|
| 311 |
|
---|
[745] | 312 | if (!ber_write_OID_String(NULL, &blob, OID)) {
|
---|
[599] | 313 | data->has_error = true;
|
---|
| 314 | return false;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | if (!asn1_write(data, blob.data, blob.length)) {
|
---|
| 318 | data_blob_free(&blob);
|
---|
| 319 | data->has_error = true;
|
---|
| 320 | return false;
|
---|
| 321 | }
|
---|
| 322 | data_blob_free(&blob);
|
---|
| 323 | return asn1_pop_tag(data);
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | /* write an octet string */
|
---|
| 327 | bool asn1_write_OctetString(struct asn1_data *data, const void *p, size_t length)
|
---|
| 328 | {
|
---|
| 329 | asn1_push_tag(data, ASN1_OCTET_STRING);
|
---|
| 330 | asn1_write(data, p, length);
|
---|
| 331 | asn1_pop_tag(data);
|
---|
| 332 | return !data->has_error;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | /* write a LDAP string */
|
---|
| 336 | bool asn1_write_LDAPString(struct asn1_data *data, const char *s)
|
---|
| 337 | {
|
---|
| 338 | asn1_write(data, s, strlen(s));
|
---|
| 339 | return !data->has_error;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | /* write a LDAP string from a DATA_BLOB */
|
---|
| 343 | bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data *data, const DATA_BLOB *s)
|
---|
| 344 | {
|
---|
| 345 | asn1_write(data, s->data, s->length);
|
---|
| 346 | return !data->has_error;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | /* write a general string */
|
---|
| 350 | bool asn1_write_GeneralString(struct asn1_data *data, const char *s)
|
---|
| 351 | {
|
---|
| 352 | asn1_push_tag(data, ASN1_GENERAL_STRING);
|
---|
| 353 | asn1_write_LDAPString(data, s);
|
---|
| 354 | asn1_pop_tag(data);
|
---|
| 355 | return !data->has_error;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | bool asn1_write_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
|
---|
| 359 | {
|
---|
| 360 | asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(num));
|
---|
| 361 | asn1_write(data, blob->data, blob->length);
|
---|
| 362 | asn1_pop_tag(data);
|
---|
| 363 | return !data->has_error;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | /* write a BOOLEAN */
|
---|
| 367 | bool asn1_write_BOOLEAN(struct asn1_data *data, bool v)
|
---|
| 368 | {
|
---|
| 369 | asn1_push_tag(data, ASN1_BOOLEAN);
|
---|
| 370 | asn1_write_uint8(data, v ? 0xFF : 0);
|
---|
| 371 | asn1_pop_tag(data);
|
---|
| 372 | return !data->has_error;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | bool asn1_read_BOOLEAN(struct asn1_data *data, bool *v)
|
---|
| 376 | {
|
---|
| 377 | uint8_t tmp = 0;
|
---|
| 378 | asn1_start_tag(data, ASN1_BOOLEAN);
|
---|
| 379 | asn1_read_uint8(data, &tmp);
|
---|
| 380 | if (tmp == 0xFF) {
|
---|
| 381 | *v = true;
|
---|
| 382 | } else {
|
---|
| 383 | *v = false;
|
---|
| 384 | }
|
---|
| 385 | asn1_end_tag(data);
|
---|
| 386 | return !data->has_error;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | /* write a BOOLEAN in a simple context */
|
---|
| 390 | bool asn1_write_BOOLEAN_context(struct asn1_data *data, bool v, int context)
|
---|
| 391 | {
|
---|
| 392 | asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(context));
|
---|
| 393 | asn1_write_uint8(data, v ? 0xFF : 0);
|
---|
| 394 | asn1_pop_tag(data);
|
---|
| 395 | return !data->has_error;
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | bool asn1_read_BOOLEAN_context(struct asn1_data *data, bool *v, int context)
|
---|
| 399 | {
|
---|
| 400 | uint8_t tmp = 0;
|
---|
| 401 | asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(context));
|
---|
| 402 | asn1_read_uint8(data, &tmp);
|
---|
| 403 | if (tmp == 0xFF) {
|
---|
| 404 | *v = true;
|
---|
| 405 | } else {
|
---|
| 406 | *v = false;
|
---|
| 407 | }
|
---|
| 408 | asn1_end_tag(data);
|
---|
| 409 | return !data->has_error;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | /* check a BOOLEAN */
|
---|
| 413 | bool asn1_check_BOOLEAN(struct asn1_data *data, bool v)
|
---|
| 414 | {
|
---|
| 415 | uint8_t b = 0;
|
---|
| 416 |
|
---|
| 417 | asn1_read_uint8(data, &b);
|
---|
| 418 | if (b != ASN1_BOOLEAN) {
|
---|
| 419 | data->has_error = true;
|
---|
| 420 | return false;
|
---|
| 421 | }
|
---|
| 422 | asn1_read_uint8(data, &b);
|
---|
| 423 | if (b != v) {
|
---|
| 424 | data->has_error = true;
|
---|
| 425 | return false;
|
---|
| 426 | }
|
---|
| 427 | return !data->has_error;
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 |
|
---|
| 431 | /* load a struct asn1_data structure with a lump of data, ready to be parsed */
|
---|
| 432 | bool asn1_load(struct asn1_data *data, DATA_BLOB blob)
|
---|
| 433 | {
|
---|
| 434 | ZERO_STRUCTP(data);
|
---|
| 435 | data->data = (uint8_t *)talloc_memdup(data, blob.data, blob.length);
|
---|
| 436 | if (!data->data) {
|
---|
| 437 | data->has_error = true;
|
---|
| 438 | return false;
|
---|
| 439 | }
|
---|
| 440 | data->length = blob.length;
|
---|
| 441 | return true;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | /* Peek into an ASN1 buffer, not advancing the pointer */
|
---|
| 445 | bool asn1_peek(struct asn1_data *data, void *p, int len)
|
---|
| 446 | {
|
---|
| 447 | if (data->has_error)
|
---|
| 448 | return false;
|
---|
| 449 |
|
---|
| 450 | if (len < 0 || data->ofs + len < data->ofs || data->ofs + len < len)
|
---|
| 451 | return false;
|
---|
| 452 |
|
---|
| 453 | if (data->ofs + len > data->length) {
|
---|
| 454 | /* we need to mark the buffer as consumed, so the caller knows
|
---|
| 455 | this was an out of data error, and not a decode error */
|
---|
| 456 | data->ofs = data->length;
|
---|
| 457 | return false;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | memcpy(p, data->data + data->ofs, len);
|
---|
| 461 | return true;
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | /* read from a ASN1 buffer, advancing the buffer pointer */
|
---|
| 465 | bool asn1_read(struct asn1_data *data, void *p, int len)
|
---|
| 466 | {
|
---|
| 467 | if (!asn1_peek(data, p, len)) {
|
---|
| 468 | data->has_error = true;
|
---|
| 469 | return false;
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | data->ofs += len;
|
---|
| 473 | return true;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | /* read a uint8_t from a ASN1 buffer */
|
---|
| 477 | bool asn1_read_uint8(struct asn1_data *data, uint8_t *v)
|
---|
| 478 | {
|
---|
| 479 | return asn1_read(data, v, 1);
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | bool asn1_peek_uint8(struct asn1_data *data, uint8_t *v)
|
---|
| 483 | {
|
---|
| 484 | return asn1_peek(data, v, 1);
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | bool asn1_peek_tag(struct asn1_data *data, uint8_t tag)
|
---|
| 488 | {
|
---|
| 489 | uint8_t b;
|
---|
| 490 |
|
---|
| 491 | if (asn1_tag_remaining(data) <= 0) {
|
---|
| 492 | return false;
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | if (!asn1_peek_uint8(data, &b))
|
---|
| 496 | return false;
|
---|
| 497 |
|
---|
| 498 | return (b == tag);
|
---|
| 499 | }
|
---|
| 500 |
|
---|
[745] | 501 | /*
|
---|
| 502 | * just get the needed size the tag would consume
|
---|
| 503 | */
|
---|
| 504 | bool asn1_peek_tag_needed_size(struct asn1_data *data, uint8_t tag, size_t *size)
|
---|
| 505 | {
|
---|
| 506 | off_t start_ofs = data->ofs;
|
---|
| 507 | uint8_t b;
|
---|
| 508 | size_t taglen = 0;
|
---|
| 509 |
|
---|
| 510 | if (data->has_error) {
|
---|
| 511 | return false;
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 | if (!asn1_read_uint8(data, &b)) {
|
---|
| 515 | data->ofs = start_ofs;
|
---|
| 516 | data->has_error = false;
|
---|
| 517 | return false;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | if (b != tag) {
|
---|
| 521 | data->ofs = start_ofs;
|
---|
| 522 | data->has_error = false;
|
---|
| 523 | return false;
|
---|
| 524 | }
|
---|
| 525 |
|
---|
| 526 | if (!asn1_read_uint8(data, &b)) {
|
---|
| 527 | data->ofs = start_ofs;
|
---|
| 528 | data->has_error = false;
|
---|
| 529 | return false;
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | if (b & 0x80) {
|
---|
| 533 | int n = b & 0x7f;
|
---|
| 534 | if (!asn1_read_uint8(data, &b)) {
|
---|
| 535 | data->ofs = start_ofs;
|
---|
| 536 | data->has_error = false;
|
---|
| 537 | return false;
|
---|
| 538 | }
|
---|
| 539 | if (n > 4) {
|
---|
| 540 | /*
|
---|
| 541 | * We should not allow more than 4 bytes
|
---|
| 542 | * for the encoding of the tag length.
|
---|
| 543 | *
|
---|
| 544 | * Otherwise we'd overflow the taglen
|
---|
| 545 | * variable on 32 bit systems.
|
---|
| 546 | */
|
---|
| 547 | data->ofs = start_ofs;
|
---|
| 548 | data->has_error = false;
|
---|
| 549 | return false;
|
---|
| 550 | }
|
---|
| 551 | taglen = b;
|
---|
| 552 | while (n > 1) {
|
---|
| 553 | if (!asn1_read_uint8(data, &b)) {
|
---|
| 554 | data->ofs = start_ofs;
|
---|
| 555 | data->has_error = false;
|
---|
| 556 | return false;
|
---|
| 557 | }
|
---|
| 558 | taglen = (taglen << 8) | b;
|
---|
| 559 | n--;
|
---|
| 560 | }
|
---|
| 561 | } else {
|
---|
| 562 | taglen = b;
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 | *size = (data->ofs - start_ofs) + taglen;
|
---|
| 566 |
|
---|
| 567 | data->ofs = start_ofs;
|
---|
| 568 | data->has_error = false;
|
---|
| 569 | return true;
|
---|
| 570 | }
|
---|
| 571 |
|
---|
[599] | 572 | /* start reading a nested asn1 structure */
|
---|
| 573 | bool asn1_start_tag(struct asn1_data *data, uint8_t tag)
|
---|
| 574 | {
|
---|
| 575 | uint8_t b;
|
---|
| 576 | struct nesting *nesting;
|
---|
| 577 |
|
---|
| 578 | if (!asn1_read_uint8(data, &b))
|
---|
| 579 | return false;
|
---|
| 580 |
|
---|
| 581 | if (b != tag) {
|
---|
| 582 | data->has_error = true;
|
---|
| 583 | return false;
|
---|
| 584 | }
|
---|
| 585 | nesting = talloc(data, struct nesting);
|
---|
| 586 | if (!nesting) {
|
---|
| 587 | data->has_error = true;
|
---|
| 588 | return false;
|
---|
| 589 | }
|
---|
| 590 |
|
---|
| 591 | if (!asn1_read_uint8(data, &b)) {
|
---|
| 592 | return false;
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | if (b & 0x80) {
|
---|
| 596 | int n = b & 0x7f;
|
---|
| 597 | if (!asn1_read_uint8(data, &b))
|
---|
| 598 | return false;
|
---|
| 599 | nesting->taglen = b;
|
---|
| 600 | while (n > 1) {
|
---|
| 601 | if (!asn1_read_uint8(data, &b))
|
---|
| 602 | return false;
|
---|
| 603 | nesting->taglen = (nesting->taglen << 8) | b;
|
---|
| 604 | n--;
|
---|
| 605 | }
|
---|
| 606 | } else {
|
---|
| 607 | nesting->taglen = b;
|
---|
| 608 | }
|
---|
| 609 | nesting->start = data->ofs;
|
---|
| 610 | nesting->next = data->nesting;
|
---|
| 611 | data->nesting = nesting;
|
---|
| 612 | if (asn1_tag_remaining(data) == -1) {
|
---|
| 613 | return false;
|
---|
| 614 | }
|
---|
| 615 | return !data->has_error;
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | /* stop reading a tag */
|
---|
| 619 | bool asn1_end_tag(struct asn1_data *data)
|
---|
| 620 | {
|
---|
| 621 | struct nesting *nesting;
|
---|
| 622 |
|
---|
| 623 | /* make sure we read it all */
|
---|
| 624 | if (asn1_tag_remaining(data) != 0) {
|
---|
| 625 | data->has_error = true;
|
---|
| 626 | return false;
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | nesting = data->nesting;
|
---|
| 630 |
|
---|
| 631 | if (!nesting) {
|
---|
| 632 | data->has_error = true;
|
---|
| 633 | return false;
|
---|
| 634 | }
|
---|
| 635 |
|
---|
| 636 | data->nesting = nesting->next;
|
---|
| 637 | talloc_free(nesting);
|
---|
| 638 | return true;
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 | /* work out how many bytes are left in this nested tag */
|
---|
| 642 | int asn1_tag_remaining(struct asn1_data *data)
|
---|
| 643 | {
|
---|
| 644 | int remaining;
|
---|
| 645 | if (data->has_error) {
|
---|
| 646 | return -1;
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | if (!data->nesting) {
|
---|
| 650 | data->has_error = true;
|
---|
| 651 | return -1;
|
---|
| 652 | }
|
---|
| 653 | remaining = data->nesting->taglen - (data->ofs - data->nesting->start);
|
---|
| 654 | if (remaining > (data->length - data->ofs)) {
|
---|
| 655 | data->has_error = true;
|
---|
| 656 | return -1;
|
---|
| 657 | }
|
---|
| 658 | return remaining;
|
---|
| 659 | }
|
---|
| 660 |
|
---|
[745] | 661 | /**
|
---|
| 662 | * Internal implementation for reading binary OIDs
|
---|
| 663 | * Reading is done as far in the buffer as valid OID
|
---|
| 664 | * till buffer ends or not valid sub-identifier is found.
|
---|
| 665 | */
|
---|
| 666 | static bool _ber_read_OID_String_impl(TALLOC_CTX *mem_ctx, DATA_BLOB blob,
|
---|
| 667 | char **OID, size_t *bytes_eaten)
|
---|
[599] | 668 | {
|
---|
| 669 | int i;
|
---|
| 670 | uint8_t *b;
|
---|
[745] | 671 | unsigned int v;
|
---|
[599] | 672 | char *tmp_oid = NULL;
|
---|
| 673 |
|
---|
| 674 | if (blob.length < 2) return false;
|
---|
| 675 |
|
---|
| 676 | b = blob.data;
|
---|
| 677 |
|
---|
| 678 | tmp_oid = talloc_asprintf(mem_ctx, "%u", b[0]/40);
|
---|
| 679 | if (!tmp_oid) goto nomem;
|
---|
| 680 | tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", b[0]%40);
|
---|
| 681 | if (!tmp_oid) goto nomem;
|
---|
| 682 |
|
---|
[745] | 683 | if (bytes_eaten != NULL) {
|
---|
| 684 | *bytes_eaten = 0;
|
---|
| 685 | }
|
---|
| 686 |
|
---|
[599] | 687 | for(i = 1, v = 0; i < blob.length; i++) {
|
---|
| 688 | v = (v<<7) | (b[i]&0x7f);
|
---|
| 689 | if ( ! (b[i] & 0x80)) {
|
---|
| 690 | tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", v);
|
---|
| 691 | v = 0;
|
---|
[745] | 692 | if (bytes_eaten)
|
---|
| 693 | *bytes_eaten = i+1;
|
---|
[599] | 694 | }
|
---|
| 695 | if (!tmp_oid) goto nomem;
|
---|
| 696 | }
|
---|
| 697 |
|
---|
[745] | 698 | *OID = tmp_oid;
|
---|
| 699 | return true;
|
---|
| 700 |
|
---|
| 701 | nomem:
|
---|
| 702 | return false;
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 | /* read an object ID from a data blob */
|
---|
| 706 | bool ber_read_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, char **OID)
|
---|
| 707 | {
|
---|
| 708 | size_t bytes_eaten;
|
---|
| 709 |
|
---|
| 710 | if (!_ber_read_OID_String_impl(mem_ctx, blob, OID, &bytes_eaten))
|
---|
[599] | 711 | return false;
|
---|
[745] | 712 |
|
---|
| 713 | return (bytes_eaten == blob.length);
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | /**
|
---|
| 717 | * Deserialize partial OID string.
|
---|
| 718 | * Partial OIDs are in the form:
|
---|
| 719 | * 1:2.5.6:0x81
|
---|
| 720 | * 1:2.5.6:0x8182
|
---|
| 721 | */
|
---|
| 722 | bool ber_read_partial_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob,
|
---|
| 723 | char **partial_oid)
|
---|
| 724 | {
|
---|
| 725 | size_t bytes_left;
|
---|
| 726 | size_t bytes_eaten;
|
---|
| 727 | char *identifier = NULL;
|
---|
| 728 | char *tmp_oid = NULL;
|
---|
| 729 |
|
---|
| 730 | if (!_ber_read_OID_String_impl(mem_ctx, blob, &tmp_oid, &bytes_eaten))
|
---|
| 731 | return false;
|
---|
| 732 |
|
---|
| 733 | if (bytes_eaten < blob.length) {
|
---|
| 734 | bytes_left = blob.length - bytes_eaten;
|
---|
| 735 | identifier = hex_encode_talloc(mem_ctx, &blob.data[bytes_eaten], bytes_left);
|
---|
| 736 | if (!identifier) goto nomem;
|
---|
| 737 |
|
---|
| 738 | *partial_oid = talloc_asprintf_append_buffer(tmp_oid, ":0x%s", identifier);
|
---|
| 739 | if (!*partial_oid) goto nomem;
|
---|
| 740 | TALLOC_FREE(identifier);
|
---|
| 741 | } else {
|
---|
| 742 | *partial_oid = tmp_oid;
|
---|
[599] | 743 | }
|
---|
| 744 |
|
---|
| 745 | return true;
|
---|
| 746 |
|
---|
[745] | 747 | nomem:
|
---|
| 748 | TALLOC_FREE(identifier);
|
---|
| 749 | TALLOC_FREE(tmp_oid);
|
---|
[599] | 750 | return false;
|
---|
| 751 | }
|
---|
| 752 |
|
---|
| 753 | /* read an object ID from a ASN1 buffer */
|
---|
[745] | 754 | bool asn1_read_OID(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **OID)
|
---|
[599] | 755 | {
|
---|
| 756 | DATA_BLOB blob;
|
---|
| 757 | int len;
|
---|
| 758 |
|
---|
| 759 | if (!asn1_start_tag(data, ASN1_OID)) return false;
|
---|
| 760 |
|
---|
| 761 | len = asn1_tag_remaining(data);
|
---|
| 762 | if (len < 0) {
|
---|
| 763 | data->has_error = true;
|
---|
| 764 | return false;
|
---|
| 765 | }
|
---|
| 766 |
|
---|
| 767 | blob = data_blob(NULL, len);
|
---|
| 768 | if (!blob.data) {
|
---|
| 769 | data->has_error = true;
|
---|
| 770 | return false;
|
---|
| 771 | }
|
---|
| 772 |
|
---|
| 773 | asn1_read(data, blob.data, len);
|
---|
| 774 | asn1_end_tag(data);
|
---|
| 775 | if (data->has_error) {
|
---|
| 776 | data_blob_free(&blob);
|
---|
| 777 | return false;
|
---|
| 778 | }
|
---|
| 779 |
|
---|
| 780 | if (!ber_read_OID_String(mem_ctx, blob, OID)) {
|
---|
| 781 | data->has_error = true;
|
---|
| 782 | data_blob_free(&blob);
|
---|
| 783 | return false;
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 | data_blob_free(&blob);
|
---|
| 787 | return true;
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 | /* check that the next object ID is correct */
|
---|
| 791 | bool asn1_check_OID(struct asn1_data *data, const char *OID)
|
---|
| 792 | {
|
---|
[745] | 793 | char *id;
|
---|
[599] | 794 |
|
---|
| 795 | if (!asn1_read_OID(data, data, &id)) return false;
|
---|
| 796 |
|
---|
| 797 | if (strcmp(id, OID) != 0) {
|
---|
[745] | 798 | talloc_free(id);
|
---|
[599] | 799 | data->has_error = true;
|
---|
| 800 | return false;
|
---|
| 801 | }
|
---|
[745] | 802 | talloc_free(id);
|
---|
[599] | 803 | return true;
|
---|
| 804 | }
|
---|
| 805 |
|
---|
| 806 | /* read a LDAPString from a ASN1 buffer */
|
---|
| 807 | bool asn1_read_LDAPString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
|
---|
| 808 | {
|
---|
| 809 | int len;
|
---|
| 810 | len = asn1_tag_remaining(data);
|
---|
| 811 | if (len < 0) {
|
---|
| 812 | data->has_error = true;
|
---|
| 813 | return false;
|
---|
| 814 | }
|
---|
| 815 | *s = talloc_array(mem_ctx, char, len+1);
|
---|
| 816 | if (! *s) {
|
---|
| 817 | data->has_error = true;
|
---|
| 818 | return false;
|
---|
| 819 | }
|
---|
| 820 | asn1_read(data, *s, len);
|
---|
| 821 | (*s)[len] = 0;
|
---|
| 822 | return !data->has_error;
|
---|
| 823 | }
|
---|
| 824 |
|
---|
| 825 |
|
---|
| 826 | /* read a GeneralString from a ASN1 buffer */
|
---|
| 827 | bool asn1_read_GeneralString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
|
---|
| 828 | {
|
---|
| 829 | if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) return false;
|
---|
| 830 | if (!asn1_read_LDAPString(data, mem_ctx, s)) return false;
|
---|
| 831 | return asn1_end_tag(data);
|
---|
| 832 | }
|
---|
| 833 |
|
---|
| 834 |
|
---|
| 835 | /* read a octet string blob */
|
---|
| 836 | bool asn1_read_OctetString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
|
---|
| 837 | {
|
---|
| 838 | int len;
|
---|
| 839 | ZERO_STRUCTP(blob);
|
---|
| 840 | if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return false;
|
---|
| 841 | len = asn1_tag_remaining(data);
|
---|
| 842 | if (len < 0) {
|
---|
| 843 | data->has_error = true;
|
---|
| 844 | return false;
|
---|
| 845 | }
|
---|
| 846 | *blob = data_blob_talloc(mem_ctx, NULL, len+1);
|
---|
| 847 | if (!blob->data) {
|
---|
| 848 | data->has_error = true;
|
---|
| 849 | return false;
|
---|
| 850 | }
|
---|
| 851 | asn1_read(data, blob->data, len);
|
---|
| 852 | asn1_end_tag(data);
|
---|
| 853 | blob->length--;
|
---|
| 854 | blob->data[len] = 0;
|
---|
| 855 |
|
---|
| 856 | if (data->has_error) {
|
---|
| 857 | data_blob_free(blob);
|
---|
| 858 | *blob = data_blob_null;
|
---|
| 859 | return false;
|
---|
| 860 | }
|
---|
| 861 | return true;
|
---|
| 862 | }
|
---|
| 863 |
|
---|
| 864 | bool asn1_read_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
|
---|
| 865 | {
|
---|
| 866 | int len;
|
---|
| 867 | ZERO_STRUCTP(blob);
|
---|
| 868 | if (!asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(num))) return false;
|
---|
| 869 | len = asn1_tag_remaining(data);
|
---|
| 870 | if (len < 0) {
|
---|
| 871 | data->has_error = true;
|
---|
| 872 | return false;
|
---|
| 873 | }
|
---|
| 874 | *blob = data_blob(NULL, len);
|
---|
| 875 | if ((len != 0) && (!blob->data)) {
|
---|
| 876 | data->has_error = true;
|
---|
| 877 | return false;
|
---|
| 878 | }
|
---|
| 879 | asn1_read(data, blob->data, len);
|
---|
| 880 | asn1_end_tag(data);
|
---|
| 881 | return !data->has_error;
|
---|
| 882 | }
|
---|
| 883 |
|
---|
| 884 | /* read an integer without tag*/
|
---|
| 885 | bool asn1_read_implicit_Integer(struct asn1_data *data, int *i)
|
---|
| 886 | {
|
---|
| 887 | uint8_t b;
|
---|
| 888 | bool first_byte = true;
|
---|
| 889 | *i = 0;
|
---|
| 890 |
|
---|
| 891 | while (!data->has_error && asn1_tag_remaining(data)>0) {
|
---|
| 892 | if (!asn1_read_uint8(data, &b)) return false;
|
---|
| 893 | if (first_byte) {
|
---|
| 894 | if (b & 0x80) {
|
---|
| 895 | /* Number is negative.
|
---|
| 896 | Set i to -1 for sign extend. */
|
---|
| 897 | *i = -1;
|
---|
| 898 | }
|
---|
| 899 | first_byte = false;
|
---|
| 900 | }
|
---|
| 901 | *i = (*i << 8) + b;
|
---|
| 902 | }
|
---|
| 903 | return !data->has_error;
|
---|
| 904 |
|
---|
| 905 | }
|
---|
| 906 |
|
---|
| 907 | /* read an integer */
|
---|
| 908 | bool asn1_read_Integer(struct asn1_data *data, int *i)
|
---|
| 909 | {
|
---|
| 910 | *i = 0;
|
---|
| 911 |
|
---|
| 912 | if (!asn1_start_tag(data, ASN1_INTEGER)) return false;
|
---|
| 913 | if (!asn1_read_implicit_Integer(data, i)) return false;
|
---|
| 914 | return asn1_end_tag(data);
|
---|
| 915 | }
|
---|
| 916 |
|
---|
| 917 | /* read a BIT STRING */
|
---|
| 918 | bool asn1_read_BitString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob, uint8_t *padding)
|
---|
| 919 | {
|
---|
| 920 | int len;
|
---|
| 921 | ZERO_STRUCTP(blob);
|
---|
| 922 | if (!asn1_start_tag(data, ASN1_BIT_STRING)) return false;
|
---|
| 923 | len = asn1_tag_remaining(data);
|
---|
| 924 | if (len < 0) {
|
---|
| 925 | data->has_error = true;
|
---|
| 926 | return false;
|
---|
| 927 | }
|
---|
| 928 | if (!asn1_read_uint8(data, padding)) return false;
|
---|
| 929 |
|
---|
| 930 | *blob = data_blob_talloc(mem_ctx, NULL, len);
|
---|
| 931 | if (!blob->data) {
|
---|
| 932 | data->has_error = true;
|
---|
| 933 | return false;
|
---|
| 934 | }
|
---|
| 935 | if (asn1_read(data, blob->data, len - 1)) {
|
---|
| 936 | blob->length--;
|
---|
| 937 | blob->data[len] = 0;
|
---|
| 938 | asn1_end_tag(data);
|
---|
| 939 | }
|
---|
| 940 |
|
---|
| 941 | if (data->has_error) {
|
---|
| 942 | data_blob_free(blob);
|
---|
| 943 | *blob = data_blob_null;
|
---|
| 944 | *padding = 0;
|
---|
| 945 | return false;
|
---|
| 946 | }
|
---|
| 947 | return true;
|
---|
| 948 | }
|
---|
| 949 |
|
---|
| 950 | /* read an integer */
|
---|
| 951 | bool asn1_read_enumerated(struct asn1_data *data, int *v)
|
---|
| 952 | {
|
---|
| 953 | *v = 0;
|
---|
| 954 |
|
---|
| 955 | if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
|
---|
| 956 | while (!data->has_error && asn1_tag_remaining(data)>0) {
|
---|
| 957 | uint8_t b;
|
---|
| 958 | asn1_read_uint8(data, &b);
|
---|
| 959 | *v = (*v << 8) + b;
|
---|
| 960 | }
|
---|
| 961 | return asn1_end_tag(data);
|
---|
| 962 | }
|
---|
| 963 |
|
---|
| 964 | /* check a enumerated value is correct */
|
---|
| 965 | bool asn1_check_enumerated(struct asn1_data *data, int v)
|
---|
| 966 | {
|
---|
| 967 | uint8_t b;
|
---|
| 968 | if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
|
---|
| 969 | asn1_read_uint8(data, &b);
|
---|
| 970 | asn1_end_tag(data);
|
---|
| 971 |
|
---|
| 972 | if (v != b)
|
---|
| 973 | data->has_error = false;
|
---|
| 974 |
|
---|
| 975 | return !data->has_error;
|
---|
| 976 | }
|
---|
| 977 |
|
---|
| 978 | /* write an enumerated value to the stream */
|
---|
| 979 | bool asn1_write_enumerated(struct asn1_data *data, uint8_t v)
|
---|
| 980 | {
|
---|
| 981 | if (!asn1_push_tag(data, ASN1_ENUMERATED)) return false;
|
---|
| 982 | asn1_write_uint8(data, v);
|
---|
| 983 | asn1_pop_tag(data);
|
---|
| 984 | return !data->has_error;
|
---|
| 985 | }
|
---|
| 986 |
|
---|
| 987 | /*
|
---|
| 988 | Get us the data just written without copying
|
---|
| 989 | */
|
---|
| 990 | bool asn1_blob(const struct asn1_data *asn1, DATA_BLOB *blob)
|
---|
| 991 | {
|
---|
| 992 | if (asn1->has_error) {
|
---|
| 993 | return false;
|
---|
| 994 | }
|
---|
| 995 | if (asn1->nesting != NULL) {
|
---|
| 996 | return false;
|
---|
| 997 | }
|
---|
| 998 | blob->data = asn1->data;
|
---|
| 999 | blob->length = asn1->length;
|
---|
| 1000 | return true;
|
---|
| 1001 | }
|
---|
| 1002 |
|
---|
| 1003 | /*
|
---|
| 1004 | Fill in an asn1 struct without making a copy
|
---|
| 1005 | */
|
---|
| 1006 | void asn1_load_nocopy(struct asn1_data *data, uint8_t *buf, size_t len)
|
---|
| 1007 | {
|
---|
| 1008 | ZERO_STRUCTP(data);
|
---|
| 1009 | data->data = buf;
|
---|
| 1010 | data->length = len;
|
---|
| 1011 | }
|
---|
| 1012 |
|
---|
| 1013 | /*
|
---|
| 1014 | check if a ASN.1 blob is a full tag
|
---|
| 1015 | */
|
---|
| 1016 | NTSTATUS asn1_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size)
|
---|
| 1017 | {
|
---|
| 1018 | struct asn1_data *asn1 = asn1_init(NULL);
|
---|
| 1019 | int size;
|
---|
| 1020 |
|
---|
| 1021 | NT_STATUS_HAVE_NO_MEMORY(asn1);
|
---|
| 1022 |
|
---|
| 1023 | asn1->data = blob.data;
|
---|
| 1024 | asn1->length = blob.length;
|
---|
| 1025 | asn1_start_tag(asn1, tag);
|
---|
| 1026 | if (asn1->has_error) {
|
---|
| 1027 | talloc_free(asn1);
|
---|
| 1028 | return STATUS_MORE_ENTRIES;
|
---|
| 1029 | }
|
---|
| 1030 | size = asn1_tag_remaining(asn1) + asn1->ofs;
|
---|
| 1031 |
|
---|
| 1032 | talloc_free(asn1);
|
---|
| 1033 |
|
---|
| 1034 | if (size > blob.length) {
|
---|
| 1035 | return STATUS_MORE_ENTRIES;
|
---|
[745] | 1036 | }
|
---|
| 1037 |
|
---|
| 1038 | *packet_size = size;
|
---|
| 1039 | return NT_STATUS_OK;
|
---|
| 1040 | }
|
---|
| 1041 |
|
---|
| 1042 | NTSTATUS asn1_peek_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size)
|
---|
| 1043 | {
|
---|
| 1044 | struct asn1_data asn1;
|
---|
| 1045 | size_t size;
|
---|
| 1046 | bool ok;
|
---|
| 1047 |
|
---|
| 1048 | ZERO_STRUCT(asn1);
|
---|
| 1049 | asn1.data = blob.data;
|
---|
| 1050 | asn1.length = blob.length;
|
---|
| 1051 |
|
---|
| 1052 | ok = asn1_peek_tag_needed_size(&asn1, tag, &size);
|
---|
| 1053 | if (!ok) {
|
---|
| 1054 | return NT_STATUS_INVALID_BUFFER_SIZE;
|
---|
| 1055 | }
|
---|
| 1056 |
|
---|
| 1057 | if (size > blob.length) {
|
---|
| 1058 | *packet_size = size;
|
---|
| 1059 | return STATUS_MORE_ENTRIES;
|
---|
[599] | 1060 | }
|
---|
| 1061 |
|
---|
| 1062 | *packet_size = size;
|
---|
| 1063 | return NT_STATUS_OK;
|
---|
| 1064 | }
|
---|