2 * Code for PuTTY to import and export private key files in other
3 * SSH clients' formats.
14 #define PUT_32BIT(cp, value) do { \
16 (cp)[2] = (value) >> 8; \
17 (cp)[1] = (value) >> 16; \
18 (cp)[0] = (value) >> 24; } while (0)
20 #define GET_32BIT(cp) \
21 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
22 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
23 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
24 ((unsigned long)(unsigned char)(cp)[3]))
26 int openssh_encrypted(char *filename
);
27 struct ssh2_userkey
*openssh_read(char *filename
, char *passphrase
);
29 int sshcom_encrypted(char *filename
, char **comment
);
30 struct ssh2_userkey
*sshcom_read(char *filename
, char *passphrase
);
33 * Given a key type, determine whether we know how to import it.
35 int import_possible(int type
)
37 if (type
== SSH_KEYTYPE_OPENSSH
)
39 if (type
== SSH_KEYTYPE_SSHCOM
)
45 * Given a key type, determine what native key type
46 * (SSH_KEYTYPE_SSH1 or SSH_KEYTYPE_SSH2) it will come out as once
49 int import_target_type(int type
)
52 * There are no known foreign SSH1 key formats.
54 return SSH_KEYTYPE_SSH2
;
58 * Determine whether a foreign key is encrypted.
60 int import_encrypted(char *filename
, int type
, char **comment
)
62 if (type
== SSH_KEYTYPE_OPENSSH
) {
63 *comment
= filename
; /* OpenSSH doesn't do key comments */
64 return openssh_encrypted(filename
);
66 if (type
== SSH_KEYTYPE_SSHCOM
) {
67 return sshcom_encrypted(filename
, comment
);
75 int import_ssh1(char *filename
, int type
, struct RSAKey
*key
, char *passphrase
)
83 struct ssh2_userkey
*import_ssh2(char *filename
, int type
, char *passphrase
)
85 if (type
== SSH_KEYTYPE_OPENSSH
)
86 return openssh_read(filename
, passphrase
);
87 if (type
== SSH_KEYTYPE_SSHCOM
)
88 return sshcom_read(filename
, passphrase
);
92 /* ----------------------------------------------------------------------
93 * Helper routines. (The base64 ones are defined in sshpubk.c.)
96 #define isbase64(c) ( ((c) >= 'A' && (c) <= 'Z') || \
97 ((c) >= 'a' && (c) <= 'z') || \
98 ((c) >= '0' && (c) <= '9') || \
99 (c) == '+' || (c) == '/' || (c) == '=' \
102 extern int base64_decode_atom(char *atom
, unsigned char *out
);
103 extern int base64_lines(int datalen
);
104 extern void base64_encode_atom(unsigned char *data
, int n
, char *out
);
105 extern void base64_encode(FILE * fp
, unsigned char *data
, int datalen
);
108 * Read an ASN.1/BER identifier and length pair.
110 * Flags are a combination of the #defines listed below.
112 * Returns -1 if unsuccessful; otherwise returns the number of
113 * bytes used out of the source data.
116 /* ASN.1 tag classes. */
117 #define ASN1_CLASS_UNIVERSAL (0 << 6)
118 #define ASN1_CLASS_APPLICATION (1 << 6)
119 #define ASN1_CLASS_CONTEXT_SPECIFIC (2 << 6)
120 #define ASN1_CLASS_PRIVATE (3 << 6)
121 #define ASN1_CLASS_MASK (3 << 6)
123 /* Primitive versus constructed bit. */
124 #define ASN1_CONSTRUCTED (1 << 5)
126 int ber_read_id_len(void *source
, int sourcelen
,
127 int *id
, int *length
, int *flags
)
129 unsigned char *p
= (unsigned char *) source
;
134 *flags
= (*p
& 0xE0);
135 if ((*p
& 0x1F) == 0x1F) {
138 *id
= (*id
<< 7) | (*p
& 0x7F);
143 *id
= (*id
<< 7) | (*p
& 0x7F);
160 *length
= (*length
<< 8) | (*p
++);
167 return p
- (unsigned char *) source
;
170 static int put_string(void *target
, void *data
, int len
)
172 unsigned char *d
= (unsigned char *)target
;
175 memcpy(d
+4, data
, len
);
179 static int put_mp(void *target
, void *data
, int len
)
181 unsigned char *d
= (unsigned char *)target
;
182 unsigned char *i
= (unsigned char *)data
;
187 memcpy(d
+5, data
, len
);
191 memcpy(d
+4, data
, len
);
196 /* ----------------------------------------------------------------------
197 * Code to read OpenSSH private keys.
200 enum { OSSH_DSA
, OSSH_RSA
};
205 unsigned char *keyblob
;
206 int keyblob_len
, keyblob_size
;
209 struct openssh_key
*load_openssh_key(char *filename
)
211 struct openssh_key
*ret
;
217 int base64_chars
= 0;
219 ret
= smalloc(sizeof(*ret
));
221 ret
->keyblob_len
= ret
->keyblob_size
= 0;
223 memset(ret
->iv
, 0, sizeof(ret
->iv
));
225 fp
= fopen(filename
, "r");
227 errmsg
= "Unable to open key file";
230 if (!fgets(buffer
, sizeof(buffer
), fp
) ||
231 0 != strncmp(buffer
, "-----BEGIN ", 11) ||
232 0 != strcmp(buffer
+strlen(buffer
)-17, "PRIVATE KEY-----\n")) {
233 errmsg
= "File does not begin with OpenSSH key header";
236 if (!strcmp(buffer
, "-----BEGIN RSA PRIVATE KEY-----\n"))
237 ret
->type
= OSSH_RSA
;
238 else if (!strcmp(buffer
, "-----BEGIN DSA PRIVATE KEY-----\n"))
239 ret
->type
= OSSH_DSA
;
241 errmsg
= "Unrecognised key type";
247 if (!fgets(buffer
, sizeof(buffer
), fp
)) {
248 errmsg
= "Unexpected end of file";
251 if (0 == strncmp(buffer
, "-----END ", 9) &&
252 0 == strcmp(buffer
+strlen(buffer
)-17, "PRIVATE KEY-----\n"))
254 if ((p
= strchr(buffer
, ':')) != NULL
) {
256 errmsg
= "Header found in body of key data";
260 while (*p
&& isspace((unsigned char)*p
)) p
++;
261 if (!strcmp(buffer
, "Proc-Type")) {
262 if (p
[0] != '4' || p
[1] != ',') {
263 errmsg
= "Proc-Type is not 4 (only 4 is supported)";
267 if (!strcmp(p
, "ENCRYPTED\n"))
269 } else if (!strcmp(buffer
, "DEK-Info")) {
272 if (strncmp(p
, "DES-EDE3-CBC,", 13)) {
273 errmsg
= "Ciphers other than DES-EDE3-CBC not supported";
277 for (i
= 0; i
< 8; i
++) {
278 if (1 != sscanf(p
, "%2x", &j
))
284 errmsg
= "Expected 16-digit iv in DEK-Info";
292 while (isbase64(*p
)) {
293 base64_bit
[base64_chars
++] = *p
;
294 if (base64_chars
== 4) {
295 unsigned char out
[3];
300 len
= base64_decode_atom(base64_bit
, out
);
303 errmsg
= "Invalid base64 encoding";
307 if (ret
->keyblob_len
+ len
> ret
->keyblob_size
) {
308 ret
->keyblob_size
= ret
->keyblob_len
+ len
+ 256;
309 ret
->keyblob
= srealloc(ret
->keyblob
, ret
->keyblob_size
);
312 memcpy(ret
->keyblob
+ ret
->keyblob_len
, out
, len
);
313 ret
->keyblob_len
+= len
;
315 memset(out
, 0, sizeof(out
));
323 if (ret
->keyblob_len
== 0 || !ret
->keyblob
) {
324 errmsg
= "Key body not present";
328 if (ret
->encrypted
&& ret
->keyblob_len
% 8 != 0) {
329 errmsg
= "Encrypted key blob is not a multiple of cipher block size";
333 memset(buffer
, 0, sizeof(buffer
));
334 memset(base64_bit
, 0, sizeof(base64_bit
));
338 memset(buffer
, 0, sizeof(buffer
));
339 memset(base64_bit
, 0, sizeof(base64_bit
));
342 memset(ret
->keyblob
, 0, ret
->keyblob_size
);
345 memset(&ret
, 0, sizeof(ret
));
351 int openssh_encrypted(char *filename
)
353 struct openssh_key
*key
= load_openssh_key(filename
);
358 ret
= key
->encrypted
;
359 memset(key
->keyblob
, 0, key
->keyblob_size
);
361 memset(&key
, 0, sizeof(key
));
366 struct ssh2_userkey
*openssh_read(char *filename
, char *passphrase
)
368 struct openssh_key
*key
= load_openssh_key(filename
);
369 struct ssh2_userkey
*retkey
;
371 int ret
, id
, len
, flags
;
373 struct ssh2_userkey
*retval
= NULL
;
376 int blobsize
, blobptr
, privptr
;
383 if (key
->encrypted
) {
385 * Derive encryption key from passphrase and iv/salt:
387 * - let block A equal MD5(passphrase || iv)
388 * - let block B equal MD5(A || passphrase || iv)
389 * - block C would be MD5(B || passphrase || iv) and so on
390 * - encryption key is the first N bytes of A || B
392 struct MD5Context md5c
;
393 unsigned char keybuf
[32];
396 MD5Update(&md5c
, passphrase
, strlen(passphrase
));
397 MD5Update(&md5c
, key
->iv
, 8);
398 MD5Final(keybuf
, &md5c
);
401 MD5Update(&md5c
, keybuf
, 16);
402 MD5Update(&md5c
, passphrase
, strlen(passphrase
));
403 MD5Update(&md5c
, key
->iv
, 8);
404 MD5Final(keybuf
+16, &md5c
);
407 * Now decrypt the key blob.
409 des3_decrypt_pubkey_ossh(keybuf
, key
->iv
,
410 key
->keyblob
, key
->keyblob_len
);
412 memset(&md5c
, 0, sizeof(md5c
));
413 memset(keybuf
, 0, sizeof(keybuf
));
417 * Now we have a decrypted key blob, which contains an ASN.1
418 * encoded private key. We must now untangle the ASN.1.
420 * We expect the whole key blob to be formatted as a SEQUENCE
421 * (0x30 followed by a length code indicating that the rest of
422 * the blob is part of the sequence). Within that SEQUENCE we
423 * expect to see a bunch of INTEGERs. What those integers mean
424 * depends on the key type:
426 * - For RSA, we expect the integers to be 0, n, e, d, p, q,
427 * dmp1, dmq1, iqmp in that order. (The last three are d mod
428 * (p-1), d mod (q-1), inverse of q mod p respectively.)
430 * - For DSA, we expect them to be 0, p, q, g, y, x in that
436 /* Expect the SEQUENCE header. Take its absence as a failure to decrypt. */
437 ret
= ber_read_id_len(p
, key
->keyblob_len
, &id
, &len
, &flags
);
439 if (ret
< 0 || id
!= 16) {
440 errmsg
= "ASN.1 decoding failure";
441 retval
= SSH2_WRONG_PASSPHRASE
;
445 /* Expect a load of INTEGERs. */
446 if (key
->type
== OSSH_RSA
)
448 else if (key
->type
== OSSH_DSA
)
452 * Space to create key blob in.
454 blobsize
= 256+key
->keyblob_len
;
455 blob
= smalloc(blobsize
);
457 if (key
->type
== OSSH_DSA
)
458 memcpy(blob
+4, "ssh-dss", 7);
459 else if (key
->type
== OSSH_RSA
)
460 memcpy(blob
+4, "ssh-rsa", 7);
464 for (i
= 0; i
< num_integers
; i
++) {
465 ret
= ber_read_id_len(p
, key
->keyblob
+key
->keyblob_len
-p
,
468 if (ret
< 0 || id
!= 2 ||
469 key
->keyblob
+key
->keyblob_len
-p
< len
) {
470 errmsg
= "ASN.1 decoding failure";
476 * The first integer should be zero always (I think
477 * this is some sort of version indication).
479 if (len
!= 1 || p
[0] != 0) {
480 errmsg
= "Version number mismatch";
483 } else if (key
->type
== OSSH_RSA
) {
485 * Integers 1 and 2 go into the public blob but in the
486 * opposite order; integers 3, 4, 5 and 8 go into the
487 * private blob. The other two (6 and 7) are ignored.
490 /* Save the details for after we deal with number 2. */
493 } else if (i
!= 6 && i
!= 7) {
494 PUT_32BIT(blob
+blobptr
, len
);
495 memcpy(blob
+blobptr
+4, p
, len
);
498 PUT_32BIT(blob
+blobptr
, modlen
);
499 memcpy(blob
+blobptr
+4, modptr
, modlen
);
504 } else if (key
->type
== OSSH_DSA
) {
506 * Integers 1-4 go into the public blob; integer 5 goes
507 * into the private blob.
509 PUT_32BIT(blob
+blobptr
, len
);
510 memcpy(blob
+blobptr
+4, p
, len
);
516 /* Skip past the number. */
521 * Now put together the actual key. Simplest way to do this is
522 * to assemble our own key blobs and feed them to the createkey
523 * functions; this is a bit faffy but it does mean we get all
524 * the sanity checks for free.
526 assert(privptr
> 0); /* should have bombed by now if not */
527 retkey
= smalloc(sizeof(struct ssh2_userkey
));
528 retkey
->alg
= (key
->type
== OSSH_RSA ?
&ssh_rsa
: &ssh_dss
);
529 retkey
->data
= retkey
->alg
->createkey(blob
, privptr
,
530 blob
+privptr
, blobptr
-privptr
);
533 errmsg
= "unable to create key data structure";
537 retkey
->comment
= dupstr("imported-openssh-key");
538 errmsg
= NULL
; /* no error */
543 memset(blob
, 0, blobsize
);
546 memset(key
->keyblob
, 0, key
->keyblob_size
);
548 memset(&key
, 0, sizeof(key
));
553 /* ----------------------------------------------------------------------
554 * Code to read ssh.com private keys.
558 * The format of the base64 blob is largely ssh2-packet-formatted,
559 * except that mpints are a bit different: they're more like the
560 * old ssh1 mpint. You have a 32-bit bit count N, followed by
561 * (N+7)/8 bytes of data.
563 * So. The blob contains:
565 * - uint32 0x3f6ff9eb (magic number)
566 * - uint32 size (total blob size)
567 * - string key-type (see below)
568 * - string cipher-type (tells you if key is encrypted)
569 * - string encrypted-blob
571 * (The first size field includes the size field itself and the
572 * magic number before it. All other size fields are ordinary ssh2
573 * strings, so the size field indicates how much data is to
576 * The encrypted blob, once decrypted, contains a single string
577 * which in turn contains the payload. (This allows padding to be
578 * added after that string while still making it clear where the
579 * real payload ends. Also it probably makes for a reasonable
582 * The payload blob, for an RSA key, contains:
585 * - mpint n (yes, the public and private stuff is intermixed)
586 * - mpint u (presumably inverse of p mod q)
587 * - mpint p (p is the smaller prime)
588 * - mpint q (q is the larger)
590 * For a DSA key, the payload blob contains:
598 * Alternatively, if the parameters are `predefined', that
599 * (0,p,g,q) sequence can be replaced by a uint32 1 and a string
600 * containing some predefined parameter specification. *shudder*,
601 * but I doubt we'll encounter this in real life.
603 * The key type strings are ghastly. The RSA key I looked at had a
606 * `if-modn{sign{rsa-pkcs1-sha1},encrypt{rsa-pkcs1v2-oaep}}'
608 * and the DSA key wasn't much better:
610 * `dl-modp{sign{dsa-nist-sha1},dh{plain}}'
612 * It isn't clear that these will always be the same. I think it
613 * might be wise just to look at the `if-modn{sign{rsa' and
614 * `dl-modp{sign{dsa' prefixes.
616 * Finally, the encryption. The cipher-type string appears to be
617 * either `none' or `3des-cbc'. Looks as if this is SSH2-style
618 * 3des-cbc (i.e. outer cbc rather than inner). The key is created
619 * from the passphrase by means of yet another hashing faff:
621 * - first 16 bytes are MD5(passphrase)
622 * - next 16 bytes are MD5(passphrase || first 16 bytes)
623 * - if there were more, they'd be MD5(passphrase || first 32),
628 char comment
[256]; /* allowing any length is overkill */
629 unsigned char *keyblob
;
630 int keyblob_len
, keyblob_size
;
633 struct sshcom_key
*load_sshcom_key(char *filename
)
635 struct sshcom_key
*ret
;
642 int base64_chars
= 0;
644 ret
= smalloc(sizeof(*ret
));
645 ret
->comment
[0] = '\0';
647 ret
->keyblob_len
= ret
->keyblob_size
= 0;
649 fp
= fopen(filename
, "r");
651 errmsg
= "Unable to open key file";
654 if (!fgets(buffer
, sizeof(buffer
), fp
) ||
655 0 != strcmp(buffer
, "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----\n")) {
656 errmsg
= "File does not begin with ssh.com key header";
662 if (!fgets(buffer
, sizeof(buffer
), fp
)) {
663 errmsg
= "Unexpected end of file";
666 if (!strcmp(buffer
, "---- END SSH2 ENCRYPTED PRIVATE KEY ----\n"))
668 if ((p
= strchr(buffer
, ':')) != NULL
) {
670 errmsg
= "Header found in body of key data";
674 while (*p
&& isspace((unsigned char)*p
)) p
++;
676 * Header lines can end in a trailing backslash for
679 while ((len
= strlen(p
)) > sizeof(buffer
) - (p
-buffer
) -1 ||
680 p
[len
-1] != '\n' || p
[len
-2] == '\\') {
681 if (len
> (p
-buffer
) + sizeof(buffer
)-2) {
682 errmsg
= "Header line too long to deal with";
685 if (!fgets(p
+len
-2, sizeof(buffer
)-(p
-buffer
)-(len
-2), fp
)) {
686 errmsg
= "Unexpected end of file";
690 p
[strcspn(p
, "\n")] = '\0';
691 if (!strcmp(buffer
, "Comment")) {
692 /* Strip quotes in comment if present. */
693 if (p
[0] == '"' && p
[strlen(p
)-1] == '"') {
695 p
[strlen(p
)-1] = '\0';
697 strncpy(ret
->comment
, p
, sizeof(ret
->comment
));
698 ret
->comment
[sizeof(ret
->comment
)-1] = '\0';
704 while (isbase64(*p
)) {
705 base64_bit
[base64_chars
++] = *p
;
706 if (base64_chars
== 4) {
707 unsigned char out
[3];
711 len
= base64_decode_atom(base64_bit
, out
);
714 errmsg
= "Invalid base64 encoding";
718 if (ret
->keyblob_len
+ len
> ret
->keyblob_size
) {
719 ret
->keyblob_size
= ret
->keyblob_len
+ len
+ 256;
720 ret
->keyblob
= srealloc(ret
->keyblob
, ret
->keyblob_size
);
723 memcpy(ret
->keyblob
+ ret
->keyblob_len
, out
, len
);
724 ret
->keyblob_len
+= len
;
732 if (ret
->keyblob_len
== 0 || !ret
->keyblob
) {
733 errmsg
= "Key body not present";
742 memset(ret
->keyblob
, 0, ret
->keyblob_size
);
745 memset(&ret
, 0, sizeof(ret
));
751 int sshcom_encrypted(char *filename
, char **comment
)
753 struct sshcom_key
*key
= load_sshcom_key(filename
);
754 int pos
, len
, answer
;
761 * Check magic number.
763 if (GET_32BIT(key
->keyblob
) != 0x3f6ff9eb)
764 return 0; /* key is invalid */
767 * Find the cipher-type string.
771 if (key
->keyblob_len
< pos
+4)
772 goto done
; /* key is far too short */
773 pos
+= 4 + GET_32BIT(key
->keyblob
+ pos
); /* skip key type */
774 if (key
->keyblob_len
< pos
+4)
775 goto done
; /* key is far too short */
776 len
= GET_32BIT(key
->keyblob
+ pos
); /* find cipher-type length */
777 if (key
->keyblob_len
< pos
+4+len
)
778 goto done
; /* cipher type string is incomplete */
779 if (len
!= 4 || 0 != memcmp(key
->keyblob
+ pos
+ 4, "none", 4))
783 *comment
= dupstr(key
->comment
);
784 memset(key
->keyblob
, 0, key
->keyblob_size
);
786 memset(&key
, 0, sizeof(key
));
791 struct mpint_pos
{ void *start
; int bytes
; };
793 int sshcom_read_mpint(void *data
, int len
, struct mpint_pos
*ret
)
797 unsigned char *d
= (unsigned char *) data
;
803 bytes
= (bits
+ 7) / 8;
814 return len
; /* ensure further calls fail as well */
817 struct ssh2_userkey
*sshcom_read(char *filename
, char *passphrase
)
819 struct sshcom_key
*key
= load_sshcom_key(filename
);
822 const char prefix_rsa
[] = "if-modn{sign{rsa";
823 const char prefix_dsa
[] = "dl-modp{sign{dsa";
824 enum { RSA
, DSA
} type
;
828 struct ssh2_userkey
*ret
= NULL
, *retkey
;
829 const struct ssh_signkey
*alg
;
830 unsigned char *blob
= NULL
;
831 int blobsize
, publen
, privlen
;
837 * Check magic number.
839 if (GET_32BIT(key
->keyblob
) != 0x3f6ff9eb) {
840 errmsg
= "Key does not begin with magic number";
845 * Determine the key type.
848 if (key
->keyblob_len
< pos
+4 ||
849 (len
= GET_32BIT(key
->keyblob
+ pos
)) > key
->keyblob_len
- pos
- 4) {
850 errmsg
= "Key blob does not contain a key type string";
853 if (len
> sizeof(prefix_rsa
) - 1 &&
854 !memcmp(key
->keyblob
+pos
+4, prefix_rsa
, sizeof(prefix_rsa
) - 1)) {
856 } else if (len
> sizeof(prefix_dsa
) - 1 &&
857 !memcmp(key
->keyblob
+pos
+4, prefix_dsa
, sizeof(prefix_dsa
) - 1)) {
860 errmsg
= "Key is of unknown type";
866 * Determine the cipher type.
868 if (key
->keyblob_len
< pos
+4 ||
869 (len
= GET_32BIT(key
->keyblob
+ pos
)) > key
->keyblob_len
- pos
- 4) {
870 errmsg
= "Key blob does not contain a cipher type string";
873 if (len
== 4 && !memcmp(key
->keyblob
+pos
+4, "none", 4))
875 else if (len
== 8 && !memcmp(key
->keyblob
+pos
+4, "3des-cbc", 8))
878 errmsg
= "Key encryption is of unknown type";
884 * Get hold of the encrypted part of the key.
886 if (key
->keyblob_len
< pos
+4 ||
887 (len
= GET_32BIT(key
->keyblob
+ pos
)) > key
->keyblob_len
- pos
- 4) {
888 errmsg
= "Key blob does not contain actual key data";
891 ciphertext
= key
->keyblob
+ pos
+ 4;
893 if (cipherlen
== 0) {
894 errmsg
= "Length of key data is zero";
899 * Decrypt it if necessary.
903 * Derive encryption key from passphrase and iv/salt:
905 * - let block A equal MD5(passphrase)
906 * - let block B equal MD5(passphrase || A)
907 * - block C would be MD5(passphrase || A || B) and so on
908 * - encryption key is the first N bytes of A || B
910 struct MD5Context md5c
;
911 unsigned char keybuf
[32], iv
[8];
913 if (cipherlen
% 8 != 0) {
914 errmsg
= "Encrypted part of key is not a multiple of cipher block"
920 MD5Update(&md5c
, passphrase
, strlen(passphrase
));
921 MD5Final(keybuf
, &md5c
);
924 MD5Update(&md5c
, passphrase
, strlen(passphrase
));
925 MD5Update(&md5c
, keybuf
, 16);
926 MD5Final(keybuf
+16, &md5c
);
929 * Now decrypt the key blob.
931 memset(iv
, 0, sizeof(iv
));
932 des3_decrypt_pubkey_ossh(keybuf
, iv
, ciphertext
, cipherlen
);
934 memset(&md5c
, 0, sizeof(md5c
));
935 memset(keybuf
, 0, sizeof(keybuf
));
938 * Hereafter we return WRONG_PASSPHRASE for any parsing
939 * error. (But only if we've just tried to decrypt it!
940 * Returning WRONG_PASSPHRASE for an unencrypted key is
944 ret
= SSH2_WRONG_PASSPHRASE
;
948 * Strip away the containing string to get to the real meat.
950 len
= GET_32BIT(ciphertext
);
951 if (len
> cipherlen
-4) {
952 errmsg
= "containing string was ill-formed";
959 * Now we break down into RSA versus DSA. In either case we'll
960 * construct public and private blobs in our own format, and
961 * end up feeding them to alg->createkey().
963 blobsize
= cipherlen
+ 256;
964 blob
= smalloc(blobsize
);
967 struct mpint_pos n
, e
, d
, u
, p
, q
;
969 pos
+= sshcom_read_mpint(ciphertext
+pos
, cipherlen
-pos
, &e
);
970 pos
+= sshcom_read_mpint(ciphertext
+pos
, cipherlen
-pos
, &d
);
971 pos
+= sshcom_read_mpint(ciphertext
+pos
, cipherlen
-pos
, &n
);
972 pos
+= sshcom_read_mpint(ciphertext
+pos
, cipherlen
-pos
, &u
);
973 pos
+= sshcom_read_mpint(ciphertext
+pos
, cipherlen
-pos
, &p
);
974 pos
+= sshcom_read_mpint(ciphertext
+pos
, cipherlen
-pos
, &q
);
976 errmsg
= "key data did not contain six integers";
982 pos
+= put_string(blob
+pos
, "ssh-rsa", 7);
983 pos
+= put_mp(blob
+pos
, e
.start
, e
.bytes
);
984 pos
+= put_mp(blob
+pos
, n
.start
, n
.bytes
);
986 pos
+= put_string(blob
+pos
, d
.start
, d
.bytes
);
987 pos
+= put_mp(blob
+pos
, q
.start
, q
.bytes
);
988 pos
+= put_mp(blob
+pos
, p
.start
, p
.bytes
);
989 pos
+= put_mp(blob
+pos
, u
.start
, u
.bytes
);
990 privlen
= pos
- publen
;
991 } else if (type
== DSA
) {
992 struct mpint_pos p
, q
, g
, x
, y
;
994 if (GET_32BIT(ciphertext
) != 0) {
995 errmsg
= "predefined DSA parameters not supported";
998 pos
+= sshcom_read_mpint(ciphertext
+pos
, cipherlen
-pos
, &p
);
999 pos
+= sshcom_read_mpint(ciphertext
+pos
, cipherlen
-pos
, &g
);
1000 pos
+= sshcom_read_mpint(ciphertext
+pos
, cipherlen
-pos
, &q
);
1001 pos
+= sshcom_read_mpint(ciphertext
+pos
, cipherlen
-pos
, &y
);
1002 pos
+= sshcom_read_mpint(ciphertext
+pos
, cipherlen
-pos
, &x
);
1004 errmsg
= "key data did not contain five integers";
1010 pos
+= put_string(blob
+pos
, "ssh-dss", 7);
1011 pos
+= put_mp(blob
+pos
, p
.start
, p
.bytes
);
1012 pos
+= put_mp(blob
+pos
, q
.start
, q
.bytes
);
1013 pos
+= put_mp(blob
+pos
, g
.start
, g
.bytes
);
1014 pos
+= put_mp(blob
+pos
, y
.start
, y
.bytes
);
1016 pos
+= put_mp(blob
+pos
, x
.start
, x
.bytes
);
1017 privlen
= pos
- publen
;
1020 assert(privlen
> 0); /* should have bombed by now if not */
1022 retkey
= smalloc(sizeof(struct ssh2_userkey
));
1024 retkey
->data
= alg
->createkey(blob
, publen
, blob
+publen
, privlen
);
1025 if (!retkey
->data
) {
1027 errmsg
= "unable to create key data structure";
1030 retkey
->comment
= dupstr(key
->comment
);
1032 errmsg
= NULL
; /* no error */
1037 memset(blob
, 0, blobsize
);
1040 memset(key
->keyblob
, 0, key
->keyblob_size
);
1041 sfree(key
->keyblob
);
1042 memset(&key
, 0, sizeof(key
));