16 const char *ssl_key_msg (ssl_key_t
*key
) {
17 return key
&& key
->msg
? (const char*)key
->msg
->ptr
: NULL
;
20 const char *ssl_keys_msg (ssl_key_pair_t
*keys
) {
21 return keys
&& keys
->msg
? (const char*)keys
->msg
->ptr
: NULL
;
25 if (!SSL_library_init()) return -1;
26 SSL_load_error_strings();
27 OpenSSL_add_all_algorithms();
28 RAND_load_file("/dev/urandom", 1024);
29 OpenSSL_add_ssl_algorithms();
34 CRYPTO_cleanup_all_ex_data();
36 #if !(OPENSSL_API_COMPAT < 0x10100000L)
37 ERR_remove_thread_state(0);
42 static str_t
*create_error_msg () {
43 str_t
*s
= stralloc(64, 64);
44 ERR_print_errors_cb(({
45 int fn (const char *str
, size_t len
, void *u
) {
46 str_t
**err_str
= (str_t
**)u
;
47 strnadd(err_str
, str
, len
);
48 return (int)(*err_str
)->len
;
54 static int create_rsa_key (RSA
*rsa
, int nbits
, ssl_key_t
*ssl_key
) {
57 if (key
&& EVP_PKEY_set1_RSA(key
, rsa
)) {
61 ssl_key
->msg
= create_error_msg();
62 if (key
) EVP_PKEY_free(key
);
66 #if OPENSSL_API_COMPAT < 0x10100000L
67 static RSA
*gen_keys (int bits
, unsigned long e_value
) {
69 BN_GENCB
*cb
= BN_GENCB_new();
72 if (cb
== NULL
|| rsa
== NULL
|| e
== NULL
)
74 for (i
= 0; i
< (int)sizeof(unsigned long) * 8; i
++) {
75 if (e_value
& (1UL << i
))
76 if (BN_set_bit(e
, i
) == 0)
79 BN_GENCB_set_old(cb
, NULL
, NULL
);
82 if (RSA_generate_key_ex(rsa
, bits
, e
, cb
)) {
95 int ssl_gen_keys (int nbits
, ssl_key_pair_t
*keys
) {
96 ssl_key_t pub
= { .msg
= NULL
, .key
= NULL
, .rsa
= NULL
},
97 priv
= { .msg
= NULL
, .key
= NULL
, .rsa
= NULL
};
98 #if OPENSSL_API_COMPAT < 0x10100000L
99 RSA
*rsa
= gen_keys(nbits
, RSA_F4
);
101 RSA
*rsa
= RSA_generate_key(nbits
, RSA_F4
, NULL
, NULL
);
104 keys
->msg
= create_error_msg();
107 if (-1 == create_rsa_key(rsa
, nbits
, &priv
)) {
109 keys
->msg
= priv
.msg
;
112 if (-1 == create_rsa_key(rsa
, nbits
, &pub
)) {
114 EVP_PKEY_free(priv
.key
);
119 if (0 >= RSA_check_key(rsa
)) {
121 EVP_PKEY_free(priv
.key
);
122 EVP_PKEY_free(pub
.key
);
123 keys
->msg
= create_error_msg();
127 keys
->priv
= priv
.key
;
132 int ssl_gen_keys_fp (int nbits
, FILE *fp_priv
, FILE *fp_pub
, const char *passwd
, ssl_key_pair_t
*keys
) {
134 if (-1 == (ret
= ssl_gen_keys(nbits
, keys
))) return -1;
135 if (!PEM_write_PUBKEY(fp_pub
, keys
->pub
)) {
136 keys
->msg
= create_error_msg();
139 strptr_t pass
= { .len
= 0, .ptr
= "" };
141 pass
.ptr
= (char*)passwd
;
142 pass
.len
= strlen(passwd
);
144 const EVP_CIPHER
*cipher
= EVP_aes_256_cbc();
145 if (!PEM_write_PrivateKey(fp_priv
, keys
->priv
, cipher
, (unsigned char*)pass
.ptr
, (int)pass
.len
, NULL
, NULL
)) {
146 keys
->msg
= create_error_msg();
152 static str_t
*create_errno_msg (const char *fname
) {
153 char *errno_msg
= strerror(errno
);
154 str_t
*msg
= mkstr(errno_msg
, strlen(errno_msg
), 64);
156 strnadd(&msg
, CONST_STR_LEN("; \""));
157 strnadd(&msg
, fname
, strlen(fname
));
158 strnadd(&msg
, CONST_STR_LEN("\""));
163 int ssl_gen_keys_fl (int nbits
, const char *priv
, const char *pub
, const char *passwd
, ssl_key_pair_t
*keys
) {
164 FILE *fp_priv
= fopen(priv
, "wt");
166 keys
->msg
= create_errno_msg(priv
);
169 FILE *fp_pub
= fopen(pub
, "wt");
172 keys
->msg
= create_errno_msg(pub
);
175 int ret
= ssl_gen_keys_fp(nbits
, fp_priv
, fp_pub
, passwd
, keys
);
181 int ssl_gen_keys_buf (int nbits
, strptr_t
*priv
, strptr_t
*pub
, const char *passwd
, ssl_key_pair_t
*keys
) {
183 priv
->ptr
= malloc(priv
->len
);
184 FILE *fp_priv
= open_memstream(&priv
->ptr
, &priv
->len
);
189 keys
->msg
= create_errno_msg(NULL
);
192 pub
->len
= nbits
/ 2;
193 pub
->ptr
= malloc(pub
->len
);
194 FILE *fp_pub
= open_memstream(&pub
->ptr
, &pub
->len
);
203 keys
->msg
= create_errno_msg(NULL
);
206 int ret
= ssl_gen_keys_fp(nbits
, fp_priv
, fp_pub
, passwd
, keys
);
207 if (0 == ret
&& (1 != fwrite("\0", 1, 1, fp_priv
) || 1 != fwrite("\0", 1, 1, fp_pub
))) {
208 keys
->msg
= create_errno_msg(NULL
);
216 static int on_password_cb (char *buf
, int size
, int rwflags
, void *u
) {
217 size_t upass
= strlen((char*)u
);
218 if (upass
> (size_t)size
)
219 upass
= (size_t)size
;
220 memcpy(buf
, u
, upass
);
224 int ssl_load_priv_fp (FILE *fp
, const char *passwd
, ssl_key_t
*key
) {
226 pem_password_cb
*fn
= passwd
? on_password_cb
: NULL
;
227 if (!PEM_read_PrivateKey(fp
, &key
->key
, fn
, (void*)passwd
)) {
228 key
->msg
= create_error_msg();
231 if (!(key
->rsa
= EVP_PKEY_get1_RSA(key
->key
))) {
234 EVP_PKEY_free(key
->key
);
236 key
->msg
= create_error_msg();
243 int ssl_load_priv_fl (const char *fname
, const char *passwd
, ssl_key_t
*key
) {
245 FILE *fp
= fopen(fname
, "r");
247 ret
= ssl_load_priv_fp(fp
, passwd
, key
);
250 key
->msg
= create_errno_msg(fname
);
254 int ssl_load_priv_buf (const char *passwd
, ssl_key_t
*key
, strptr_t
*buf
) {
256 FILE *fp
= fmemopen(buf
->ptr
, buf
->len
, "r");
258 ret
= ssl_load_priv_fp(fp
, passwd
, key
);
261 key
->msg
= create_errno_msg(NULL
);
265 int ssl_load_pub_fp (FILE *fp
, ssl_key_t
*key
) {
267 if (!PEM_read_PUBKEY(fp
, &key
->key
, NULL
,NULL
)) {
268 key
->msg
= create_error_msg();
271 if (!(key
->rsa
= EVP_PKEY_get1_RSA(key
->key
))) {
273 EVP_PKEY_free(key
->key
);
275 key
->msg
= create_error_msg();
282 int ssl_load_pub_fl (const char *fname
, ssl_key_t
*key
) {
284 FILE *fp
= fopen(fname
, "r");
286 ret
= ssl_load_pub_fp(fp
, key
);
289 key
->msg
= create_errno_msg(fname
);
293 int ssl_load_pub_buf (const char *buf
, size_t buf_len
, ssl_key_t
*key
) {
295 FILE *fp
= fmemopen((void*)buf
, buf_len
, "r");
297 ret
= ssl_load_pub_fp(fp
, key
);
300 key
->msg
= create_errno_msg(NULL
);
304 int ssl_encrypt_pub (ssl_key_t
*key
, const char *buf
, size_t buf_len
, str_t
**result
) {
305 int encrypt_len
= RSA_size(key
->rsa
);
306 str_t
*ret
= stralloc(encrypt_len
, 64);
308 if (-1 == (encrypt_len
= RSA_public_encrypt(buf_len
, (unsigned char*)buf
, (unsigned char*)ret
->ptr
, key
->rsa
, RSA_PKCS1_OAEP_PADDING
))) {
309 key
->msg
= create_error_msg();
313 ret
->len
= encrypt_len
;
318 int ssl_decrypt_priv (ssl_key_t
*key
, const char *buf
, size_t buf_len
, str_t
**result
) {
319 int encrypt_len
= RSA_size(key
->rsa
);
320 str_t
*res
= stralloc(encrypt_len
, 64);
322 memset(res
->ptr
, 0, res
->bufsize
);
323 if (-1 == (encrypt_len
= RSA_private_decrypt(buf_len
, (unsigned char*)buf
, (unsigned char*)res
->ptr
, key
->rsa
, RSA_PKCS1_OAEP_PADDING
))) {
324 key
->msg
= create_error_msg();
328 res
->len
= encrypt_len
;
333 int ssl_sign (const unsigned char *msg
, size_t mlen
, unsigned char **sig
, size_t *slen
, int dig
, ssl_key_t
*pkey
) {
342 if (!(ctx
= EVP_MD_CTX_create()))
345 dig
= NID_sha1WithRSAEncryption
;
346 if (!(md
= EVP_get_digestbynid(dig
)) ||
347 0 == EVP_DigestInit_ex(ctx
, md
, NULL
) ||
348 0 == EVP_DigestSignInit(ctx
, NULL
, md
, NULL
, pkey
->key
) ||
349 0 == EVP_DigestSignUpdate(ctx
, msg
, mlen
) ||
350 0 == EVP_DigestSignFinal(ctx
, NULL
, slen
) ||
351 !(*sig
= OPENSSL_malloc(*slen
)) ||
352 0 == EVP_DigestSignFinal(ctx
, *sig
, slen
))
363 EVP_MD_CTX_destroy(ctx
);
367 int ssl_verify (const unsigned char *msg
, size_t mlen
, const unsigned char *sig
, size_t slen
, int dig
, ssl_key_t
*pkey
) {
371 if (!(ctx
= EVP_MD_CTX_create()))
374 dig
= NID_sha1WithRSAEncryption
;
375 if (!(md
= EVP_get_digestbynid(dig
)) ||
376 0 == EVP_DigestInit_ex(ctx
, md
, NULL
) ||
377 0 == EVP_DigestVerifyInit(ctx
, NULL
, md
, NULL
, pkey
->key
) ||
378 0 == EVP_DigestVerifyUpdate(ctx
, msg
, mlen
))
381 if (0 == EVP_DigestVerifyFinal(ctx
, sig
, slen
))
385 EVP_MD_CTX_destroy(ctx
);
389 ssl_key_t
*ssl_key_init () {
390 ssl_key_t
*k
= malloc(sizeof(ssl_key_t
));
397 ssl_key_pair_t
*ssl_key_pair_init () {
398 ssl_key_pair_t
*k
= malloc(sizeof(ssl_key_pair_t
));
406 void ssl_free_key (ssl_key_t
*key
) {
407 if (key
->key
) EVP_PKEY_free(key
->key
);
408 if (key
->rsa
) RSA_free(key
->rsa
);
412 void ssl_free_keys (ssl_key_pair_t
*keys
) {
413 if (keys
->rsa
) RSA_free(keys
->rsa
);
414 if (keys
->priv
) EVP_PKEY_free(keys
->priv
);
415 if (keys
->pub
) EVP_PKEY_free(keys
->pub
);
416 if (keys
->msg
) free(keys
->msg
);