summaryrefslogtreecommitdiff
path: root/ssh-keygen.c
diff options
context:
space:
mode:
Diffstat (limited to 'ssh-keygen.c')
-rw-r--r--ssh-keygen.c71
1 files changed, 53 insertions, 18 deletions
diff --git a/ssh-keygen.c b/ssh-keygen.c
index b3074e8de..496393ff0 100644
--- a/ssh-keygen.c
+++ b/ssh-keygen.c
@@ -12,13 +12,14 @@
12 */ 12 */
13 13
14#include "includes.h" 14#include "includes.h"
15RCSID("$OpenBSD: ssh-keygen.c,v 1.52 2001/03/26 08:07:09 markus Exp $"); 15RCSID("$OpenBSD: ssh-keygen.c,v 1.53 2001/03/26 23:23:24 markus Exp $");
16 16
17#include <openssl/evp.h> 17#include <openssl/evp.h>
18#include <openssl/pem.h> 18#include <openssl/pem.h>
19 19
20#include "xmalloc.h" 20#include "xmalloc.h"
21#include "key.h" 21#include "key.h"
22#include "rsa.h"
22#include "authfile.h" 23#include "authfile.h"
23#include "uuencode.h" 24#include "uuencode.h"
24#include "buffer.h" 25#include "buffer.h"
@@ -169,8 +170,10 @@ buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
169{ 170{
170 int bits = buffer_get_int(b); 171 int bits = buffer_get_int(b);
171 int bytes = (bits + 7) / 8; 172 int bytes = (bits + 7) / 8;
173
172 if (buffer_len(b) < bytes) 174 if (buffer_len(b) < bytes)
173 fatal("buffer_get_bignum_bits: input buffer too small"); 175 fatal("buffer_get_bignum_bits: input buffer too small: "
176 "need %d have %d", bytes, buffer_len(b));
174 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value); 177 BN_bin2bn((u_char *)buffer_ptr(b), bytes, value);
175 buffer_consume(b, bytes); 178 buffer_consume(b, bytes);
176} 179}
@@ -179,9 +182,8 @@ Key *
179do_convert_private_ssh2_from_blob(char *blob, int blen) 182do_convert_private_ssh2_from_blob(char *blob, int blen)
180{ 183{
181 Buffer b; 184 Buffer b;
182 DSA *dsa;
183 Key *key = NULL; 185 Key *key = NULL;
184 int ignore, magic, rlen; 186 int ignore, magic, rlen, ktype;
185 char *type, *cipher; 187 char *type, *cipher;
186 188
187 buffer_init(&b); 189 buffer_init(&b);
@@ -199,33 +201,64 @@ do_convert_private_ssh2_from_blob(char *blob, int blen)
199 ignore = buffer_get_int(&b); 201 ignore = buffer_get_int(&b);
200 ignore = buffer_get_int(&b); 202 ignore = buffer_get_int(&b);
201 ignore = buffer_get_int(&b); 203 ignore = buffer_get_int(&b);
202 xfree(type);
203 204
204 if (strcmp(cipher, "none") != 0) { 205 if (strcmp(cipher, "none") != 0) {
205 error("unsupported cipher %s", cipher); 206 error("unsupported cipher %s", cipher);
206 xfree(cipher); 207 xfree(cipher);
207 buffer_free(&b); 208 buffer_free(&b);
209 xfree(type);
208 return NULL; 210 return NULL;
209 } 211 }
210 xfree(cipher); 212 xfree(cipher);
211 213
212 key = key_new(KEY_DSA); 214 if (strstr(type, "dsa")) {
213 dsa = key->dsa; 215 ktype = KEY_DSA;
214 dsa->priv_key = BN_new(); 216 } else if (strstr(type, "rsa")) {
215 if (dsa->priv_key == NULL) { 217 ktype = KEY_RSA;
216 error("alloc priv_key failed"); 218 } else {
217 key_free(key); 219 xfree(type);
218 return NULL; 220 return NULL;
219 } 221 }
220 buffer_get_bignum_bits(&b, dsa->p); 222 key = key_new_private(ktype);
221 buffer_get_bignum_bits(&b, dsa->g); 223 xfree(type);
222 buffer_get_bignum_bits(&b, dsa->q); 224
223 buffer_get_bignum_bits(&b, dsa->pub_key); 225 switch (key->type) {
224 buffer_get_bignum_bits(&b, dsa->priv_key); 226 case KEY_DSA:
227 buffer_get_bignum_bits(&b, key->dsa->p);
228 buffer_get_bignum_bits(&b, key->dsa->g);
229 buffer_get_bignum_bits(&b, key->dsa->q);
230 buffer_get_bignum_bits(&b, key->dsa->pub_key);
231 buffer_get_bignum_bits(&b, key->dsa->priv_key);
232 break;
233 case KEY_RSA:
234 if (!BN_set_word(key->rsa->e, (u_long) buffer_get_char(&b))) {
235 buffer_free(&b);
236 key_free(key);
237 return NULL;
238 }
239 buffer_get_bignum_bits(&b, key->rsa->d);
240 buffer_get_bignum_bits(&b, key->rsa->n);
241 buffer_get_bignum_bits(&b, key->rsa->iqmp);
242 buffer_get_bignum_bits(&b, key->rsa->q);
243 buffer_get_bignum_bits(&b, key->rsa->p);
244 generate_additional_parameters(key->rsa);
245 break;
246 }
225 rlen = buffer_len(&b); 247 rlen = buffer_len(&b);
226 if(rlen != 0) 248 if(rlen != 0)
227 error("do_convert_private_ssh2_from_blob: remaining bytes in key blob %d", rlen); 249 error("do_convert_private_ssh2_from_blob: "
250 "remaining bytes in key blob %d", rlen);
228 buffer_free(&b); 251 buffer_free(&b);
252#ifdef DEBUG_PK
253 {
254 u_int slen;
255 u_char *sig, data[10] = "abcde12345";
256
257 key_sign(key, &sig, &slen, data, sizeof data);
258 key_verify(key, sig, slen, data, sizeof data);
259 free(sig);
260 }
261#endif
229 return key; 262 return key;
230} 263}
231 264
@@ -288,7 +321,9 @@ do_convert_from_ssh2(struct passwd *pw)
288 exit(1); 321 exit(1);
289 } 322 }
290 ok = private ? 323 ok = private ?
291 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) : 324 (k->type == KEY_DSA ?
325 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
326 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
292 key_write(k, stdout); 327 key_write(k, stdout);
293 if (!ok) { 328 if (!ok) {
294 fprintf(stderr, "key write failed"); 329 fprintf(stderr, "key write failed");