| 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 |  | 
|---|
| 217 | bool ber_write_OID_String(DATA_BLOB *blob, const char *OID) | 
|---|
| 218 | { | 
|---|
| 219 | uint_t v, v2; | 
|---|
| 220 | const char *p = (const char *)OID; | 
|---|
| 221 | char *newp; | 
|---|
| 222 | int i; | 
|---|
| 223 |  | 
|---|
| 224 | v = strtoul(p, &newp, 10); | 
|---|
| 225 | if (newp[0] != '.') return false; | 
|---|
| 226 | p = newp + 1; | 
|---|
| 227 |  | 
|---|
| 228 | v2 = strtoul(p, &newp, 10); | 
|---|
| 229 | if (newp[0] != '.') return false; | 
|---|
| 230 | p = newp + 1; | 
|---|
| 231 |  | 
|---|
| 232 | /*the ber representation can't use more space then the string one */ | 
|---|
| 233 | *blob = data_blob(NULL, strlen(OID)); | 
|---|
| 234 | if (!blob->data) return false; | 
|---|
| 235 |  | 
|---|
| 236 | blob->data[0] = 40*v + v2; | 
|---|
| 237 |  | 
|---|
| 238 | i = 1; | 
|---|
| 239 | while (*p) { | 
|---|
| 240 | v = strtoul(p, &newp, 10); | 
|---|
| 241 | if (newp[0] == '.') { | 
|---|
| 242 | p = newp + 1; | 
|---|
| 243 | } else if (newp[0] == '\0') { | 
|---|
| 244 | p = newp; | 
|---|
| 245 | } else { | 
|---|
| 246 | data_blob_free(blob); | 
|---|
| 247 | return false; | 
|---|
| 248 | } | 
|---|
| 249 | if (v >= (1<<28)) blob->data[i++] = (0x80 | ((v>>28)&0x7f)); | 
|---|
| 250 | if (v >= (1<<21)) blob->data[i++] = (0x80 | ((v>>21)&0x7f)); | 
|---|
| 251 | if (v >= (1<<14)) blob->data[i++] = (0x80 | ((v>>14)&0x7f)); | 
|---|
| 252 | if (v >= (1<<7)) blob->data[i++] = (0x80 | ((v>>7)&0x7f)); | 
|---|
| 253 | blob->data[i++] = (v&0x7f); | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | blob->length = i; | 
|---|
| 257 |  | 
|---|
| 258 | return true; | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | /* write an object ID to a ASN1 buffer */ | 
|---|
| 262 | bool asn1_write_OID(struct asn1_data *data, const char *OID) | 
|---|
| 263 | { | 
|---|
| 264 | DATA_BLOB blob; | 
|---|
| 265 |  | 
|---|
| 266 | if (!asn1_push_tag(data, ASN1_OID)) return false; | 
|---|
| 267 |  | 
|---|
| 268 | if (!ber_write_OID_String(&blob, OID)) { | 
|---|
| 269 | data->has_error = true; | 
|---|
| 270 | return false; | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | if (!asn1_write(data, blob.data, blob.length)) { | 
|---|
| 274 | data_blob_free(&blob); | 
|---|
| 275 | data->has_error = true; | 
|---|
| 276 | return false; | 
|---|
| 277 | } | 
|---|
| 278 | data_blob_free(&blob); | 
|---|
| 279 | return asn1_pop_tag(data); | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | /* write an octet string */ | 
|---|
| 283 | bool asn1_write_OctetString(struct asn1_data *data, const void *p, size_t length) | 
|---|
| 284 | { | 
|---|
| 285 | asn1_push_tag(data, ASN1_OCTET_STRING); | 
|---|
| 286 | asn1_write(data, p, length); | 
|---|
| 287 | asn1_pop_tag(data); | 
|---|
| 288 | return !data->has_error; | 
|---|
| 289 | } | 
|---|
| 290 |  | 
|---|
| 291 | /* write a LDAP string */ | 
|---|
| 292 | bool asn1_write_LDAPString(struct asn1_data *data, const char *s) | 
|---|
| 293 | { | 
|---|
| 294 | asn1_write(data, s, strlen(s)); | 
|---|
| 295 | return !data->has_error; | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | /* write a LDAP string from a DATA_BLOB */ | 
|---|
| 299 | bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data *data, const DATA_BLOB *s) | 
|---|
| 300 | { | 
|---|
| 301 | asn1_write(data, s->data, s->length); | 
|---|
| 302 | return !data->has_error; | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 | /* write a general string */ | 
|---|
| 306 | bool asn1_write_GeneralString(struct asn1_data *data, const char *s) | 
|---|
| 307 | { | 
|---|
| 308 | asn1_push_tag(data, ASN1_GENERAL_STRING); | 
|---|
| 309 | asn1_write_LDAPString(data, s); | 
|---|
| 310 | asn1_pop_tag(data); | 
|---|
| 311 | return !data->has_error; | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | bool asn1_write_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob) | 
|---|
| 315 | { | 
|---|
| 316 | asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(num)); | 
|---|
| 317 | asn1_write(data, blob->data, blob->length); | 
|---|
| 318 | asn1_pop_tag(data); | 
|---|
| 319 | return !data->has_error; | 
|---|
| 320 | } | 
|---|
| 321 |  | 
|---|
| 322 | /* write a BOOLEAN */ | 
|---|
| 323 | bool asn1_write_BOOLEAN(struct asn1_data *data, bool v) | 
|---|
| 324 | { | 
|---|
| 325 | asn1_push_tag(data, ASN1_BOOLEAN); | 
|---|
| 326 | asn1_write_uint8(data, v ? 0xFF : 0); | 
|---|
| 327 | asn1_pop_tag(data); | 
|---|
| 328 | return !data->has_error; | 
|---|
| 329 | } | 
|---|
| 330 |  | 
|---|
| 331 | bool asn1_read_BOOLEAN(struct asn1_data *data, bool *v) | 
|---|
| 332 | { | 
|---|
| 333 | uint8_t tmp = 0; | 
|---|
| 334 | asn1_start_tag(data, ASN1_BOOLEAN); | 
|---|
| 335 | asn1_read_uint8(data, &tmp); | 
|---|
| 336 | if (tmp == 0xFF) { | 
|---|
| 337 | *v = true; | 
|---|
| 338 | } else { | 
|---|
| 339 | *v = false; | 
|---|
| 340 | } | 
|---|
| 341 | asn1_end_tag(data); | 
|---|
| 342 | return !data->has_error; | 
|---|
| 343 | } | 
|---|
| 344 |  | 
|---|
| 345 | /* write a BOOLEAN in a simple context */ | 
|---|
| 346 | bool asn1_write_BOOLEAN_context(struct asn1_data *data, bool v, int context) | 
|---|
| 347 | { | 
|---|
| 348 | asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(context)); | 
|---|
| 349 | asn1_write_uint8(data, v ? 0xFF : 0); | 
|---|
| 350 | asn1_pop_tag(data); | 
|---|
| 351 | return !data->has_error; | 
|---|
| 352 | } | 
|---|
| 353 |  | 
|---|
| 354 | bool asn1_read_BOOLEAN_context(struct asn1_data *data, bool *v, int context) | 
|---|
| 355 | { | 
|---|
| 356 | uint8_t tmp = 0; | 
|---|
| 357 | asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(context)); | 
|---|
| 358 | asn1_read_uint8(data, &tmp); | 
|---|
| 359 | if (tmp == 0xFF) { | 
|---|
| 360 | *v = true; | 
|---|
| 361 | } else { | 
|---|
| 362 | *v = false; | 
|---|
| 363 | } | 
|---|
| 364 | asn1_end_tag(data); | 
|---|
| 365 | return !data->has_error; | 
|---|
| 366 | } | 
|---|
| 367 |  | 
|---|
| 368 | /* check a BOOLEAN */ | 
|---|
| 369 | bool asn1_check_BOOLEAN(struct asn1_data *data, bool v) | 
|---|
| 370 | { | 
|---|
| 371 | uint8_t b = 0; | 
|---|
| 372 |  | 
|---|
| 373 | asn1_read_uint8(data, &b); | 
|---|
| 374 | if (b != ASN1_BOOLEAN) { | 
|---|
| 375 | data->has_error = true; | 
|---|
| 376 | return false; | 
|---|
| 377 | } | 
|---|
| 378 | asn1_read_uint8(data, &b); | 
|---|
| 379 | if (b != v) { | 
|---|
| 380 | data->has_error = true; | 
|---|
| 381 | return false; | 
|---|
| 382 | } | 
|---|
| 383 | return !data->has_error; | 
|---|
| 384 | } | 
|---|
| 385 |  | 
|---|
| 386 |  | 
|---|
| 387 | /* load a struct asn1_data structure with a lump of data, ready to be parsed */ | 
|---|
| 388 | bool asn1_load(struct asn1_data *data, DATA_BLOB blob) | 
|---|
| 389 | { | 
|---|
| 390 | ZERO_STRUCTP(data); | 
|---|
| 391 | data->data = (uint8_t *)talloc_memdup(data, blob.data, blob.length); | 
|---|
| 392 | if (!data->data) { | 
|---|
| 393 | data->has_error = true; | 
|---|
| 394 | return false; | 
|---|
| 395 | } | 
|---|
| 396 | data->length = blob.length; | 
|---|
| 397 | return true; | 
|---|
| 398 | } | 
|---|
| 399 |  | 
|---|
| 400 | /* Peek into an ASN1 buffer, not advancing the pointer */ | 
|---|
| 401 | bool asn1_peek(struct asn1_data *data, void *p, int len) | 
|---|
| 402 | { | 
|---|
| 403 | if (data->has_error) | 
|---|
| 404 | return false; | 
|---|
| 405 |  | 
|---|
| 406 | if (len < 0 || data->ofs + len < data->ofs || data->ofs + len < len) | 
|---|
| 407 | return false; | 
|---|
| 408 |  | 
|---|
| 409 | if (data->ofs + len > data->length) { | 
|---|
| 410 | /* we need to mark the buffer as consumed, so the caller knows | 
|---|
| 411 | this was an out of data error, and not a decode error */ | 
|---|
| 412 | data->ofs = data->length; | 
|---|
| 413 | return false; | 
|---|
| 414 | } | 
|---|
| 415 |  | 
|---|
| 416 | memcpy(p, data->data + data->ofs, len); | 
|---|
| 417 | return true; | 
|---|
| 418 | } | 
|---|
| 419 |  | 
|---|
| 420 | /* read from a ASN1 buffer, advancing the buffer pointer */ | 
|---|
| 421 | bool asn1_read(struct asn1_data *data, void *p, int len) | 
|---|
| 422 | { | 
|---|
| 423 | if (!asn1_peek(data, p, len)) { | 
|---|
| 424 | data->has_error = true; | 
|---|
| 425 | return false; | 
|---|
| 426 | } | 
|---|
| 427 |  | 
|---|
| 428 | data->ofs += len; | 
|---|
| 429 | return true; | 
|---|
| 430 | } | 
|---|
| 431 |  | 
|---|
| 432 | /* read a uint8_t from a ASN1 buffer */ | 
|---|
| 433 | bool asn1_read_uint8(struct asn1_data *data, uint8_t *v) | 
|---|
| 434 | { | 
|---|
| 435 | return asn1_read(data, v, 1); | 
|---|
| 436 | } | 
|---|
| 437 |  | 
|---|
| 438 | bool asn1_peek_uint8(struct asn1_data *data, uint8_t *v) | 
|---|
| 439 | { | 
|---|
| 440 | return asn1_peek(data, v, 1); | 
|---|
| 441 | } | 
|---|
| 442 |  | 
|---|
| 443 | bool asn1_peek_tag(struct asn1_data *data, uint8_t tag) | 
|---|
| 444 | { | 
|---|
| 445 | uint8_t b; | 
|---|
| 446 |  | 
|---|
| 447 | if (asn1_tag_remaining(data) <= 0) { | 
|---|
| 448 | return false; | 
|---|
| 449 | } | 
|---|
| 450 |  | 
|---|
| 451 | if (!asn1_peek_uint8(data, &b)) | 
|---|
| 452 | return false; | 
|---|
| 453 |  | 
|---|
| 454 | return (b == tag); | 
|---|
| 455 | } | 
|---|
| 456 |  | 
|---|
| 457 | /* start reading a nested asn1 structure */ | 
|---|
| 458 | bool asn1_start_tag(struct asn1_data *data, uint8_t tag) | 
|---|
| 459 | { | 
|---|
| 460 | uint8_t b; | 
|---|
| 461 | struct nesting *nesting; | 
|---|
| 462 |  | 
|---|
| 463 | if (!asn1_read_uint8(data, &b)) | 
|---|
| 464 | return false; | 
|---|
| 465 |  | 
|---|
| 466 | if (b != tag) { | 
|---|
| 467 | data->has_error = true; | 
|---|
| 468 | return false; | 
|---|
| 469 | } | 
|---|
| 470 | nesting = talloc(data, struct nesting); | 
|---|
| 471 | if (!nesting) { | 
|---|
| 472 | data->has_error = true; | 
|---|
| 473 | return false; | 
|---|
| 474 | } | 
|---|
| 475 |  | 
|---|
| 476 | if (!asn1_read_uint8(data, &b)) { | 
|---|
| 477 | return false; | 
|---|
| 478 | } | 
|---|
| 479 |  | 
|---|
| 480 | if (b & 0x80) { | 
|---|
| 481 | int n = b & 0x7f; | 
|---|
| 482 | if (!asn1_read_uint8(data, &b)) | 
|---|
| 483 | return false; | 
|---|
| 484 | nesting->taglen = b; | 
|---|
| 485 | while (n > 1) { | 
|---|
| 486 | if (!asn1_read_uint8(data, &b)) | 
|---|
| 487 | return false; | 
|---|
| 488 | nesting->taglen = (nesting->taglen << 8) | b; | 
|---|
| 489 | n--; | 
|---|
| 490 | } | 
|---|
| 491 | } else { | 
|---|
| 492 | nesting->taglen = b; | 
|---|
| 493 | } | 
|---|
| 494 | nesting->start = data->ofs; | 
|---|
| 495 | nesting->next = data->nesting; | 
|---|
| 496 | data->nesting = nesting; | 
|---|
| 497 | if (asn1_tag_remaining(data) == -1) { | 
|---|
| 498 | return false; | 
|---|
| 499 | } | 
|---|
| 500 | return !data->has_error; | 
|---|
| 501 | } | 
|---|
| 502 |  | 
|---|
| 503 | /* stop reading a tag */ | 
|---|
| 504 | bool asn1_end_tag(struct asn1_data *data) | 
|---|
| 505 | { | 
|---|
| 506 | struct nesting *nesting; | 
|---|
| 507 |  | 
|---|
| 508 | /* make sure we read it all */ | 
|---|
| 509 | if (asn1_tag_remaining(data) != 0) { | 
|---|
| 510 | data->has_error = true; | 
|---|
| 511 | return false; | 
|---|
| 512 | } | 
|---|
| 513 |  | 
|---|
| 514 | nesting = data->nesting; | 
|---|
| 515 |  | 
|---|
| 516 | if (!nesting) { | 
|---|
| 517 | data->has_error = true; | 
|---|
| 518 | return false; | 
|---|
| 519 | } | 
|---|
| 520 |  | 
|---|
| 521 | data->nesting = nesting->next; | 
|---|
| 522 | talloc_free(nesting); | 
|---|
| 523 | return true; | 
|---|
| 524 | } | 
|---|
| 525 |  | 
|---|
| 526 | /* work out how many bytes are left in this nested tag */ | 
|---|
| 527 | int asn1_tag_remaining(struct asn1_data *data) | 
|---|
| 528 | { | 
|---|
| 529 | int remaining; | 
|---|
| 530 | if (data->has_error) { | 
|---|
| 531 | return -1; | 
|---|
| 532 | } | 
|---|
| 533 |  | 
|---|
| 534 | if (!data->nesting) { | 
|---|
| 535 | data->has_error = true; | 
|---|
| 536 | return -1; | 
|---|
| 537 | } | 
|---|
| 538 | remaining = data->nesting->taglen - (data->ofs - data->nesting->start); | 
|---|
| 539 | if (remaining > (data->length - data->ofs)) { | 
|---|
| 540 | data->has_error = true; | 
|---|
| 541 | return -1; | 
|---|
| 542 | } | 
|---|
| 543 | return remaining; | 
|---|
| 544 | } | 
|---|
| 545 |  | 
|---|
| 546 | /* read an object ID from a data blob */ | 
|---|
| 547 | bool ber_read_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, const char **OID) | 
|---|
| 548 | { | 
|---|
| 549 | int i; | 
|---|
| 550 | uint8_t *b; | 
|---|
| 551 | uint_t v; | 
|---|
| 552 | char *tmp_oid = NULL; | 
|---|
| 553 |  | 
|---|
| 554 | if (blob.length < 2) return false; | 
|---|
| 555 |  | 
|---|
| 556 | b = blob.data; | 
|---|
| 557 |  | 
|---|
| 558 | tmp_oid = talloc_asprintf(mem_ctx, "%u",  b[0]/40); | 
|---|
| 559 | if (!tmp_oid) goto nomem; | 
|---|
| 560 | tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u",  b[0]%40); | 
|---|
| 561 | if (!tmp_oid) goto nomem; | 
|---|
| 562 |  | 
|---|
| 563 | for(i = 1, v = 0; i < blob.length; i++) { | 
|---|
| 564 | v = (v<<7) | (b[i]&0x7f); | 
|---|
| 565 | if ( ! (b[i] & 0x80)) { | 
|---|
| 566 | tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u",  v); | 
|---|
| 567 | v = 0; | 
|---|
| 568 | } | 
|---|
| 569 | if (!tmp_oid) goto nomem; | 
|---|
| 570 | } | 
|---|
| 571 |  | 
|---|
| 572 | if (v != 0) { | 
|---|
| 573 | talloc_free(tmp_oid); | 
|---|
| 574 | return false; | 
|---|
| 575 | } | 
|---|
| 576 |  | 
|---|
| 577 | *OID = tmp_oid; | 
|---|
| 578 | return true; | 
|---|
| 579 |  | 
|---|
| 580 | nomem: | 
|---|
| 581 | return false; | 
|---|
| 582 | } | 
|---|
| 583 |  | 
|---|
| 584 | /* read an object ID from a ASN1 buffer */ | 
|---|
| 585 | bool asn1_read_OID(struct asn1_data *data, TALLOC_CTX *mem_ctx, const char **OID) | 
|---|
| 586 | { | 
|---|
| 587 | DATA_BLOB blob; | 
|---|
| 588 | int len; | 
|---|
| 589 |  | 
|---|
| 590 | if (!asn1_start_tag(data, ASN1_OID)) return false; | 
|---|
| 591 |  | 
|---|
| 592 | len = asn1_tag_remaining(data); | 
|---|
| 593 | if (len < 0) { | 
|---|
| 594 | data->has_error = true; | 
|---|
| 595 | return false; | 
|---|
| 596 | } | 
|---|
| 597 |  | 
|---|
| 598 | blob = data_blob(NULL, len); | 
|---|
| 599 | if (!blob.data) { | 
|---|
| 600 | data->has_error = true; | 
|---|
| 601 | return false; | 
|---|
| 602 | } | 
|---|
| 603 |  | 
|---|
| 604 | asn1_read(data, blob.data, len); | 
|---|
| 605 | asn1_end_tag(data); | 
|---|
| 606 | if (data->has_error) { | 
|---|
| 607 | data_blob_free(&blob); | 
|---|
| 608 | return false; | 
|---|
| 609 | } | 
|---|
| 610 |  | 
|---|
| 611 | if (!ber_read_OID_String(mem_ctx, blob, OID)) { | 
|---|
| 612 | data->has_error = true; | 
|---|
| 613 | data_blob_free(&blob); | 
|---|
| 614 | return false; | 
|---|
| 615 | } | 
|---|
| 616 |  | 
|---|
| 617 | data_blob_free(&blob); | 
|---|
| 618 | return true; | 
|---|
| 619 | } | 
|---|
| 620 |  | 
|---|
| 621 | /* check that the next object ID is correct */ | 
|---|
| 622 | bool asn1_check_OID(struct asn1_data *data, const char *OID) | 
|---|
| 623 | { | 
|---|
| 624 | const char *id; | 
|---|
| 625 |  | 
|---|
| 626 | if (!asn1_read_OID(data, data, &id)) return false; | 
|---|
| 627 |  | 
|---|
| 628 | if (strcmp(id, OID) != 0) { | 
|---|
| 629 | talloc_free(discard_const(id)); | 
|---|
| 630 | data->has_error = true; | 
|---|
| 631 | return false; | 
|---|
| 632 | } | 
|---|
| 633 | talloc_free(discard_const(id)); | 
|---|
| 634 | return true; | 
|---|
| 635 | } | 
|---|
| 636 |  | 
|---|
| 637 | /* read a LDAPString from a ASN1 buffer */ | 
|---|
| 638 | bool asn1_read_LDAPString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s) | 
|---|
| 639 | { | 
|---|
| 640 | int len; | 
|---|
| 641 | len = asn1_tag_remaining(data); | 
|---|
| 642 | if (len < 0) { | 
|---|
| 643 | data->has_error = true; | 
|---|
| 644 | return false; | 
|---|
| 645 | } | 
|---|
| 646 | *s = talloc_array(mem_ctx, char, len+1); | 
|---|
| 647 | if (! *s) { | 
|---|
| 648 | data->has_error = true; | 
|---|
| 649 | return false; | 
|---|
| 650 | } | 
|---|
| 651 | asn1_read(data, *s, len); | 
|---|
| 652 | (*s)[len] = 0; | 
|---|
| 653 | return !data->has_error; | 
|---|
| 654 | } | 
|---|
| 655 |  | 
|---|
| 656 |  | 
|---|
| 657 | /* read a GeneralString from a ASN1 buffer */ | 
|---|
| 658 | bool asn1_read_GeneralString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s) | 
|---|
| 659 | { | 
|---|
| 660 | if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) return false; | 
|---|
| 661 | if (!asn1_read_LDAPString(data, mem_ctx, s)) return false; | 
|---|
| 662 | return asn1_end_tag(data); | 
|---|
| 663 | } | 
|---|
| 664 |  | 
|---|
| 665 |  | 
|---|
| 666 | /* read a octet string blob */ | 
|---|
| 667 | bool asn1_read_OctetString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob) | 
|---|
| 668 | { | 
|---|
| 669 | int len; | 
|---|
| 670 | ZERO_STRUCTP(blob); | 
|---|
| 671 | if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return false; | 
|---|
| 672 | len = asn1_tag_remaining(data); | 
|---|
| 673 | if (len < 0) { | 
|---|
| 674 | data->has_error = true; | 
|---|
| 675 | return false; | 
|---|
| 676 | } | 
|---|
| 677 | *blob = data_blob_talloc(mem_ctx, NULL, len+1); | 
|---|
| 678 | if (!blob->data) { | 
|---|
| 679 | data->has_error = true; | 
|---|
| 680 | return false; | 
|---|
| 681 | } | 
|---|
| 682 | asn1_read(data, blob->data, len); | 
|---|
| 683 | asn1_end_tag(data); | 
|---|
| 684 | blob->length--; | 
|---|
| 685 | blob->data[len] = 0; | 
|---|
| 686 |  | 
|---|
| 687 | if (data->has_error) { | 
|---|
| 688 | data_blob_free(blob); | 
|---|
| 689 | *blob = data_blob_null; | 
|---|
| 690 | return false; | 
|---|
| 691 | } | 
|---|
| 692 | return true; | 
|---|
| 693 | } | 
|---|
| 694 |  | 
|---|
| 695 | bool asn1_read_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob) | 
|---|
| 696 | { | 
|---|
| 697 | int len; | 
|---|
| 698 | ZERO_STRUCTP(blob); | 
|---|
| 699 | if (!asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(num))) return false; | 
|---|
| 700 | len = asn1_tag_remaining(data); | 
|---|
| 701 | if (len < 0) { | 
|---|
| 702 | data->has_error = true; | 
|---|
| 703 | return false; | 
|---|
| 704 | } | 
|---|
| 705 | *blob = data_blob(NULL, len); | 
|---|
| 706 | if ((len != 0) && (!blob->data)) { | 
|---|
| 707 | data->has_error = true; | 
|---|
| 708 | return false; | 
|---|
| 709 | } | 
|---|
| 710 | asn1_read(data, blob->data, len); | 
|---|
| 711 | asn1_end_tag(data); | 
|---|
| 712 | return !data->has_error; | 
|---|
| 713 | } | 
|---|
| 714 |  | 
|---|
| 715 | /* read an integer without tag*/ | 
|---|
| 716 | bool asn1_read_implicit_Integer(struct asn1_data *data, int *i) | 
|---|
| 717 | { | 
|---|
| 718 | uint8_t b; | 
|---|
| 719 | bool first_byte = true; | 
|---|
| 720 | *i = 0; | 
|---|
| 721 |  | 
|---|
| 722 | while (!data->has_error && asn1_tag_remaining(data)>0) { | 
|---|
| 723 | if (!asn1_read_uint8(data, &b)) return false; | 
|---|
| 724 | if (first_byte) { | 
|---|
| 725 | if (b & 0x80) { | 
|---|
| 726 | /* Number is negative. | 
|---|
| 727 | Set i to -1 for sign extend. */ | 
|---|
| 728 | *i = -1; | 
|---|
| 729 | } | 
|---|
| 730 | first_byte = false; | 
|---|
| 731 | } | 
|---|
| 732 | *i = (*i << 8) + b; | 
|---|
| 733 | } | 
|---|
| 734 | return !data->has_error; | 
|---|
| 735 |  | 
|---|
| 736 | } | 
|---|
| 737 |  | 
|---|
| 738 | /* read an integer */ | 
|---|
| 739 | bool asn1_read_Integer(struct asn1_data *data, int *i) | 
|---|
| 740 | { | 
|---|
| 741 | *i = 0; | 
|---|
| 742 |  | 
|---|
| 743 | if (!asn1_start_tag(data, ASN1_INTEGER)) return false; | 
|---|
| 744 | if (!asn1_read_implicit_Integer(data, i)) return false; | 
|---|
| 745 | return asn1_end_tag(data); | 
|---|
| 746 | } | 
|---|
| 747 |  | 
|---|
| 748 | /* read a BIT STRING */ | 
|---|
| 749 | bool asn1_read_BitString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob, uint8_t *padding) | 
|---|
| 750 | { | 
|---|
| 751 | int len; | 
|---|
| 752 | ZERO_STRUCTP(blob); | 
|---|
| 753 | if (!asn1_start_tag(data, ASN1_BIT_STRING)) return false; | 
|---|
| 754 | len = asn1_tag_remaining(data); | 
|---|
| 755 | if (len < 0) { | 
|---|
| 756 | data->has_error = true; | 
|---|
| 757 | return false; | 
|---|
| 758 | } | 
|---|
| 759 | if (!asn1_read_uint8(data, padding)) return false; | 
|---|
| 760 |  | 
|---|
| 761 | *blob = data_blob_talloc(mem_ctx, NULL, len); | 
|---|
| 762 | if (!blob->data) { | 
|---|
| 763 | data->has_error = true; | 
|---|
| 764 | return false; | 
|---|
| 765 | } | 
|---|
| 766 | if (asn1_read(data, blob->data, len - 1)) { | 
|---|
| 767 | blob->length--; | 
|---|
| 768 | blob->data[len] = 0; | 
|---|
| 769 | asn1_end_tag(data); | 
|---|
| 770 | } | 
|---|
| 771 |  | 
|---|
| 772 | if (data->has_error) { | 
|---|
| 773 | data_blob_free(blob); | 
|---|
| 774 | *blob = data_blob_null; | 
|---|
| 775 | *padding = 0; | 
|---|
| 776 | return false; | 
|---|
| 777 | } | 
|---|
| 778 | return true; | 
|---|
| 779 | } | 
|---|
| 780 |  | 
|---|
| 781 | /* read an integer */ | 
|---|
| 782 | bool asn1_read_enumerated(struct asn1_data *data, int *v) | 
|---|
| 783 | { | 
|---|
| 784 | *v = 0; | 
|---|
| 785 |  | 
|---|
| 786 | if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false; | 
|---|
| 787 | while (!data->has_error && asn1_tag_remaining(data)>0) { | 
|---|
| 788 | uint8_t b; | 
|---|
| 789 | asn1_read_uint8(data, &b); | 
|---|
| 790 | *v = (*v << 8) + b; | 
|---|
| 791 | } | 
|---|
| 792 | return asn1_end_tag(data); | 
|---|
| 793 | } | 
|---|
| 794 |  | 
|---|
| 795 | /* check a enumerated value is correct */ | 
|---|
| 796 | bool asn1_check_enumerated(struct asn1_data *data, int v) | 
|---|
| 797 | { | 
|---|
| 798 | uint8_t b; | 
|---|
| 799 | if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false; | 
|---|
| 800 | asn1_read_uint8(data, &b); | 
|---|
| 801 | asn1_end_tag(data); | 
|---|
| 802 |  | 
|---|
| 803 | if (v != b) | 
|---|
| 804 | data->has_error = false; | 
|---|
| 805 |  | 
|---|
| 806 | return !data->has_error; | 
|---|
| 807 | } | 
|---|
| 808 |  | 
|---|
| 809 | /* write an enumerated value to the stream */ | 
|---|
| 810 | bool asn1_write_enumerated(struct asn1_data *data, uint8_t v) | 
|---|
| 811 | { | 
|---|
| 812 | if (!asn1_push_tag(data, ASN1_ENUMERATED)) return false; | 
|---|
| 813 | asn1_write_uint8(data, v); | 
|---|
| 814 | asn1_pop_tag(data); | 
|---|
| 815 | return !data->has_error; | 
|---|
| 816 | } | 
|---|
| 817 |  | 
|---|
| 818 | /* | 
|---|
| 819 | Get us the data just written without copying | 
|---|
| 820 | */ | 
|---|
| 821 | bool asn1_blob(const struct asn1_data *asn1, DATA_BLOB *blob) | 
|---|
| 822 | { | 
|---|
| 823 | if (asn1->has_error) { | 
|---|
| 824 | return false; | 
|---|
| 825 | } | 
|---|
| 826 | if (asn1->nesting != NULL) { | 
|---|
| 827 | return false; | 
|---|
| 828 | } | 
|---|
| 829 | blob->data = asn1->data; | 
|---|
| 830 | blob->length = asn1->length; | 
|---|
| 831 | return true; | 
|---|
| 832 | } | 
|---|
| 833 |  | 
|---|
| 834 | /* | 
|---|
| 835 | Fill in an asn1 struct without making a copy | 
|---|
| 836 | */ | 
|---|
| 837 | void asn1_load_nocopy(struct asn1_data *data, uint8_t *buf, size_t len) | 
|---|
| 838 | { | 
|---|
| 839 | ZERO_STRUCTP(data); | 
|---|
| 840 | data->data = buf; | 
|---|
| 841 | data->length = len; | 
|---|
| 842 | } | 
|---|
| 843 |  | 
|---|
| 844 | /* | 
|---|
| 845 | check if a ASN.1 blob is a full tag | 
|---|
| 846 | */ | 
|---|
| 847 | NTSTATUS asn1_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size) | 
|---|
| 848 | { | 
|---|
| 849 | struct asn1_data *asn1 = asn1_init(NULL); | 
|---|
| 850 | int size; | 
|---|
| 851 |  | 
|---|
| 852 | NT_STATUS_HAVE_NO_MEMORY(asn1); | 
|---|
| 853 |  | 
|---|
| 854 | asn1->data = blob.data; | 
|---|
| 855 | asn1->length = blob.length; | 
|---|
| 856 | asn1_start_tag(asn1, tag); | 
|---|
| 857 | if (asn1->has_error) { | 
|---|
| 858 | talloc_free(asn1); | 
|---|
| 859 | return STATUS_MORE_ENTRIES; | 
|---|
| 860 | } | 
|---|
| 861 | size = asn1_tag_remaining(asn1) + asn1->ofs; | 
|---|
| 862 |  | 
|---|
| 863 | talloc_free(asn1); | 
|---|
| 864 |  | 
|---|
| 865 | if (size > blob.length) { | 
|---|
| 866 | return STATUS_MORE_ENTRIES; | 
|---|
| 867 | } | 
|---|
| 868 |  | 
|---|
| 869 | *packet_size = size; | 
|---|
| 870 | return NT_STATUS_OK; | 
|---|
| 871 | } | 
|---|