]> git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/plainrsa-gen.c
ipsec-34.0.1.tar.gz
[apple/ipsec.git] / ipsec-tools / racoon / plainrsa-gen.c
1 /* $Id: plainrsa-gen.c,v 1.4.8.2 2005/04/21 09:07:20 monas Exp $ */
2 /*
3 * Copyright (C) 2004 SuSE Linux AG, Nuernberg, Germany.
4 * Contributed by: Michal Ludvig <mludvig@suse.cz>, SUSE Labs
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /* This file contains a generator for FreeS/WAN-style ipsec.secrets RSA keys. */
33
34 #include "config.h"
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/socket.h>
43 #include <unistd.h>
44
45 #include <openssl/bio.h>
46 #include <openssl/bn.h>
47 #include <openssl/err.h>
48 #include <openssl/objects.h>
49 #include <openssl/rsa.h>
50 #include <openssl/evp.h>
51 #ifdef HAVE_OPENSSL_ENGINE_H
52 #include <openssl/engine.h>
53 #endif
54
55 #include "misc.h"
56 #include "vmbuf.h"
57 #include "plog.h"
58 #include "crypto_openssl.h"
59
60 //#include "package_version.h"
61
62 int print_pid = 0;
63
64 void
65 usage (char *argv0)
66 {
67 //%%% fprintf(stderr, "Plain RSA key generator, part of %s\n", TOP_PACKAGE_STRING);
68 fprintf(stderr, "Plain RSA key generator\n");
69 fprintf(stderr, "By Michal Ludvig (http://www.logix.cz/michal)\n");
70 fprintf(stderr, "\n");
71 fprintf(stderr, "Usage: %s [options]\n", argv0);
72 fprintf(stderr, "\n");
73 fprintf(stderr, " -b bits Generate <bits> long RSA key (default=1024)\n");
74 fprintf(stderr, " -e pubexp Public exponent to use (default=0x3)\n");
75 fprintf(stderr, " -f filename Filename to store the key to (default=stdout)\n");
76 fprintf(stderr, " -h Help\n");
77 fprintf(stderr, "\n");
78 fprintf(stderr, "Report bugs to <ipsec-tools-devel@lists.sourceforge.net>\n");
79 exit(1);
80 }
81
82 /*
83 * See RFC 2065, section 3.5 for details about the output format.
84 */
85 vchar_t *
86 mix_b64_pubkey(RSA *key)
87 {
88 char *binbuf;
89 long binlen, ret;
90 vchar_t *res;
91
92 binlen = 1 + BN_num_bytes(key->e) + BN_num_bytes(key->n);
93 binbuf = malloc(binlen);
94 memset(binbuf, 0, binlen);
95 binbuf[0] = BN_bn2bin(key->e, (unsigned char *) &binbuf[1]);
96 ret = BN_bn2bin(key->n, (unsigned char *) (&binbuf[binbuf[0] + 1]));
97 if (1 + binbuf[0] + ret != binlen) {
98 plog(LLV_ERROR, LOCATION, NULL,
99 "Pubkey generation failed. This is really strange...\n");
100 return NULL;
101 }
102
103 return base64_encode(binbuf, binlen);
104 }
105
106 char *
107 lowercase(char *input)
108 {
109 char *ptr = input;
110 while (*ptr) {
111 if (*ptr >= 'A' && *ptr <= 'F')
112 *ptr -= 'A' - 'a';
113 *ptr++;
114 }
115
116 return input;
117 }
118
119 int
120 gen_rsa_key(FILE *fp, size_t bits, unsigned long exp)
121 {
122 RSA *key;
123 vchar_t *pubkey64 = NULL;
124
125 key = RSA_generate_key(bits, exp, NULL, NULL);
126 if (!key) {
127 fprintf(stderr, "RSA_generate_key(): %s\n", eay_strerror());
128 return -1;
129 }
130
131 pubkey64 = mix_b64_pubkey(key);
132 if (!pubkey64) {
133 fprintf(stderr, "mix_b64_pubkey(): %s\n", eay_strerror());
134 return -1;
135 }
136
137 fprintf(fp, "# : PUB 0s%s\n", pubkey64->v);
138 fprintf(fp, ": RSA\t{\n");
139 fprintf(fp, "\t# RSA %zu bits\n", bits);
140 fprintf(fp, "\t# pubkey=0s%s\n", pubkey64->v);
141 fprintf(fp, "\tModulus: 0x%s\n", lowercase(BN_bn2hex(key->n)));
142 fprintf(fp, "\tPublicExponent: 0x%s\n", lowercase(BN_bn2hex(key->e)));
143 fprintf(fp, "\tPrivateExponent: 0x%s\n", lowercase(BN_bn2hex(key->d)));
144 fprintf(fp, "\tPrime1: 0x%s\n", lowercase(BN_bn2hex(key->p)));
145 fprintf(fp, "\tPrime2: 0x%s\n", lowercase(BN_bn2hex(key->q)));
146 fprintf(fp, "\tExponent1: 0x%s\n", lowercase(BN_bn2hex(key->dmp1)));
147 fprintf(fp, "\tExponent2: 0x%s\n", lowercase(BN_bn2hex(key->dmq1)));
148 fprintf(fp, "\tCoefficient: 0x%s\n", lowercase(BN_bn2hex(key->iqmp)));
149 fprintf(fp, " }\n");
150
151 vfree(pubkey64);
152
153 return 0;
154 }
155
156 int
157 main (int argc, char *argv[])
158 {
159 FILE *fp = stdout;
160 size_t bits = 1024;
161 unsigned int pubexp = 0x3;
162 struct stat st;
163 extern char *optarg;
164 extern int optind;
165 int c;
166 char *fname = NULL;
167
168 while ((c = getopt(argc, argv, "e:b:f:h")) != -1)
169 switch (c) {
170 case 'e':
171 if (strncmp(optarg, "0x", 2) == 0)
172 sscanf(optarg, "0x%x", &pubexp);
173 else
174 pubexp = atoi(optarg);
175 break;
176 case 'b':
177 bits = atoi(optarg);
178 break;
179 case 'f':
180 fname = optarg;
181 break;
182 case 'h':
183 default:
184 usage(argv[0]);
185 }
186
187 if (fname) {
188 if (stat(fname, &st) >= 0) {
189 fprintf(stderr, "%s: file exists! Please use a different name.\n", fname);
190 exit(1);
191 }
192
193 umask(0077);
194 fp = fopen(fname, "w");
195 if (fp == NULL) {
196 fprintf(stderr, "%s: %s\n", fname, strerror(errno));
197 exit(1);
198 }
199 }
200
201 ploginit();
202 eay_init();
203
204 gen_rsa_key(fp, bits, pubexp);
205
206 fclose(fp);
207
208 return 0;
209 }