preparations for user list in memory
[paraslash.git] / crypt.c
1 /*
2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file crypt.c openssl-based RSA encryption/decryption routines */
20
21 #include <openssl/pem.h>
22 #include "para.h"
23 #include "error.h"
24 #include "string.h"
25
26 /** \cond used to distinguish between loading of private/public key */
27 #define LOAD_PUBLIC_KEY 0
28 #define LOAD_PRIVATE_KEY 1
29 /** \endcond **/
30
31 static EVP_PKEY *load_key(const char *file, int private)
32 {
33 BIO *key;
34 EVP_PKEY *pkey = NULL;
35
36 key = BIO_new(BIO_s_file());
37 if (!key)
38 return NULL;
39 if (BIO_read_filename(key, file) > 0) {
40 if (private == LOAD_PRIVATE_KEY)
41 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
42 else
43 pkey = PEM_read_bio_PUBKEY(key, NULL, NULL, NULL);
44 }
45 BIO_free(key);
46 return pkey;
47 }
48
49
50 int get_rsa_key(char *key_file, RSA **rsa, int private)
51 {
52 EVP_PKEY *key = load_key(key_file, private);
53
54 if (!key)
55 return (private == LOAD_PRIVATE_KEY)? -E_PRIVATE_KEY : -E_PUBLIC_KEY;
56 *rsa = EVP_PKEY_get1_RSA(key);
57 EVP_PKEY_free(key);
58 if (!*rsa)
59 return -E_RSA;
60 return RSA_size(*rsa);
61 }
62
63 /**
64 * decrypt a buffer using an RSA key
65 *
66 * \param key_file full path of the rsa key
67 * \param outbuf the output buffer
68 * \param inbuf the encrypted input buffer
69 * \param rsa_inlen the length of \a inbuf
70 *
71 * The \a outbuf must be large enough to hold at least \a rsa_inlen bytes.
72 *
73 * \return The size of the recovered plaintext on success, negative on errors.
74 *
75 * \sa RSA_private_decrypt(3)
76 **/
77 int para_decrypt_buffer(char *key_file, unsigned char *outbuf, unsigned char *inbuf,
78 int rsa_inlen)
79 {
80 RSA *rsa;
81 int ret = get_rsa_key(key_file, &rsa, LOAD_PRIVATE_KEY);
82
83 if (ret < 0)
84 return ret;
85 ret = RSA_private_decrypt(rsa_inlen, inbuf, outbuf, rsa, RSA_PKCS1_PADDING);
86 return (ret > 0)? ret : -E_DECRYPT;
87 }
88
89 /**
90 * decrypt the challenge number sent by para_server
91 *
92 * \param key_file full path of the rsa key
93 * \param challenge_nr result is stored here
94 * \param inbuf the input buffer
95 * \param rsa_inlen the length of \a inbuf
96 *
97 * \return positive on success, negative on errors
98 *
99 * \sa para_decrypt_buffer()
100 */
101 int para_decrypt_challenge(char *key_file, long unsigned *challenge_nr,
102 unsigned char *inbuf, int rsa_inlen)
103 {
104 unsigned char *rsa_out = OPENSSL_malloc(rsa_inlen + 1);
105 int ret = para_decrypt_buffer(key_file, rsa_out, inbuf, rsa_inlen);
106
107 if (ret >= 0) {
108 rsa_out[ret] = '\0';
109 ret = sscanf((char *)rsa_out, "%lu", challenge_nr) == 1?
110 1 : -E_CHALLENGE;
111 }
112 OPENSSL_free(rsa_out);
113 return ret;
114 }
115
116 /**
117 * encrypt a buffer using an RSA key
118 *
119 * \param key_file full path of the rsa key
120 * \param inbuf the input buffer
121 * \param len the length of \a inbuf
122 * \param outbuf the output buffer
123 *
124 * \return The size of the encrypted data on success, negative on errors
125 *
126 * \sa RSA_public_encrypt(3)
127 */
128 int para_encrypt_buffer(char *key_file, unsigned char *inbuf,
129 const unsigned len, unsigned char *outbuf)
130 {
131 RSA *rsa;
132 int ret = get_rsa_key(key_file, &rsa, LOAD_PUBLIC_KEY);
133
134 if (ret < 0)
135 return ret;
136 ret = RSA_public_encrypt(len, inbuf, outbuf, rsa, RSA_PKCS1_PADDING);
137 return ret < 0? -E_ENCRYPT : ret;
138 }
139
140 /**
141 * encrypt the given challenge number
142 *
143 * \param key_file full path of the rsa key
144 * \param challenge_nr the number to be encrypted
145 * \param outbuf the output buffer
146 *
147 * \a outbuf must be at least 64 bytes long
148 *
149 * \return The size of the encrypted data on success, negative on errors
150 *
151 * \sa para_encrypt_buffer()
152 *
153 */
154 int para_encrypt_challenge(char *key_file, long unsigned challenge_nr,
155 unsigned char *outbuf)
156 {
157 unsigned char *inbuf = (unsigned char*) make_message("%lu", challenge_nr);
158 int ret = para_encrypt_buffer(key_file, inbuf, strlen((char *)inbuf), outbuf);
159 free(inbuf);
160 return ret;
161 }
162