1 /* $OpenBSD: signature.c,v 1.10 1999/10/26 22:31:39 angelos Exp $ */ 2 /* 3 * The author of this code is Angelos D. Keromytis (angelos@dsl.cis.upenn.edu) 4 * 5 * This code was written by Angelos D. Keromytis in Philadelphia, PA, USA, 6 * in April-May 1998 7 * 8 * Copyright (C) 1998, 1999 by Angelos D. Keromytis. 9 * 10 * Permission to use, copy, and modify this software without fee 11 * is hereby granted, provided that this entire notice is included in 12 * all copies of any software which is or includes a copy or 13 * modification of this software. 14 * 15 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 16 * IMPLIED WARRANTY. IN PARTICULAR, THE AUTHORS MAKES NO 17 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 18 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 19 * PURPOSE. 20 */ 21 22 /* 23 * Support for X509 keys and signing added by Ben Laurie <ben@algroup.co.uk> 24 * 3 May 1999 25 */ 26 27 #if HAVE_CONFIG_H 28 #include "config.h" 29 #endif /* HAVE_CONFIG_H */ 30 31 #include <sys/types.h> 32 #include <stdlib.h> 33 #include <stdio.h> 34 35 #if STDC_HEADERS 36 #include <string.h> 37 #endif /* STDC_HEADERS */ 38 39 #if HAVE_LIMITS_H 40 #include <limits.h> 41 #endif /* HAVE_LIMITS_H */ 42 43 #include "header.h" 44 #include "keynote.h" 45 #include "assertion.h" 46 #include "signature.h" 47 48 static const char hextab[] = { 49 '0', '1', '2', '3', '4', '5', '6', '7', 50 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 51 }; 52 53 /* 54 * Actual conversion to hex. 55 */ 56 static void 57 bin2hex(unsigned char *data, unsigned char *buffer, int len) 58 { 59 int off = 0; 60 61 while(len > 0) 62 { 63 buffer[off++] = hextab[*data >> 4]; 64 buffer[off++] = hextab[*data & 0xF]; 65 data++; 66 len--; 67 } 68 } 69 70 /* 71 * Encode a binary string with hex encoding. Return 0 on success. 72 */ 73 int 74 kn_encode_hex(unsigned char *buf, char **dest, int len) 75 { 76 keynote_errno = 0; 77 if (dest == (char **) NULL) 78 { 79 keynote_errno = ERROR_SYNTAX; 80 return -1; 81 } 82 83 *dest = (char *) calloc(2 * len + 1, sizeof(char)); 84 if (*dest == (char *) NULL) 85 { 86 keynote_errno = ERROR_MEMORY; 87 return -1; 88 } 89 90 bin2hex(buf, *dest, len); 91 return 0; 92 } 93 94 /* 95 * Decode a hex encoding. Return 0 on success. The second argument 96 * will be half as large as the first. 97 */ 98 int 99 kn_decode_hex(char *hex, char **dest) 100 { 101 int i, decodedlen; 102 char ptr[3]; 103 104 keynote_errno = 0; 105 if (dest == (char **) NULL) 106 { 107 keynote_errno = ERROR_SYNTAX; 108 return -1; 109 } 110 111 if (strlen(hex) % 2) /* Should be even */ 112 { 113 keynote_errno = ERROR_SYNTAX; 114 return -1; 115 } 116 117 decodedlen = strlen(hex) / 2; 118 *dest = (char *) calloc(decodedlen, sizeof(char)); 119 if (*dest == (char *) NULL) 120 { 121 keynote_errno = ERROR_MEMORY; 122 return -1; 123 } 124 125 ptr[2] = '\0'; 126 for (i = 0; i < decodedlen; i++) 127 { 128 ptr[0] = hex[2 * i]; 129 ptr[1] = hex[(2 * i) + 1]; 130 (*dest)[i] = (unsigned char) strtoul(ptr, (char **) NULL, 16); 131 } 132 133 return 0; 134 } 135 136 void 137 keynote_free_key(void *key, int type) 138 { 139 if (key == (void *) NULL) 140 return; 141 142 #ifdef CRYPTO 143 /* DSA keys */ 144 if (type == KEYNOTE_ALGORITHM_DSA) 145 { 146 DSA_free(key); 147 return; 148 } 149 150 /* RSA keys */ 151 if (type == KEYNOTE_ALGORITHM_RSA) 152 { 153 RSA_free(key); 154 return; 155 } 156 157 /* X509 keys */ 158 if (type == KEYNOTE_ALGORITHM_X509) 159 { 160 RSA_free(key); /* RSA-specific */ 161 return; 162 } 163 #endif /* CRYPTO */ 164 165 #ifdef PGPLIB 166 /* PGP keys */ 167 if (type == KEYNOTE_ALGORITHM_PGP) 168 { 169 /* Unsupported yet */ 170 return; 171 } 172 #endif /* PGPLIB */ 173 174 /* BINARY keys */ 175 if (type == KEYNOTE_ALGORITHM_BINARY) 176 { 177 free(((struct keynote_binary *) key)->bn_key); 178 free(key); 179 return; 180 } 181 182 /* Catch-all case */ 183 if (type == KEYNOTE_ALGORITHM_NONE) 184 free(key); 185 } 186 187 /* 188 * Map a signature to an algorithm. Return algorithm number (defined in 189 * keynote.h), or KEYNOTE_ALGORITHM_NONE if unknown. 190 * Also return in the second, third and fourth arguments the digest 191 * algorithm, ASCII and internal encodings respectively. 192 */ 193 static int 194 keynote_get_sig_algorithm(char *sig, int *hash, int *enc, int *internal) 195 { 196 if (sig == (char *) NULL) 197 return KEYNOTE_ALGORITHM_NONE; 198 199 if (!strncasecmp(SIG_DSA_SHA1_HEX, sig, SIG_DSA_SHA1_HEX_LEN)) 200 { 201 *hash = KEYNOTE_HASH_SHA1; 202 *enc = ENCODING_HEX; 203 *internal = INTERNAL_ENC_ASN1; 204 return KEYNOTE_ALGORITHM_DSA; 205 } 206 207 if (!strncasecmp(SIG_DSA_SHA1_BASE64, sig, SIG_DSA_SHA1_BASE64_LEN)) 208 { 209 *hash = KEYNOTE_HASH_SHA1; 210 *enc = ENCODING_BASE64; 211 *internal = INTERNAL_ENC_ASN1; 212 return KEYNOTE_ALGORITHM_DSA; 213 } 214 215 if (!strncasecmp(SIG_RSA_MD5_PKCS1_HEX, sig, SIG_RSA_MD5_PKCS1_HEX_LEN)) 216 { 217 *hash = KEYNOTE_HASH_MD5; 218 *enc = ENCODING_HEX; 219 *internal = INTERNAL_ENC_PKCS1; 220 return KEYNOTE_ALGORITHM_RSA; 221 } 222 223 if (!strncasecmp(SIG_RSA_SHA1_PKCS1_HEX, sig, SIG_RSA_SHA1_PKCS1_HEX_LEN)) 224 { 225 *hash = KEYNOTE_HASH_SHA1; 226 *enc = ENCODING_HEX; 227 *internal = INTERNAL_ENC_PKCS1; 228 return KEYNOTE_ALGORITHM_RSA; 229 } 230 231 if (!strncasecmp(SIG_RSA_MD5_PKCS1_BASE64, sig, 232 SIG_RSA_MD5_PKCS1_BASE64_LEN)) 233 { 234 *hash = KEYNOTE_HASH_MD5; 235 *enc = ENCODING_BASE64; 236 *internal = INTERNAL_ENC_PKCS1; 237 return KEYNOTE_ALGORITHM_RSA; 238 } 239 240 if (!strncasecmp(SIG_RSA_SHA1_PKCS1_BASE64, sig, 241 SIG_RSA_SHA1_PKCS1_BASE64_LEN)) 242 { 243 *hash = KEYNOTE_HASH_SHA1; 244 *enc = ENCODING_BASE64; 245 *internal = INTERNAL_ENC_PKCS1; 246 return KEYNOTE_ALGORITHM_RSA; 247 } 248 249 if (!strncasecmp(SIG_X509_SHA1_BASE64, sig, SIG_X509_SHA1_BASE64_LEN)) 250 { 251 *hash = KEYNOTE_HASH_SHA1; 252 *enc = ENCODING_BASE64; 253 *internal = INTERNAL_ENC_ASN1; 254 return KEYNOTE_ALGORITHM_X509; 255 } 256 257 if (!strncasecmp(SIG_X509_SHA1_HEX, sig, SIG_X509_SHA1_HEX_LEN)) 258 { 259 *hash = KEYNOTE_HASH_SHA1; 260 *enc = ENCODING_HEX; 261 *internal = INTERNAL_ENC_ASN1; 262 return KEYNOTE_ALGORITHM_X509; 263 } 264 265 #if 0 /* Not supported yet */ 266 if (!strncasecmp(SIG_ELGAMAL_SHA1_HEX, sig, SIG_ELGAMAL_SHA1_HEX_LEN)) 267 { 268 *hash = KEYNOTE_HASH_SHA1; 269 *enc = ENCODING_HEX; 270 *internal = INTERNAL_ENC_ASN1; 271 return KEYNOTE_ALGORITHM_ELGAMAL; 272 } 273 274 if (!strncasecmp(SIG_ELGAMAL_SHA1_BASE64, sig, 275 SIG_ELGAMAL_SHA1_BASE64_LEN)) 276 { 277 *hash = KEYNOTE_HASH_SHA1; 278 *enc = ENCODING_BASE64; 279 *internal = INTERNAL_ENC_ASN1; 280 return KEYNOTE_ALGORITHM_ELGAMAL; 281 } 282 #endif /* 0 */ 283 284 #ifdef PGPLIB 285 if (!strncasecmp(SIG_PGP_NATIVE, sig, SIG_PGP_NATIVE_LEN)) 286 { 287 *hash = KEYNOTE_HASH_NONE; 288 *enc = ENCODING_NATIVE; 289 *internal = INTERNAL_ENC_NATIVE; 290 return KEYNOTE_ALGORITHM_PGP; 291 } 292 #endif /* PGPLIB */ 293 294 *hash = KEYNOTE_HASH_NONE; 295 *enc = ENCODING_NONE; 296 *internal = INTERNAL_ENC_NONE; 297 return KEYNOTE_ALGORITHM_NONE; 298 } 299 300 /* 301 * Map a key to an algorithm. Return algorithm number (defined in 302 * keynote.h), or KEYNOTE_ALGORITHM_NONE if unknown. 303 * This latter is also a valid algorithm (for logical tags). Also return 304 * in the second and third arguments the ASCII and internal encodings. 305 */ 306 int 307 keynote_get_key_algorithm(char *key, int *encoding, int *internalencoding) 308 { 309 #ifdef CRYPTO 310 if (!strncasecmp(DSA_HEX, key, DSA_HEX_LEN)) 311 { 312 *internalencoding = INTERNAL_ENC_ASN1; 313 *encoding = ENCODING_HEX; 314 return KEYNOTE_ALGORITHM_DSA; 315 } 316 317 if (!strncasecmp(DSA_BASE64, key, DSA_BASE64_LEN)) 318 { 319 *internalencoding = INTERNAL_ENC_ASN1; 320 *encoding = ENCODING_BASE64; 321 return KEYNOTE_ALGORITHM_DSA; 322 } 323 324 if (!strncasecmp(RSA_PKCS1_HEX, key, RSA_PKCS1_HEX_LEN)) 325 { 326 *internalencoding = INTERNAL_ENC_PKCS1; 327 *encoding = ENCODING_HEX; 328 return KEYNOTE_ALGORITHM_RSA; 329 } 330 331 if (!strncasecmp(RSA_PKCS1_BASE64, key, RSA_PKCS1_BASE64_LEN)) 332 { 333 *internalencoding = INTERNAL_ENC_PKCS1; 334 *encoding = ENCODING_BASE64; 335 return KEYNOTE_ALGORITHM_RSA; 336 } 337 338 if (!strncasecmp(X509_BASE64, key, X509_BASE64_LEN)) 339 { 340 *internalencoding = INTERNAL_ENC_ASN1; 341 *encoding = ENCODING_BASE64; 342 return KEYNOTE_ALGORITHM_X509; 343 } 344 345 if (!strncasecmp(X509_HEX, key, X509_HEX_LEN)) 346 { 347 *internalencoding = INTERNAL_ENC_ASN1; 348 *encoding = ENCODING_HEX; 349 return KEYNOTE_ALGORITHM_X509; 350 } 351 352 #if 0 /* Not supported yet */ 353 if (!strncasecmp(ELGAMAL_HEX, key, ELGAMAL_HEX_LEN)) 354 { 355 *internalencoding = INTERNAL_ENC_ASN1; 356 *encoding = ENCODING_HEX; 357 return KEYNOTE_ALGORITHM_ELGAMAL; 358 } 359 360 if (!strncasecmp(ELGAMAL_BASE64, key, ELGAMAL_BASE64_LEN)) 361 { 362 *internalencoding = INTERNAL_ENC_ASN1; 363 *encoding = ENCODING_BASE64; 364 return KEYNOTE_ALGORITHM_ELGAMAL; 365 } 366 #endif /* 0 */ 367 #endif /* CRYPTO */ 368 369 #ifdef PGPLIB 370 if (!strncasecmp(PGP_NATIVE, key, PGP_NATIVE_LEN)) 371 { 372 *internalencoding = INTERNAL_ENC_NATIVE; 373 *encoding = ENCODING_NATIVE; 374 return KEYNOTE_ALGORITHM_PGP; 375 } 376 #endif /* PGPLIB */ 377 378 if (!strncasecmp(BINARY_HEX, key, BINARY_HEX_LEN)) 379 { 380 *internalencoding = INTERNAL_ENC_NONE; 381 *encoding = ENCODING_HEX; 382 return KEYNOTE_ALGORITHM_BINARY; 383 } 384 385 if (!strncasecmp(BINARY_BASE64, key, BINARY_BASE64_LEN)) 386 { 387 *internalencoding = INTERNAL_ENC_NONE; 388 *encoding = ENCODING_BASE64; 389 return KEYNOTE_ALGORITHM_BINARY; 390 } 391 392 *internalencoding = INTERNAL_ENC_NONE; 393 *encoding = ENCODING_NONE; 394 return KEYNOTE_ALGORITHM_NONE; 395 } 396 397 /* 398 * Same as keynote_get_key_algorithm(), only verify that this is 399 * a private key (just look at the prefix). 400 */ 401 static int 402 keynote_get_private_key_algorithm(char *key, int *encoding, 403 int *internalencoding) 404 { 405 if (strncasecmp(KEYNOTE_PRIVATE_KEY_PREFIX, key, 406 KEYNOTE_PRIVATE_KEY_PREFIX_LEN)) 407 { 408 *internalencoding = INTERNAL_ENC_NONE; 409 *encoding = ENCODING_NONE; 410 return KEYNOTE_ALGORITHM_NONE; 411 } 412 413 return keynote_get_key_algorithm(key + KEYNOTE_PRIVATE_KEY_PREFIX_LEN, 414 encoding, internalencoding); 415 } 416 417 /* 418 * Decode a string to a key. Return 0 on success. 419 */ 420 int 421 kn_decode_key(struct keynote_deckey *dc, char *key, int keytype) 422 { 423 #ifdef CRYPTO 424 void *kk = (void *) NULL; 425 X509 *px509Cert; 426 EVP_PKEY *pPublicKey; 427 #endif /* CRYPTO */ 428 unsigned char *ptr = (char *) NULL, *decoded = (char *) NULL; 429 int encoding, internalencoding, len = 0; 430 431 keynote_errno = 0; 432 if (keytype == KEYNOTE_PRIVATE_KEY) 433 dc->dec_algorithm = keynote_get_private_key_algorithm(key, &encoding, 434 &internalencoding); 435 else 436 dc->dec_algorithm = keynote_get_key_algorithm(key, &encoding, 437 &internalencoding); 438 if (dc->dec_algorithm == KEYNOTE_ALGORITHM_NONE) 439 { 440 dc->dec_key = (void *) strdup(key); 441 if (dc->dec_key == (void *) NULL) 442 { 443 keynote_errno = ERROR_MEMORY; 444 return -1; 445 } 446 447 return 0; 448 } 449 450 key = index(key, ':'); /* Move forward, to the Encoding. We're guaranteed 451 * to have a ':' character, since this is a key */ 452 key++; 453 454 /* Remove ASCII encoding */ 455 switch (encoding) 456 { 457 case ENCODING_NONE: 458 break; 459 460 case ENCODING_HEX: 461 len = strlen(key) / 2; 462 if (kn_decode_hex(key, (char **) &decoded) != 0) 463 return -1; 464 ptr = decoded; 465 break; 466 467 case ENCODING_BASE64: 468 len = strlen(key); 469 if (len % 4) /* Base64 encoding must be a multiple of 4 */ 470 { 471 keynote_errno = ERROR_SYNTAX; 472 return -1; 473 } 474 475 len = 3 * (len / 4); 476 decoded = (unsigned char *) calloc(len, sizeof(unsigned char)); 477 ptr = decoded; 478 if (decoded == (unsigned char *) NULL) 479 { 480 keynote_errno = ERROR_MEMORY; 481 return -1; 482 } 483 484 if ((len = kn_decode_base64(key, decoded, len)) == -1) 485 return -1; 486 break; 487 488 case ENCODING_NATIVE: 489 decoded = strdup(key); 490 if (decoded == (unsigned char *) NULL) 491 { 492 keynote_errno = ERROR_MEMORY; 493 return -1; 494 } 495 len = strlen(key); 496 ptr = decoded; 497 break; 498 499 default: 500 keynote_errno = ERROR_SYNTAX; 501 return -1; 502 } 503 504 #ifdef CRYPTO 505 /* DSA-HEX */ 506 if ((dc->dec_algorithm == KEYNOTE_ALGORITHM_DSA) && 507 (internalencoding == INTERNAL_ENC_ASN1)) 508 { 509 dc->dec_key = DSA_new(); 510 if (dc->dec_key == (DSA *) NULL) 511 { 512 keynote_errno = ERROR_MEMORY; 513 return -1; 514 } 515 516 kk = dc->dec_key; 517 if (keytype == KEYNOTE_PRIVATE_KEY) 518 { 519 if (d2i_DSAPrivateKey((DSA **) &kk, &decoded, len) == (DSA *) NULL) 520 { 521 if (ptr != (unsigned char *) NULL) 522 free(ptr); 523 DSA_free(kk); 524 keynote_errno = ERROR_SYNTAX; /* Could be a memory error */ 525 return -1; 526 } 527 } 528 else 529 { 530 if (d2i_DSAPublicKey((DSA **) &kk, &decoded, len) == (DSA *) NULL) 531 { 532 if (ptr != (unsigned char *) NULL) 533 free(ptr); 534 DSA_free(kk); 535 keynote_errno = ERROR_SYNTAX; /* Could be a memory error */ 536 return -1; 537 } 538 } 539 540 if (ptr != (unsigned char *) NULL) 541 free(ptr); 542 543 return 0; 544 } 545 546 /* RSA-PKCS1-HEX */ 547 if ((dc->dec_algorithm == KEYNOTE_ALGORITHM_RSA) && 548 (internalencoding == INTERNAL_ENC_PKCS1)) 549 { 550 dc->dec_key = RSA_new(); 551 if (dc->dec_key == (RSA *) NULL) 552 { 553 keynote_errno = ERROR_MEMORY; 554 return -1; 555 } 556 557 kk = dc->dec_key; 558 if (keytype == KEYNOTE_PRIVATE_KEY) 559 { 560 if (d2i_RSAPrivateKey((RSA **) &kk, &decoded, len) == (RSA *) NULL) 561 { 562 if (ptr != (unsigned char *) NULL) 563 free(ptr); 564 RSA_free(kk); 565 keynote_errno = ERROR_SYNTAX; /* Could be a memory error */ 566 return -1; 567 } 568 } 569 else 570 { 571 if (d2i_RSAPublicKey((RSA **) &kk, &decoded, len) == (RSA *) NULL) 572 { 573 if (ptr != (unsigned char *) NULL) 574 free(ptr); 575 RSA_free(kk); 576 keynote_errno = ERROR_SYNTAX; /* Could be a memory error */ 577 return -1; 578 } 579 } 580 581 if (ptr != (unsigned char *) NULL) 582 free(ptr); 583 584 return 0; 585 } 586 587 /* X509 Cert */ 588 if ((dc->dec_algorithm == KEYNOTE_ALGORITHM_X509) && 589 (internalencoding == INTERNAL_ENC_ASN1) && 590 (keytype == KEYNOTE_PUBLIC_KEY)) 591 { 592 if ((px509Cert = X509_new()) == (X509 *) NULL) 593 { 594 if (ptr) 595 free(ptr); 596 keynote_errno = ERROR_MEMORY; 597 return -1; 598 } 599 600 if(d2i_X509(&px509Cert, &decoded, len) == NULL) 601 { 602 if (ptr) 603 free(ptr); 604 X509_free(px509Cert); 605 keynote_errno = ERROR_SYNTAX; 606 return -1; 607 } 608 609 if ((pPublicKey = X509_get_pubkey(px509Cert)) == (EVP_PKEY *) NULL) 610 { 611 if (ptr) 612 free(ptr); 613 X509_free(px509Cert); 614 keynote_errno = ERROR_SYNTAX; 615 return -1; 616 } 617 618 /* RSA-specific */ 619 dc->dec_key = pPublicKey->pkey.rsa; 620 621 if(ptr) 622 free(ptr); 623 return 0; 624 } 625 #endif /* CRYPTO */ 626 627 /* BINARY keys */ 628 if ((dc->dec_algorithm == KEYNOTE_ALGORITHM_BINARY) && 629 (internalencoding == INTERNAL_ENC_NONE)) 630 { 631 dc->dec_key = (void *) calloc(1, sizeof(struct keynote_binary)); 632 if (dc->dec_key == (struct keynote_binary *) NULL) 633 { 634 keynote_errno = ERROR_MEMORY; 635 return -1; 636 } 637 638 ((struct keynote_binary *) dc->dec_key)->bn_key = decoded; 639 ((struct keynote_binary *) dc->dec_key)->bn_len = len; 640 return RESULT_TRUE; 641 } 642 643 /* Add support for more algorithms here */ 644 645 if (ptr != (unsigned char *) NULL) 646 free(ptr); 647 648 /* This shouldn't ever be reached really */ 649 keynote_errno = ERROR_SYNTAX; 650 return -1; 651 } 652 653 /* 654 * Compare two keys for equality. Return RESULT_TRUE if equal, 655 * RESULT_FALSE otherwise. 656 */ 657 int 658 kn_keycompare(void *key1, void *key2, int algorithm) 659 { 660 #ifdef CRYPTO 661 DSA *p1, *p2; 662 RSA *p3, *p4; 663 #endif /* CRYPTO */ 664 struct keynote_binary *bn1, *bn2; 665 666 if ((key1 == (void *) NULL) || 667 (key2 == (void *) NULL)) 668 return RESULT_FALSE; 669 670 switch (algorithm) 671 { 672 case KEYNOTE_ALGORITHM_NONE: 673 if (!strcmp((char *) key1, (char *) key2)) 674 return RESULT_TRUE; 675 else 676 return RESULT_FALSE; 677 678 case KEYNOTE_ALGORITHM_DSA: 679 #ifdef CRYPTO 680 p1 = (DSA *) key1; 681 p2 = (DSA *) key2; 682 if (!BN_cmp(p1->p, p2->p) && 683 !BN_cmp(p1->q, p2->q) && 684 !BN_cmp(p1->g, p2->g) && 685 !BN_cmp(p1->pub_key, p2->pub_key)) 686 return RESULT_TRUE; 687 else 688 return RESULT_FALSE; 689 #else /* CRYPTO */ 690 return RESULT_FALSE; 691 #endif /* CRYPTO */ 692 693 case KEYNOTE_ALGORITHM_X509: 694 #ifdef CRYPTO 695 p3 = (RSA *) key1; 696 p4 = (RSA *) key2; 697 if (!BN_cmp(p3->n, p4->n) && 698 !BN_cmp(p3->e, p4->e)) 699 return RESULT_TRUE; 700 else 701 return RESULT_FALSE; 702 #else /* CRYPTO */ 703 return RESULT_FALSE; 704 #endif /* CRYPTO */ 705 706 case KEYNOTE_ALGORITHM_RSA: 707 #ifdef CRYPTO 708 p3 = (RSA *) key1; 709 p4 = (RSA *) key2; 710 if (!BN_cmp(p3->n, p4->n) && 711 !BN_cmp(p3->e, p4->e)) 712 return RESULT_TRUE; 713 else 714 return RESULT_FALSE; 715 #else /* CRYPTO */ 716 return RESULT_FALSE; 717 #endif /* CRYPTO */ 718 719 case KEYNOTE_ALGORITHM_ELGAMAL: 720 /* Not supported yet */ 721 return RESULT_FALSE; 722 723 case KEYNOTE_ALGORITHM_PGP: 724 /* Not supported yet */ 725 return RESULT_FALSE; 726 727 case KEYNOTE_ALGORITHM_BINARY: 728 bn1 = (struct keynote_binary *) key1; 729 bn2 = (struct keynote_binary *) key2; 730 if ((bn1->bn_len == bn2->bn_len) && 731 !memcmp(bn1->bn_key, bn2->bn_key, bn1->bn_len)) 732 return RESULT_TRUE; 733 else 734 return RESULT_FALSE; 735 736 default: 737 return RESULT_FALSE; 738 } 739 } 740 741 /* 742 * Verify the signature on an assertion; return SIGRESULT_TRUE is 743 * success, SIGRESULT_FALSE otherwise. 744 */ 745 int 746 keynote_sigverify_assertion(struct assertion *as) 747 { 748 #if defined(CRYPTO) || defined(PGPLIB) 749 int hashtype, enc, intenc, alg = KEYNOTE_ALGORITHM_NONE, hashlen = 0; 750 unsigned char *sig, *decoded = (char *) NULL, *ptr; 751 #ifdef CRYPTO 752 unsigned char res2[20]; 753 SHA_CTX shscontext; 754 MD5_CTX md5context; 755 int len = 0; 756 DSA *dsa; 757 RSA *rsa; 758 #endif /* CRYPTO */ 759 if ((as->as_signature == (char *) NULL) || 760 (as->as_startofsignature == (char *) NULL) || 761 (as->as_allbutsignature == (char *) NULL) || 762 (as->as_allbutsignature - as->as_startofsignature <= 0)) 763 return SIGRESULT_FALSE; 764 765 alg = keynote_get_sig_algorithm(as->as_signature, &hashtype, &enc, 766 &intenc); 767 if (alg == KEYNOTE_ALGORITHM_NONE) 768 return SIGRESULT_FALSE; 769 770 /* Check for matching algorithms */ 771 if ((alg != as->as_signeralgorithm) && 772 !((alg == KEYNOTE_ALGORITHM_RSA) && 773 (as->as_signeralgorithm == KEYNOTE_ALGORITHM_X509)) && 774 !((alg == KEYNOTE_ALGORITHM_X509) && 775 (as->as_signeralgorithm == KEYNOTE_ALGORITHM_RSA))) 776 return SIGRESULT_FALSE; 777 778 sig = index(as->as_signature, ':'); /* Move forward to the Encoding. We 779 * are guaranteed to have a ':' 780 * character, since this is a valid 781 * signature */ 782 sig++; 783 784 switch (hashtype) 785 { 786 case KEYNOTE_HASH_SHA1: 787 #ifdef CRYPTO 788 hashlen = 20; 789 memset(res2, 0, hashlen); 790 SHA1_Init(&shscontext); 791 SHA1_Update(&shscontext, as->as_startofsignature, 792 as->as_allbutsignature - as->as_startofsignature); 793 SHA1_Update(&shscontext, as->as_signature, 794 (char *) sig - as->as_signature); 795 SHA1_Final(res2, &shscontext); 796 #endif /* CRYPTO */ 797 break; 798 799 case KEYNOTE_HASH_MD5: 800 #ifdef CRYPTO 801 hashlen = 16; 802 memset(res2, 0, hashlen); 803 MD5_Init(&md5context); 804 MD5_Update(&md5context, as->as_startofsignature, 805 as->as_allbutsignature - as->as_startofsignature); 806 MD5_Update(&md5context, as->as_signature, 807 (char *) sig - as->as_signature); 808 MD5_Final(res2, &md5context); 809 #endif /* CRYPTO */ 810 break; 811 812 case KEYNOTE_HASH_NONE: 813 break; 814 } 815 816 /* Remove ASCII encoding */ 817 switch (enc) 818 { 819 case ENCODING_NONE: 820 ptr = (char *) NULL; 821 break; 822 823 case ENCODING_HEX: 824 len = strlen(sig) / 2; 825 if (kn_decode_hex(sig, (char **) &decoded) != 0) 826 return -1; 827 ptr = decoded; 828 break; 829 830 case ENCODING_BASE64: 831 len = strlen(sig); 832 if (len % 4) /* Base64 encoding must be a multiple of 4 */ 833 { 834 keynote_errno = ERROR_SYNTAX; 835 return -1; 836 } 837 838 len = 3 * (len / 4); 839 decoded = (unsigned char *) calloc(len, sizeof(unsigned char)); 840 ptr = decoded; 841 if (decoded == (unsigned char *) NULL) 842 { 843 keynote_errno = ERROR_MEMORY; 844 return -1; 845 } 846 847 len = kn_decode_base64(sig, decoded, len); 848 if ((len == -1) || (len == 0) || (len == 1)) 849 return -1; 850 break; 851 852 case ENCODING_NATIVE: 853 decoded = (unsigned char *) strdup(sig); 854 if (decoded == (unsigned char *) NULL) 855 { 856 keynote_errno = ERROR_MEMORY; 857 return -1; 858 } 859 len = strlen(sig); 860 ptr = decoded; 861 break; 862 863 default: 864 keynote_errno = ERROR_SYNTAX; 865 return -1; 866 } 867 868 /* DSA */ 869 if ((alg == KEYNOTE_ALGORITHM_DSA) && (intenc == INTERNAL_ENC_ASN1)) 870 { 871 dsa = (DSA *) as->as_authorizer; 872 if (DSA_verify(0, res2, hashlen, decoded, len, dsa) == 1) 873 { 874 if (ptr != (unsigned char *) NULL) 875 free(ptr); 876 return SIGRESULT_TRUE; 877 } 878 } 879 else /* RSA */ 880 if ((alg == KEYNOTE_ALGORITHM_RSA) && (intenc == INTERNAL_ENC_PKCS1)) 881 { 882 rsa = (RSA *) as->as_authorizer; 883 if (RSA_verify_ASN1_OCTET_STRING(RSA_PKCS1_PADDING, res2, hashlen, 884 decoded, len, rsa) == 1) 885 { 886 if (ptr != (unsigned char *) NULL) 887 free(ptr); 888 return SIGRESULT_TRUE; 889 } 890 } 891 else 892 if ((alg == KEYNOTE_ALGORITHM_X509) && (intenc == INTERNAL_ENC_ASN1)) 893 { 894 /* RSA-specific */ 895 rsa = (RSA *) as->as_authorizer; 896 if (RSA_verify(NID_shaWithRSAEncryption, res2, hashlen, decoded, 897 len, rsa) == 1) 898 { 899 if (ptr != (unsigned char *) NULL) 900 free(ptr); 901 return SIGRESULT_TRUE; 902 } 903 } 904 905 /* Handle more algorithms here */ 906 907 if (ptr != (unsigned char *) NULL) 908 free(ptr); 909 #endif /* CRYPTO || PGPLIB */ 910 911 return SIGRESULT_FALSE; 912 } 913 914 /* 915 * Sign an assertion. 916 */ 917 static char * 918 keynote_sign_assertion(struct assertion *as, char *sigalg, void *key, 919 int keyalg, int verifyflag) 920 { 921 #if defined(CRYPTO) || defined(PGPLIB) 922 #ifdef CRYPTO 923 int slen, i, hashlen = 0, hashtype, alg, encoding, internalenc; 924 unsigned char *sig = (char *) NULL, *finalbuf = (char *) NULL; 925 unsigned char res2[LARGEST_HASH_SIZE], *sbuf = (char *) NULL; 926 BIO *biokey = (BIO *) NULL; 927 DSA *dsa = (DSA *) NULL; 928 RSA *rsa = (RSA *) NULL; 929 SHA_CTX shscontext; 930 MD5_CTX md5context; 931 #endif /* CRYPTO */ 932 933 if ((as->as_signature_string_s == (char *) NULL) || 934 (as->as_startofsignature == (char *) NULL) || 935 (as->as_allbutsignature == (char *) NULL) || 936 (as->as_allbutsignature - as->as_startofsignature <= 0) || 937 (as->as_authorizer == (void *) NULL) || 938 (key == (void *) NULL) || 939 (as->as_signeralgorithm == KEYNOTE_ALGORITHM_NONE)) 940 { 941 keynote_errno = ERROR_SYNTAX; 942 return (char *) NULL; 943 } 944 945 alg = keynote_get_sig_algorithm(sigalg, &hashtype, &encoding, 946 &internalenc); 947 if (((alg != as->as_signeralgorithm) && 948 !((alg == KEYNOTE_ALGORITHM_RSA) && 949 (as->as_signeralgorithm == KEYNOTE_ALGORITHM_X509)) && 950 !((alg == KEYNOTE_ALGORITHM_X509) && 951 (as->as_signeralgorithm == KEYNOTE_ALGORITHM_RSA))) || 952 ((alg != keyalg) && 953 !((alg == KEYNOTE_ALGORITHM_RSA) && 954 (keyalg == KEYNOTE_ALGORITHM_X509)) && 955 !((alg == KEYNOTE_ALGORITHM_X509) && 956 (keyalg == KEYNOTE_ALGORITHM_RSA)))) 957 { 958 keynote_errno = ERROR_SYNTAX; 959 return (char *) NULL; 960 } 961 962 sig = index(sigalg, ':'); 963 if (sig == (unsigned char *) NULL) 964 { 965 keynote_errno = ERROR_SYNTAX; 966 return (char *) NULL; 967 } 968 969 sig++; 970 971 switch (hashtype) 972 { 973 case KEYNOTE_HASH_SHA1: 974 #ifdef CRYPTO 975 hashlen = 20; 976 memset(res2, 0, hashlen); 977 SHA1_Init(&shscontext); 978 SHA1_Update(&shscontext, as->as_startofsignature, 979 as->as_allbutsignature - as->as_startofsignature); 980 SHA1_Update(&shscontext, sigalg, (char *) sig - sigalg); 981 SHA1_Final(res2, &shscontext); 982 #endif /* CRYPTO */ 983 break; 984 985 case KEYNOTE_HASH_MD5: 986 #ifdef CRYPTO 987 hashlen = 16; 988 memset(res2, 0, hashlen); 989 MD5_Init(&md5context); 990 MD5_Update(&md5context, as->as_startofsignature, 991 as->as_allbutsignature - as->as_startofsignature); 992 MD5_Update(&md5context, sigalg, (char *) sig - sigalg); 993 MD5_Final(res2, &md5context); 994 #endif /* CRYPTO */ 995 break; 996 997 case KEYNOTE_HASH_NONE: 998 break; 999 } 1000 1001 #ifdef CRYPTO 1002 if ((alg == KEYNOTE_ALGORITHM_DSA) && 1003 (hashtype == KEYNOTE_HASH_SHA1) && 1004 (internalenc == INTERNAL_ENC_ASN1) && 1005 ((encoding == ENCODING_HEX) || (encoding == ENCODING_BASE64))) 1006 { 1007 dsa = (DSA *) key; 1008 sbuf = (unsigned char *) calloc(DSA_size(dsa), sizeof(unsigned char)); 1009 if (sbuf == (unsigned char *) NULL) 1010 { 1011 keynote_errno = ERROR_MEMORY; 1012 return (char *) NULL; 1013 } 1014 1015 if (DSA_sign(0, res2, hashlen, sbuf, &slen, dsa) <= 0) 1016 { 1017 free(sbuf); 1018 keynote_errno = ERROR_SYNTAX; 1019 return (char *) NULL; 1020 } 1021 } 1022 else 1023 if ((alg == KEYNOTE_ALGORITHM_RSA) && 1024 ((hashtype == KEYNOTE_HASH_SHA1) || 1025 (hashtype == KEYNOTE_HASH_MD5)) && 1026 (internalenc == INTERNAL_ENC_PKCS1) && 1027 ((encoding == ENCODING_HEX) || (encoding == ENCODING_BASE64))) 1028 { 1029 rsa = (RSA *) key; 1030 sbuf = (unsigned char *) calloc(RSA_size(rsa), 1031 sizeof(unsigned char)); 1032 if (sbuf == (unsigned char *) NULL) 1033 { 1034 keynote_errno = ERROR_MEMORY; 1035 return (char *) NULL; 1036 } 1037 1038 if (RSA_sign_ASN1_OCTET_STRING(RSA_PKCS1_PADDING, res2, hashlen, 1039 sbuf, &slen, rsa) <= 0) 1040 { 1041 free(sbuf); 1042 keynote_errno = ERROR_SYNTAX; 1043 return (char *) NULL; 1044 } 1045 } 1046 else 1047 if ((alg == KEYNOTE_ALGORITHM_X509) && 1048 (hashtype == KEYNOTE_HASH_SHA1) && 1049 (internalenc == INTERNAL_ENC_ASN1)) 1050 { 1051 if ((biokey = BIO_new(BIO_s_mem())) == (BIO *) NULL) 1052 { 1053 keynote_errno = ERROR_SYNTAX; 1054 return (char *) NULL; 1055 } 1056 1057 if (BIO_write(biokey, key, strlen(key) + 1) <= 0) 1058 { 1059 BIO_free(biokey); 1060 keynote_errno = ERROR_SYNTAX; 1061 return (char *) NULL; 1062 } 1063 1064 /* RSA-specific */ 1065 #if SSLEAY_VERSION_NUMBER >= 0x00904100L 1066 rsa = (RSA *) PEM_read_bio_RSAPrivateKey(biokey, NULL, NULL, NULL); 1067 #else /* SSLEAY_VERSION_NUMBER */ 1068 rsa = (RSA *) PEM_read_bio_RSAPrivateKey(biokey, NULL, NULL); 1069 #endif /* SSLEAY_VERSION_NUMBER */ 1070 if (rsa == (RSA *) NULL) 1071 { 1072 BIO_free(biokey); 1073 keynote_errno = ERROR_SYNTAX; 1074 return (char *) NULL; 1075 } 1076 1077 sbuf = calloc(RSA_size(rsa), sizeof(char)); 1078 if (sbuf == (unsigned char *) NULL) 1079 { 1080 BIO_free(biokey); 1081 RSA_free(rsa); 1082 keynote_errno = ERROR_MEMORY; 1083 return (char *) NULL; 1084 } 1085 1086 if (RSA_sign(NID_shaWithRSAEncryption, res2, hashlen, sbuf, &slen, 1087 rsa) <= 0) 1088 { 1089 BIO_free(biokey); 1090 RSA_free(rsa); 1091 free(sbuf); 1092 keynote_errno = ERROR_SIGN_FAILURE; 1093 return NULL; 1094 } 1095 1096 BIO_free(biokey); 1097 RSA_free(rsa); 1098 } 1099 else /* Other algorithms here */ 1100 { 1101 keynote_errno = ERROR_SYNTAX; 1102 return (char *) NULL; 1103 } 1104 1105 /* ASCII encoding */ 1106 switch (encoding) 1107 { 1108 case ENCODING_HEX: 1109 i = kn_encode_hex(sbuf, (char **) &finalbuf, slen); 1110 free(sbuf); 1111 if (i != 0) 1112 return (char *) NULL; 1113 break; 1114 1115 case ENCODING_BASE64: 1116 finalbuf = (unsigned char *) calloc(2 * slen, 1117 sizeof(unsigned char)); 1118 if (finalbuf == (unsigned char *) NULL) 1119 { 1120 keynote_errno = ERROR_MEMORY; 1121 free(sbuf); 1122 return (char *) NULL; 1123 } 1124 1125 if ((slen = kn_encode_base64(sbuf, slen, finalbuf, 1126 2 * slen)) == -1) 1127 { 1128 free(sbuf); 1129 return (char *) NULL; 1130 } 1131 break; 1132 1133 default: 1134 free(sbuf); 1135 keynote_errno = ERROR_SYNTAX; 1136 return (char *) NULL; 1137 } 1138 1139 /* Replace as->as_signature */ 1140 as->as_signature = (char *) calloc(strlen(sigalg) + 1141 strlen(finalbuf) + 1, sizeof(char)); 1142 if (as->as_signature == (char *) NULL) 1143 { 1144 free(finalbuf); 1145 keynote_errno = ERROR_MEMORY; 1146 return (char *) NULL; 1147 } 1148 1149 /* Concatenate algorithm name and signature value */ 1150 sprintf(as->as_signature, "%s%s", sigalg, finalbuf); 1151 free(finalbuf); 1152 finalbuf = as->as_signature; 1153 1154 /* Verify the newly-created signature if requested */ 1155 if (verifyflag) 1156 { 1157 /* Do the signature verification */ 1158 if (keynote_sigverify_assertion(as) != SIGRESULT_TRUE) 1159 { 1160 as->as_signature = (char *) NULL; 1161 free(finalbuf); 1162 if (keynote_errno == 0) 1163 keynote_errno = ERROR_SYNTAX; 1164 return (char *) NULL; 1165 } 1166 1167 as->as_signature = (char *) NULL; 1168 } 1169 else 1170 as->as_signature = (char *) NULL; 1171 1172 /* Everything ok */ 1173 return (char *) finalbuf; 1174 #endif /* CRYPTO */ 1175 #else /* CRYPTO || PGPLIB */ 1176 keynote_errno = ERROR_SYNTAX; 1177 return (char *) NULL; 1178 #endif /* CRYPTO || PGPLIB */ 1179 } 1180 1181 /* 1182 * Verify the signature on an assertion. 1183 */ 1184 int 1185 kn_verify_assertion(char *buf, int len) 1186 { 1187 struct assertion *as; 1188 int res; 1189 1190 keynote_errno = 0; 1191 as = keynote_parse_assertion(buf, len, ASSERT_FLAG_SIGVER); 1192 if (as == (struct assertion *) NULL) 1193 return -1; 1194 1195 res = keynote_sigverify_assertion(as); 1196 keynote_free_assertion(as); 1197 return res; 1198 } 1199 1200 /* 1201 * Produce the signature for an assertion. 1202 */ 1203 char * 1204 kn_sign_assertion(char *buf, int buflen, char *key, char *sigalg, int vflag) 1205 { 1206 int i, alg, hashtype, encoding, internalenc; 1207 struct keynote_deckey dc; 1208 struct assertion *as; 1209 char *s, *sig; 1210 1211 keynote_errno = 0; 1212 s = (char *) NULL; 1213 1214 if ((sigalg == (char *) NULL) || (buf == (char *) NULL) || 1215 (key == (char *) NULL)) 1216 { 1217 keynote_errno = ERROR_NOTFOUND; 1218 return (char *) NULL; 1219 } 1220 1221 if (sigalg[strlen(sigalg) - 1] != ':') 1222 { 1223 keynote_errno = ERROR_SYNTAX; 1224 return (char *) NULL; 1225 } 1226 1227 /* We're using a different format for X509 private keys, so... */ 1228 alg = keynote_get_sig_algorithm(sigalg, &hashtype, &encoding, 1229 &internalenc); 1230 if (alg != KEYNOTE_ALGORITHM_X509) 1231 { 1232 /* Parse the private key */ 1233 s = keynote_get_private_key(key); 1234 if (s == (char *) NULL) 1235 return (char *) NULL; 1236 1237 /* Decode private key */ 1238 i = kn_decode_key(&dc, s, KEYNOTE_PRIVATE_KEY); 1239 if (i == -1) 1240 { 1241 free(s); 1242 return (char *) NULL; 1243 } 1244 } 1245 else /* X509 private key */ 1246 { 1247 dc.dec_key = key; 1248 dc.dec_algorithm = alg; 1249 } 1250 1251 as = keynote_parse_assertion(buf, buflen, ASSERT_FLAG_SIGGEN); 1252 if (as == (struct assertion *) NULL) 1253 { 1254 if (alg != KEYNOTE_ALGORITHM_X509) 1255 { 1256 keynote_free_key(dc.dec_key, dc.dec_algorithm); 1257 free(s); 1258 } 1259 return (char *) NULL; 1260 } 1261 1262 sig = keynote_sign_assertion(as, sigalg, dc.dec_key, dc.dec_algorithm, 1263 vflag); 1264 if (alg != KEYNOTE_ALGORITHM_X509) 1265 keynote_free_key(dc.dec_key, dc.dec_algorithm); 1266 keynote_free_assertion(as); 1267 if (s != (char *) NULL) 1268 free(s); 1269 return sig; 1270 } 1271 1272 /* 1273 * ASCII-encode a key. 1274 */ 1275 char * 1276 kn_encode_key(struct keynote_deckey *dc, int iencoding, 1277 int encoding, int keytype) 1278 { 1279 #ifdef CRYPTO 1280 char *foo, *ptr; 1281 DSA *dsa; 1282 RSA *rsa; 1283 int i; 1284 #endif /* CRYPTO */ 1285 struct keynote_binary *bn; 1286 char *s; 1287 1288 keynote_errno = 0; 1289 if ((dc == (struct keynote_deckey *) NULL) || 1290 (dc->dec_key == (void *) NULL)) 1291 { 1292 keynote_errno = ERROR_NOTFOUND; 1293 return (char *) NULL; 1294 } 1295 1296 #ifdef CRYPTO 1297 /* DSA keys */ 1298 if ((dc->dec_algorithm == KEYNOTE_ALGORITHM_DSA) && 1299 (iencoding == INTERNAL_ENC_ASN1) && 1300 ((encoding == ENCODING_HEX) || (encoding == ENCODING_BASE64))) 1301 { 1302 dsa = (DSA *) dc->dec_key; 1303 if (keytype == KEYNOTE_PUBLIC_KEY) 1304 i = i2d_DSAPublicKey(dsa, NULL); 1305 else 1306 i = i2d_DSAPrivateKey(dsa, NULL); 1307 1308 if (i <= 0) 1309 { 1310 keynote_errno = ERROR_SYNTAX; 1311 return (char *) NULL; 1312 } 1313 1314 ptr = foo = (char *) calloc(i, sizeof(char)); 1315 if (foo == (char *) NULL) 1316 { 1317 keynote_errno = ERROR_MEMORY; 1318 return (char *) NULL; 1319 } 1320 1321 dsa->write_params = 1; 1322 if (keytype == KEYNOTE_PUBLIC_KEY) 1323 i2d_DSAPublicKey(dsa, (unsigned char **) &foo); 1324 else 1325 i2d_DSAPrivateKey(dsa, (unsigned char **) &foo); 1326 1327 if (encoding == ENCODING_HEX) 1328 { 1329 if (kn_encode_hex(ptr, &s, i) != 0) 1330 { 1331 free(ptr); 1332 return (char *) NULL; 1333 } 1334 1335 free(ptr); 1336 return s; 1337 } 1338 else 1339 if (encoding == ENCODING_BASE64) 1340 { 1341 s = (char *) calloc(2 * i, sizeof(char)); 1342 if (s == (char *) NULL) 1343 { 1344 free(ptr); 1345 keynote_errno = ERROR_MEMORY; 1346 return (char *) NULL; 1347 } 1348 1349 if (kn_encode_base64(ptr, i, s, 2 * i) == -1) 1350 { 1351 free(s); 1352 free(ptr); 1353 return (char *) NULL; 1354 } 1355 1356 free(ptr); 1357 return s; 1358 } 1359 } 1360 1361 /* RSA keys */ 1362 if ((dc->dec_algorithm == KEYNOTE_ALGORITHM_RSA) && 1363 (iencoding == INTERNAL_ENC_PKCS1) && 1364 ((encoding == ENCODING_HEX) || (encoding == ENCODING_BASE64))) 1365 { 1366 rsa = (RSA *) dc->dec_key; 1367 if (keytype == KEYNOTE_PUBLIC_KEY) 1368 i = i2d_RSAPublicKey(rsa, NULL); 1369 else 1370 i = i2d_RSAPrivateKey(rsa, NULL); 1371 1372 if (i <= 0) 1373 { 1374 keynote_errno = ERROR_SYNTAX; 1375 return (char *) NULL; 1376 } 1377 1378 ptr = foo = (char *) calloc(i, sizeof(char)); 1379 if (foo == (char *) NULL) 1380 { 1381 keynote_errno = ERROR_MEMORY; 1382 return (char *) NULL; 1383 } 1384 1385 if (keytype == KEYNOTE_PUBLIC_KEY) 1386 i2d_RSAPublicKey(rsa, (unsigned char **) &foo); 1387 else 1388 i2d_RSAPrivateKey(rsa, (unsigned char **) &foo); 1389 1390 if (encoding == ENCODING_HEX) 1391 { 1392 if (kn_encode_hex(ptr, &s, i) != 0) 1393 { 1394 free(ptr); 1395 return (char *) NULL; 1396 } 1397 1398 free(ptr); 1399 return s; 1400 } 1401 else 1402 if (encoding == ENCODING_BASE64) 1403 { 1404 s = (char *) calloc(2 * i, sizeof(char)); 1405 if (s == (char *) NULL) 1406 { 1407 free(ptr); 1408 keynote_errno = ERROR_MEMORY; 1409 return (char *) NULL; 1410 } 1411 1412 if (kn_encode_base64(ptr, i, s, 2 * i) == -1) 1413 { 1414 free(s); 1415 free(ptr); 1416 return (char *) NULL; 1417 } 1418 1419 free(ptr); 1420 return s; 1421 } 1422 } 1423 #endif /* CRYPTO */ 1424 1425 /* BINARY keys */ 1426 if ((dc->dec_algorithm == KEYNOTE_ALGORITHM_BINARY) && 1427 (iencoding == INTERNAL_ENC_NONE) && 1428 ((encoding == ENCODING_HEX) || (encoding == ENCODING_BASE64))) 1429 { 1430 bn = (struct keynote_binary *) dc->dec_key; 1431 1432 if (encoding == ENCODING_HEX) 1433 { 1434 if (kn_encode_hex(bn->bn_key, &s, bn->bn_len) != 0) 1435 return (char *) NULL; 1436 1437 return s; 1438 } 1439 else 1440 if (encoding == ENCODING_BASE64) 1441 { 1442 s = (char *) calloc(2 * bn->bn_len, sizeof(char)); 1443 if (s == (char *) NULL) 1444 { 1445 keynote_errno = ERROR_MEMORY; 1446 return (char *) NULL; 1447 } 1448 1449 if (kn_encode_base64(bn->bn_key, bn->bn_len, s, 1450 2 * bn->bn_len) == -1) 1451 { 1452 free(s); 1453 return (char *) NULL; 1454 } 1455 1456 return s; 1457 } 1458 } 1459 1460 keynote_errno = ERROR_NOTFOUND; 1461 return (char *) NULL; 1462 } 1463