1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <crypto.h>
#include <openssl/err.h> /* ERR_* */
#include <openssl/rand.h> /* RAND_* */
#include <openssl/bn.h> /* BN_* */
#if OPENSSL_VERSION_NUMBER > 0x30000000
#include <openssl/provider.h> /* OSSL_PROVIDER_* */
#endif
#include <cf3.defs.h>
#include <lastseen.h>
#include <files_interfaces.h>
#include <hash.h>
#include <pipes.h>
#include <mutex.h>
#include <known_dirs.h>
#include <bootstrap.h>
#include <misc_lib.h> /* UnexpectedError,ProgrammingError */
#include <file_lib.h>
#ifdef DARWIN
// On Mac OSX 10.7 and later, majority of functions in /usr/include/openssl/crypto.h
// are deprecated. No known replacement, so shutting up compiler warnings
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#if OPENSSL_VERSION_NUMBER < 0x10100000
void CRYPTO_set_id_callback(unsigned long (*func)(void));
#endif
static void RandomSeed(void);
static void SetupOpenSSLThreadLocks(void);
static void CleanupOpenSSLThreadLocks(void);
/* TODO move crypto.[ch] to libutils. Will need to remove all manipulation of
* lastseen db. */
static bool crypto_initialized = false; /* GLOBAL_X */
#if OPENSSL_VERSION_NUMBER > 0x30000000
static OSSL_PROVIDER *legacy_provider = NULL;
static OSSL_PROVIDER *default_provider = NULL;
#endif
const char *CryptoLastErrorString()
{
const char *errmsg = ERR_reason_error_string(ERR_get_error());
return (errmsg != NULL) ? errmsg : "no error message";
}
void CryptoInitialize()
{
if (!crypto_initialized)
{
SetupOpenSSLThreadLocks();
OpenSSL_add_all_algorithms();
OpenSSL_add_all_digests();
ERR_load_crypto_strings();
#if OPENSSL_VERSION_NUMBER > 0x30000000
/* We need Blowfish for legacy encrypted network stuff and in OpenSSL
* 3+, it's only available when the legacy provider is loaded. And we
* also need the default provider. */
legacy_provider = OSSL_PROVIDER_load(NULL, "legacy");
default_provider = OSSL_PROVIDER_load(NULL, "default");
#endif
RandomSeed();
crypto_initialized = true;
}
}
void CryptoDeInitialize()
{
if (crypto_initialized)
{
char randfile[CF_BUFSIZE];
snprintf(randfile, CF_BUFSIZE, "%s%crandseed",
GetWorkDir(), FILE_SEPARATOR);
/* Only write out a seed if the file doesn't exist
* and we have enough entropy to do so. If RAND_write_File
* returns a bad value, delete the poor seed.
*/
if (access(randfile, R_OK) && errno == ENOENT && RAND_write_file(randfile) != 1024)
{
Log(LOG_LEVEL_WARNING,
"Could not write randomness to '%s'", randfile);
unlink(randfile); /* do not reuse entropy */
}
chmod(randfile, 0600);
EVP_cleanup();
CleanupOpenSSLThreadLocks();
ERR_free_strings();
#if OPENSSL_VERSION_NUMBER > 0x30000000
if (legacy_provider != NULL)
{
OSSL_PROVIDER_unload(legacy_provider);
legacy_provider = NULL;
}
if (default_provider != NULL)
{
OSSL_PROVIDER_unload(default_provider);
default_provider = NULL;
}
#endif
crypto_initialized = false;
}
}
static void RandomSeed(void)
{
/* 1. Seed the weak C PRNGs. */
/* Mix various stuff. */
pid_t pid = getpid();
size_t fqdn_len = strlen(VFQNAME) > 0 ? strlen(VFQNAME) : 1;
time_t start_time = CFSTARTTIME;
time_t now = time(NULL);
srand((unsigned) pid * start_time ^
(unsigned) fqdn_len * now);
srand48((long) pid * start_time ^
(long) fqdn_len * now);
/* 2. Seed the strong OpenSSL PRNG. */
#ifndef __MINGW32__
RAND_poll(); /* windows may hang */
#else
RAND_screen(); /* noop unless openssl is very old */
#endif
if (RAND_status() != 1)
{
/* randseed file is written on deinitialization of crypto system */
char randfile[CF_BUFSIZE];
snprintf(randfile, CF_BUFSIZE, "%s%crandseed",
GetWorkDir(), FILE_SEPARATOR);
Log(LOG_LEVEL_VERBOSE, "Looking for a source of entropy in '%s'",
randfile);
if (RAND_load_file(randfile, -1) != 1024)
{
Log(LOG_LEVEL_CRIT,
"Could not read randomness from '%s'", randfile);
unlink(randfile); /* kill randseed if error reading it */
}
/* If we've used the random seed, then delete */
unlink(randfile);
}
}
/* PEM functions need the const cast away, but hopefully the default
* call-back doesn't actually modify its user-data. */
static const char priv_passphrase[] = PRIVKEY_PASSPHRASE;
/**
* @param[in] priv_key_path path to the private key to use (%NULL to use the default)
* @param[in] pub_key_path path to the private key to use (%NULL to use the default)
* @param[out] priv_key a place to store the loaded private key (or %NULL to
* use the global PRIVKEY variable)
* @param[out] pub_key a place to store the loaded public key (or %NULL to
* use the global PUBKEY variable)
* @return true the error is not so severe that we must stop
*/
bool LoadSecretKeys(const char *const priv_key_path,
const char *const pub_key_path,
RSA **priv_key, RSA **pub_key)
{
{
char *privkeyfile = NULL;
if (priv_key_path == NULL)
{
privkeyfile = PrivateKeyFile(GetWorkDir());
}
FILE *fp = safe_fopen(privkeyfile != NULL ? privkeyfile : priv_key_path, "r");
if (!fp)
{
/* VERBOSE in case it's a custom, local-only installation. */
Log(LOG_LEVEL_VERBOSE,
"Couldn't find a private key at '%s', use cf-key to get one. (fopen: %s)",
privkeyfile != NULL ? privkeyfile : priv_key_path, GetErrorStr());
free(privkeyfile);
return false;
}
if (priv_key == NULL)
{
/* if no place to store the private key was specified, use the
* global variable PRIVKEY */
priv_key = &PRIVKEY;
}
if (*priv_key != NULL)
{
DESTROY_AND_NULL(RSA_free, *priv_key);
}
// Can read both encrypted (old) and unencrypted(new) key files:
*priv_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, (void*) priv_passphrase);
if (*priv_key == NULL)
{
Log(LOG_LEVEL_ERR,
"Error reading private key. (PEM_read_RSAPrivateKey: %s)",
CryptoLastErrorString());
*priv_key = NULL;
fclose(fp);
return false;
}
fclose(fp);
Log(LOG_LEVEL_VERBOSE, "Loaded private key at '%s'", privkeyfile);
free(privkeyfile);
}
{
char *pubkeyfile = NULL;
if (pub_key_path == NULL)
{
pubkeyfile = PublicKeyFile(GetWorkDir());
}
FILE *fp = safe_fopen(pubkeyfile != NULL ? pubkeyfile : pub_key_path, "r");
if (!fp)
{
/* VERBOSE in case it's a custom, local-only installation. */
Log(LOG_LEVEL_VERBOSE,
"Couldn't find a public key at '%s', use cf-key to get one (fopen: %s)",
pubkeyfile != NULL ? pubkeyfile : pub_key_path, GetErrorStr());
free(pubkeyfile);
return false;
}
if (pub_key == NULL)
{
/* if no place to store the public key was specified, use the
* global variable PUBKEY */
pub_key = &PUBKEY;
}
if (*pub_key != NULL)
{
DESTROY_AND_NULL(RSA_free, *pub_key);
}
*pub_key = PEM_read_RSAPublicKey(fp, NULL, NULL, (void*) priv_passphrase);
if (*pub_key == NULL)
{
Log(LOG_LEVEL_ERR,
"Error reading public key at '%s'. (PEM_read_RSAPublicKey: %s)",
pubkeyfile, CryptoLastErrorString());
fclose(fp);
free(pubkeyfile);
return false;
}
Log(LOG_LEVEL_VERBOSE, "Loaded public key '%s'", pubkeyfile);
free(pubkeyfile);
fclose(fp);
}
if (*pub_key != NULL)
{
const BIGNUM *n, *e;
RSA_get0_key(*pub_key, &n, &e, NULL);
if ((BN_num_bits(e) < 2) || (!BN_is_odd(e)))
{
Log(LOG_LEVEL_ERR, "The public key RSA exponent is too small or not odd");
return false;
}
}
return true;
}
void PolicyHubUpdateKeys(const char *policy_server)
{
if (GetAmPolicyHub() && PUBKEY != NULL)
{
unsigned char digest[EVP_MAX_MD_SIZE + 1];
const char* const workdir = GetWorkDir();
char dst_public_key_filename[CF_BUFSIZE] = "";
{
char buffer[CF_HOSTKEY_STRING_SIZE];
HashPubKey(PUBKEY, digest, CF_DEFAULT_DIGEST);
snprintf(dst_public_key_filename, sizeof(dst_public_key_filename),
"%s/ppkeys/%s-%s.pub",
workdir, "root",
HashPrintSafe(buffer, sizeof(buffer), digest,
CF_DEFAULT_DIGEST, true));
MapName(dst_public_key_filename);
}
struct stat sb;
if ((stat(dst_public_key_filename, &sb) == -1))
{
char src_public_key_filename[CF_BUFSIZE] = "";
snprintf(src_public_key_filename, CF_MAXVARSIZE, "%s/ppkeys/localhost.pub", workdir);
MapName(src_public_key_filename);
// copy localhost.pub to root-HASH.pub on policy server
if (!LinkOrCopy(src_public_key_filename, dst_public_key_filename, false))
{
Log(LOG_LEVEL_ERR, "Unable to copy policy server's own public key from '%s' to '%s'", src_public_key_filename, dst_public_key_filename);
}
if (policy_server)
{
LastSaw(policy_server, digest, LAST_SEEN_ROLE_CONNECT);
}
}
}
}
/*********************************************************************/
/**
* @brief Search for a key given an IP address, by getting the
* key hash value from lastseen db.
* @return NULL if the key was not found in any form.
*/
RSA *HavePublicKeyByIP(const char *username, const char *ipaddress)
{
char hash[CF_HOSTKEY_STRING_SIZE];
/* Get the key hash for that address from lastseen db. */
bool found = Address2Hostkey(hash, sizeof(hash), ipaddress);
/* If not found, by passing "" as digest, we effectively look only for
* the old-style key file, e.g. root-1.2.3.4.pub. */
return HavePublicKey(username, ipaddress,
found ? hash : "");
}
static const char *const pub_passphrase = "public";
/**
* @brief Search for a key:
* 1. username-hash.pub
* 2. username-ip.pub
* @return NULL if key not found in any form
*/
RSA *HavePublicKey(const char *username, const char *ipaddress, const char *digest)
{
char keyname[CF_MAXVARSIZE], newname[CF_BUFSIZE], oldname[CF_BUFSIZE];
struct stat statbuf;
RSA *newkey = NULL;
const char* const workdir = GetWorkDir();
snprintf(keyname, CF_MAXVARSIZE, "%s-%s", username, digest);
snprintf(newname, CF_BUFSIZE, "%s/ppkeys/%s.pub", workdir, keyname);
MapName(newname);
if (stat(newname, &statbuf) == -1)
{
Log(LOG_LEVEL_VERBOSE, "Did not find new key format '%s'", newname);
snprintf(oldname, CF_BUFSIZE, "%s/ppkeys/%s-%s.pub",
workdir, username, ipaddress);
MapName(oldname);
Log(LOG_LEVEL_VERBOSE, "Trying old style '%s'", oldname);
if (stat(oldname, &statbuf) == -1)
{
Log(LOG_LEVEL_DEBUG, "Did not have old-style key '%s'", oldname);
return NULL;
}
if (strlen(digest) > 0)
{
Log(LOG_LEVEL_INFO, "Renaming old key from '%s' to '%s'", oldname, newname);
if (rename(oldname, newname) != 0)
{
Log(LOG_LEVEL_ERR, "Could not rename from old key format '%s' to new '%s'. (rename: %s)", oldname, newname, GetErrorStr());
}
}
else
{
/* We don't know the digest (e.g. because we are a client and have
no lastseen-map yet), so we're using old file format
(root-IP.pub). */
Log(LOG_LEVEL_VERBOSE,
"We have no digest yet, using old keyfile name: %s",
oldname);
snprintf(newname, sizeof(newname), "%s", oldname);
}
}
FILE *fp = safe_fopen(newname, "r");
if (fp == NULL)
{
Log(LOG_LEVEL_ERR, "Couldn't open public key file '%s' (fopen: %s)",
newname, GetErrorStr());
return NULL;
}
if ((newkey = PEM_read_RSAPublicKey(fp, NULL, NULL,
(void *)pub_passphrase)) == NULL)
{
Log(LOG_LEVEL_ERR,
"Error reading public key from '%s' (PEM_read_RSAPublicKey: %s)",
newname, CryptoLastErrorString());
fclose(fp);
return NULL;
}
fclose(fp);
{
const BIGNUM *n, *e;
RSA_get0_key(newkey, &n, &e, NULL);
if ((BN_num_bits(e) < 2) || (!BN_is_odd(e)))
{
Log(LOG_LEVEL_ERR, "RSA Exponent too small or not odd for key: %s",
newname);
RSA_free(newkey);
return NULL;
}
}
return newkey;
}
/*********************************************************************/
bool SavePublicKey(const char *user, const char *digest, const RSA *key)
{
char keyname[CF_MAXVARSIZE], filename[CF_BUFSIZE];
struct stat statbuf;
int ret;
ret = snprintf(keyname, sizeof(keyname), "%s-%s", user, digest);
if (ret < 0)
{
Log(LOG_LEVEL_ERR, "snprintf failed: %s", GetErrorStr());
return false;
}
else if ((unsigned long) ret >= sizeof(keyname))
{
Log(LOG_LEVEL_ERR, "USERNAME-KEY (%s-%s) string too long!",
user, digest);
return false;
}
ret = snprintf(filename, sizeof(filename), "%s/ppkeys/%s.pub",
GetWorkDir(), keyname);
if (ret < 0)
{
Log(LOG_LEVEL_ERR, "snprintf failed: %s", GetErrorStr());
return false;
}
else if ((unsigned long) ret >= sizeof(filename))
{
Log(LOG_LEVEL_ERR, "Filename too long!");
return false;
}
MapName(filename);
if (stat(filename, &statbuf) != -1)
{
Log(LOG_LEVEL_VERBOSE,
"Public key file '%s' already exists, not rewriting",
filename);
return true;
}
Log(LOG_LEVEL_VERBOSE, "Saving public key to file '%s'", filename);
FILE *fp = safe_fopen_create_perms(filename, "w", CF_PERMS_DEFAULT);
if (fp == NULL)
{
Log(LOG_LEVEL_ERR, "Unable to write a public key '%s'. (fopen: %s)", filename, GetErrorStr());
return false;
}
if (!PEM_write_RSAPublicKey(fp, key))
{
Log(LOG_LEVEL_ERR,
"Error saving public key to '%s'. (PEM_write_RSAPublicKey: %s)",
filename, CryptoLastErrorString());
fclose(fp);
return false;
}
fclose(fp);
return true;
}
RSA *LoadPublicKey(const char *filename)
{
FILE *fp;
RSA *key;
const BIGNUM *n, *e;
fp = safe_fopen(filename, "r");
if (fp == NULL)
{
Log(LOG_LEVEL_ERR, "Cannot open public key file '%s' (fopen: %s)", filename, GetErrorStr());
return NULL;
};
if ((key = PEM_read_RSAPublicKey(fp, NULL, NULL,
(void *)priv_passphrase)) == NULL)
{
Log(LOG_LEVEL_ERR,
"Error while reading public key '%s' (PEM_read_RSAPublicKey: %s)",
filename,
CryptoLastErrorString());
fclose(fp);
return NULL;
};
fclose(fp);
RSA_get0_key(key, &n, &e, NULL);
if (BN_num_bits(e) < 2 || !BN_is_odd(e))
{
Log(LOG_LEVEL_ERR, "Error while reading public key '%s' - RSA Exponent is too small or not odd. (BN_num_bits: %s)",
filename, GetErrorStr());
return NULL;
};
return key;
}
/** Return a string with the printed digest of the given key file,
or NULL if an error occurred. */
char *LoadPubkeyDigest(const char *filename)
{
unsigned char digest[EVP_MAX_MD_SIZE + 1];
RSA *key = NULL;
char *buffer = xmalloc(CF_HOSTKEY_STRING_SIZE);
key = LoadPublicKey(filename);
if (key == NULL)
{
return NULL;
}
HashPubKey(key, digest, CF_DEFAULT_DIGEST);
HashPrintSafe(buffer, CF_HOSTKEY_STRING_SIZE,
digest, CF_DEFAULT_DIGEST, true);
RSA_free(key);
return buffer;
}
/** Return a string with the printed digest of the given key file. */
char *GetPubkeyDigest(RSA *pubkey)
{
unsigned char digest[EVP_MAX_MD_SIZE + 1];
char *buffer = xmalloc(CF_HOSTKEY_STRING_SIZE);
HashPubKey(pubkey, digest, CF_DEFAULT_DIGEST);
HashPrintSafe(buffer, CF_HOSTKEY_STRING_SIZE,
digest, CF_DEFAULT_DIGEST, true);
return buffer;
}
/**
* Trust the given key. If #ipaddress is not NULL, then also
* update the "last seen" database. The IP address is required for
* trusting a server key (on the client); it is -currently- optional
* for trusting a client key (on the server).
*/
bool TrustKey(const char *filename, const char *ipaddress, const char *username)
{
RSA* key;
char *digest;
key = LoadPublicKey(filename);
if (key == NULL)
{
return false;
}
digest = GetPubkeyDigest(key);
if (digest == NULL)
{
RSA_free(key);
return false;
}
if (ipaddress != NULL)
{
Log(LOG_LEVEL_VERBOSE,
"Adding a CONNECT entry in lastseen db: IP '%s', key '%s'",
ipaddress, digest);
LastSaw1(ipaddress, digest, LAST_SEEN_ROLE_CONNECT);
}
bool ret = SavePublicKey(username, digest, key);
RSA_free(key);
free(digest);
return ret;
}
int EncryptString(char *out, size_t out_size, const char *in, int plainlen,
char type, unsigned char *key)
{
int cipherlen = 0, tmplen;
unsigned char iv[32] =
{ 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 };
if (key == NULL)
ProgrammingError("EncryptString: session key == NULL");
size_t max_ciphertext_size = CipherTextSizeMax(CfengineCipher(type), plainlen);
if(max_ciphertext_size > out_size)
{
ProgrammingError("EncryptString: output buffer too small: max_ciphertext_size (%zd) > out_size (%zd)",
max_ciphertext_size, out_size);
}
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, CfengineCipher(type), NULL, key, iv);
if (!EVP_EncryptUpdate(ctx, out, &cipherlen, in, plainlen))
{
EVP_CIPHER_CTX_free(ctx);
return -1;
}
if (!EVP_EncryptFinal_ex(ctx, out + cipherlen, &tmplen))
{
EVP_CIPHER_CTX_free(ctx);
return -1;
}
cipherlen += tmplen;
if (cipherlen < 0)
{
ProgrammingError("EncryptString: chipherlen (%d) < 0", cipherlen);
}
else if ((size_t) cipherlen > max_ciphertext_size)
{
ProgrammingError("EncryptString: too large ciphertext written: cipherlen (%d) > max_ciphertext_size (%zd)",
cipherlen, max_ciphertext_size);
}
EVP_CIPHER_CTX_free(ctx);
return cipherlen;
}
size_t CipherBlockSizeBytes(const EVP_CIPHER *cipher)
{
return EVP_CIPHER_block_size(cipher);
}
size_t CipherTextSizeMax(const EVP_CIPHER* cipher, size_t plaintext_size)
{
// see man EVP_DecryptUpdate() and EVP_DecryptFinal_ex()
size_t padding_size = (CipherBlockSizeBytes(cipher) * 2) - 1;
// check for potential integer overflow, leave some buffer
if(plaintext_size > SIZE_MAX - padding_size)
{
ProgrammingError("CipherTextSizeMax: plaintext_size is too large (%zu)",
plaintext_size);
}
return plaintext_size + padding_size;
}
size_t PlainTextSizeMax(const EVP_CIPHER* cipher, size_t ciphertext_size)
{
// see man EVP_DecryptUpdate() and EVP_DecryptFinal_ex()
size_t padding_size = (CipherBlockSizeBytes(cipher) * 2);
// check for potential integer overflow, leave some buffer
if(ciphertext_size > SIZE_MAX - padding_size)
{
ProgrammingError("PlainTextSizeMax: ciphertext_size is too large (%zu)",
ciphertext_size);
}
return ciphertext_size + padding_size;
}
/*********************************************************************/
int DecryptString(char *out, size_t out_size, const char *in, int cipherlen,
char type, unsigned char *key)
{
int plainlen = 0, tmplen;
unsigned char iv[32] =
{ 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 };
if (key == NULL)
ProgrammingError("DecryptString: session key == NULL");
size_t max_plaintext_size = PlainTextSizeMax(CfengineCipher(type), cipherlen);
if(max_plaintext_size > out_size)
{
ProgrammingError("DecryptString: output buffer too small: max_plaintext_size (%zd) > out_size (%zd)",
max_plaintext_size, out_size);
}
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_DecryptInit_ex(ctx, CfengineCipher(type), NULL, key, iv);
if (!EVP_DecryptUpdate(ctx, out, &plainlen, in, cipherlen))
{
Log(LOG_LEVEL_ERR, "Failed to decrypt string");
EVP_CIPHER_CTX_free(ctx);
return -1;
}
if (!EVP_DecryptFinal_ex(ctx, out + plainlen, &tmplen))
{
unsigned long err = ERR_get_error();
Log(LOG_LEVEL_ERR, "Failed to decrypt at final of cipher length %d. (EVP_DecryptFinal_ex: %s)", cipherlen, ERR_error_string(err, NULL));
EVP_CIPHER_CTX_free(ctx);
return -1;
}
plainlen += tmplen;
if (plainlen < 0)
{
ProgrammingError("DecryptString: plainlen (%d) < 0", plainlen);
}
if ((size_t) plainlen > max_plaintext_size)
{
ProgrammingError("DecryptString: too large plaintext written: plainlen (%d) > max_plaintext_size (%zd)",
plainlen, max_plaintext_size);
}
EVP_CIPHER_CTX_free(ctx);
return plainlen;
}
/*********************************************************************/
void DebugBinOut(char *buffer, int len, char *comment)
{
unsigned char *sp;
char buf[CF_BUFSIZE];
char hexStr[3]; // one byte as hex
if (len < 0)
{
Log(LOG_LEVEL_ERR, "Debug binary print negative len param (len = %d)", len);
}
else if ((unsigned long) len >= (sizeof(buf) / 2)) // hex uses two chars per byte
{
Log(LOG_LEVEL_DEBUG, "Debug binary print is too large (len = %d)", len);
return;
}
memset(buf, 0, sizeof(buf));
for (sp = buffer; sp < (unsigned char *) (buffer + len); sp++)
{
xsnprintf(hexStr, sizeof(hexStr), "%2.2x", (int) *sp);
strcat(buf, hexStr);
}
Log(LOG_LEVEL_VERBOSE, "BinaryBuffer, %d bytes, comment '%s', buffer '%s'", len, comment, buf);
}
char *PublicKeyFile(const char *workdir)
{
char *keyfile;
xasprintf(&keyfile,
"%s" FILE_SEPARATOR_STR "ppkeys" FILE_SEPARATOR_STR "localhost.pub", workdir);
return keyfile;
}
char *PrivateKeyFile(const char *workdir)
{
char *keyfile;
xasprintf(&keyfile,
"%s" FILE_SEPARATOR_STR "ppkeys" FILE_SEPARATOR_STR "localhost.priv", workdir);
return keyfile;
}
/*********************************************************************
* Functions for threadsafe OpenSSL usage *
* Only pthread support - we don't create threads with any other API *
*********************************************************************/
static pthread_mutex_t *cf_openssl_locks = NULL;
#ifndef __MINGW32__
#if OPENSSL_VERSION_NUMBER < 0x10100000
unsigned long ThreadId_callback(void)
{
return (unsigned long) pthread_self();
}
#endif
#endif
#if OPENSSL_VERSION_NUMBER < 0x10100000
static void OpenSSLLock_callback(int mode, int index, char *file, int line)
{
if (mode & CRYPTO_LOCK)
{
int ret = pthread_mutex_lock(&(cf_openssl_locks[index]));
if (ret != 0)
{
Log(LOG_LEVEL_ERR,
"OpenSSL locking failure at %s:%d! (pthread_mutex_lock: %s)",
file, line, GetErrorStrFromCode(ret));
}
}
else
{
int ret = pthread_mutex_unlock(&(cf_openssl_locks[index]));
if (ret != 0)
{
Log(LOG_LEVEL_ERR,
"OpenSSL locking failure at %s:%d! (pthread_mutex_unlock: %s)",
file, line, GetErrorStrFromCode(ret));
}
}
}
#endif // Callback only for openssl < 1.1.0
static void SetupOpenSSLThreadLocks(void)
{
const int num_locks = CRYPTO_num_locks();
assert(cf_openssl_locks == NULL);
cf_openssl_locks = xmalloc(num_locks * sizeof(*cf_openssl_locks));
for (int i = 0; i < num_locks; i++)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
int ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
if (ret != 0)
{
Log(LOG_LEVEL_ERR,
"Failed to use error-checking mutexes for openssl,"
" falling back to normal ones (pthread_mutexattr_settype: %s)",
GetErrorStrFromCode(ret));
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
}
ret = pthread_mutex_init(&cf_openssl_locks[i], &attr);
if (ret != 0)
{
Log(LOG_LEVEL_CRIT,
"Failed to use initialise mutexes for openssl"
" (pthread_mutex_init: %s)!",
GetErrorStrFromCode(ret));
}
pthread_mutexattr_destroy(&attr);
}
#ifndef __MINGW32__
#if OPENSSL_VERSION_NUMBER < 0x10100000
CRYPTO_set_id_callback((unsigned long (*)())ThreadId_callback);
#endif
#endif
#if OPENSSL_VERSION_NUMBER < 0x10100000
CRYPTO_set_locking_callback((void (*)())OpenSSLLock_callback);
#endif
}
static void CleanupOpenSSLThreadLocks(void)
{
const int numLocks = CRYPTO_num_locks();
CRYPTO_set_locking_callback(NULL);
#ifndef __MINGW32__
#if OPENSSL_VERSION_NUMBER < 0x10100000
CRYPTO_set_id_callback(NULL);
#endif
#endif
for (int i = 0; i < numLocks; i++)
{
pthread_mutex_destroy(&(cf_openssl_locks[i]));
}
free(cf_openssl_locks);
cf_openssl_locks = NULL;
}
|