1 /*- 2 * Copyright (c) 2009 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Alistair Crooks (agc@NetBSD.org) 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 /* 30 * Copyright (c) 2005-2008 Nominet UK (www.nic.uk) 31 * All rights reserved. 32 * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted 33 * their moral rights under the UK Copyright Design and Patents Act 1988 to 34 * be recorded as the authors of this copyright work. 35 * 36 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 37 * use this file except in compliance with the License. 38 * 39 * You may obtain a copy of the License at 40 * http://www.apache.org/licenses/LICENSE-2.0 41 * 42 * Unless required by applicable law or agreed to in writing, software 43 * distributed under the License is distributed on an "AS IS" BASIS, 44 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 45 * 46 * See the License for the specific language governing permissions and 47 * limitations under the License. 48 */ 49 50 /** \file 51 * \brief Parser for OpenPGP packets 52 */ 53 #include "config.h" 54 55 #ifdef HAVE_OPENSSL_CAST_H 56 #include <openssl/cast.h> 57 #endif 58 59 #include "packet.h" 60 #include "packet-parse.h" 61 #include "keyring.h" 62 #include "errors.h" 63 #include "packet-show.h" 64 #include "create.h" 65 66 #include "readerwriter.h" 67 #include "netpgpdefs.h" 68 #include "parse_local.h" 69 70 #include <stdarg.h> 71 #include <stdlib.h> 72 #include <string.h> 73 74 #ifdef HAVE_UNISTD_H 75 #include <unistd.h> 76 #endif 77 78 #include <errno.h> 79 80 #ifdef HAVE_LIMITS_H 81 #include <limits.h> 82 #endif 83 84 #define ERRP(cbinfo, cont, err) do { \ 85 cont.u.error.error = err; \ 86 CALLBACK(cbinfo, OPS_PARSER_ERROR, &cont); \ 87 return false; \ 88 /*NOTREACHED*/ \ 89 } while(/*CONSTCOND*/0) 90 91 /** 92 * limited_read_data reads the specified amount of the subregion's data 93 * into a data_t structure 94 * 95 * \param data Empty structure which will be filled with data 96 * \param len Number of octets to read 97 * \param subregion 98 * \param pinfo How to parse 99 * 100 * \return 1 on success, 0 on failure 101 */ 102 static int 103 limited_read_data(__ops_data_t * data, unsigned int len, 104 __ops_region_t * subregion, __ops_parseinfo_t * pinfo) 105 { 106 data->len = len; 107 108 if (subregion->length - subregion->length_read < len) { 109 (void) fprintf(stderr, "limited_data_read: bad length\n"); 110 return 0; 111 } 112 113 data->contents = calloc(1, data->len); 114 if (!data->contents) 115 return 0; 116 117 if (!__ops_limited_read(data->contents, data->len, subregion, &pinfo->errors, 118 &pinfo->readinfo, &pinfo->cbinfo)) 119 return 0; 120 121 return 1; 122 } 123 124 /** 125 * read_data reads the remainder of the subregion's data 126 * into a data_t structure 127 * 128 * \param data 129 * \param subregion 130 * \param pinfo 131 * 132 * \return 1 on success, 0 on failure 133 */ 134 static int 135 read_data(__ops_data_t * data, __ops_region_t * subregion, 136 __ops_parseinfo_t * pinfo) 137 { 138 int len; 139 140 len = subregion->length - subregion->length_read; 141 142 if (len >= 0) { 143 return (limited_read_data(data, (unsigned)len, subregion, pinfo)); 144 } 145 return 0; 146 } 147 148 /** 149 * Reads the remainder of the subregion as a string. 150 * It is the user's responsibility to free the memory allocated here. 151 */ 152 153 static int 154 read_unsigned_string(unsigned char **str, __ops_region_t * subregion, 155 __ops_parseinfo_t * pinfo) 156 { 157 size_t len = 0; 158 159 len = subregion->length - subregion->length_read; 160 161 *str = calloc(1, len + 1); 162 if (!(*str)) 163 return 0; 164 165 if (len && !__ops_limited_read(*str, len, subregion, &pinfo->errors, 166 &pinfo->readinfo, &pinfo->cbinfo)) 167 return 0; 168 169 /* ! ensure the string is NULL-terminated */ 170 171 (*str)[len] = '\0'; 172 173 return 1; 174 } 175 176 static int 177 read_string(char **str, __ops_region_t * subregion, __ops_parseinfo_t * pinfo) 178 { 179 return (read_unsigned_string((unsigned char **) str, subregion, pinfo)); 180 } 181 182 void 183 __ops_init_subregion(__ops_region_t * subregion, __ops_region_t * region) 184 { 185 (void) memset(subregion, 0x0, sizeof(*subregion)); 186 subregion->parent = region; 187 } 188 189 /* 190 * XXX: replace __ops_ptag_t with something more appropriate for limiting reads 191 */ 192 193 /** 194 * low-level function to read data from reader function 195 * 196 * Use this function, rather than calling the reader directly. 197 * 198 * If the accumulate flag is set in *pinfo, the function 199 * adds the read data to the accumulated data, and updates 200 * the accumulated length. This is useful if, for example, 201 * the application wants access to the raw data as well as the 202 * parsed data. 203 * 204 * This function will also try to read the entire amount asked for, but not 205 * if it is over INT_MAX. Obviously many callers will know that they 206 * never ask for that much and so can avoid the extra complexity of 207 * dealing with return codes and filled-in lengths. 208 * 209 * \param *dest 210 * \param *plength 211 * \param flags 212 * \param *pinfo 213 * 214 * \return OPS_R_OK 215 * \return OPS_R_PARTIAL_READ 216 * \return OPS_R_EOF 217 * \return OPS_R_EARLY_EOF 218 * 219 * \sa #__ops_reader_ret_t for details of return codes 220 */ 221 222 static int 223 sub_base_read(void *dest, size_t length, __ops_error_t ** errors, 224 __ops_reader_t * readinfo, __ops_callback_data_t * cbinfo) 225 { 226 size_t n; 227 228 /* reading more than this would look like an error */ 229 if (length > INT_MAX) 230 length = INT_MAX; 231 232 for (n = 0; n < length;) { 233 int r = readinfo->reader((char *) dest + n, length - n, errors, readinfo, cbinfo); 234 235 if (r > (int) (length - n)) { 236 (void) fprintf(stderr, "sub_base_read: bad read\n"); 237 return 0; 238 } 239 240 /* 241 * XXX: should we save the error and return what was read so 242 * far? 243 */ 244 if (r < 0) 245 return r; 246 247 if (r == 0) 248 break; 249 250 n += r; 251 } 252 253 if (n == 0) 254 return 0; 255 256 if (readinfo->accumulate) { 257 if (readinfo->asize < readinfo->alength) { 258 (void) fprintf(stderr, "sub_base_read: bad size\n"); 259 return 0; 260 } 261 if (readinfo->alength + n > readinfo->asize) { 262 readinfo->asize = readinfo->asize * 2 + n; 263 readinfo->accumulated = realloc(readinfo->accumulated, readinfo->asize); 264 } 265 if (readinfo->asize < readinfo->alength + n) { 266 (void) fprintf(stderr, "sub_base_read: bad realloc\n"); 267 return 0; 268 } 269 (void) memcpy(readinfo->accumulated + readinfo->alength, dest, n); 270 } 271 /* we track length anyway, because it is used for packet offsets */ 272 readinfo->alength += n; 273 /* and also the position */ 274 readinfo->position += n; 275 276 return n; 277 } 278 279 int 280 __ops_stacked_read(void *dest, size_t length, __ops_error_t ** errors, 281 __ops_reader_t * readinfo, __ops_callback_data_t * cbinfo) 282 { 283 return sub_base_read(dest, length, errors, readinfo->next, cbinfo); 284 } 285 286 /* This will do a full read so long as length < MAX_INT */ 287 static int 288 base_read(unsigned char *dest, size_t length, 289 __ops_parseinfo_t * pinfo) 290 { 291 return sub_base_read(dest, length, &pinfo->errors, &pinfo->readinfo, 292 &pinfo->cbinfo); 293 } 294 295 /* 296 * Read a full size_t's worth. If the return is < than length, then 297 * *last_read tells you why - < 0 for an error, == 0 for EOF 298 */ 299 300 static size_t 301 full_read(unsigned char *dest, size_t length, int *last_read, 302 __ops_error_t ** errors, __ops_reader_t * readinfo, 303 __ops_callback_data_t * cbinfo) 304 { 305 size_t t; 306 int r = 0; /* preset in case some loon calls with length 307 * == 0 */ 308 309 for (t = 0; t < length;) { 310 r = sub_base_read(dest + t, length - t, errors, readinfo, cbinfo); 311 312 if (r <= 0) { 313 *last_read = r; 314 return t; 315 } 316 t += r; 317 } 318 319 *last_read = r; 320 321 return t; 322 } 323 324 325 326 /** Read a scalar value of selected length from reader. 327 * 328 * Read an unsigned scalar value from reader in Big Endian representation. 329 * 330 * This function does not know or care about packet boundaries. It 331 * also assumes that an EOF is an error. 332 * 333 * \param *result The scalar value is stored here 334 * \param *reader Our reader 335 * \param length How many bytes to read 336 * \return true on success, false on failure 337 */ 338 static bool 339 _read_scalar(unsigned *result, unsigned length, 340 __ops_parseinfo_t * pinfo) 341 { 342 unsigned t = 0; 343 344 if (length > sizeof(*result)) { 345 (void) fprintf(stderr, "_read_scalar: bad length\n"); 346 return 0; 347 } 348 349 while (length--) { 350 unsigned char c[1]; 351 int r; 352 353 r = base_read(c, 1, pinfo); 354 if (r != 1) 355 return false; 356 t = (t << 8) + c[0]; 357 } 358 359 *result = t; 360 return true; 361 } 362 363 /** 364 * \ingroup Core_ReadPackets 365 * \brief Read bytes from a region within the packet. 366 * 367 * Read length bytes into the buffer pointed to by *dest. 368 * Make sure we do not read over the packet boundary. 369 * Updates the Packet Tag's __ops_ptag_t::length_read. 370 * 371 * If length would make us read over the packet boundary, or if 372 * reading fails, we call the callback with an error. 373 * 374 * Note that if the region is indeterminate, this can return a short 375 * read - check region->last_read for the length. EOF is indicated by 376 * a success return and region->last_read == 0 in this case (for a 377 * region of known length, EOF is an error). 378 * 379 * This function makes sure to respect packet boundaries. 380 * 381 * \param dest The destination buffer 382 * \param length How many bytes to read 383 * \param region Pointer to packet region 384 * \param errors Error stack 385 * \param readinfo Reader info 386 * \param cbinfo Callback info 387 * \return true on success, false on error 388 */ 389 bool 390 __ops_limited_read(unsigned char *dest, size_t length, 391 __ops_region_t * region, __ops_error_t ** errors, 392 __ops_reader_t * readinfo, 393 __ops_callback_data_t * cbinfo) 394 { 395 size_t r; 396 int lr; 397 398 if (!region->indeterminate && region->length_read + length > region->length) { 399 OPS_ERROR(errors, OPS_E_P_NOT_ENOUGH_DATA, "Not enough data"); 400 return false; 401 } 402 r = full_read(dest, length, &lr, errors, readinfo, cbinfo); 403 404 if (lr < 0) { 405 OPS_ERROR(errors, OPS_E_R_READ_FAILED, "Read failed"); 406 return false; 407 } 408 if (!region->indeterminate && r != length) { 409 OPS_ERROR(errors, OPS_E_R_READ_FAILED, "Read failed"); 410 return false; 411 } 412 region->last_read = r; 413 do { 414 region->length_read += r; 415 if (region->parent && region->length > region->parent->length) { 416 (void) fprintf(stderr, 417 "ops_limited_read: bad length\n"); 418 return false; 419 } 420 } while ((region = region->parent) != NULL); 421 return true; 422 } 423 424 /** 425 \ingroup Core_ReadPackets 426 \brief Call __ops_limited_read on next in stack 427 */ 428 bool 429 __ops_stacked_limited_read(unsigned char *dest, unsigned length, 430 __ops_region_t * region, 431 __ops_error_t ** errors, 432 __ops_reader_t * readinfo, 433 __ops_callback_data_t * cbinfo) 434 { 435 return __ops_limited_read(dest, length, region, errors, readinfo->next, cbinfo); 436 } 437 438 static bool 439 limited_read(unsigned char *dest, unsigned length, 440 __ops_region_t * region, __ops_parseinfo_t * info) 441 { 442 return __ops_limited_read(dest, length, region, &info->errors, 443 &info->readinfo, &info->cbinfo); 444 } 445 446 static bool 447 exact_limited_read(unsigned char *dest, unsigned length, 448 __ops_region_t * region, 449 __ops_parseinfo_t * pinfo) 450 { 451 bool ret; 452 453 pinfo->exact_read = true; 454 ret = limited_read(dest, length, region, pinfo); 455 pinfo->exact_read = false; 456 457 return ret; 458 } 459 460 /** Skip over length bytes of this packet. 461 * 462 * Calls limited_read() to skip over some data. 463 * 464 * This function makes sure to respect packet boundaries. 465 * 466 * \param length How many bytes to skip 467 * \param *region Pointer to packet region 468 * \param *pinfo How to parse 469 * \return 1 on success, 0 on error (calls the cb with OPS_PARSER_ERROR in limited_read()). 470 */ 471 static int 472 limited_skip(unsigned length, __ops_region_t * region, 473 __ops_parseinfo_t * pinfo) 474 { 475 unsigned char buf[NETPGP_BUFSIZ]; 476 477 while (length) { 478 unsigned n = length % NETPGP_BUFSIZ; 479 480 if (!limited_read(buf, n, region, pinfo)) 481 return 0; 482 length -= n; 483 } 484 return 1; 485 } 486 487 /** Read a scalar. 488 * 489 * Read a big-endian scalar of length bytes, respecting packet 490 * boundaries (by calling limited_read() to read the raw data). 491 * 492 * This function makes sure to respect packet boundaries. 493 * 494 * \param *dest The scalar value is stored here 495 * \param length How many bytes make up this scalar (at most 4) 496 * \param *region Pointer to current packet region 497 * \param *pinfo How to parse 498 * \param *cb The callback 499 * \return 1 on success, 0 on error (calls the cb with OPS_PARSER_ERROR in limited_read()). 500 * 501 * \see RFC4880 3.1 502 */ 503 static int 504 limited_read_scalar(unsigned *dest, unsigned length, 505 __ops_region_t * region, 506 __ops_parseinfo_t * pinfo) 507 { 508 unsigned char c[4] = ""; 509 unsigned t; 510 unsigned n; 511 512 if (length > 4) { 513 (void) fprintf(stderr, "limited_read_scalar: bad length\n"); 514 return 0; 515 } 516 /*LINTED*/ 517 if (/*CONSTCOND*/sizeof(*dest) < 4) { 518 (void) fprintf(stderr, "limited_read_scalar: bad dest\n"); 519 return 0; 520 } 521 if (!limited_read(c, length, region, pinfo)) 522 return 0; 523 524 for (t = 0, n = 0; n < length; ++n) 525 t = (t << 8) + c[n]; 526 *dest = t; 527 528 return 1; 529 } 530 531 /** Read a scalar. 532 * 533 * Read a big-endian scalar of length bytes, respecting packet 534 * boundaries (by calling limited_read() to read the raw data). 535 * 536 * The value read is stored in a size_t, which is a different size 537 * from an unsigned on some platforms. 538 * 539 * This function makes sure to respect packet boundaries. 540 * 541 * \param *dest The scalar value is stored here 542 * \param length How many bytes make up this scalar (at most 4) 543 * \param *region Pointer to current packet region 544 * \param *pinfo How to parse 545 * \param *cb The callback 546 * \return 1 on success, 0 on error (calls the cb with OPS_PARSER_ERROR in limited_read()). 547 * 548 * \see RFC4880 3.1 549 */ 550 static int 551 limited_read_size_t_scalar(size_t * dest, unsigned length, 552 __ops_region_t * region, 553 __ops_parseinfo_t * pinfo) 554 { 555 unsigned tmp; 556 557 /* 558 * Note that because the scalar is at most 4 bytes, we don't care if 559 * size_t is bigger than usigned 560 */ 561 if (!limited_read_scalar(&tmp, length, region, pinfo)) 562 return 0; 563 564 *dest = tmp; 565 return 1; 566 } 567 568 /** Read a timestamp. 569 * 570 * Timestamps in OpenPGP are unix time, i.e. seconds since The Epoch (1.1.1970). They are stored in an unsigned scalar 571 * of 4 bytes. 572 * 573 * This function reads the timestamp using limited_read_scalar(). 574 * 575 * This function makes sure to respect packet boundaries. 576 * 577 * \param *dest The timestamp is stored here 578 * \param *ptag Pointer to current packet's Packet Tag. 579 * \param *reader Our reader 580 * \param *cb The callback 581 * \return see limited_read_scalar() 582 * 583 * \see RFC4880 3.5 584 */ 585 static int 586 limited_read_time(time_t *dest, __ops_region_t * region, 587 __ops_parseinfo_t * pinfo) 588 { 589 unsigned char c[1]; 590 time_t mytime = 0; 591 int i = 0; 592 593 /* 594 * Cannot assume that time_t is 4 octets long - 595 * SunOS 5.10 and NetBSD both have 64-bit time_ts. 596 */ 597 if (/* CONSTCOND */sizeof(time_t) == 4) { 598 return limited_read_scalar((unsigned *)(void *)dest, 4, region, pinfo); 599 } 600 for (i = 0; i < 4; i++) { 601 if (!limited_read(c, 1, region, pinfo)) 602 return 0; 603 mytime = (mytime << 8) + c[0]; 604 } 605 *dest = mytime; 606 return 1; 607 } 608 609 /** 610 * \ingroup Core_MPI 611 * Read a multiprecision integer. 612 * 613 * Large numbers (multiprecision integers, MPI) are stored in OpenPGP in two parts. First there is a 2 byte scalar 614 * indicating the length of the following MPI in Bits. Then follow the bits that make up the actual number, most 615 * significant bits first (Big Endian). The most significant bit in the MPI is supposed to be 1 (unless the MPI is 616 * encrypted - then it may be different as the bit count refers to the plain text but the bits are encrypted). 617 * 618 * Unused bits (i.e. those filling up the most significant byte from the left to the first bits that counts) are 619 * supposed to be cleared - I guess. XXX - does anything actually say so? 620 * 621 * This function makes sure to respect packet boundaries. 622 * 623 * \param **pgn return the integer there - the BIGNUM is created by BN_bin2bn() and probably needs to be freed 624 * by the caller XXX right ben? 625 * \param *ptag Pointer to current packet's Packet Tag. 626 * \param *reader Our reader 627 * \param *cb The callback 628 * \return 1 on success, 0 on error (by limited_read_scalar() or limited_read() or if the MPI is not properly formed (XXX 629 * see comment below - the callback is called with a OPS_PARSER_ERROR in case of an error) 630 * 631 * \see RFC4880 3.2 632 */ 633 static int 634 limited_read_mpi(BIGNUM ** pbn, __ops_region_t * region, 635 __ops_parseinfo_t * pinfo) 636 { 637 unsigned char buf[NETPGP_BUFSIZ] = ""; 638 /* an MPI has a 2 byte length part. 639 * Length is given in bits, so the 640 * largest we should ever need for 641 * the buffer is NETPGP_BUFSIZ bytes. */ 642 unsigned length; 643 unsigned nonzero; 644 bool ret; 645 646 pinfo->reading_mpi_len = true; 647 ret = limited_read_scalar(&length, 2, region, pinfo); 648 649 pinfo->reading_mpi_len = false; 650 if (!ret) 651 return 0; 652 653 nonzero = length & 7; /* there should be this many zero bits in the 654 * MS byte */ 655 if (!nonzero) 656 nonzero = 8; 657 length = (length + 7) / 8; 658 659 if (length == 0) { 660 /* if we try to read a length of 0, then fail */ 661 if (__ops_get_debug_level(__FILE__)) { 662 (void) fprintf(stderr, "limited_read_mpi: 0 length\n"); 663 } 664 return 0; 665 } 666 if (length > NETPGP_BUFSIZ) { 667 (void) fprintf(stderr, "limited_read_mpi: bad length\n"); 668 return 0; 669 } 670 if (!limited_read(buf, length, region, pinfo)) 671 return 0; 672 673 if (((unsigned)buf[0] >> nonzero) != 0 || 674 !((unsigned)buf[0] & (1U << (nonzero - 1U)))) { 675 OPS_ERROR(&pinfo->errors, OPS_E_P_MPI_FORMAT_ERROR, "MPI Format error"); 676 /* XXX: Ben, one part of 677 * this constraint does 678 * not apply to 679 * encrypted MPIs the 680 * draft says. -- peter */ 681 return 0; 682 } 683 *pbn = BN_bin2bn(buf, (int)length, NULL); 684 return 1; 685 } 686 687 /** Read some data with a New-Format length from reader. 688 * 689 * \sa Internet-Draft RFC4880.txt Section 4.2.2 690 * 691 * \param *length Where the decoded length will be put 692 * \param *pinfo How to parse 693 * \return true if OK, else false 694 * 695 */ 696 697 static bool 698 read_new_length(unsigned *length, __ops_parseinfo_t * pinfo) 699 { 700 unsigned char c[1]; 701 702 if (base_read(c, 1, pinfo) != 1) 703 return false; 704 if (c[0] < 192) { 705 /* 1. One-octet packet */ 706 *length = c[0]; 707 return true; 708 } else if (c[0] >= 192 && c[0] <= 223) { 709 /* 2. Two-octet packet */ 710 unsigned t = (c[0] - 192) << 8; 711 712 if (base_read(c, 1, pinfo) != 1) 713 return false; 714 *length = t + c[0] + 192; 715 return true; 716 } else if (c[0] == 255) { 717 /* 3. Five-Octet packet */ 718 return _read_scalar(length, 4, pinfo); 719 } else if (c[0] >= 224 && c[0] < 255) { 720 /* 4. Partial Body Length */ 721 /* XXX - agc - gpg multi-recipient encryption uses this */ 722 OPS_ERROR(&pinfo->errors, OPS_E_UNIMPLEMENTED, 723 "New format Partial Body Length fields not yet implemented"); 724 return false; 725 } 726 return false; 727 } 728 729 /** Read the length information for a new format Packet Tag. 730 * 731 * New style Packet Tags encode the length in one to five octets. This function reads the right amount of bytes and 732 * decodes it to the proper length information. 733 * 734 * This function makes sure to respect packet boundaries. 735 * 736 * \param *length return the length here 737 * \param *ptag Pointer to current packet's Packet Tag. 738 * \param *reader Our reader 739 * \param *cb The callback 740 * \return 1 on success, 0 on error (by limited_read_scalar() or limited_read() or if the MPI is not properly formed (XXX 741 * see comment below) 742 * 743 * \see RFC4880 4.2.2 744 * \see __ops_ptag_t 745 */ 746 static int 747 limited_read_new_length(unsigned *length, __ops_region_t * region, 748 __ops_parseinfo_t * pinfo) 749 { 750 unsigned char c[1] = ""; 751 752 if (!limited_read(c, 1, region, pinfo)) 753 return 0; 754 if (c[0] < 192) { 755 *length = c[0]; 756 return 1; 757 } 758 if (c[0] < 255) { 759 unsigned t = (c[0] - 192) << 8; 760 761 if (!limited_read(c, 1, region, pinfo)) 762 return 0; 763 *length = t + c[0] + 192; 764 return 1; 765 } 766 return limited_read_scalar(length, 4, region, pinfo); 767 } 768 769 /** 770 \ingroup Core_Create 771 \brief Free allocated memory 772 */ 773 static void 774 data_free(__ops_data_t * data) 775 { 776 (void) free(data->contents); 777 data->contents = NULL; 778 data->len = 0; 779 } 780 781 /** 782 \ingroup Core_Create 783 \brief Free allocated memory 784 */ 785 static void 786 string_free(char **str) 787 { 788 (void) free(*str); 789 *str = NULL; 790 } 791 792 /** 793 \ingroup Core_Create 794 \brief Free allocated memory 795 */ 796 /* ! Free packet memory, set pointer to NULL */ 797 void 798 __ops_subpacket_free(__ops_subpacket_t *packet) 799 { 800 (void) free(packet->raw); 801 packet->raw = NULL; 802 } 803 804 /** 805 \ingroup Core_Create 806 \brief Free allocated memory 807 */ 808 static void 809 __ops_headers_free(__ops_headers_t *headers) 810 { 811 unsigned n; 812 813 for (n = 0; n < headers->headerc; ++n) { 814 (void) free(headers->headers[n].key); 815 (void) free(headers->headers[n].value); 816 } 817 (void) free(headers->headers); 818 headers->headers = NULL; 819 } 820 821 /** 822 \ingroup Core_Create 823 \brief Free allocated memory 824 */ 825 static void 826 cleartext_trailer_free(__ops_cleartext_trailer_t * trailer) 827 { 828 (void) free(trailer->hash); 829 trailer->hash = NULL; 830 } 831 832 /** 833 \ingroup Core_Create 834 \brief Free allocated memory 835 */ 836 static void 837 __ops_cmd_get_passphrase_free(__ops_seckey_passphrase_t * skp) 838 { 839 if (skp->passphrase && *skp->passphrase) { 840 (void) free(*skp->passphrase); 841 *skp->passphrase = NULL; 842 } 843 } 844 845 /** 846 \ingroup Core_Create 847 \brief Free the memory used when parsing this signature sub-packet type 848 */ 849 static void 850 ss_userdefined_free(__ops_ss_userdefined_t * ss_userdefined) 851 { 852 data_free(&ss_userdefined->data); 853 } 854 855 /** 856 \ingroup Core_Create 857 \brief Free the memory used when parsing this signature sub-packet type 858 */ 859 static void 860 ss_reserved_free(__ops_ss_unknown_t * ss_unknown) 861 { 862 data_free(&ss_unknown->data); 863 } 864 865 /** 866 \ingroup Core_Create 867 \brief Free the memory used when parsing this packet type 868 */ 869 static void 870 trust_free(__ops_trust_t * trust) 871 { 872 data_free(&trust->data); 873 } 874 875 /** 876 * \ingroup Core_Create 877 * \brief Free the memory used when parsing a private/experimental PKA signature 878 * \param unknown_sig 879 */ 880 static void 881 free_unknown_sig_pka(__ops_unknown_sig_t * unknown_sig) 882 { 883 data_free(&unknown_sig->data); 884 } 885 886 /** 887 \ingroup Core_Create 888 \brief Free allocated memory 889 */ 890 static void 891 free_BN(BIGNUM ** pp) 892 { 893 BN_free(*pp); 894 *pp = NULL; 895 } 896 897 /** 898 * \ingroup Core_Create 899 * \brief Free the memory used when parsing a signature 900 * \param sig 901 */ 902 static void 903 sig_free(__ops_sig_t * sig) 904 { 905 switch (sig->info.key_alg) { 906 case OPS_PKA_RSA: 907 case OPS_PKA_RSA_SIGN_ONLY: 908 free_BN(&sig->info.sig.rsa.sig); 909 break; 910 911 case OPS_PKA_DSA: 912 free_BN(&sig->info.sig.dsa.r); 913 free_BN(&sig->info.sig.dsa.s); 914 break; 915 916 case OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN: 917 free_BN(&sig->info.sig.elgamal.r); 918 free_BN(&sig->info.sig.elgamal.s); 919 break; 920 921 case OPS_PKA_PRIVATE00: 922 case OPS_PKA_PRIVATE01: 923 case OPS_PKA_PRIVATE02: 924 case OPS_PKA_PRIVATE03: 925 case OPS_PKA_PRIVATE04: 926 case OPS_PKA_PRIVATE05: 927 case OPS_PKA_PRIVATE06: 928 case OPS_PKA_PRIVATE07: 929 case OPS_PKA_PRIVATE08: 930 case OPS_PKA_PRIVATE09: 931 case OPS_PKA_PRIVATE10: 932 free_unknown_sig_pka(&sig->info.sig.unknown); 933 break; 934 935 default: 936 (void) fprintf(stderr, "sig_free: bad sig type\n"); 937 } 938 } 939 940 /** 941 \ingroup Core_Create 942 \brief Free the memory used when parsing this signature sub-packet type 943 \param ss_skapref 944 */ 945 static void 946 ss_skapref_free(__ops_ss_skapref_t *ss_skapref) 947 { 948 data_free(&ss_skapref->data); 949 } 950 951 /** 952 \ingroup Core_Create 953 \brief Free the memory used when parsing this signature sub-packet type 954 \param ss_hashpref 955 */ 956 static void 957 ss_hashpref_free(__ops_ss_hashpref_t *ss_hashpref) 958 { 959 data_free(&ss_hashpref->data); 960 } 961 962 /** 963 \ingroup Core_Create 964 \brief Free the memory used when parsing this signature sub-packet type 965 */ 966 static void 967 ss_zpref_free(__ops_ss_zpref_t *ss_zpref) 968 { 969 data_free(&ss_zpref->data); 970 } 971 972 /** 973 \ingroup Core_Create 974 \brief Free the memory used when parsing this signature sub-packet type 975 */ 976 static void 977 ss_key_flags_free(__ops_ss_key_flags_t *ss_key_flags) 978 { 979 data_free(&ss_key_flags->data); 980 } 981 982 /** 983 \ingroup Core_Create 984 \brief Free the memory used when parsing this signature sub-packet type 985 */ 986 static void 987 ss_key_server_prefs_free(__ops_ss_key_server_prefs_t *ss_key_server_prefs) 988 { 989 data_free(&ss_key_server_prefs->data); 990 } 991 992 /** 993 \ingroup Core_Create 994 \brief Free the memory used when parsing this signature sub-packet type 995 */ 996 static void 997 ss_features_free(__ops_ss_features_t *ss_features) 998 { 999 data_free(&ss_features->data); 1000 } 1001 1002 /** 1003 \ingroup Core_Create 1004 \brief Free the memory used when parsing this signature sub-packet type 1005 */ 1006 static void 1007 ss_notation_data_free(__ops_ss_notation_data_t *ss_notation_data) 1008 { 1009 data_free(&ss_notation_data->name); 1010 data_free(&ss_notation_data->value); 1011 } 1012 1013 /** 1014 \ingroup Core_Create 1015 \brief Free allocated memory 1016 */ 1017 /* ! Free the memory used when parsing this signature sub-packet type */ 1018 static void 1019 ss_regexp_free(__ops_ss_regexp_t *regexp) 1020 { 1021 string_free(®exp->regexp); 1022 } 1023 1024 /** 1025 \ingroup Core_Create 1026 \brief Free allocated memory 1027 */ 1028 /* ! Free the memory used when parsing this signature sub-packet type */ 1029 static void 1030 ss_policy_free(__ops_ss_policy_t *policy) 1031 { 1032 string_free(&policy->url); 1033 } 1034 1035 /** 1036 \ingroup Core_Create 1037 \brief Free allocated memory 1038 */ 1039 /* ! Free the memory used when parsing this signature sub-packet type */ 1040 static void 1041 ss_pref_keyserv_free(__ops_ss_pref_keyserv_t *preferred_key_server) 1042 { 1043 string_free(&preferred_key_server->name); 1044 } 1045 1046 /** 1047 \ingroup Core_Create 1048 \brief Free the memory used when parsing this signature sub-packet type 1049 */ 1050 static void 1051 ss_revocation_free(__ops_ss_revocation_t *ss_revocation) 1052 { 1053 string_free(&ss_revocation->reason); 1054 } 1055 1056 static void 1057 ss_embedded_sig_free(__ops_ss_embedded_sig_t *ss_embedded_sig) 1058 { 1059 data_free(&ss_embedded_sig->sig); 1060 } 1061 1062 /** 1063 \ingroup Core_Create 1064 \brief Free allocated memory 1065 */ 1066 /* ! Free any memory allocated when parsing the packet content */ 1067 void 1068 __ops_parser_content_free(__ops_packet_t * c) 1069 { 1070 switch (c->tag) { 1071 case OPS_PARSER_PTAG: 1072 case OPS_PTAG_CT_COMPRESSED: 1073 case OPS_PTAG_SS_CREATION_TIME: 1074 case OPS_PTAG_SS_EXPIRATION_TIME: 1075 case OPS_PTAG_SS_KEY_EXPIRATION_TIME: 1076 case OPS_PTAG_SS_TRUST: 1077 case OPS_PTAG_SS_ISSUER_KEY_ID: 1078 case OPS_PTAG_CT_ONE_PASS_SIGNATURE: 1079 case OPS_PTAG_SS_PRIMARY_USER_ID: 1080 case OPS_PTAG_SS_REVOCABLE: 1081 case OPS_PTAG_SS_REVOCATION_KEY: 1082 case OPS_PTAG_CT_LITERAL_DATA_HEADER: 1083 case OPS_PTAG_CT_LITERAL_DATA_BODY: 1084 case OPS_PTAG_CT_SIGNED_CLEARTEXT_BODY: 1085 case OPS_PTAG_CT_UNARMOURED_TEXT: 1086 case OPS_PTAG_CT_ARMOUR_TRAILER: 1087 case OPS_PTAG_CT_SIGNATURE_HEADER: 1088 case OPS_PTAG_CT_SE_DATA_HEADER: 1089 case OPS_PTAG_CT_SE_IP_DATA_HEADER: 1090 case OPS_PTAG_CT_SE_IP_DATA_BODY: 1091 case OPS_PTAG_CT_MDC: 1092 case OPS_PARSER_CMD_GET_SECRET_KEY: 1093 break; 1094 1095 case OPS_PTAG_CT_SIGNED_CLEARTEXT_HEADER: 1096 __ops_headers_free(&c->u.cleartext_head.headers); 1097 break; 1098 1099 case OPS_PTAG_CT_ARMOUR_HEADER: 1100 __ops_headers_free(&c->u.armour_header.headers); 1101 break; 1102 1103 case OPS_PTAG_CT_SIGNED_CLEARTEXT_TRAILER: 1104 cleartext_trailer_free(&c->u.cleartext_trailer); 1105 break; 1106 1107 case OPS_PTAG_CT_TRUST: 1108 trust_free(&c->u.trust); 1109 break; 1110 1111 case OPS_PTAG_CT_SIGNATURE: 1112 case OPS_PTAG_CT_SIGNATURE_FOOTER: 1113 sig_free(&c->u.sig); 1114 break; 1115 1116 case OPS_PTAG_CT_PUBLIC_KEY: 1117 case OPS_PTAG_CT_PUBLIC_SUBKEY: 1118 __ops_pubkey_free(&c->u.pubkey); 1119 break; 1120 1121 case OPS_PTAG_CT_USER_ID: 1122 __ops_user_id_free(&c->u.user_id); 1123 break; 1124 1125 case OPS_PTAG_SS_SIGNERS_USER_ID: 1126 __ops_user_id_free(&c->u.ss_signer); 1127 break; 1128 1129 case OPS_PTAG_CT_USER_ATTRIBUTE: 1130 __ops_user_attribute_free(&c->u.user_attribute); 1131 break; 1132 1133 case OPS_PTAG_SS_PREFERRED_SKA: 1134 ss_skapref_free(&c->u.ss_skapref); 1135 break; 1136 1137 case OPS_PTAG_SS_PREFERRED_HASH: 1138 ss_hashpref_free(&c->u.ss_hashpref); 1139 break; 1140 1141 case OPS_PTAG_SS_PREFERRED_COMPRESSION: 1142 ss_zpref_free(&c->u.ss_zpref); 1143 break; 1144 1145 case OPS_PTAG_SS_KEY_FLAGS: 1146 ss_key_flags_free(&c->u.ss_key_flags); 1147 break; 1148 1149 case OPS_PTAG_SS_KEY_SERVER_PREFS: 1150 ss_key_server_prefs_free(&c->u.ss_key_server_prefs); 1151 break; 1152 1153 case OPS_PTAG_SS_FEATURES: 1154 ss_features_free(&c->u.ss_features); 1155 break; 1156 1157 case OPS_PTAG_SS_NOTATION_DATA: 1158 ss_notation_data_free(&c->u.ss_notation_data); 1159 break; 1160 1161 case OPS_PTAG_SS_REGEXP: 1162 ss_regexp_free(&c->u.ss_regexp); 1163 break; 1164 1165 case OPS_PTAG_SS_POLICY_URI: 1166 ss_policy_free(&c->u.ss_policy); 1167 break; 1168 1169 case OPS_PTAG_SS_PREFERRED_KEY_SERVER: 1170 ss_pref_keyserv_free(&c->u.ss_pref_keyserv); 1171 break; 1172 1173 case OPS_PTAG_SS_USERDEFINED00: 1174 case OPS_PTAG_SS_USERDEFINED01: 1175 case OPS_PTAG_SS_USERDEFINED02: 1176 case OPS_PTAG_SS_USERDEFINED03: 1177 case OPS_PTAG_SS_USERDEFINED04: 1178 case OPS_PTAG_SS_USERDEFINED05: 1179 case OPS_PTAG_SS_USERDEFINED06: 1180 case OPS_PTAG_SS_USERDEFINED07: 1181 case OPS_PTAG_SS_USERDEFINED08: 1182 case OPS_PTAG_SS_USERDEFINED09: 1183 case OPS_PTAG_SS_USERDEFINED10: 1184 ss_userdefined_free(&c->u.ss_userdefined); 1185 break; 1186 1187 case OPS_PTAG_SS_RESERVED: 1188 ss_reserved_free(&c->u.ss_unknown); 1189 break; 1190 1191 case OPS_PTAG_SS_REVOCATION_REASON: 1192 ss_revocation_free(&c->u.ss_revocation); 1193 break; 1194 1195 case OPS_PTAG_SS_EMBEDDED_SIGNATURE: 1196 ss_embedded_sig_free(&c->u.ss_embedded_sig); 1197 break; 1198 1199 case OPS_PARSER_PACKET_END: 1200 __ops_subpacket_free(&c->u.packet); 1201 break; 1202 1203 case OPS_PARSER_ERROR: 1204 case OPS_PARSER_ERRCODE: 1205 break; 1206 1207 case OPS_PTAG_CT_SECRET_KEY: 1208 case OPS_PTAG_CT_ENCRYPTED_SECRET_KEY: 1209 __ops_seckey_free(&c->u.seckey); 1210 break; 1211 1212 case OPS_PTAG_CT_PK_SESSION_KEY: 1213 case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY: 1214 __ops_pk_sesskey_free(&c->u.pk_sesskey); 1215 break; 1216 1217 case OPS_PARSER_CMD_GET_SK_PASSPHRASE: 1218 __ops_cmd_get_passphrase_free(&c->u.skey_passphrase); 1219 break; 1220 1221 default: 1222 fprintf(stderr, "Can't free %d (0x%x)\n", c->tag, c->tag); 1223 } 1224 } 1225 1226 /** 1227 \ingroup Core_Create 1228 \brief Free allocated memory 1229 */ 1230 void 1231 __ops_pk_sesskey_free(__ops_pk_sesskey_t * sk) 1232 { 1233 switch (sk->alg) { 1234 case OPS_PKA_RSA: 1235 free_BN(&sk->parameters.rsa.encrypted_m); 1236 break; 1237 1238 case OPS_PKA_ELGAMAL: 1239 free_BN(&sk->parameters.elgamal.g_to_k); 1240 free_BN(&sk->parameters.elgamal.encrypted_m); 1241 break; 1242 1243 default: 1244 (void) fprintf(stderr, "__ops_pk_sesskey_free: bad alg\n"); 1245 break; 1246 } 1247 } 1248 1249 /** 1250 \ingroup Core_Create 1251 \brief Free allocated memory 1252 */ 1253 /* ! Free the memory used when parsing a public key */ 1254 void 1255 __ops_pubkey_free(__ops_pubkey_t * p) 1256 { 1257 switch (p->alg) { 1258 case OPS_PKA_RSA: 1259 case OPS_PKA_RSA_ENCRYPT_ONLY: 1260 case OPS_PKA_RSA_SIGN_ONLY: 1261 free_BN(&p->key.rsa.n); 1262 free_BN(&p->key.rsa.e); 1263 break; 1264 1265 case OPS_PKA_DSA: 1266 free_BN(&p->key.dsa.p); 1267 free_BN(&p->key.dsa.q); 1268 free_BN(&p->key.dsa.g); 1269 free_BN(&p->key.dsa.y); 1270 break; 1271 1272 case OPS_PKA_ELGAMAL: 1273 case OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN: 1274 free_BN(&p->key.elgamal.p); 1275 free_BN(&p->key.elgamal.g); 1276 free_BN(&p->key.elgamal.y); 1277 break; 1278 1279 case 0: 1280 /* nothing to free */ 1281 break; 1282 1283 default: 1284 (void) fprintf(stderr, "__ops_pubkey_free: bad alg\n"); 1285 } 1286 } 1287 1288 /** 1289 \ingroup Core_ReadPackets 1290 */ 1291 static int 1292 parse_pubkey_data(__ops_pubkey_t * key, __ops_region_t * region, 1293 __ops_parseinfo_t * pinfo) 1294 { 1295 unsigned char c[1] = ""; 1296 1297 if (region->length_read != 0) { 1298 /* We should not have read anything so far */ 1299 (void) fprintf(stderr, "parse_pubkey_data: bad length\n"); 1300 return 0; 1301 } 1302 1303 if (!limited_read(c, 1, region, pinfo)) { 1304 return 0; 1305 } 1306 key->version = c[0]; 1307 if (key->version < 2 || key->version > 4) { 1308 OPS_ERROR_1(&pinfo->errors, OPS_E_PROTO_BAD_PUBLIC_KEY_VRSN, 1309 "Bad public key version (0x%02x)", key->version); 1310 return 0; 1311 } 1312 if (!limited_read_time(&key->birthtime, region, pinfo)) { 1313 return 0; 1314 } 1315 1316 key->days_valid = 0; 1317 if ((key->version == 2 || key->version == 3) && 1318 !limited_read_scalar(&key->days_valid, 2, region, pinfo)) { 1319 return 0; 1320 } 1321 1322 if (!limited_read(c, 1, region, pinfo)) 1323 return 0; 1324 1325 key->alg = c[0]; 1326 1327 switch (key->alg) { 1328 case OPS_PKA_DSA: 1329 if (!limited_read_mpi(&key->key.dsa.p, region, pinfo) || 1330 !limited_read_mpi(&key->key.dsa.q, region, pinfo) || 1331 !limited_read_mpi(&key->key.dsa.g, region, pinfo) || 1332 !limited_read_mpi(&key->key.dsa.y, region, pinfo)) { 1333 return 0; 1334 } 1335 break; 1336 1337 case OPS_PKA_RSA: 1338 case OPS_PKA_RSA_ENCRYPT_ONLY: 1339 case OPS_PKA_RSA_SIGN_ONLY: 1340 if (!limited_read_mpi(&key->key.rsa.n, region, pinfo) || 1341 !limited_read_mpi(&key->key.rsa.e, region, pinfo)) { 1342 return 0; 1343 } 1344 break; 1345 1346 case OPS_PKA_ELGAMAL: 1347 case OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN: 1348 if (!limited_read_mpi(&key->key.elgamal.p, region, pinfo) || 1349 !limited_read_mpi(&key->key.elgamal.g, region, pinfo) || 1350 !limited_read_mpi(&key->key.elgamal.y, region, pinfo)) { 1351 return 0; 1352 } 1353 break; 1354 1355 default: 1356 OPS_ERROR_1(&pinfo->errors, 1357 OPS_E_ALG_UNSUPPORTED_PUBLIC_KEY_ALG, 1358 "Unsupported Public Key algorithm (%s)", 1359 __ops_show_pka(key->alg)); 1360 return 0; 1361 } 1362 1363 return 1; 1364 } 1365 1366 1367 /** 1368 * \ingroup Core_ReadPackets 1369 * \brief Parse a public key packet. 1370 * 1371 * This function parses an entire v3 (== v2) or v4 public key packet for RSA, ElGamal, and DSA keys. 1372 * 1373 * Once the key has been parsed successfully, it is passed to the callback. 1374 * 1375 * \param *ptag Pointer to the current Packet Tag. This function should consume the entire packet. 1376 * \param *reader Our reader 1377 * \param *cb The callback 1378 * \return 1 on success, 0 on error 1379 * 1380 * \see RFC4880 5.5.2 1381 */ 1382 static int 1383 parse_pubkey(__ops_content_tag_t tag, __ops_region_t * region, 1384 __ops_parseinfo_t * pinfo) 1385 { 1386 __ops_packet_t pkt; 1387 1388 if (!parse_pubkey_data(&pkt.u.pubkey, region, pinfo)) 1389 return 0; 1390 1391 /* XXX: this test should be done for all packets, surely? */ 1392 if (region->length_read != region->length) { 1393 OPS_ERROR_1(&pinfo->errors, OPS_E_R_UNCONSUMED_DATA, 1394 "Unconsumed data (%d)", region->length - region->length_read); 1395 return 0; 1396 } 1397 CALLBACK(&pinfo->cbinfo, tag, &pkt); 1398 1399 return 1; 1400 } 1401 1402 1403 /** 1404 \ingroup Core_Create 1405 \brief Free allocated memory 1406 */ 1407 /* ! Free the memory used when parsing this packet type */ 1408 void 1409 __ops_user_attribute_free(__ops_user_attribute_t * user_att) 1410 { 1411 data_free(&user_att->data); 1412 } 1413 1414 /** 1415 * \ingroup Core_ReadPackets 1416 * \brief Parse one user attribute packet. 1417 * 1418 * User attribute packets contain one or more attribute subpackets. 1419 * For now, handle the whole packet as raw data. 1420 */ 1421 1422 static int 1423 parse_user_attribute(__ops_region_t * region, __ops_parseinfo_t * pinfo) 1424 { 1425 1426 __ops_packet_t pkt; 1427 1428 /* 1429 * xxx- treat as raw data for now. Could break down further into 1430 * attribute sub-packets later - rachel 1431 */ 1432 1433 if (region->length_read != 0) { 1434 /* We should not have read anything so far */ 1435 (void) fprintf(stderr, "parse_user_attribute: bad length\n"); 1436 return 0; 1437 } 1438 1439 if (!read_data(&pkt.u.user_attribute.data, region, pinfo)) 1440 return 0; 1441 1442 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_USER_ATTRIBUTE, &pkt); 1443 1444 return 1; 1445 } 1446 1447 /** 1448 \ingroup Core_Create 1449 \brief Free allocated memory 1450 */ 1451 /* ! Free the memory used when parsing this packet type */ 1452 void 1453 __ops_user_id_free(__ops_user_id_t * id) 1454 { 1455 (void) free(id->user_id); 1456 id->user_id = NULL; 1457 } 1458 1459 /** 1460 * \ingroup Core_ReadPackets 1461 * \brief Parse a user id. 1462 * 1463 * This function parses an user id packet, which is basically just a char array the size of the packet. 1464 * 1465 * The char array is to be treated as an UTF-8 string. 1466 * 1467 * The userid gets null terminated by this function. Freeing it is the responsibility of the caller. 1468 * 1469 * Once the userid has been parsed successfully, it is passed to the callback. 1470 * 1471 * \param *ptag Pointer to the Packet Tag. This function should consume the entire packet. 1472 * \param *reader Our reader 1473 * \param *cb The callback 1474 * \return 1 on success, 0 on error 1475 * 1476 * \see RFC4880 5.11 1477 */ 1478 static int 1479 parse_user_id(__ops_region_t * region, __ops_parseinfo_t * pinfo) 1480 { 1481 __ops_packet_t pkt; 1482 1483 if (region->length_read != 0) { 1484 /* We should not have read anything so far */ 1485 (void) fprintf(stderr, "parse_user_id: bad length\n"); 1486 return 0; 1487 } 1488 1489 /* XXX should we not like check malloc's return value? */ 1490 pkt.u.user_id.user_id = calloc(1, region->length + 1); 1491 1492 if (region->length && 1493 !limited_read(pkt.u.user_id.user_id, region->length, region, 1494 pinfo)) { 1495 return 0; 1496 } 1497 pkt.u.user_id.user_id[region->length] = '\0'; 1498 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_USER_ID, &pkt); 1499 return 1; 1500 } 1501 1502 static __ops_hash_t * 1503 parse_hash_find(__ops_parseinfo_t * pinfo, 1504 const unsigned char keyid[OPS_KEY_ID_SIZE]) 1505 { 1506 size_t n; 1507 1508 for (n = 0; n < pinfo->nhashes; ++n) { 1509 if (memcmp(pinfo->hashes[n].keyid, keyid, 1510 OPS_KEY_ID_SIZE) == 0) { 1511 return &pinfo->hashes[n].hash; 1512 } 1513 } 1514 return NULL; 1515 } 1516 1517 /** 1518 * \ingroup Core_Parse 1519 * \brief Parse a version 3 signature. 1520 * 1521 * This function parses an version 3 signature packet, handling RSA and DSA signatures. 1522 * 1523 * Once the signature has been parsed successfully, it is passed to the callback. 1524 * 1525 * \param *ptag Pointer to the Packet Tag. This function should consume the entire packet. 1526 * \param *reader Our reader 1527 * \param *cb The callback 1528 * \return 1 on success, 0 on error 1529 * 1530 * \see RFC4880 5.2.2 1531 */ 1532 static int 1533 parse_v3_sig(__ops_region_t * region, 1534 __ops_parseinfo_t * pinfo) 1535 { 1536 __ops_packet_t pkt; 1537 unsigned char c[1] = ""; 1538 1539 /* clear signature */ 1540 (void) memset(&pkt.u.sig, 0x0, sizeof(pkt.u.sig)); 1541 1542 pkt.u.sig.info.version = OPS_V3; 1543 1544 /* hash info length */ 1545 if (!limited_read(c, 1, region, pinfo)) { 1546 return 0; 1547 } 1548 if (c[0] != 5) { 1549 ERRP(&pinfo->cbinfo, pkt, "bad hash info length"); 1550 } 1551 1552 if (!limited_read(c, 1, region, pinfo)) { 1553 return 0; 1554 } 1555 pkt.u.sig.info.type = c[0]; 1556 /* XXX: check signature type */ 1557 1558 if (!limited_read_time(&pkt.u.sig.info.birthtime, 1559 region, pinfo)) { 1560 return 0; 1561 } 1562 pkt.u.sig.info.birthtime_set = true; 1563 1564 if (!limited_read(pkt.u.sig.info.signer_id, OPS_KEY_ID_SIZE, 1565 region, pinfo)) { 1566 return 0; 1567 } 1568 pkt.u.sig.info.signer_id_set = true; 1569 1570 if (!limited_read(c, 1, region, pinfo)) { 1571 return 0; 1572 } 1573 pkt.u.sig.info.key_alg = c[0]; 1574 /* XXX: check algorithm */ 1575 1576 if (!limited_read(c, 1, region, pinfo)) { 1577 return 0; 1578 } 1579 pkt.u.sig.info.hash_alg = c[0]; 1580 /* XXX: check algorithm */ 1581 1582 if (!limited_read(pkt.u.sig.hash2, 2, region, pinfo)) { 1583 return 0; 1584 } 1585 1586 switch (pkt.u.sig.info.key_alg) { 1587 case OPS_PKA_RSA: 1588 case OPS_PKA_RSA_SIGN_ONLY: 1589 if (!limited_read_mpi(&pkt.u.sig.info.sig.rsa.sig, region, 1590 pinfo)) { 1591 return 0; 1592 } 1593 break; 1594 1595 case OPS_PKA_DSA: 1596 if (!limited_read_mpi(&pkt.u.sig.info.sig.dsa.r, region, 1597 pinfo) || 1598 !limited_read_mpi(&pkt.u.sig.info.sig.dsa.s, region, 1599 pinfo)) { 1600 return 0; 1601 } 1602 break; 1603 1604 case OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN: 1605 if (!limited_read_mpi(&pkt.u.sig.info.sig.elgamal.r, region, 1606 pinfo) || 1607 !limited_read_mpi(&pkt.u.sig.info.sig.elgamal.s, region, 1608 pinfo)) { 1609 return 0; 1610 } 1611 break; 1612 1613 default: 1614 OPS_ERROR_1(&pinfo->errors, 1615 OPS_E_ALG_UNSUPPORTED_SIGNATURE_ALG, 1616 "Unsupported signature key algorithm (%s)", 1617 __ops_show_pka(pkt.u.sig.info.key_alg)); 1618 return 0; 1619 } 1620 1621 if (region->length_read != region->length) { 1622 OPS_ERROR_1(&pinfo->errors, OPS_E_R_UNCONSUMED_DATA, 1623 "Unconsumed data (%d)", 1624 region->length - region->length_read); 1625 return 0; 1626 } 1627 if (pkt.u.sig.info.signer_id_set) { 1628 pkt.u.sig.hash = parse_hash_find(pinfo, 1629 pkt.u.sig.info.signer_id); 1630 } 1631 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_SIGNATURE, &pkt); 1632 return 1; 1633 } 1634 1635 /** 1636 * \ingroup Core_ReadPackets 1637 * \brief Parse one signature sub-packet. 1638 * 1639 * Version 4 signatures can have an arbitrary amount of (hashed and unhashed) subpackets. Subpackets are used to hold 1640 * optional attributes of subpackets. 1641 * 1642 * This function parses one such signature subpacket. 1643 * 1644 * Once the subpacket has been parsed successfully, it is passed to the callback. 1645 * 1646 * \param *ptag Pointer to the Packet Tag. This function should consume the entire subpacket. 1647 * \param *reader Our reader 1648 * \param *cb The callback 1649 * \return 1 on success, 0 on error 1650 * 1651 * \see RFC4880 5.2.3 1652 */ 1653 static int 1654 parse_one_sig_subpacket(__ops_sig_t * sig, 1655 __ops_region_t * region, 1656 __ops_parseinfo_t * pinfo) 1657 { 1658 __ops_region_t subregion; 1659 unsigned char c[1] = ""; 1660 __ops_packet_t pkt; 1661 unsigned t8, t7; 1662 bool doread = true; 1663 unsigned char bools[1] = ""; 1664 1665 __ops_init_subregion(&subregion, region); 1666 if (!limited_read_new_length(&subregion.length, region, pinfo)) { 1667 return 0; 1668 } 1669 1670 if (subregion.length > region->length) { 1671 ERRP(&pinfo->cbinfo, pkt, "Subpacket too long"); 1672 } 1673 1674 if (!limited_read(c, 1, &subregion, pinfo)) { 1675 return 0; 1676 } 1677 1678 t8 = (c[0] & 0x7f) / 8; 1679 t7 = 1 << (c[0] & 7); 1680 1681 pkt.critical = (unsigned)c[0] >> 7; 1682 pkt.tag = OPS_PTAG_SIGNATURE_SUBPACKET_BASE + (c[0] & 0x7f); 1683 1684 /* Application wants it delivered raw */ 1685 if (pinfo->ss_raw[t8] & t7) { 1686 pkt.u.ss_raw.tag = pkt.tag; 1687 pkt.u.ss_raw.length = subregion.length - 1; 1688 pkt.u.ss_raw.raw = calloc(1, pkt.u.ss_raw.length); 1689 if (!limited_read(pkt.u.ss_raw.raw, pkt.u.ss_raw.length, 1690 &subregion, pinfo)) { 1691 return 0; 1692 } 1693 CALLBACK(&pinfo->cbinfo, OPS_PTAG_RAW_SS, &pkt); 1694 return 1; 1695 } 1696 switch (pkt.tag) { 1697 case OPS_PTAG_SS_CREATION_TIME: 1698 case OPS_PTAG_SS_EXPIRATION_TIME: 1699 case OPS_PTAG_SS_KEY_EXPIRATION_TIME: 1700 if (!limited_read_time(&pkt.u.ss_time.time, &subregion, pinfo)) 1701 return 0; 1702 if (pkt.tag == OPS_PTAG_SS_CREATION_TIME) { 1703 sig->info.birthtime = pkt.u.ss_time.time; 1704 sig->info.birthtime_set = true; 1705 } 1706 break; 1707 1708 case OPS_PTAG_SS_TRUST: 1709 if (!limited_read(&pkt.u.ss_trust.level, 1, &subregion, pinfo) 1710 || !limited_read(&pkt.u.ss_trust.amount, 1, &subregion, pinfo)) 1711 return 0; 1712 break; 1713 1714 case OPS_PTAG_SS_REVOCABLE: 1715 if (!limited_read(bools, 1, &subregion, pinfo)) { 1716 return 0; 1717 } 1718 pkt.u.ss_revocable.revocable = !!bools[0]; 1719 break; 1720 1721 case OPS_PTAG_SS_ISSUER_KEY_ID: 1722 if (!limited_read(pkt.u.ss_issuer_key_id.key_id, 1723 OPS_KEY_ID_SIZE, &subregion, pinfo)) { 1724 return 0; 1725 } 1726 (void) memcpy(sig->info.signer_id, 1727 pkt.u.ss_issuer_key_id.key_id, OPS_KEY_ID_SIZE); 1728 sig->info.signer_id_set = true; 1729 break; 1730 1731 case OPS_PTAG_SS_PREFERRED_SKA: 1732 if (!read_data(&pkt.u.ss_skapref.data, &subregion, pinfo)) { 1733 return 0; 1734 } 1735 break; 1736 1737 case OPS_PTAG_SS_PREFERRED_HASH: 1738 if (!read_data(&pkt.u.ss_hashpref.data, &subregion, pinfo)) { 1739 return 0; 1740 } 1741 break; 1742 1743 case OPS_PTAG_SS_PREFERRED_COMPRESSION: 1744 if (!read_data(&pkt.u.ss_zpref.data, 1745 &subregion, pinfo)) { 1746 return 0; 1747 } 1748 break; 1749 1750 case OPS_PTAG_SS_PRIMARY_USER_ID: 1751 if (!limited_read(bools, 1, &subregion, pinfo)) { 1752 return 0; 1753 } 1754 pkt.u.ss_primary_user_id.primary_user_id = !!bools[0]; 1755 break; 1756 1757 case OPS_PTAG_SS_KEY_FLAGS: 1758 if (!read_data(&pkt.u.ss_key_flags.data, &subregion, pinfo)) { 1759 return 0; 1760 } 1761 break; 1762 1763 case OPS_PTAG_SS_KEY_SERVER_PREFS: 1764 if (!read_data(&pkt.u.ss_key_server_prefs.data, &subregion, 1765 pinfo)) { 1766 return 0; 1767 } 1768 break; 1769 1770 case OPS_PTAG_SS_FEATURES: 1771 if (!read_data(&pkt.u.ss_features.data, &subregion, pinfo)) { 1772 return 0; 1773 } 1774 break; 1775 1776 case OPS_PTAG_SS_SIGNERS_USER_ID: 1777 if (!read_unsigned_string(&pkt.u.ss_signer.user_id, 1778 &subregion, pinfo)) { 1779 return 0; 1780 } 1781 break; 1782 1783 case OPS_PTAG_SS_EMBEDDED_SIGNATURE: 1784 /* \todo should do something with this sig? */ 1785 if (!read_data(&pkt.u.ss_embedded_sig.sig, &subregion, pinfo)) { 1786 return 0; 1787 } 1788 break; 1789 1790 case OPS_PTAG_SS_NOTATION_DATA: 1791 if (!limited_read_data(&pkt.u.ss_notation_data.flags, 4, 1792 &subregion, pinfo)) 1793 return 0; 1794 if (!limited_read_size_t_scalar( 1795 &pkt.u.ss_notation_data.name.len, 2, 1796 &subregion, pinfo)) 1797 return 0; 1798 if (!limited_read_size_t_scalar( 1799 &pkt.u.ss_notation_data.value.len, 2, 1800 &subregion, pinfo)) 1801 return 0; 1802 if (!limited_read_data(&pkt.u.ss_notation_data.name, 1803 pkt.u.ss_notation_data.name.len, 1804 &subregion, pinfo)) 1805 return 0; 1806 if (!limited_read_data(&pkt.u.ss_notation_data.value, 1807 pkt.u.ss_notation_data.value.len, 1808 &subregion, pinfo)) 1809 return 0; 1810 break; 1811 1812 case OPS_PTAG_SS_POLICY_URI: 1813 if (!read_string(&pkt.u.ss_policy.url, &subregion, pinfo)) { 1814 return 0; 1815 } 1816 break; 1817 1818 case OPS_PTAG_SS_REGEXP: 1819 if (!read_string(&pkt.u.ss_regexp.regexp, &subregion, pinfo)) { 1820 return 0; 1821 } 1822 break; 1823 1824 case OPS_PTAG_SS_PREFERRED_KEY_SERVER: 1825 if (!read_string(&pkt.u.ss_pref_keyserv.name, 1826 &subregion, pinfo)) 1827 return 0; 1828 break; 1829 1830 case OPS_PTAG_SS_USERDEFINED00: 1831 case OPS_PTAG_SS_USERDEFINED01: 1832 case OPS_PTAG_SS_USERDEFINED02: 1833 case OPS_PTAG_SS_USERDEFINED03: 1834 case OPS_PTAG_SS_USERDEFINED04: 1835 case OPS_PTAG_SS_USERDEFINED05: 1836 case OPS_PTAG_SS_USERDEFINED06: 1837 case OPS_PTAG_SS_USERDEFINED07: 1838 case OPS_PTAG_SS_USERDEFINED08: 1839 case OPS_PTAG_SS_USERDEFINED09: 1840 case OPS_PTAG_SS_USERDEFINED10: 1841 if (!read_data(&pkt.u.ss_userdefined.data, &subregion, pinfo)) 1842 return 0; 1843 break; 1844 1845 case OPS_PTAG_SS_RESERVED: 1846 if (!read_data(&pkt.u.ss_unknown.data, &subregion, pinfo)) 1847 return 0; 1848 break; 1849 1850 case OPS_PTAG_SS_REVOCATION_REASON: 1851 /* first byte is the machine-readable code */ 1852 if (!limited_read(&pkt.u.ss_revocation.code, 1, 1853 &subregion, pinfo)) { 1854 return 0; 1855 } 1856 /* the rest is a human-readable UTF-8 string */ 1857 if (!read_string(&pkt.u.ss_revocation.reason, &subregion, 1858 pinfo)) { 1859 return 0; 1860 } 1861 break; 1862 1863 case OPS_PTAG_SS_REVOCATION_KEY: 1864 /* octet 0 = class. Bit 0x80 must be set */ 1865 if (!limited_read(&pkt.u.ss_revocation_key.class, 1, 1866 &subregion, pinfo)) { 1867 return 0; 1868 } 1869 if (!(pkt.u.ss_revocation_key.class & 0x80)) { 1870 printf("Warning: OPS_PTAG_SS_REVOCATION_KEY class: " 1871 "Bit 0x80 should be set\n"); 1872 return 0; 1873 } 1874 /* octet 1 = algid */ 1875 if (!limited_read(&pkt.u.ss_revocation_key.algid, 1, 1876 &subregion, pinfo)) { 1877 return 0; 1878 } 1879 /* octets 2-21 = fingerprint */ 1880 if (!limited_read(&pkt.u.ss_revocation_key.fingerprint[0], 20, 1881 &subregion, pinfo)) { 1882 return 0; 1883 } 1884 break; 1885 1886 default: 1887 if (pinfo->ss_parsed[t8] & t7) { 1888 OPS_ERROR_1(&pinfo->errors, OPS_E_PROTO_UNKNOWN_SS, 1889 "Unknown signature subpacket type (%d)", 1890 c[0] & 0x7f); 1891 } 1892 doread = false; 1893 break; 1894 } 1895 1896 /* Application doesn't want it delivered parsed */ 1897 if (!(pinfo->ss_parsed[t8] & t7)) { 1898 if (pkt.critical) { 1899 OPS_ERROR_1(&pinfo->errors, 1900 OPS_E_PROTO_CRITICAL_SS_IGNORED, 1901 "Critical signature subpacket ignored (%d)", 1902 c[0] & 0x7f); 1903 } 1904 if (!doread && 1905 !limited_skip(subregion.length - 1, &subregion, pinfo)) { 1906 return 0; 1907 } 1908 if (doread) { 1909 __ops_parser_content_free(&pkt); 1910 } 1911 return 1; 1912 } 1913 if (doread && subregion.length_read != subregion.length) { 1914 OPS_ERROR_1(&pinfo->errors, OPS_E_R_UNCONSUMED_DATA, 1915 "Unconsumed data (%d)", 1916 subregion.length - subregion.length_read); 1917 return 0; 1918 } 1919 CALLBACK(&pinfo->cbinfo, pkt.tag, &pkt); 1920 return 1; 1921 } 1922 1923 /** 1924 * \ingroup Core_ReadPackets 1925 * \brief Parse several signature subpackets. 1926 * 1927 * Hashed and unhashed subpacket sets are preceded by an octet count that specifies the length of the complete set. 1928 * This function parses this length and then calls parse_one_sig_subpacket() for each subpacket until the 1929 * entire set is consumed. 1930 * 1931 * This function does not call the callback directly, parse_one_sig_subpacket() does for each subpacket. 1932 * 1933 * \param *ptag Pointer to the Packet Tag. 1934 * \param *reader Our reader 1935 * \param *cb The callback 1936 * \return 1 on success, 0 on error 1937 * 1938 * \see RFC4880 5.2.3 1939 */ 1940 static int 1941 parse_sig_subpackets(__ops_sig_t * sig, 1942 __ops_region_t * region, 1943 __ops_parseinfo_t * pinfo) 1944 { 1945 __ops_packet_t pkt; 1946 __ops_region_t subregion; 1947 1948 __ops_init_subregion(&subregion, region); 1949 if (!limited_read_scalar(&subregion.length, 2, region, pinfo)) { 1950 return 0; 1951 } 1952 1953 if (subregion.length > region->length) { 1954 ERRP(&pinfo->cbinfo, pkt, "Subpacket set too long"); 1955 } 1956 1957 while (subregion.length_read < subregion.length) { 1958 if (!parse_one_sig_subpacket(sig, &subregion, pinfo)) { 1959 return 0; 1960 } 1961 } 1962 1963 if (subregion.length_read != subregion.length) { 1964 if (!limited_skip(subregion.length - subregion.length_read, 1965 &subregion, pinfo)) { 1966 ERRP(&pinfo->cbinfo, pkt, 1967 "Read failed while recovering from subpacket length mismatch"); 1968 } 1969 ERRP(&pinfo->cbinfo, pkt, "Subpacket length mismatch"); 1970 } 1971 return 1; 1972 } 1973 1974 /** 1975 * \ingroup Core_ReadPackets 1976 * \brief Parse a version 4 signature. 1977 * 1978 * This function parses a version 4 signature including all its hashed and unhashed subpackets. 1979 * 1980 * Once the signature packet has been parsed successfully, it is passed to the callback. 1981 * 1982 * \param *ptag Pointer to the Packet Tag. 1983 * \param *reader Our reader 1984 * \param *cb The callback 1985 * \return 1 on success, 0 on error 1986 * 1987 * \see RFC4880 5.2.3 1988 */ 1989 static int 1990 parse_v4_sig(__ops_region_t * region, __ops_parseinfo_t * pinfo) 1991 { 1992 unsigned char c[1] = ""; 1993 __ops_packet_t pkt; 1994 1995 /* debug=1; */ 1996 if (__ops_get_debug_level(__FILE__)) { 1997 fprintf(stderr, "\nparse_v4_sig\n"); 1998 } 1999 /* clear signature */ 2000 (void) memset(&pkt.u.sig, 0x0, sizeof(pkt.u.sig)); 2001 2002 /* 2003 * We need to hash the packet data from version through the hashed 2004 * subpacket data 2005 */ 2006 2007 pkt.u.sig.v4_hashed_data_start = pinfo->readinfo.alength - 1; 2008 2009 /* Set version,type,algorithms */ 2010 2011 pkt.u.sig.info.version = OPS_V4; 2012 2013 if (!limited_read(c, 1, region, pinfo)) 2014 return 0; 2015 pkt.u.sig.info.type = c[0]; 2016 if (__ops_get_debug_level(__FILE__)) { 2017 fprintf(stderr, "signature type=%d (%s)\n", 2018 pkt.u.sig.info.type, 2019 __ops_show_sig_type(pkt.u.sig.info.type)); 2020 } 2021 /* XXX: check signature type */ 2022 2023 if (!limited_read(c, 1, region, pinfo)) { 2024 return 0; 2025 } 2026 pkt.u.sig.info.key_alg = c[0]; 2027 /* XXX: check algorithm */ 2028 if (__ops_get_debug_level(__FILE__)) { 2029 (void) fprintf(stderr, "key_alg=%d (%s)\n", 2030 pkt.u.sig.info.key_alg, 2031 __ops_show_pka(pkt.u.sig.info.key_alg)); 2032 } 2033 if (!limited_read(c, 1, region, pinfo)) { 2034 return 0; 2035 } 2036 pkt.u.sig.info.hash_alg = c[0]; 2037 /* XXX: check algorithm */ 2038 if (__ops_get_debug_level(__FILE__)) { 2039 fprintf(stderr, "hash_alg=%d %s\n", 2040 pkt.u.sig.info.hash_alg, 2041 __ops_show_hash_alg(pkt.u.sig.info.hash_alg)); 2042 } 2043 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_SIGNATURE_HEADER, &pkt); 2044 2045 if (!parse_sig_subpackets(&pkt.u.sig, region, pinfo)) { 2046 return 0; 2047 } 2048 2049 pkt.u.sig.info.v4_hashed_data_length = pinfo->readinfo.alength 2050 - pkt.u.sig.v4_hashed_data_start; 2051 2052 /* copy hashed subpackets */ 2053 if (pkt.u.sig.info.v4_hashed_data) { 2054 (void) free(pkt.u.sig.info.v4_hashed_data); 2055 } 2056 pkt.u.sig.info.v4_hashed_data = calloc(1, 2057 pkt.u.sig.info.v4_hashed_data_length); 2058 2059 if (!pinfo->readinfo.accumulate) { 2060 /* We must accumulate, else we can't check the signature */ 2061 fprintf(stderr, "*** ERROR: must set accumulate to true\n"); 2062 return 0; 2063 } 2064 (void) memcpy(pkt.u.sig.info.v4_hashed_data, 2065 pinfo->readinfo.accumulated + pkt.u.sig.v4_hashed_data_start, 2066 pkt.u.sig.info.v4_hashed_data_length); 2067 2068 if (!parse_sig_subpackets(&pkt.u.sig, region, pinfo)) { 2069 return 0; 2070 } 2071 2072 if (!limited_read(pkt.u.sig.hash2, 2, region, pinfo)) { 2073 return 0; 2074 } 2075 2076 switch (pkt.u.sig.info.key_alg) { 2077 case OPS_PKA_RSA: 2078 if (!limited_read_mpi(&pkt.u.sig.info.sig.rsa.sig, region, 2079 pinfo)) { 2080 return 0; 2081 } 2082 break; 2083 2084 case OPS_PKA_DSA: 2085 if (!limited_read_mpi(&pkt.u.sig.info.sig.dsa.r, region, 2086 pinfo)) { 2087 /* 2088 * usually if this fails, it just means we've reached 2089 * the end of the keyring 2090 */ 2091 if (__ops_get_debug_level(__FILE__)) { 2092 (void) fprintf(stderr, 2093 "Error reading DSA r field in signature"); 2094 } 2095 return 0; 2096 } 2097 if (!limited_read_mpi(&pkt.u.sig.info.sig.dsa.s, region, 2098 pinfo)) { 2099 ERRP(&pinfo->cbinfo, pkt, 2100 "Error reading DSA s field in signature"); 2101 } 2102 break; 2103 2104 case OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN: 2105 if (!limited_read_mpi(&pkt.u.sig.info.sig.elgamal.r, region, 2106 pinfo) || 2107 !limited_read_mpi(&pkt.u.sig.info.sig.elgamal.s, region, 2108 pinfo)) { 2109 return 0; 2110 } 2111 break; 2112 2113 case OPS_PKA_PRIVATE00: 2114 case OPS_PKA_PRIVATE01: 2115 case OPS_PKA_PRIVATE02: 2116 case OPS_PKA_PRIVATE03: 2117 case OPS_PKA_PRIVATE04: 2118 case OPS_PKA_PRIVATE05: 2119 case OPS_PKA_PRIVATE06: 2120 case OPS_PKA_PRIVATE07: 2121 case OPS_PKA_PRIVATE08: 2122 case OPS_PKA_PRIVATE09: 2123 case OPS_PKA_PRIVATE10: 2124 if (!read_data(&pkt.u.sig.info.sig.unknown.data, region, 2125 pinfo)) { 2126 return 0; 2127 } 2128 break; 2129 2130 default: 2131 OPS_ERROR_1(&pinfo->errors, OPS_E_ALG_UNSUPPORTED_SIGNATURE_ALG, 2132 "Bad v4 signature key algorithm (%s)", 2133 __ops_show_pka(pkt.u.sig.info.key_alg)); 2134 return 0; 2135 } 2136 if (region->length_read != region->length) { 2137 OPS_ERROR_1(&pinfo->errors, OPS_E_R_UNCONSUMED_DATA, 2138 "Unconsumed data (%d)", 2139 region->length - region->length_read); 2140 return 0; 2141 } 2142 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_SIGNATURE_FOOTER, &pkt); 2143 return 1; 2144 } 2145 2146 /** 2147 * \ingroup Core_ReadPackets 2148 * \brief Parse a signature subpacket. 2149 * 2150 * This function calls the appropriate function to handle v3 or v4 signatures. 2151 * 2152 * Once the signature packet has been parsed successfully, it is passed to the callback. 2153 * 2154 * \param *ptag Pointer to the Packet Tag. 2155 * \param *reader Our reader 2156 * \param *cb The callback 2157 * \return 1 on success, 0 on error 2158 */ 2159 static int 2160 parse_sig(__ops_region_t * region, __ops_parseinfo_t * pinfo) 2161 { 2162 unsigned char c[1] = ""; 2163 __ops_packet_t pkt; 2164 2165 if (region->length_read != 0) { 2166 /* We should not have read anything so far */ 2167 (void) fprintf(stderr, "parse_sig: bad length\n"); 2168 return 0; 2169 } 2170 2171 (void) memset(&pkt, 0x0, sizeof(pkt)); 2172 2173 if (!limited_read(c, 1, region, pinfo)) 2174 return 0; 2175 2176 if (c[0] == 2 || c[0] == 3) 2177 return parse_v3_sig(region, pinfo); 2178 else if (c[0] == 4) 2179 return parse_v4_sig(region, pinfo); 2180 2181 OPS_ERROR_1(&pinfo->errors, OPS_E_PROTO_BAD_SIGNATURE_VRSN, 2182 "Bad signature version (%d)", c[0]); 2183 return 0; 2184 } 2185 2186 /** 2187 \ingroup Core_ReadPackets 2188 \brief Parse Compressed packet 2189 */ 2190 static int 2191 parse_compressed(__ops_region_t * region, __ops_parseinfo_t * pinfo) 2192 { 2193 __ops_packet_t pkt; 2194 unsigned char c[1] = ""; 2195 2196 if (!limited_read(c, 1, region, pinfo)) { 2197 return 0; 2198 } 2199 2200 pkt.u.compressed.type = c[0]; 2201 2202 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_COMPRESSED, &pkt); 2203 2204 /* 2205 * The content of a compressed data packet is more OpenPGP packets 2206 * once decompressed, so recursively handle them 2207 */ 2208 2209 return __ops_decompress(region, pinfo, pkt.u.compressed.type); 2210 } 2211 2212 /* XXX: this could be improved by sharing all hashes that are the */ 2213 /* same, then duping them just before checking the signature. */ 2214 static void 2215 parse_hash_init(__ops_parseinfo_t * pinfo, __ops_hash_alg_t type, 2216 const unsigned char *keyid) 2217 { 2218 __ops_parse_hash_info_t *hash; 2219 2220 pinfo->hashes = realloc(pinfo->hashes, 2221 (pinfo->nhashes + 1) * sizeof(*pinfo->hashes)); 2222 hash = &pinfo->hashes[pinfo->nhashes++]; 2223 2224 __ops_hash_any(&hash->hash, type); 2225 hash->hash.init(&hash->hash); 2226 (void) memcpy(hash->keyid, keyid, sizeof(hash->keyid)); 2227 } 2228 2229 /** 2230 \ingroup Core_ReadPackets 2231 \brief Parse a One Pass Signature packet 2232 */ 2233 static int 2234 parse_one_pass(__ops_region_t * region, __ops_parseinfo_t * pinfo) 2235 { 2236 unsigned char c[1] = ""; 2237 __ops_packet_t pkt; 2238 2239 if (!limited_read(&pkt.u.one_pass_sig.version, 1, region, pinfo)) { 2240 return 0; 2241 } 2242 if (pkt.u.one_pass_sig.version != 3) { 2243 OPS_ERROR_1(&pinfo->errors, OPS_E_PROTO_BAD_ONE_PASS_SIG_VRSN, 2244 "Bad one-pass signature version (%d)", 2245 pkt.u.one_pass_sig.version); 2246 return 0; 2247 } 2248 if (!limited_read(c, 1, region, pinfo)) { 2249 return 0; 2250 } 2251 pkt.u.one_pass_sig.sig_type = c[0]; 2252 2253 if (!limited_read(c, 1, region, pinfo)) { 2254 return 0; 2255 } 2256 pkt.u.one_pass_sig.hash_alg = c[0]; 2257 2258 if (!limited_read(c, 1, region, pinfo)) { 2259 return 0; 2260 } 2261 pkt.u.one_pass_sig.key_alg = c[0]; 2262 2263 if (!limited_read(pkt.u.one_pass_sig.keyid, 2264 sizeof(pkt.u.one_pass_sig.keyid), region, pinfo)) { 2265 return 0; 2266 } 2267 2268 if (!limited_read(c, 1, region, pinfo)) { 2269 return 0; 2270 } 2271 pkt.u.one_pass_sig.nested = !!c[0]; 2272 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_ONE_PASS_SIGNATURE, &pkt); 2273 /* XXX: we should, perhaps, let the app choose whether to hash or not */ 2274 parse_hash_init(pinfo, pkt.u.one_pass_sig.hash_alg, 2275 pkt.u.one_pass_sig.keyid); 2276 return 1; 2277 } 2278 2279 /** 2280 \ingroup Core_ReadPackets 2281 \brief Parse a Trust packet 2282 */ 2283 static int 2284 parse_trust(__ops_region_t * region, __ops_parseinfo_t * pinfo) 2285 { 2286 __ops_packet_t pkt; 2287 2288 if (!read_data(&pkt.u.trust.data, region, pinfo)) { 2289 return 0; 2290 } 2291 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_TRUST, &pkt); 2292 return 1; 2293 } 2294 2295 static void 2296 parse_hash_data(__ops_parseinfo_t * pinfo, const void *data, 2297 size_t length) 2298 { 2299 size_t n; 2300 2301 for (n = 0; n < pinfo->nhashes; ++n) { 2302 pinfo->hashes[n].hash.add(&pinfo->hashes[n].hash, data, length); 2303 } 2304 } 2305 2306 /** 2307 \ingroup Core_ReadPackets 2308 \brief Parse a Literal Data packet 2309 */ 2310 static int 2311 parse_litdata(__ops_region_t * region, __ops_parseinfo_t * pinfo) 2312 { 2313 __ops_packet_t pkt; 2314 __ops_memory_t *mem; 2315 unsigned char c[1] = ""; 2316 2317 if (!limited_read(c, 1, region, pinfo)) { 2318 return 0; 2319 } 2320 pkt.u.litdata_header.format = c[0]; 2321 2322 if (!limited_read(c, 1, region, pinfo)) { 2323 return 0; 2324 } 2325 if (!limited_read((unsigned char *)pkt.u.litdata_header.filename, 2326 (unsigned)c[0], region, pinfo)) { 2327 return 0; 2328 } 2329 pkt.u.litdata_header.filename[c[0]] = '\0'; 2330 2331 if (!limited_read_time(&pkt.u.litdata_header.mtime, region, pinfo)) { 2332 return 0; 2333 } 2334 2335 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_LITERAL_DATA_HEADER, &pkt); 2336 2337 mem = pkt.u.litdata_body.mem = __ops_memory_new(); 2338 __ops_memory_init(pkt.u.litdata_body.mem, 2339 (unsigned)(region->length * 1.01) + 12); 2340 pkt.u.litdata_body.data = mem->buf; 2341 2342 while (region->length_read < region->length) { 2343 unsigned readc = region->length - region->length_read; 2344 2345 if (!limited_read(mem->buf, readc, region, pinfo)) { 2346 return 0; 2347 } 2348 pkt.u.litdata_body.length = readc; 2349 parse_hash_data(pinfo, pkt.u.litdata_body.data, 2350 region->length); 2351 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_LITERAL_DATA_BODY, 2352 &pkt); 2353 } 2354 2355 /* XXX - get rid of mem here? */ 2356 2357 return 1; 2358 } 2359 2360 /** 2361 * \ingroup Core_Create 2362 * 2363 * __ops_seckey_free() frees the memory associated with "key". Note that 2364 * the key itself is not freed. 2365 * 2366 * \param key 2367 */ 2368 2369 void 2370 __ops_seckey_free(__ops_seckey_t * key) 2371 { 2372 switch (key->pubkey.alg) { 2373 case OPS_PKA_RSA: 2374 case OPS_PKA_RSA_ENCRYPT_ONLY: 2375 case OPS_PKA_RSA_SIGN_ONLY: 2376 free_BN(&key->key.rsa.d); 2377 free_BN(&key->key.rsa.p); 2378 free_BN(&key->key.rsa.q); 2379 free_BN(&key->key.rsa.u); 2380 break; 2381 2382 case OPS_PKA_DSA: 2383 free_BN(&key->key.dsa.x); 2384 break; 2385 2386 default: 2387 (void) fprintf(stderr, 2388 "__ops_seckey_free: Unknown algorithm: %d (%s)\n", 2389 key->pubkey.alg, 2390 __ops_show_pka(key->pubkey.alg)); 2391 } 2392 2393 __ops_pubkey_free(&key->pubkey); 2394 } 2395 2396 static int 2397 consume_packet(__ops_region_t * region, __ops_parseinfo_t * pinfo, 2398 bool warn) 2399 { 2400 __ops_packet_t pkt; 2401 __ops_data_t remainder; 2402 2403 if (region->indeterminate) { 2404 ERRP(&pinfo->cbinfo, pkt, 2405 "Can't consume indeterminate packets"); 2406 } 2407 2408 if (read_data(&remainder, region, pinfo)) { 2409 /* now throw it away */ 2410 data_free(&remainder); 2411 if (warn) { 2412 OPS_ERROR(&pinfo->errors, OPS_E_P_PACKET_CONSUMED, 2413 "Warning: packet consumer"); 2414 } 2415 return 1; 2416 } 2417 OPS_ERROR(&pinfo->errors, OPS_E_P_PACKET_NOT_CONSUMED, 2418 (warn) ? "Warning: Packet was not consumed" : 2419 "Packet was not consumed"); 2420 return warn; 2421 } 2422 2423 /** 2424 * \ingroup Core_ReadPackets 2425 * \brief Parse a secret key 2426 */ 2427 static int 2428 parse_seckey(__ops_region_t * region, __ops_parseinfo_t * pinfo) 2429 { 2430 __ops_packet_t pkt; 2431 __ops_region_t encregion; 2432 __ops_region_t *saved_region = NULL; 2433 unsigned char c[1] = ""; 2434 __ops_crypt_t decrypt; 2435 __ops_hash_t checkhash; 2436 unsigned blocksize; 2437 bool crypted; 2438 int ret = 1; 2439 2440 if (__ops_get_debug_level(__FILE__)) { 2441 fprintf(stderr, "\n---------\nparse_seckey:\n"); 2442 fprintf(stderr, 2443 "region length=%d, length_read=%d, remainder=%d\n", 2444 region->length, region->length_read, 2445 region->length - region->length_read); 2446 } 2447 (void) memset(&pkt, 0x0, sizeof(pkt)); 2448 if (!parse_pubkey_data(&pkt.u.seckey.pubkey, region, pinfo)) 2449 return 0; 2450 2451 if (__ops_get_debug_level(__FILE__)) { 2452 fprintf(stderr, "parse_seckey: public key parsed\n"); 2453 __ops_print_pubkey(&pkt.u.seckey.pubkey); 2454 } 2455 pinfo->reading_v3_secret = pkt.u.seckey.pubkey.version != OPS_V4; 2456 2457 if (!limited_read(c, 1, region, pinfo)) { 2458 return 0; 2459 } 2460 pkt.u.seckey.s2k_usage = c[0]; 2461 #if 0 2462 if (pkt.u.seckey.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED) 2463 checksum_length = 20; 2464 #endif 2465 2466 if (pkt.u.seckey.s2k_usage == OPS_S2KU_ENCRYPTED || 2467 pkt.u.seckey.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED) { 2468 if (!limited_read(c, 1, region, pinfo)) { 2469 return 0; 2470 } 2471 pkt.u.seckey.alg = c[0]; 2472 if (!limited_read(c, 1, region, pinfo)) { 2473 return 0; 2474 } 2475 pkt.u.seckey.s2k_specifier = c[0]; 2476 switch (pkt.u.seckey.s2k_specifier) { 2477 case OPS_S2KS_SIMPLE: 2478 case OPS_S2KS_SALTED: 2479 case OPS_S2KS_ITERATED_AND_SALTED: 2480 break; 2481 default: 2482 (void) fprintf(stderr, 2483 "parse_seckey: bad seckey\n"); 2484 return 0; 2485 } 2486 if (!limited_read(c, 1, region, pinfo)) { 2487 return 0; 2488 } 2489 pkt.u.seckey.hash_alg = c[0]; 2490 if (pkt.u.seckey.s2k_specifier != OPS_S2KS_SIMPLE && 2491 !limited_read(pkt.u.seckey.salt, 8, region, 2492 pinfo)) { 2493 return 0; 2494 } 2495 if (pkt.u.seckey.s2k_specifier == 2496 OPS_S2KS_ITERATED_AND_SALTED) { 2497 if (!limited_read(c, 1, region, pinfo)) { 2498 return 0; 2499 } 2500 pkt.u.seckey.octetc = 2501 (16 + ((unsigned)c[0] & 15)) << 2502 (((unsigned)c[0] >> 4) + 6); 2503 } 2504 } else if (pkt.u.seckey.s2k_usage != OPS_S2KU_NONE) { 2505 /* this is V3 style, looks just like a V4 simple hash */ 2506 pkt.u.seckey.alg = c[0]; 2507 pkt.u.seckey.s2k_usage = OPS_S2KU_ENCRYPTED; 2508 pkt.u.seckey.s2k_specifier = OPS_S2KS_SIMPLE; 2509 pkt.u.seckey.hash_alg = OPS_HASH_MD5; 2510 } 2511 crypted = pkt.u.seckey.s2k_usage == OPS_S2KU_ENCRYPTED || 2512 pkt.u.seckey.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED; 2513 2514 if (crypted) { 2515 int n; 2516 __ops_packet_t seckey; 2517 char *passphrase; 2518 unsigned char key[OPS_MAX_KEY_SIZE + OPS_MAX_HASH_SIZE]; 2519 __ops_hash_t hashes[(OPS_MAX_KEY_SIZE + OPS_MIN_HASH_SIZE - 1) / OPS_MIN_HASH_SIZE]; 2520 size_t passlen; 2521 int keysize; 2522 int hashsize; 2523 2524 blocksize = __ops_block_size(pkt.u.seckey.alg); 2525 if (blocksize == 0 || blocksize > OPS_MAX_BLOCK_SIZE) { 2526 (void) fprintf(stderr, 2527 "parse_seckey: bad blocksize\n"); 2528 return 0; 2529 } 2530 2531 if (!limited_read(pkt.u.seckey.iv, blocksize, region, 2532 pinfo)) { 2533 return 0; 2534 } 2535 (void) memset(&seckey, 0x0, sizeof(seckey)); 2536 passphrase = NULL; 2537 seckey.u.skey_passphrase.passphrase = &passphrase; 2538 seckey.u.skey_passphrase.seckey = 2539 &pkt.u.seckey; 2540 CALLBACK(&pinfo->cbinfo, OPS_PARSER_CMD_GET_SK_PASSPHRASE, 2541 &seckey); 2542 if (!passphrase) { 2543 if (__ops_get_debug_level(__FILE__)) { 2544 /* \todo make into proper error */ 2545 (void) fprintf(stderr, 2546 "parse_seckey: can't get passphrase\n"); 2547 } 2548 if (!consume_packet(region, pinfo, false)) { 2549 return 0; 2550 } 2551 2552 CALLBACK(&pinfo->cbinfo, 2553 OPS_PTAG_CT_ENCRYPTED_SECRET_KEY, &pkt); 2554 2555 return 1; 2556 } 2557 keysize = __ops_key_size(pkt.u.seckey.alg); 2558 if (keysize == 0 || keysize > OPS_MAX_KEY_SIZE) { 2559 (void) fprintf(stderr, 2560 "parse_seckey: bad keysize\n"); 2561 return 0; 2562 } 2563 2564 hashsize = __ops_hash_size(pkt.u.seckey.hash_alg); 2565 if (hashsize == 0 || hashsize > OPS_MAX_HASH_SIZE) { 2566 (void) fprintf(stderr, 2567 "parse_seckey: bad hashsize\n"); 2568 return 0; 2569 } 2570 2571 for (n = 0; n * hashsize < keysize; ++n) { 2572 int i; 2573 2574 __ops_hash_any(&hashes[n], 2575 pkt.u.seckey.hash_alg); 2576 hashes[n].init(&hashes[n]); 2577 /* preload hashes with zeroes... */ 2578 for (i = 0; i < n; ++i) { 2579 hashes[n].add(&hashes[n], 2580 (const unsigned char *) "", 1); 2581 } 2582 } 2583 passlen = strlen(passphrase); 2584 for (n = 0; n * hashsize < keysize; ++n) { 2585 unsigned i; 2586 2587 switch (pkt.u.seckey.s2k_specifier) { 2588 case OPS_S2KS_SALTED: 2589 hashes[n].add(&hashes[n], 2590 pkt.u.seckey.salt, 2591 OPS_SALT_SIZE); 2592 /* FALLTHROUGH */ 2593 case OPS_S2KS_SIMPLE: 2594 hashes[n].add(&hashes[n], 2595 (unsigned char *) passphrase, passlen); 2596 break; 2597 2598 case OPS_S2KS_ITERATED_AND_SALTED: 2599 for (i = 0; i < pkt.u.seckey.octetc; 2600 i += passlen + OPS_SALT_SIZE) { 2601 unsigned j; 2602 2603 j = passlen + OPS_SALT_SIZE; 2604 if (i + j > pkt.u.seckey.octetc && i != 0) { 2605 j = pkt.u.seckey.octetc - i; 2606 } 2607 hashes[n].add(&hashes[n], 2608 pkt.u.seckey.salt, 2609 (unsigned)(j > OPS_SALT_SIZE) ? 2610 OPS_SALT_SIZE : j); 2611 if (j > OPS_SALT_SIZE) { 2612 hashes[n].add(&hashes[n], 2613 (unsigned char *) passphrase, 2614 j - OPS_SALT_SIZE); 2615 } 2616 } 2617 2618 } 2619 } 2620 2621 for (n = 0; n * hashsize < keysize; ++n) { 2622 int r; 2623 2624 r = hashes[n].finish(&hashes[n], key + n * hashsize); 2625 if (r != hashsize) { 2626 (void) fprintf(stderr, 2627 "parse_seckey: bad r\n"); 2628 return 0; 2629 } 2630 } 2631 2632 (void) memset(passphrase, 0x0, passlen); 2633 (void) free(passphrase); 2634 2635 __ops_crypt_any(&decrypt, pkt.u.seckey.alg); 2636 if (__ops_get_debug_level(__FILE__)) { 2637 unsigned int i = 0; 2638 fprintf(stderr, "\nREADING:\niv="); 2639 for (i = 0; 2640 i < __ops_block_size(pkt.u.seckey.alg); 2641 i++) { 2642 fprintf(stderr, "%02x ", pkt.u.seckey.iv[i]); 2643 } 2644 fprintf(stderr, "\n"); 2645 fprintf(stderr, "key="); 2646 for (i = 0; i < CAST_KEY_LENGTH; i++) { 2647 fprintf(stderr, "%02x ", key[i]); 2648 } 2649 fprintf(stderr, "\n"); 2650 } 2651 decrypt.set_iv(&decrypt, pkt.u.seckey.iv); 2652 decrypt.set_key(&decrypt, key); 2653 2654 /* now read encrypted data */ 2655 2656 __ops_reader_push_decrypt(pinfo, &decrypt, region); 2657 2658 /* 2659 * Since all known encryption for PGP doesn't compress, we 2660 * can limit to the same length as the current region (for 2661 * now). 2662 */ 2663 __ops_init_subregion(&encregion, NULL); 2664 encregion.length = region->length - region->length_read; 2665 if (pkt.u.seckey.pubkey.version != OPS_V4) { 2666 encregion.length -= 2; 2667 } 2668 saved_region = region; 2669 region = &encregion; 2670 } 2671 if (pkt.u.seckey.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED) { 2672 __ops_hash_sha1(&checkhash); 2673 __ops_reader_push_hash(pinfo, &checkhash); 2674 } else { 2675 __ops_reader_push_sum16(pinfo); 2676 } 2677 2678 switch (pkt.u.seckey.pubkey.alg) { 2679 case OPS_PKA_RSA: 2680 case OPS_PKA_RSA_ENCRYPT_ONLY: 2681 case OPS_PKA_RSA_SIGN_ONLY: 2682 if (!limited_read_mpi(&pkt.u.seckey.key.rsa.d, region, 2683 pinfo) || 2684 !limited_read_mpi(&pkt.u.seckey.key.rsa.p, region, 2685 pinfo) || 2686 !limited_read_mpi(&pkt.u.seckey.key.rsa.q, region, 2687 pinfo) || 2688 !limited_read_mpi(&pkt.u.seckey.key.rsa.u, region, 2689 pinfo)) { 2690 ret = 0; 2691 } 2692 break; 2693 2694 case OPS_PKA_DSA: 2695 2696 if (!limited_read_mpi(&pkt.u.seckey.key.dsa.x, region, 2697 pinfo)) { 2698 ret = 0; 2699 } 2700 break; 2701 2702 default: 2703 OPS_ERROR_2(&pinfo->errors, 2704 OPS_E_ALG_UNSUPPORTED_PUBLIC_KEY_ALG, 2705 "Unsupported Public Key algorithm %d (%s)", 2706 pkt.u.seckey.pubkey.alg, 2707 __ops_show_pka(pkt.u.seckey.pubkey.alg)); 2708 ret = 0; 2709 } 2710 2711 if (__ops_get_debug_level(__FILE__)) { 2712 (void) fprintf(stderr, "4 MPIs read\n"); 2713 } 2714 pinfo->reading_v3_secret = false; 2715 2716 if (pkt.u.seckey.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED) { 2717 unsigned char hash[20]; 2718 2719 __ops_reader_pop_hash(pinfo); 2720 checkhash.finish(&checkhash, hash); 2721 2722 if (crypted && 2723 pkt.u.seckey.pubkey.version != OPS_V4) { 2724 __ops_reader_pop_decrypt(pinfo); 2725 region = saved_region; 2726 } 2727 if (ret) { 2728 if (!limited_read(pkt.u.seckey.checkhash, 2729 20, region, pinfo)) { 2730 return 0; 2731 } 2732 2733 if (memcmp(hash, pkt.u.seckey.checkhash, 20)) { 2734 ERRP(&pinfo->cbinfo, pkt, 2735 "Hash mismatch in secret key"); 2736 } 2737 } 2738 } else { 2739 unsigned short sum; 2740 2741 sum = __ops_reader_pop_sum16(pinfo); 2742 if (crypted && 2743 pkt.u.seckey.pubkey.version != OPS_V4) { 2744 __ops_reader_pop_decrypt(pinfo); 2745 region = saved_region; 2746 } 2747 if (ret) { 2748 if (!limited_read_scalar(&pkt.u.seckey.checksum, 2, 2749 region, pinfo)) 2750 return 0; 2751 2752 if (sum != pkt.u.seckey.checksum) { 2753 ERRP(&pinfo->cbinfo, pkt, 2754 "Checksum mismatch in secret key"); 2755 } 2756 } 2757 } 2758 2759 if (crypted && pkt.u.seckey.pubkey.version == OPS_V4) { 2760 __ops_reader_pop_decrypt(pinfo); 2761 } 2762 if (ret && region->length_read != region->length) { 2763 (void) fprintf(stderr, "parse_seckey: bad length\n"); 2764 return 0; 2765 } 2766 if (!ret) { 2767 return 0; 2768 } 2769 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_SECRET_KEY, &pkt); 2770 if (__ops_get_debug_level(__FILE__)) { 2771 (void) fprintf(stderr, "--- end of parse_seckey\n\n"); 2772 } 2773 return 1; 2774 } 2775 2776 /** 2777 \ingroup Core_ReadPackets 2778 \brief Parse a Public Key Session Key packet 2779 */ 2780 static int 2781 parse_pk_sesskey(__ops_region_t * region, 2782 __ops_parseinfo_t * pinfo) 2783 { 2784 const __ops_seckey_t *secret; 2785 __ops_packet_t sesskey; 2786 __ops_packet_t pkt; 2787 unsigned char *iv; 2788 unsigned char c[1] = ""; 2789 unsigned char cs[2]; 2790 unsigned k; 2791 BIGNUM *enc_m; 2792 int n; 2793 2794 /* Can't rely on it being CAST5 */ 2795 /* \todo FIXME RW */ 2796 /* const size_t sz_unencoded_m_buf=CAST_KEY_LENGTH+1+2; */ 2797 unsigned char unencoded_m_buf[1024]; 2798 2799 if (!limited_read(c, 1, region, pinfo)) { 2800 return 0; 2801 } 2802 pkt.u.pk_sesskey.version = c[0]; 2803 if (pkt.u.pk_sesskey.version != OPS_PKSK_V3) { 2804 OPS_ERROR_1(&pinfo->errors, OPS_E_PROTO_BAD_PKSK_VRSN, 2805 "Bad public-key encrypted session key version (%d)", 2806 pkt.u.pk_sesskey.version); 2807 return 0; 2808 } 2809 if (!limited_read(pkt.u.pk_sesskey.key_id, 2810 sizeof(pkt.u.pk_sesskey.key_id), region, pinfo)) { 2811 return 0; 2812 } 2813 if (__ops_get_debug_level(__FILE__)) { 2814 int i; 2815 int x = sizeof(pkt.u.pk_sesskey.key_id); 2816 2817 printf("session key: public key id: x=%d\n", x); 2818 for (i = 0; i < x; i++) { 2819 printf("%2x ", pkt.u.pk_sesskey.key_id[i]); 2820 } 2821 printf("\n"); 2822 } 2823 if (!limited_read(c, 1, region, pinfo)) { 2824 return 0; 2825 } 2826 pkt.u.pk_sesskey.alg = c[0]; 2827 switch (pkt.u.pk_sesskey.alg) { 2828 case OPS_PKA_RSA: 2829 if (!limited_read_mpi( 2830 &pkt.u.pk_sesskey.parameters.rsa.encrypted_m, 2831 region, pinfo)) { 2832 return 0; 2833 } 2834 enc_m = pkt.u.pk_sesskey.parameters.rsa.encrypted_m; 2835 break; 2836 2837 case OPS_PKA_ELGAMAL: 2838 if (!limited_read_mpi( 2839 &pkt.u.pk_sesskey.parameters.elgamal.g_to_k, 2840 region, pinfo) || 2841 !limited_read_mpi( 2842 &pkt.u.pk_sesskey.parameters.elgamal.encrypted_m, 2843 region, pinfo)) { 2844 return 0; 2845 } 2846 enc_m = pkt.u.pk_sesskey.parameters.elgamal.encrypted_m; 2847 break; 2848 2849 default: 2850 OPS_ERROR_1(&pinfo->errors, 2851 OPS_E_ALG_UNSUPPORTED_PUBLIC_KEY_ALG, 2852 "Unknown public key algorithm in session key (%s)", 2853 __ops_show_pka(pkt.u.pk_sesskey.alg)); 2854 return 0; 2855 } 2856 2857 (void) memset(&sesskey, 0x0, sizeof(sesskey)); 2858 secret = NULL; 2859 sesskey.u.get_seckey.seckey = &secret; 2860 sesskey.u.get_seckey.pk_sesskey = &pkt.u.pk_sesskey; 2861 2862 CALLBACK(&pinfo->cbinfo, OPS_PARSER_CMD_GET_SECRET_KEY, &sesskey); 2863 2864 if (!secret) { 2865 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY, 2866 &pkt); 2867 return 1; 2868 } 2869 /* n=__ops_decrypt_mpi(buf,sizeof(buf),enc_m,secret); */ 2870 n = __ops_decrypt_and_unencode_mpi(unencoded_m_buf, 2871 sizeof(unencoded_m_buf), enc_m, secret); 2872 2873 if (n < 1) { 2874 ERRP(&pinfo->cbinfo, pkt, "decrypted message too short"); 2875 return 0; 2876 } 2877 /* PKA */ 2878 pkt.u.pk_sesskey.symm_alg = unencoded_m_buf[0]; 2879 2880 if (!__ops_is_sa_supported(pkt.u.pk_sesskey.symm_alg)) { 2881 /* ERR1P */ 2882 OPS_ERROR_1(&pinfo->errors, OPS_E_ALG_UNSUPPORTED_SYMMETRIC_ALG, 2883 "Symmetric algorithm %s not supported", 2884 __ops_show_symm_alg( 2885 pkt.u.pk_sesskey.symm_alg)); 2886 return 0; 2887 } 2888 k = __ops_key_size(pkt.u.pk_sesskey.symm_alg); 2889 2890 if ((unsigned) n != k + 3) { 2891 OPS_ERROR_2(&pinfo->errors, OPS_E_PROTO_DECRYPTED_MSG_WRONG_LEN, 2892 "decrypted message wrong length (got %d expected %d)", 2893 n, k + 3); 2894 return 0; 2895 } 2896 if (k > sizeof(pkt.u.pk_sesskey.key)) { 2897 (void) fprintf(stderr, "parse_pk_sesskey: bad keylength\n"); 2898 return 0; 2899 } 2900 2901 (void) memcpy(pkt.u.pk_sesskey.key, unencoded_m_buf + 1, k); 2902 2903 if (__ops_get_debug_level(__FILE__)) { 2904 unsigned int j; 2905 printf("session key recovered (len=%d):\n", k); 2906 for (j = 0; j < k; j++) 2907 printf("%2x ", pkt.u.pk_sesskey.key[j]); 2908 printf("\n"); 2909 } 2910 pkt.u.pk_sesskey.checksum = unencoded_m_buf[k + 1] + 2911 (unencoded_m_buf[k + 2] << 8); 2912 if (__ops_get_debug_level(__FILE__)) { 2913 printf("session key checksum: %2x %2x\n", 2914 unencoded_m_buf[k + 1], unencoded_m_buf[k + 2]); 2915 } 2916 /* Check checksum */ 2917 2918 __ops_calc_sesskey_checksum(&pkt.u.pk_sesskey, &cs[0]); 2919 if (unencoded_m_buf[k + 1] != cs[0] || 2920 unencoded_m_buf[k + 2] != cs[1]) { 2921 OPS_ERROR_4(&pinfo->errors, OPS_E_PROTO_BAD_SK_CHECKSUM, 2922 "Session key checksum wrong: expected %2x %2x, got %2x %2x", 2923 cs[0], cs[1], unencoded_m_buf[k + 1], 2924 unencoded_m_buf[k + 2]); 2925 return 0; 2926 } 2927 /* all is well */ 2928 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_PK_SESSION_KEY, &pkt); 2929 2930 __ops_crypt_any(&pinfo->decrypt, 2931 pkt.u.pk_sesskey.symm_alg); 2932 iv = calloc(1, pinfo->decrypt.blocksize); 2933 pinfo->decrypt.set_iv(&pinfo->decrypt, iv); 2934 pinfo->decrypt.set_key(&pinfo->decrypt, pkt.u.pk_sesskey.key); 2935 __ops_encrypt_init(&pinfo->decrypt); 2936 (void) free(iv); 2937 return 1; 2938 } 2939 2940 static int 2941 __ops_decrypt_se_data(__ops_content_tag_t tag, __ops_region_t * region, 2942 __ops_parseinfo_t * pinfo) 2943 { 2944 int r = 1; 2945 __ops_crypt_t *decrypt = __ops_parse_get_decrypt(pinfo); 2946 2947 if (decrypt) { 2948 unsigned char buf[OPS_MAX_BLOCK_SIZE + 2] = ""; 2949 size_t b = decrypt->blocksize; 2950 /* __ops_packet_t pkt; */ 2951 __ops_region_t encregion; 2952 2953 2954 __ops_reader_push_decrypt(pinfo, decrypt, region); 2955 2956 __ops_init_subregion(&encregion, NULL); 2957 encregion.length = b + 2; 2958 2959 if (!exact_limited_read(buf, b + 2, &encregion, pinfo)) 2960 return 0; 2961 2962 if (buf[b - 2] != buf[b] || buf[b - 1] != buf[b + 1]) { 2963 __ops_reader_pop_decrypt(pinfo); 2964 OPS_ERROR_4(&pinfo->errors, 2965 OPS_E_PROTO_BAD_SYMMETRIC_DECRYPT, 2966 "Bad symmetric decrypt (%02x%02x vs %02x%02x)", 2967 buf[b - 2], buf[b - 1], buf[b], buf[b + 1]); 2968 return 0; 2969 } 2970 if (tag == OPS_PTAG_CT_SE_DATA_BODY) { 2971 decrypt->decrypt_resync(decrypt); 2972 decrypt->block_encrypt(decrypt, decrypt->civ, 2973 decrypt->civ); 2974 } 2975 r = __ops_parse(pinfo, 0); 2976 2977 __ops_reader_pop_decrypt(pinfo); 2978 } else { 2979 __ops_packet_t pkt; 2980 2981 while (region->length_read < region->length) { 2982 unsigned len; 2983 2984 len = region->length - region->length_read; 2985 if (len > sizeof(pkt.u.se_data_body.data)) 2986 len = sizeof(pkt.u.se_data_body.data); 2987 2988 if (!limited_read(pkt.u.se_data_body.data, len, 2989 region, pinfo)) { 2990 return 0; 2991 } 2992 pkt.u.se_data_body.length = len; 2993 CALLBACK(&pinfo->cbinfo, tag, &pkt); 2994 } 2995 } 2996 2997 return r; 2998 } 2999 3000 static int 3001 __ops_decrypt_se_ip_data(__ops_content_tag_t tag, __ops_region_t * region, 3002 __ops_parseinfo_t * pinfo) 3003 { 3004 int r = 1; 3005 __ops_crypt_t *decrypt = __ops_parse_get_decrypt(pinfo); 3006 3007 if (decrypt) { 3008 __ops_reader_push_decrypt(pinfo, decrypt, region); 3009 __ops_reader_push_se_ip_data(pinfo, decrypt, region); 3010 3011 r = __ops_parse(pinfo, 0); 3012 3013 __ops_reader_pop_se_ip_data(pinfo); 3014 __ops_reader_pop_decrypt(pinfo); 3015 } else { 3016 __ops_packet_t pkt; 3017 3018 while (region->length_read < region->length) { 3019 unsigned len; 3020 3021 len = region->length - region->length_read; 3022 if (len > sizeof(pkt.u.se_data_body.data)) { 3023 len = sizeof(pkt.u.se_data_body.data); 3024 } 3025 3026 if (!limited_read(pkt.u.se_data_body.data, 3027 len, region, pinfo)) { 3028 return 0; 3029 } 3030 3031 pkt.u.se_data_body.length = len; 3032 3033 CALLBACK(&pinfo->cbinfo, tag, &pkt); 3034 } 3035 } 3036 3037 return r; 3038 } 3039 3040 /** 3041 \ingroup Core_ReadPackets 3042 \brief Read a Symmetrically Encrypted packet 3043 */ 3044 static int 3045 parse_se_data(__ops_region_t * region, __ops_parseinfo_t * pinfo) 3046 { 3047 __ops_packet_t pkt; 3048 3049 /* there's no info to go with this, so just announce it */ 3050 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_SE_DATA_HEADER, &pkt); 3051 3052 /* 3053 * The content of an encrypted data packet is more OpenPGP packets 3054 * once decrypted, so recursively handle them 3055 */ 3056 return __ops_decrypt_se_data(OPS_PTAG_CT_SE_DATA_BODY, region, pinfo); 3057 } 3058 3059 /** 3060 \ingroup Core_ReadPackets 3061 \brief Read a Symmetrically Encrypted Integrity Protected packet 3062 */ 3063 static int 3064 parse_se_ip_data(__ops_region_t * region, __ops_parseinfo_t * pinfo) 3065 { 3066 unsigned char c[1] = ""; 3067 __ops_packet_t pkt; 3068 3069 if (!limited_read(c, 1, region, pinfo)) 3070 return 0; 3071 pkt.u.se_ip_data_header.version = c[0]; 3072 3073 if (pkt.u.se_ip_data_header.version != OPS_SE_IP_V1) { 3074 (void) fprintf(stderr, "parse_se_ip_data: bad version\n"); 3075 return 0; 3076 } 3077 3078 /* 3079 * The content of an encrypted data packet is more OpenPGP packets 3080 * once decrypted, so recursively handle them 3081 */ 3082 return __ops_decrypt_se_ip_data(OPS_PTAG_CT_SE_IP_DATA_BODY, region, 3083 pinfo); 3084 } 3085 3086 /** 3087 \ingroup Core_ReadPackets 3088 \brief Read a MDC packet 3089 */ 3090 static int 3091 parse_mdc(__ops_region_t * region, __ops_parseinfo_t * pinfo) 3092 { 3093 __ops_packet_t pkt; 3094 3095 if (!limited_read((unsigned char *)(void *)&pkt.u.mdc, 3096 OPS_SHA1_HASH_SIZE, region, pinfo)) { 3097 return 0; 3098 } 3099 CALLBACK(&pinfo->cbinfo, OPS_PTAG_CT_MDC, &pkt); 3100 return 1; 3101 } 3102 3103 /** 3104 * \ingroup Core_ReadPackets 3105 * \brief Parse one packet. 3106 * 3107 * This function parses the packet tag. It computes the value of the 3108 * content tag and then calls the appropriate function to handle the 3109 * content. 3110 * 3111 * \param *pinfo How to parse 3112 * \param *pktlen On return, will contain number of bytes in packet 3113 * \return 1 on success, 0 on error, -1 on EOF */ 3114 static int 3115 __ops_parse_packet(__ops_parseinfo_t * pinfo, unsigned long *pktlen) 3116 { 3117 __ops_packet_t pkt; 3118 __ops_region_t region; 3119 unsigned char ptag[1]; 3120 bool indeterminate = false; 3121 int ret; 3122 3123 pkt.u.ptag.position = pinfo->readinfo.position; 3124 3125 ret = base_read(ptag, 1, pinfo); 3126 3127 if (__ops_get_debug_level(__FILE__)) { 3128 (void) fprintf(stderr, 3129 "__ops_parse_packet: base_read returned %d\n", 3130 ret); 3131 } 3132 3133 /* errors in the base read are effectively EOF. */ 3134 if (ret <= 0) { 3135 return -1; 3136 } 3137 3138 *pktlen = 0; 3139 3140 if (!(*ptag & OPS_PTAG_ALWAYS_SET)) { 3141 pkt.u.error.error = "Format error (ptag bit not set)"; 3142 CALLBACK(&pinfo->cbinfo, OPS_PARSER_ERROR, &pkt); 3143 return 0; 3144 } 3145 pkt.u.ptag.new_format = !!(*ptag & OPS_PTAG_NEW_FORMAT); 3146 if (pkt.u.ptag.new_format) { 3147 pkt.u.ptag.type = 3148 *ptag & OPS_PTAG_NF_CONTENT_TAG_MASK; 3149 pkt.u.ptag.length_type = 0; 3150 if (!read_new_length(&pkt.u.ptag.length, pinfo)) { 3151 return 0; 3152 } 3153 } else { 3154 bool rb; 3155 3156 rb = false; 3157 pkt.u.ptag.type = ((unsigned)*ptag & 3158 OPS_PTAG_OF_CONTENT_TAG_MASK) 3159 >> OPS_PTAG_OF_CONTENT_TAG_SHIFT; 3160 pkt.u.ptag.length_type = 3161 *ptag & OPS_PTAG_OF_LENGTH_TYPE_MASK; 3162 switch (pkt.u.ptag.length_type) { 3163 case OPS_PTAG_OLD_LEN_1: 3164 rb = _read_scalar(&pkt.u.ptag.length, 1, pinfo); 3165 break; 3166 3167 case OPS_PTAG_OLD_LEN_2: 3168 rb = _read_scalar(&pkt.u.ptag.length, 2, pinfo); 3169 break; 3170 3171 case OPS_PTAG_OLD_LEN_4: 3172 rb = _read_scalar(&pkt.u.ptag.length, 4, pinfo); 3173 break; 3174 3175 case OPS_PTAG_OLD_LEN_INDETERMINATE: 3176 pkt.u.ptag.length = 0; 3177 indeterminate = true; 3178 rb = true; 3179 break; 3180 } 3181 if (!rb) { 3182 return 0; 3183 } 3184 } 3185 3186 CALLBACK(&pinfo->cbinfo, OPS_PARSER_PTAG, &pkt); 3187 3188 __ops_init_subregion(®ion, NULL); 3189 region.length = pkt.u.ptag.length; 3190 region.indeterminate = indeterminate; 3191 if (__ops_get_debug_level(__FILE__)) { 3192 (void) fprintf(stderr, "__ops_parse_packet: type %d\n", 3193 pkt.u.ptag.type); 3194 } 3195 switch (pkt.u.ptag.type) { 3196 case OPS_PTAG_CT_SIGNATURE: 3197 ret = parse_sig(®ion, pinfo); 3198 break; 3199 3200 case OPS_PTAG_CT_PUBLIC_KEY: 3201 case OPS_PTAG_CT_PUBLIC_SUBKEY: 3202 ret = parse_pubkey(pkt.u.ptag.type, ®ion, 3203 pinfo); 3204 break; 3205 3206 case OPS_PTAG_CT_TRUST: 3207 ret = parse_trust(®ion, pinfo); 3208 break; 3209 3210 case OPS_PTAG_CT_USER_ID: 3211 ret = parse_user_id(®ion, pinfo); 3212 break; 3213 3214 case OPS_PTAG_CT_COMPRESSED: 3215 ret = parse_compressed(®ion, pinfo); 3216 break; 3217 3218 case OPS_PTAG_CT_ONE_PASS_SIGNATURE: 3219 ret = parse_one_pass(®ion, pinfo); 3220 break; 3221 3222 case OPS_PTAG_CT_LITERAL_DATA: 3223 ret = parse_litdata(®ion, pinfo); 3224 break; 3225 3226 case OPS_PTAG_CT_USER_ATTRIBUTE: 3227 ret = parse_user_attribute(®ion, pinfo); 3228 break; 3229 3230 case OPS_PTAG_CT_SECRET_KEY: 3231 ret = parse_seckey(®ion, pinfo); 3232 break; 3233 3234 case OPS_PTAG_CT_SECRET_SUBKEY: 3235 ret = parse_seckey(®ion, pinfo); 3236 break; 3237 3238 case OPS_PTAG_CT_PK_SESSION_KEY: 3239 ret = parse_pk_sesskey(®ion, pinfo); 3240 break; 3241 3242 case OPS_PTAG_CT_SE_DATA: 3243 ret = parse_se_data(®ion, pinfo); 3244 break; 3245 3246 case OPS_PTAG_CT_SE_IP_DATA: 3247 ret = parse_se_ip_data(®ion, pinfo); 3248 break; 3249 3250 case OPS_PTAG_CT_MDC: 3251 ret = parse_mdc(®ion, pinfo); 3252 break; 3253 3254 default: 3255 OPS_ERROR_1(&pinfo->errors, OPS_E_P_UNKNOWN_TAG, 3256 "Unknown content tag 0x%x", 3257 pkt.u.ptag.type); 3258 ret = 0; 3259 } 3260 3261 /* Ensure that the entire packet has been consumed */ 3262 3263 if (region.length != region.length_read && !region.indeterminate) { 3264 if (!consume_packet(®ion, pinfo, false)) { 3265 ret = -1; 3266 } 3267 } 3268 3269 /* also consume it if there's been an error? */ 3270 /* \todo decide what to do about an error on an */ 3271 /* indeterminate packet */ 3272 if (ret == 0) { 3273 if (!consume_packet(®ion, pinfo, false)) { 3274 ret = -1; 3275 } 3276 } 3277 /* set pktlen */ 3278 3279 *pktlen = pinfo->readinfo.alength; 3280 3281 /* do callback on entire packet, if desired and there was no error */ 3282 3283 if (ret > 0 && pinfo->readinfo.accumulate) { 3284 pkt.u.packet.length = pinfo->readinfo.alength; 3285 pkt.u.packet.raw = pinfo->readinfo.accumulated; 3286 pinfo->readinfo.accumulated = NULL; 3287 pinfo->readinfo.asize = 0; 3288 CALLBACK(&pinfo->cbinfo, OPS_PARSER_PACKET_END, &pkt); 3289 } 3290 pinfo->readinfo.alength = 0; 3291 3292 return (ret < 0) ? -1 : (ret) ? 1 : 0; 3293 } 3294 3295 /** 3296 * \ingroup Core_ReadPackets 3297 * 3298 * \brief Parse packets from an input stream until EOF or error. 3299 * 3300 * \details Setup the necessary parsing configuration in "pinfo" 3301 * before calling __ops_parse(). 3302 * 3303 * That information includes : 3304 * 3305 * - a "reader" function to be used to get the data to be parsed 3306 * 3307 * - a "callback" function to be called when this library has identified 3308 * a parseable object within the data 3309 * 3310 * - whether the calling function wants the signature subpackets 3311 * returned raw, parsed or not at all. 3312 * 3313 * After returning, pinfo->errors holds any errors encountered while parsing. 3314 * 3315 * \param pinfo Parsing configuration 3316 * \return 1 on success in all packets, 0 on error in any packet 3317 * 3318 * \sa CoreAPI Overview 3319 * 3320 * \sa __ops_print_errors() 3321 * 3322 */ 3323 3324 int 3325 __ops_parse(__ops_parseinfo_t *pinfo, int perrors) 3326 { 3327 unsigned long pktlen; 3328 int r; 3329 3330 if (pinfo->synthlit) { 3331 #if 0 3332 r = __ops_parse_packet(pinfo->synthsig, &pktlen); 3333 r = __ops_parse_packet(pinfo->synthlit, &pktlen); 3334 #endif 3335 } 3336 do { 3337 r = __ops_parse_packet(pinfo, &pktlen); 3338 } while (r != -1); 3339 if (perrors) { 3340 __ops_print_errors(pinfo->errors); 3341 } 3342 return (pinfo->errors == NULL); 3343 } 3344 3345 /** 3346 * \ingroup Core_ReadPackets 3347 * 3348 * \brief Specifies whether one or more signature 3349 * subpacket types should be returned parsed; or raw; or ignored. 3350 * 3351 * \param pinfo Pointer to previously allocated structure 3352 * \param tag Packet tag. OPS_PTAG_SS_ALL for all SS tags; or one individual signature subpacket tag 3353 * \param type Parse type 3354 * \todo Make all packet types optional, not just subpackets */ 3355 void 3356 __ops_parse_options(__ops_parseinfo_t * pinfo, 3357 __ops_content_tag_t tag, 3358 __ops_parse_type_t type) 3359 { 3360 int t8, t7; 3361 3362 if (tag == OPS_PTAG_SS_ALL) { 3363 int n; 3364 3365 for (n = 0; n < 256; ++n) { 3366 __ops_parse_options(pinfo, 3367 OPS_PTAG_SIGNATURE_SUBPACKET_BASE + n, 3368 type); 3369 } 3370 return; 3371 } 3372 if (tag < OPS_PTAG_SIGNATURE_SUBPACKET_BASE || 3373 tag > OPS_PTAG_SIGNATURE_SUBPACKET_BASE + NTAGS - 1) { 3374 (void) fprintf(stderr, "__ops_parse_options: bad tag\n"); 3375 return; 3376 } 3377 t8 = (tag - OPS_PTAG_SIGNATURE_SUBPACKET_BASE) / 8; 3378 t7 = 1 << ((tag - OPS_PTAG_SIGNATURE_SUBPACKET_BASE) & 7); 3379 switch (type) { 3380 case OPS_PARSE_RAW: 3381 pinfo->ss_raw[t8] |= t7; 3382 pinfo->ss_parsed[t8] &= ~t7; 3383 break; 3384 3385 case OPS_PARSE_PARSED: 3386 pinfo->ss_raw[t8] &= ~t7; 3387 pinfo->ss_parsed[t8] |= t7; 3388 break; 3389 3390 case OPS_PARSE_IGNORE: 3391 pinfo->ss_raw[t8] &= ~t7; 3392 pinfo->ss_parsed[t8] &= ~t7; 3393 break; 3394 } 3395 } 3396 3397 /** 3398 \ingroup Core_ReadPackets 3399 \brief Creates a new zero-ed __ops_parseinfo_t struct 3400 \sa __ops_parseinfo_delete() 3401 */ 3402 __ops_parseinfo_t * 3403 __ops_parseinfo_new(void) 3404 { 3405 return calloc(1, sizeof(__ops_parseinfo_t)); 3406 } 3407 3408 /** 3409 \ingroup Core_ReadPackets 3410 \brief Free __ops_parseinfo_t struct and its contents 3411 \sa __ops_parseinfo_new() 3412 */ 3413 void 3414 __ops_parseinfo_delete(__ops_parseinfo_t * pinfo) 3415 { 3416 __ops_callback_data_t *cbinfo, *next; 3417 3418 for (cbinfo = pinfo->cbinfo.next; cbinfo; cbinfo = next) { 3419 next = cbinfo->next; 3420 (void) free(cbinfo); 3421 } 3422 if (pinfo->readinfo.destroyer) { 3423 pinfo->readinfo.destroyer(&pinfo->readinfo); 3424 } 3425 __ops_free_errors(pinfo->errors); 3426 if (pinfo->readinfo.accumulated) { 3427 (void) free(pinfo->readinfo.accumulated); 3428 } 3429 (void) free(pinfo); 3430 } 3431 3432 /** 3433 \ingroup Core_ReadPackets 3434 \brief Returns the parse_info's reader_info 3435 \return Pointer to the reader_info inside the parse_info 3436 */ 3437 __ops_reader_t * 3438 __ops_parse_get_rinfo(__ops_parseinfo_t * pinfo) 3439 { 3440 return &pinfo->readinfo; 3441 } 3442 3443 /** 3444 \ingroup Core_ReadPackets 3445 \brief Sets the parse_info's callback 3446 This is used when adding the first callback in a stack of callbacks. 3447 \sa __ops_parse_cb_push() 3448 */ 3449 3450 void 3451 __ops_parse_cb_set(__ops_parseinfo_t * pinfo, __ops_parse_cb_t * cb, void *arg) 3452 { 3453 pinfo->cbinfo.cb = cb; 3454 pinfo->cbinfo.arg = arg; 3455 pinfo->cbinfo.errors = &pinfo->errors; 3456 } 3457 3458 /** 3459 \ingroup Core_ReadPackets 3460 \brief Adds a further callback to a stack of callbacks 3461 \sa __ops_parse_cb_set() 3462 */ 3463 void 3464 __ops_parse_cb_push(__ops_parseinfo_t * pinfo, __ops_parse_cb_t * cb, void *arg) 3465 { 3466 __ops_callback_data_t *cbinfo = calloc(1, sizeof(*cbinfo)); 3467 3468 *cbinfo = pinfo->cbinfo; 3469 pinfo->cbinfo.next = cbinfo; 3470 __ops_parse_cb_set(pinfo, cb, arg); 3471 } 3472 3473 /** 3474 \ingroup Core_ReadPackets 3475 \brief Returns callback's arg 3476 */ 3477 void * 3478 __ops_parse_cb_get_arg(__ops_callback_data_t * cbinfo) 3479 { 3480 return cbinfo->arg; 3481 } 3482 3483 /** 3484 \ingroup Core_ReadPackets 3485 \brief Returns callback's errors 3486 */ 3487 void * 3488 __ops_parse_cb_get_errors(__ops_callback_data_t * cbinfo) 3489 { 3490 return cbinfo->errors; 3491 } 3492 3493 /** 3494 \ingroup Core_ReadPackets 3495 \brief Calls the parse_cb_info's callback if present 3496 \return Return value from callback, if present; else OPS_FINISHED 3497 */ 3498 __ops_parse_cb_return_t 3499 __ops_parse_cb(const __ops_packet_t * pkt, 3500 __ops_callback_data_t * cbinfo) 3501 { 3502 return (cbinfo->cb) ? cbinfo->cb(pkt, cbinfo) : OPS_FINISHED; 3503 } 3504 3505 /** 3506 \ingroup Core_ReadPackets 3507 \brief Calls the next callback in the stack 3508 \return Return value from callback 3509 */ 3510 __ops_parse_cb_return_t 3511 __ops_parse_stacked_cb(const __ops_packet_t * pkt, 3512 __ops_callback_data_t * cbinfo) 3513 { 3514 return __ops_parse_cb(pkt, cbinfo->next); 3515 } 3516 3517 /** 3518 \ingroup Core_ReadPackets 3519 \brief Returns the parse_info's errors 3520 \return parse_info's errors 3521 */ 3522 __ops_error_t * 3523 __ops_parseinfo_get_errors(__ops_parseinfo_t * pinfo) 3524 { 3525 return pinfo->errors; 3526 } 3527 3528 __ops_crypt_t * 3529 __ops_parse_get_decrypt(__ops_parseinfo_t * pinfo) 3530 { 3531 return (pinfo->decrypt.alg) ? &pinfo->decrypt : NULL; 3532 } 3533