2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
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.
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.
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.
19 /** \file crypt.c openssl-based RSA encryption/decryption routines */
26 static EVP_PKEY
*load_key(const char *file
, int private)
29 EVP_PKEY
*pkey
= NULL
;
31 key
= BIO_new(BIO_s_file());
34 if (BIO_read_filename(key
, file
) > 0) {
35 if (private == LOAD_PRIVATE_KEY
)
36 pkey
= PEM_read_bio_PrivateKey(key
, NULL
, NULL
, NULL
);
38 pkey
= PEM_read_bio_PUBKEY(key
, NULL
, NULL
, NULL
);
45 * read an RSA key from a file
47 * \param key_file the file containing the key
48 * \param rsa RSA structure is returned here
49 * \param private if non-zero, read the private key, otherwise the public key
51 * \return The size of the RSA key on success, negative on errors.
53 * \sa openssl(1), rsa(1).
55 int get_rsa_key(char *key_file
, RSA
**rsa
, int private)
57 EVP_PKEY
*key
= load_key(key_file
, private);
60 return (private == LOAD_PRIVATE_KEY
)? -E_PRIVATE_KEY
62 *rsa
= EVP_PKEY_get1_RSA(key
);
66 return RSA_size(*rsa
);
70 * free an RSA structure
72 * \param rsa pointer to the RSA struct to free
74 * This must be called for any key obtained by get_rsa_key().
76 void rsa_free(RSA
*rsa
)
83 * decrypt a buffer using an RSA key
85 * \param key_file full path of the rsa key
86 * \param outbuf the output buffer
87 * \param inbuf the encrypted input buffer
88 * \param rsa_inlen the length of \a inbuf
90 * The \a outbuf must be large enough to hold at least \a rsa_inlen bytes.
92 * \return The size of the recovered plaintext on success, negative on errors.
94 * \sa RSA_private_decrypt(3)
96 int para_decrypt_buffer(char *key_file
, unsigned char *outbuf
, unsigned char *inbuf
,
100 int ret
, inlen
= rsa_inlen
;
104 ret
= get_rsa_key(key_file
, &rsa
, LOAD_PRIVATE_KEY
);
107 ret
= RSA_private_decrypt(inlen
, inbuf
, outbuf
, rsa
, RSA_PKCS1_PADDING
);
109 return (ret
> 0)? ret
: -E_DECRYPT
;
113 * decrypt the challenge number sent by para_server
115 * \param key_file full path of the rsa key
116 * \param challenge_nr result is stored here
117 * \param inbuf the input buffer
118 * \param rsa_inlen the length of \a inbuf
120 * \return positive on success, negative on errors
122 * \sa para_decrypt_buffer()
124 int para_decrypt_challenge(char *key_file
, long unsigned *challenge_nr
,
125 unsigned char *inbuf
, unsigned rsa_inlen
)
127 unsigned char *rsa_out
= OPENSSL_malloc(rsa_inlen
+ 1);
128 int ret
= para_decrypt_buffer(key_file
, rsa_out
, inbuf
, rsa_inlen
);
132 ret
= sscanf((char *)rsa_out
, "%lu", challenge_nr
) == 1?
135 OPENSSL_free(rsa_out
);
140 * encrypt a buffer using an RSA key
142 * \param rsa: public rsa key
143 * \param inbuf the input buffer
144 * \param len the length of \a inbuf
145 * \param outbuf the output buffer
147 * \return The size of the encrypted data on success, negative on errors
149 * \sa RSA_public_encrypt(3)
151 int para_encrypt_buffer(RSA
*rsa
, unsigned char *inbuf
,
152 unsigned len
, unsigned char *outbuf
)
154 int ret
, flen
= len
; /* RSA_public_encrypt expects a signed int */
158 ret
= RSA_public_encrypt(flen
, inbuf
, outbuf
, rsa
, RSA_PKCS1_PADDING
);
159 return ret
< 0? -E_ENCRYPT
: ret
;
163 * encrypt the given challenge number
165 * \param rsa: public rsa key
166 * \param challenge_nr the number to be encrypted
167 * \param outbuf the output buffer
169 * \a outbuf must be at least 64 bytes long
171 * \return The size of the encrypted data on success, negative on errors
173 * \sa para_encrypt_buffer()
176 int para_encrypt_challenge(RSA
* rsa
, long unsigned challenge_nr
,
177 unsigned char *outbuf
)
179 unsigned char *inbuf
= (unsigned char*) make_message("%lu", challenge_nr
);
180 int ret
= para_encrypt_buffer(rsa
, inbuf
, strlen((char *)inbuf
), outbuf
);