Move RSA key generation into the wrappers.
[tinc] / src / openssl / rsagen.c
1 /*
2     rsagen.c -- RSA key generation and export
3     Copyright (C) 2008 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id$
20 */
21
22 #include "system.h"
23
24 #include <openssl/pem.h>
25 #include <openssl/err.h>
26
27 #include "logger.h"
28 #include "rsagen.h"
29
30 /* This function prettyprints the key generation process */
31
32 static void indicator(int a, int b, void *p) {
33         switch (a) {
34                 case 0:
35                         fprintf(stderr, ".");
36                         break;
37
38                 case 1:
39                         fprintf(stderr, "+");
40                         break;
41
42                 case 2:
43                         fprintf(stderr, "-");
44                         break;
45
46                 case 3:
47                         switch (b) {
48                                 case 0:
49                                         fprintf(stderr, " p\n");
50                                         break;
51
52                                 case 1:
53                                         fprintf(stderr, " q\n");
54                                         break;
55
56                                 default:
57                                         fprintf(stderr, "?");
58                         }
59                         break;
60
61                 default:
62                         fprintf(stderr, "?");
63         }
64 }
65
66 // Generate RSA key
67
68 bool rsa_generate(rsa_t *rsa, size_t bits, unsigned long exponent) {
69         *rsa = RSA_generate_key(bits, exponent, indicator, NULL);
70
71         return *rsa;
72 }
73
74 // Write PEM RSA keys
75
76 bool rsa_write_pem_public_key(rsa_t *rsa, FILE *fp) {
77         PEM_write_RSAPublicKey(fp, *rsa);
78
79         return true;
80 }
81
82 bool rsa_write_pem_private_key(rsa_t *rsa, FILE *fp) {
83         PEM_write_RSAPrivateKey(fp, *rsa, NULL, NULL, 0, NULL, NULL);
84         return true;
85 }