[206] | 1 | /*
|
---|
| 2 | Unix SMB/CIFS implementation.
|
---|
| 3 | simple SPNEGO 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 |
|
---|
| 22 | /* free an asn1 structure */
|
---|
| 23 | void asn1_free(ASN1_DATA *data)
|
---|
| 24 | {
|
---|
| 25 | struct nesting *nesting = data->nesting;
|
---|
| 26 |
|
---|
| 27 | while (nesting) {
|
---|
| 28 | struct nesting *nnext = nesting->next;
|
---|
| 29 | free(nesting);
|
---|
| 30 | nesting = nnext;
|
---|
| 31 | };
|
---|
| 32 | data->nesting = NULL;
|
---|
| 33 | SAFE_FREE(data->data);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | /* write to the ASN1 buffer, advancing the buffer pointer */
|
---|
| 37 | bool asn1_write(ASN1_DATA *data, const void *p, int len)
|
---|
| 38 | {
|
---|
| 39 | if (data->has_error) return false;
|
---|
| 40 | if (data->length < data->ofs+len) {
|
---|
| 41 | data->data = SMB_REALLOC_ARRAY(data->data, unsigned char,
|
---|
| 42 | data->ofs+len);
|
---|
| 43 | if (!data->data) {
|
---|
| 44 | data->has_error = true;
|
---|
| 45 | return false;
|
---|
| 46 | }
|
---|
| 47 | data->length = data->ofs+len;
|
---|
| 48 | }
|
---|
| 49 | memcpy(data->data + data->ofs, p, len);
|
---|
| 50 | data->ofs += len;
|
---|
| 51 | return true;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | /* useful fn for writing a uint8 */
|
---|
| 55 | bool asn1_write_uint8(ASN1_DATA *data, uint8 v)
|
---|
| 56 | {
|
---|
| 57 | return asn1_write(data, &v, 1);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /* push a tag onto the asn1 data buffer. Used for nested structures */
|
---|
| 61 | bool asn1_push_tag(ASN1_DATA *data, uint8 tag)
|
---|
| 62 | {
|
---|
| 63 | struct nesting *nesting;
|
---|
| 64 |
|
---|
| 65 | asn1_write_uint8(data, tag);
|
---|
| 66 | nesting = SMB_MALLOC_P(struct nesting);
|
---|
| 67 | if (!nesting) {
|
---|
| 68 | data->has_error = true;
|
---|
| 69 | return false;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | nesting->start = data->ofs;
|
---|
| 73 | nesting->next = data->nesting;
|
---|
| 74 | data->nesting = nesting;
|
---|
| 75 | return asn1_write_uint8(data, 0xff);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /* pop a tag */
|
---|
| 79 | bool asn1_pop_tag(ASN1_DATA *data)
|
---|
| 80 | {
|
---|
| 81 | struct nesting *nesting;
|
---|
| 82 | size_t len;
|
---|
| 83 |
|
---|
| 84 | if (data->has_error) {
|
---|
| 85 | return false;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | nesting = data->nesting;
|
---|
| 89 |
|
---|
| 90 | if (!nesting) {
|
---|
| 91 | data->has_error = true;
|
---|
| 92 | return false;
|
---|
| 93 | }
|
---|
| 94 | len = data->ofs - (nesting->start+1);
|
---|
| 95 | /* yes, this is ugly. We don't know in advance how many bytes the length
|
---|
| 96 | of a tag will take, so we assumed 1 byte. If we were wrong then we
|
---|
| 97 | need to correct our mistake */
|
---|
| 98 | if (len > 0xFFFF) {
|
---|
| 99 | data->data[nesting->start] = 0x83;
|
---|
| 100 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 101 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 102 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 103 | memmove(data->data+nesting->start+4, data->data+nesting->start+1, len);
|
---|
| 104 | data->data[nesting->start+1] = (len>>16) & 0xFF;
|
---|
| 105 | data->data[nesting->start+2] = (len>>8) & 0xFF;
|
---|
| 106 | data->data[nesting->start+3] = len&0xff;
|
---|
| 107 | } else if (len > 255) {
|
---|
| 108 | data->data[nesting->start] = 0x82;
|
---|
| 109 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 110 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 111 | memmove(data->data+nesting->start+3, data->data+nesting->start+1, len);
|
---|
| 112 | data->data[nesting->start+1] = len>>8;
|
---|
| 113 | data->data[nesting->start+2] = len&0xff;
|
---|
| 114 | } else if (len > 127) {
|
---|
| 115 | data->data[nesting->start] = 0x81;
|
---|
| 116 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
| 117 | memmove(data->data+nesting->start+2, data->data+nesting->start+1, len);
|
---|
| 118 | data->data[nesting->start+1] = len;
|
---|
| 119 | } else {
|
---|
| 120 | data->data[nesting->start] = len;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | data->nesting = nesting->next;
|
---|
| 124 | free(nesting);
|
---|
| 125 | return true;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 | /* write an integer */
|
---|
| 130 | bool asn1_write_Integer(ASN1_DATA *data, int i)
|
---|
| 131 | {
|
---|
| 132 | if (!asn1_push_tag(data, ASN1_INTEGER)) return false;
|
---|
| 133 | do {
|
---|
| 134 | asn1_write_uint8(data, i);
|
---|
| 135 | i = i >> 8;
|
---|
| 136 | } while (i);
|
---|
| 137 | return asn1_pop_tag(data);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /* write an object ID to a ASN1 buffer */
|
---|
| 141 | bool asn1_write_OID(ASN1_DATA *data, const char *OID)
|
---|
| 142 | {
|
---|
| 143 | unsigned v, v2;
|
---|
| 144 | const char *p = (const char *)OID;
|
---|
| 145 | char *newp;
|
---|
| 146 |
|
---|
| 147 | if (!asn1_push_tag(data, ASN1_OID))
|
---|
| 148 | return false;
|
---|
| 149 | v = strtol(p, &newp, 10);
|
---|
| 150 | p = newp;
|
---|
| 151 | v2 = strtol(p, &newp, 10);
|
---|
| 152 | p = newp;
|
---|
| 153 | if (!asn1_write_uint8(data, 40*v + v2))
|
---|
| 154 | return false;
|
---|
| 155 |
|
---|
| 156 | while (*p) {
|
---|
| 157 | v = strtol(p, &newp, 10);
|
---|
| 158 | p = newp;
|
---|
| 159 | if (v >= (1<<28)) asn1_write_uint8(data, 0x80 | ((v>>28)&0xff));
|
---|
| 160 | if (v >= (1<<21)) asn1_write_uint8(data, 0x80 | ((v>>21)&0xff));
|
---|
| 161 | if (v >= (1<<14)) asn1_write_uint8(data, 0x80 | ((v>>14)&0xff));
|
---|
| 162 | if (v >= (1<<7)) asn1_write_uint8(data, 0x80 | ((v>>7)&0xff));
|
---|
| 163 | if (!asn1_write_uint8(data, v&0x7f))
|
---|
| 164 | return false;
|
---|
| 165 | }
|
---|
| 166 | return asn1_pop_tag(data);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /* write an octet string */
|
---|
| 170 | bool asn1_write_OctetString(ASN1_DATA *data, const void *p, size_t length)
|
---|
| 171 | {
|
---|
| 172 | asn1_push_tag(data, ASN1_OCTET_STRING);
|
---|
| 173 | asn1_write(data, p, length);
|
---|
| 174 | asn1_pop_tag(data);
|
---|
| 175 | return !data->has_error;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /* write a general string */
|
---|
| 179 | bool asn1_write_GeneralString(ASN1_DATA *data, const char *s)
|
---|
| 180 | {
|
---|
| 181 | asn1_push_tag(data, ASN1_GENERAL_STRING);
|
---|
| 182 | asn1_write(data, s, strlen(s));
|
---|
| 183 | asn1_pop_tag(data);
|
---|
| 184 | return !data->has_error;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /* write a BOOLEAN */
|
---|
| 188 | bool asn1_write_BOOLEAN(ASN1_DATA *data, bool v)
|
---|
| 189 | {
|
---|
| 190 | asn1_write_uint8(data, ASN1_BOOLEAN);
|
---|
| 191 | asn1_write_uint8(data, v);
|
---|
| 192 | return !data->has_error;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | /* write a BOOLEAN - hmm, I suspect this one is the correct one, and the
|
---|
| 196 | above boolean is bogus. Need to check */
|
---|
| 197 | bool asn1_write_BOOLEAN2(ASN1_DATA *data, bool v)
|
---|
| 198 | {
|
---|
| 199 | asn1_push_tag(data, ASN1_BOOLEAN);
|
---|
| 200 | asn1_write_uint8(data, v);
|
---|
| 201 | asn1_pop_tag(data);
|
---|
| 202 | return !data->has_error;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /* check a BOOLEAN */
|
---|
| 206 | bool asn1_check_BOOLEAN(ASN1_DATA *data, bool v)
|
---|
| 207 | {
|
---|
| 208 | uint8 b = 0;
|
---|
| 209 |
|
---|
| 210 | asn1_read_uint8(data, &b);
|
---|
| 211 | if (b != ASN1_BOOLEAN) {
|
---|
| 212 | data->has_error = true;
|
---|
| 213 | return false;
|
---|
| 214 | }
|
---|
| 215 | asn1_read_uint8(data, &b);
|
---|
| 216 | if (b != v) {
|
---|
| 217 | data->has_error = true;
|
---|
| 218 | return false;
|
---|
| 219 | }
|
---|
| 220 | return !data->has_error;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 |
|
---|
| 224 | /* load a ASN1_DATA structure with a lump of data, ready to be parsed */
|
---|
| 225 | bool asn1_load(ASN1_DATA *data, DATA_BLOB blob)
|
---|
| 226 | {
|
---|
| 227 | ZERO_STRUCTP(data);
|
---|
| 228 | data->data = (unsigned char *)memdup(blob.data, blob.length);
|
---|
| 229 | if (!data->data) {
|
---|
| 230 | data->has_error = true;
|
---|
| 231 | return false;
|
---|
| 232 | }
|
---|
| 233 | data->length = blob.length;
|
---|
| 234 | return true;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | /* read from a ASN1 buffer, advancing the buffer pointer */
|
---|
| 238 | bool asn1_read(ASN1_DATA *data, void *p, int len)
|
---|
| 239 | {
|
---|
| 240 | if (data->has_error)
|
---|
| 241 | return false;
|
---|
| 242 |
|
---|
| 243 | if (len < 0 || data->ofs + len < data->ofs || data->ofs + len < len) {
|
---|
| 244 | data->has_error = true;
|
---|
| 245 | return false;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | if (data->ofs + len > data->length) {
|
---|
| 249 | data->has_error = true;
|
---|
| 250 | return false;
|
---|
| 251 | }
|
---|
| 252 | memcpy(p, data->data + data->ofs, len);
|
---|
| 253 | data->ofs += len;
|
---|
| 254 | return true;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | /* read a uint8 from a ASN1 buffer */
|
---|
| 258 | bool asn1_read_uint8(ASN1_DATA *data, uint8 *v)
|
---|
| 259 | {
|
---|
| 260 | return asn1_read(data, v, 1);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /*
|
---|
| 264 | * Check thta the value of the ASN1 buffer at the current offset equals tag.
|
---|
| 265 | */
|
---|
| 266 | bool asn1_check_tag(ASN1_DATA *data, uint8 tag)
|
---|
| 267 | {
|
---|
| 268 | if (data->has_error || data->ofs >= data->length || data->ofs < 0) {
|
---|
| 269 | data->has_error = true;
|
---|
| 270 | return false;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | return (tag == data->data[data->ofs]);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | /* start reading a nested asn1 structure */
|
---|
| 277 | bool asn1_start_tag(ASN1_DATA *data, uint8 tag)
|
---|
| 278 | {
|
---|
| 279 | uint8 b;
|
---|
| 280 | struct nesting *nesting;
|
---|
| 281 |
|
---|
| 282 | if (!asn1_read_uint8(data, &b))
|
---|
| 283 | return false;
|
---|
| 284 |
|
---|
| 285 | if (b != tag) {
|
---|
| 286 | data->has_error = true;
|
---|
| 287 | return false;
|
---|
| 288 | }
|
---|
| 289 | nesting = SMB_MALLOC_P(struct nesting);
|
---|
| 290 | if (!nesting) {
|
---|
| 291 | data->has_error = true;
|
---|
| 292 | return false;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | if (!asn1_read_uint8(data, &b)) {
|
---|
| 296 | SAFE_FREE(nesting);
|
---|
| 297 | return false;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | if (b & 0x80) {
|
---|
| 301 | int n = b & 0x7f;
|
---|
| 302 | if (!asn1_read_uint8(data, &b)) {
|
---|
| 303 | SAFE_FREE(nesting);
|
---|
| 304 | return false;
|
---|
| 305 | }
|
---|
| 306 | nesting->taglen = b;
|
---|
| 307 | while (n > 1) {
|
---|
| 308 | if (!asn1_read_uint8(data, &b)) {
|
---|
| 309 | SAFE_FREE(nesting);
|
---|
| 310 | return false;
|
---|
| 311 | }
|
---|
| 312 | nesting->taglen = (nesting->taglen << 8) | b;
|
---|
| 313 | n--;
|
---|
| 314 | }
|
---|
| 315 | } else {
|
---|
| 316 | nesting->taglen = b;
|
---|
| 317 | }
|
---|
| 318 | nesting->start = data->ofs;
|
---|
| 319 | nesting->next = data->nesting;
|
---|
| 320 | data->nesting = nesting;
|
---|
| 321 | return !data->has_error;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 |
|
---|
| 325 | /* stop reading a tag */
|
---|
| 326 | bool asn1_end_tag(ASN1_DATA *data)
|
---|
| 327 | {
|
---|
| 328 | struct nesting *nesting;
|
---|
| 329 |
|
---|
| 330 | /* make sure we read it all */
|
---|
| 331 | if (asn1_tag_remaining(data) != 0) {
|
---|
| 332 | data->has_error = true;
|
---|
| 333 | return false;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | nesting = data->nesting;
|
---|
| 337 |
|
---|
| 338 | if (!nesting) {
|
---|
| 339 | data->has_error = true;
|
---|
| 340 | return false;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | data->nesting = nesting->next;
|
---|
| 344 | free(nesting);
|
---|
| 345 | return true;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | /* work out how many bytes are left in this nested tag */
|
---|
| 349 | int asn1_tag_remaining(ASN1_DATA *data)
|
---|
| 350 | {
|
---|
| 351 | if (data->has_error)
|
---|
| 352 | return 0;
|
---|
| 353 |
|
---|
| 354 | if (!data->nesting) {
|
---|
| 355 | data->has_error = true;
|
---|
| 356 | return -1;
|
---|
| 357 | }
|
---|
| 358 | return data->nesting->taglen - (data->ofs - data->nesting->start);
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | /* read an object ID from a ASN1 buffer */
|
---|
| 362 | bool asn1_read_OID(ASN1_DATA *data, char **OID)
|
---|
| 363 | {
|
---|
| 364 | uint8 b = 0;
|
---|
| 365 | char *oid_str = NULL;
|
---|
| 366 |
|
---|
| 367 | *OID = NULL;
|
---|
| 368 |
|
---|
| 369 | if (!asn1_start_tag(data, ASN1_OID)) {
|
---|
| 370 | return false;
|
---|
| 371 | }
|
---|
| 372 | asn1_read_uint8(data, &b);
|
---|
| 373 |
|
---|
| 374 | oid_str = talloc_asprintf(NULL,
|
---|
| 375 | "%u",
|
---|
| 376 | b/40);
|
---|
| 377 | if (!oid_str) {
|
---|
| 378 | data->has_error = true;
|
---|
| 379 | goto out;
|
---|
| 380 | }
|
---|
| 381 | oid_str = talloc_asprintf_append(oid_str,
|
---|
| 382 | " %u",
|
---|
| 383 | b%40);
|
---|
| 384 | if (!oid_str) {
|
---|
| 385 | data->has_error = true;
|
---|
| 386 | goto out;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | while (asn1_tag_remaining(data) > 0) {
|
---|
| 390 | unsigned v = 0;
|
---|
| 391 | do {
|
---|
| 392 | asn1_read_uint8(data, &b);
|
---|
| 393 | v = (v<<7) | (b&0x7f);
|
---|
| 394 | } while (!data->has_error && b & 0x80);
|
---|
| 395 | oid_str = talloc_asprintf_append(oid_str,
|
---|
| 396 | " %u",
|
---|
| 397 | v);
|
---|
| 398 | if (!oid_str) {
|
---|
| 399 | data->has_error = true;
|
---|
| 400 | goto out;
|
---|
| 401 | }
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | out:
|
---|
| 405 |
|
---|
| 406 | asn1_end_tag(data);
|
---|
| 407 |
|
---|
| 408 | if (!data->has_error) {
|
---|
| 409 | *OID = SMB_STRDUP(oid_str);
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | TALLOC_FREE(oid_str);
|
---|
| 413 |
|
---|
| 414 | return !data->has_error;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | /* check that the next object ID is correct */
|
---|
| 418 | bool asn1_check_OID(ASN1_DATA *data, const char *OID)
|
---|
| 419 | {
|
---|
| 420 | char *id;
|
---|
| 421 |
|
---|
| 422 | if (!asn1_read_OID(data, &id)) {
|
---|
| 423 | return false;
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | if (strcmp(id, OID) != 0) {
|
---|
| 427 | data->has_error = true;
|
---|
| 428 | free(id);
|
---|
| 429 | return false;
|
---|
| 430 | }
|
---|
| 431 | free(id);
|
---|
| 432 | return true;
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | /* read a GeneralString from a ASN1 buffer */
|
---|
| 436 | bool asn1_read_GeneralString(ASN1_DATA *data, char **s)
|
---|
| 437 | {
|
---|
| 438 | int len;
|
---|
| 439 | char *str;
|
---|
| 440 |
|
---|
| 441 | *s = NULL;
|
---|
| 442 |
|
---|
| 443 | if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) {
|
---|
| 444 | return false;
|
---|
| 445 | }
|
---|
| 446 | len = asn1_tag_remaining(data);
|
---|
| 447 | if (len < 0) {
|
---|
| 448 | data->has_error = true;
|
---|
| 449 | return false;
|
---|
| 450 | }
|
---|
| 451 | str = SMB_MALLOC_ARRAY(char, len+1);
|
---|
| 452 | if (!str) {
|
---|
| 453 | data->has_error = true;
|
---|
| 454 | return false;
|
---|
| 455 | }
|
---|
| 456 | asn1_read(data, str, len);
|
---|
| 457 | str[len] = 0;
|
---|
| 458 | asn1_end_tag(data);
|
---|
| 459 |
|
---|
| 460 | if (!data->has_error) {
|
---|
| 461 | *s = str;
|
---|
| 462 | }
|
---|
| 463 | return !data->has_error;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | /* read a octet string blob */
|
---|
| 467 | bool asn1_read_OctetString(ASN1_DATA *data, DATA_BLOB *blob)
|
---|
| 468 | {
|
---|
| 469 | int len;
|
---|
| 470 | ZERO_STRUCTP(blob);
|
---|
| 471 | if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return false;
|
---|
| 472 | len = asn1_tag_remaining(data);
|
---|
| 473 | if (len < 0) {
|
---|
| 474 | data->has_error = true;
|
---|
| 475 | return false;
|
---|
| 476 | }
|
---|
| 477 | *blob = data_blob(NULL, len);
|
---|
| 478 | asn1_read(data, blob->data, len);
|
---|
| 479 | asn1_end_tag(data);
|
---|
| 480 | return !data->has_error;
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | /* read an interger */
|
---|
| 484 | bool asn1_read_Integer(ASN1_DATA *data, int *i)
|
---|
| 485 | {
|
---|
| 486 | uint8 b;
|
---|
| 487 | *i = 0;
|
---|
| 488 |
|
---|
| 489 | if (!asn1_start_tag(data, ASN1_INTEGER)) return false;
|
---|
| 490 | while (asn1_tag_remaining(data)>0) {
|
---|
| 491 | asn1_read_uint8(data, &b);
|
---|
| 492 | *i = (*i << 8) + b;
|
---|
| 493 | }
|
---|
| 494 | return asn1_end_tag(data);
|
---|
| 495 |
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | /* check a enumarted value is correct */
|
---|
| 499 | bool asn1_check_enumerated(ASN1_DATA *data, int v)
|
---|
| 500 | {
|
---|
| 501 | uint8 b;
|
---|
| 502 | if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
|
---|
| 503 | asn1_read_uint8(data, &b);
|
---|
| 504 | asn1_end_tag(data);
|
---|
| 505 |
|
---|
| 506 | if (v != b)
|
---|
| 507 | data->has_error = false;
|
---|
| 508 |
|
---|
| 509 | return !data->has_error;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | /* write an enumarted value to the stream */
|
---|
| 513 | bool asn1_write_enumerated(ASN1_DATA *data, uint8 v)
|
---|
| 514 | {
|
---|
| 515 | if (!asn1_push_tag(data, ASN1_ENUMERATED)) return false;
|
---|
| 516 | asn1_write_uint8(data, v);
|
---|
| 517 | asn1_pop_tag(data);
|
---|
| 518 | return !data->has_error;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | bool ber_write_OID_String(DATA_BLOB *blob, const char *OID)
|
---|
| 522 | {
|
---|
| 523 | uint_t v, v2;
|
---|
| 524 | const char *p = (const char *)OID;
|
---|
| 525 | char *newp;
|
---|
| 526 | int i;
|
---|
| 527 |
|
---|
| 528 | v = strtoul(p, &newp, 10);
|
---|
| 529 | if (newp[0] != '.') return false;
|
---|
| 530 | p = newp + 1;
|
---|
| 531 |
|
---|
| 532 | v2 = strtoul(p, &newp, 10);
|
---|
| 533 | if (newp[0] != '.') return false;
|
---|
| 534 | p = newp + 1;
|
---|
| 535 |
|
---|
| 536 | /*the ber representation can't use more space then the string one */
|
---|
| 537 | *blob = data_blob(NULL, strlen(OID));
|
---|
| 538 | if (!blob->data) return false;
|
---|
| 539 |
|
---|
| 540 | blob->data[0] = 40*v + v2;
|
---|
| 541 |
|
---|
| 542 | i = 1;
|
---|
| 543 | while (*p) {
|
---|
| 544 | v = strtoul(p, &newp, 10);
|
---|
| 545 | if (newp[0] == '.') {
|
---|
| 546 | p = newp + 1;
|
---|
| 547 | } else if (newp[0] == '\0') {
|
---|
| 548 | p = newp;
|
---|
| 549 | } else {
|
---|
| 550 | data_blob_free(blob);
|
---|
| 551 | return false;
|
---|
| 552 | }
|
---|
| 553 | if (v >= (1<<28)) blob->data[i++] = (0x80 | ((v>>28)&0x7f));
|
---|
| 554 | if (v >= (1<<21)) blob->data[i++] = (0x80 | ((v>>21)&0x7f));
|
---|
| 555 | if (v >= (1<<14)) blob->data[i++] = (0x80 | ((v>>14)&0x7f));
|
---|
| 556 | if (v >= (1<<7)) blob->data[i++] = (0x80 | ((v>>7)&0x7f));
|
---|
| 557 | blob->data[i++] = (v&0x7f);
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 | blob->length = i;
|
---|
| 561 |
|
---|
| 562 | return true;
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 | /* read an object ID from a data blob */
|
---|
| 566 | bool ber_read_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, const char **OID)
|
---|
| 567 | {
|
---|
| 568 | int i;
|
---|
| 569 | uint8_t *b;
|
---|
| 570 | uint_t v;
|
---|
| 571 | char *tmp_oid = NULL;
|
---|
| 572 |
|
---|
| 573 | if (blob.length < 2) return false;
|
---|
| 574 |
|
---|
| 575 | b = blob.data;
|
---|
| 576 |
|
---|
| 577 | tmp_oid = talloc_asprintf(mem_ctx, "%u", b[0]/40);
|
---|
| 578 | if (!tmp_oid) goto nomem;
|
---|
| 579 | tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", b[0]%40);
|
---|
| 580 | if (!tmp_oid) goto nomem;
|
---|
| 581 |
|
---|
| 582 | for(i = 1, v = 0; i < blob.length; i++) {
|
---|
| 583 | v = (v<<7) | (b[i]&0x7f);
|
---|
| 584 | if ( ! (b[i] & 0x80)) {
|
---|
| 585 | tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", v);
|
---|
| 586 | v = 0;
|
---|
| 587 | }
|
---|
| 588 | if (!tmp_oid) goto nomem;
|
---|
| 589 | }
|
---|
| 590 |
|
---|
| 591 | if (v != 0) {
|
---|
| 592 | talloc_free(tmp_oid);
|
---|
| 593 | return false;
|
---|
| 594 | }
|
---|
| 595 |
|
---|
| 596 | *OID = tmp_oid;
|
---|
| 597 | return true;
|
---|
| 598 |
|
---|
| 599 | nomem:
|
---|
| 600 | return false;
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 |
|
---|