1 /*
2   Copyright 2021 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #ifndef CFENGINE_CRYPTO_H
26 #define CFENGINE_CRYPTO_H
27 
28 #include <platform.h>
29 
30 #include <openssl/rsa.h>
31 
32 #include <logging.h>
33 
34 // This passphrase was used to encrypt private keys.
35 // We no longer encrypt new keys, but the passphrase is kept
36 // for backwards compatibility - old encrypted keys will still work.
37 #define PRIVKEY_PASSPHRASE "Cfengine passphrase"
38 #define PRIVKEY_PASSPHRASE_LEN 19
39 
40 void CryptoInitialize(void);
41 void CryptoDeInitialize(void);
42 
43 const char *CryptoLastErrorString(void);
44 void DebugBinOut(char *buffer, int len, char *com);
45 bool LoadSecretKeys(const char *const priv_key_path,
46                     const char *const pub_key_path,
47                     RSA **priv_key, RSA **pub_key);
48 void PolicyHubUpdateKeys(const char *policy_server);
49 int EncryptString(char *out, size_t out_size, const char *in, int plainlen,
50                   char type, unsigned char *key);
51 size_t CipherBlockSizeBytes(const EVP_CIPHER *cipher);
52 size_t CipherTextSizeMax(const EVP_CIPHER* cipher, size_t plaintext_size);
53 size_t PlainTextSizeMax(const EVP_CIPHER* cipher, size_t ciphertext_size);
54 int DecryptString(char *out, size_t out_size, const char *in, int cipherlen,
55                   char type, unsigned char *key);
56 RSA *HavePublicKey(const char *username, const char *ipaddress, const char *digest);
57 RSA *HavePublicKeyByIP(const char *username, const char *ipaddress);
58 bool SavePublicKey(const char *username, const char *digest, const RSA *key);
59 RSA *LoadPublicKey(const char *filename);
60 char *LoadPubkeyDigest(const char *pubkey);
61 char *GetPubkeyDigest(RSA *pubkey);
62 bool TrustKey(const char *pubkey, const char *ipaddress, const char *username);
63 
64 char *PublicKeyFile(const char *workdir);
65 char *PrivateKeyFile(const char *workdir);
66 LogLevel CryptoGetMissingKeyLogLevel(void);
67 
68 #endif
69