annotate keyimport.c @ 1911:ced53051e200

Add ecdsa OpenSSH format for dropbearconvert
author Matt Johnston <matt@ucc.asn.au>
date Tue, 29 Mar 2022 23:27:55 +0800
parents eadd023fde4d
children 8b4274d34fe8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 * Based on PuTTY's import.c for importing/exporting OpenSSH and SSH.com
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * keyfiles.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 * Modifications copyright 2003 Matt Johnston
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * PuTTY is copyright 1997-2003 Simon Tatham.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 * Justin Bradford, and CORE SDI S.A.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 * Permission is hereby granted, free of charge, to any person
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 * obtaining a copy of this software and associated documentation files
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 * (the "Software"), to deal in the Software without restriction,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 * including without limitation the rights to use, copy, modify, merge,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 * publish, distribute, sublicense, and/or sell copies of the Software,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 * and to permit persons to whom the Software is furnished to do so,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 * subject to the following conditions:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 * The above copyright notice and this permission notice shall be
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 * included in all copies or substantial portions of the Software.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 #include "keyimport.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 #include "bignum.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 #include "buffer.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #include "dbutil.h"
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
37 #include "ecc.h"
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
38 #include "ssh.h"
1674
ba6fc7afe1c5 use sigtype where appropriate
Matt Johnston <matt@ucc.asn.au>
parents: 1659
diff changeset
39 #include "rsa.h"
ba6fc7afe1c5 use sigtype where appropriate
Matt Johnston <matt@ucc.asn.au>
parents: 1659
diff changeset
40 #include "dss.h"
ba6fc7afe1c5 use sigtype where appropriate
Matt Johnston <matt@ucc.asn.au>
parents: 1659
diff changeset
41 #include "ed25519.h"
1911
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
42 #include "ecdsa.h"
1908
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
43 #include "signkey_ossh.h"
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
44
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
45 static const unsigned char OSSH_PKEY_BLOB[] =
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
46 "openssh-key-v1\0" /* AUTH_MAGIC */
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
47 "\0\0\0\4none" /* cipher name*/
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
48 "\0\0\0\4none" /* kdf name */
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
49 "\0\0\0\0" /* kdf */
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
50 "\0\0\0\1"; /* key num */
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
51 #define OSSH_PKEY_BLOBLEN (sizeof(OSSH_PKEY_BLOB) - 1)
1295
750ec4ec4cbe Convert #ifdef to #if, other build changes
Matt Johnston <matt@ucc.asn.au>
parents: 1294
diff changeset
52 #if DROPBEAR_ECDSA
836
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
53 static const unsigned char OID_SEC256R1_BLOB[] = {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07};
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
54 static const unsigned char OID_SEC384R1_BLOB[] = {0x2b, 0x81, 0x04, 0x00, 0x22};
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
55 static const unsigned char OID_SEC521R1_BLOB[] = {0x2b, 0x81, 0x04, 0x00, 0x23};
1294
56aba7dedbea options for disabling "normal" DH
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
56 #endif
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 #define PUT_32BIT(cp, value) do { \
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 (cp)[3] = (unsigned char)(value); \
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 (cp)[2] = (unsigned char)((value) >> 8); \
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 (cp)[1] = (unsigned char)((value) >> 16); \
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 (cp)[0] = (unsigned char)((value) >> 24); } while (0)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 #define GET_32BIT(cp) \
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
65 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
66 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
67 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
68 ((unsigned long)(unsigned char)(cp)[3]))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 static int openssh_encrypted(const char *filename);
1459
06d52bcb8094 Pointer parameter could be declared as pointing to const
Francois Perrad <francois.perrad@gadz.org>
parents: 1395
diff changeset
71 static sign_key *openssh_read(const char *filename, const char *passphrase);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 static int openssh_write(const char *filename, sign_key *key,
1459
06d52bcb8094 Pointer parameter could be declared as pointing to const
Francois Perrad <francois.perrad@gadz.org>
parents: 1395
diff changeset
73 const char *passphrase);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 static int dropbear_write(const char*filename, sign_key * key);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 static sign_key *dropbear_read(const char* filename);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
78 static int toint(unsigned u);
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
79
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 #if 0
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 static int sshcom_encrypted(const char *filename, char **comment);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 static struct ssh2_userkey *sshcom_read(const char *filename, char *passphrase);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 static int sshcom_write(const char *filename, struct ssh2_userkey *key,
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
84 char *passphrase);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 #endif
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 int import_encrypted(const char* filename, int filetype) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 if (filetype == KEYFILE_OPENSSH) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 return openssh_encrypted(filename);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 #if 0
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 } else if (filetype == KEYFILE_SSHCOM) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 return sshcom_encrypted(filename, NULL);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 #endif
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
95 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
96 return 0;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98
1459
06d52bcb8094 Pointer parameter could be declared as pointing to const
Francois Perrad <francois.perrad@gadz.org>
parents: 1395
diff changeset
99 sign_key *import_read(const char *filename, const char *passphrase, int filetype) {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 if (filetype == KEYFILE_OPENSSH) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 return openssh_read(filename, passphrase);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 } else if (filetype == KEYFILE_DROPBEAR) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 return dropbear_read(filename);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 #if 0
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 } else if (filetype == KEYFILE_SSHCOM) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 return sshcom_read(filename, passphrase);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 #endif
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
110 return NULL;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112
1459
06d52bcb8094 Pointer parameter could be declared as pointing to const
Francois Perrad <francois.perrad@gadz.org>
parents: 1395
diff changeset
113 int import_write(const char *filename, sign_key *key, const char *passphrase,
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 int filetype) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 if (filetype == KEYFILE_OPENSSH) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 return openssh_write(filename, key, passphrase);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 } else if (filetype == KEYFILE_DROPBEAR) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 return dropbear_write(filename, key);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 #if 0
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 } else if (filetype == KEYFILE_SSHCOM) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 return sshcom_write(filename, key, passphrase);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 #endif
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
125 return 0;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 static sign_key *dropbear_read(const char* filename) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 buffer * buf = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 sign_key *ret = NULL;
846
b298bb438625 refactor key generation, make it generate as required.
Matt Johnston <matt@ucc.asn.au>
parents: 845
diff changeset
132 enum signkey_type type;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133
73
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
134 buf = buf_new(MAX_PRIVKEY_SIZE);
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
135 if (buf_readfile(buf, filename) == DROPBEAR_FAILURE) {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 goto error;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 buf_setpos(buf, 0);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 ret = new_sign_key();
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 type = DROPBEAR_SIGNKEY_ANY;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 if (buf_get_priv_key(buf, ret, &type) == DROPBEAR_FAILURE){
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 goto error;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 buf_free(buf);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147
836
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
148 ret->type = type;
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
149
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 return ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 error:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 if (buf) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 buf_free(buf);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 if (ret) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 sign_key_free(ret);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 return NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 /* returns 0 on fail, 1 on success */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 static int dropbear_write(const char*filename, sign_key * key) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 buffer * buf;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 FILE*fp;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 int len;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 int ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169
73
0bf5cebe622c Dropbearkey can now print out pubkey portions
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
170 buf = buf_new(MAX_PRIVKEY_SIZE);
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
171 buf_put_priv_key(buf, key, key->type);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172
87
680a0bc9df0a Some small fixes for unused vars, and old messages
Matt Johnston <matt@ucc.asn.au>
parents: 73
diff changeset
173 fp = fopen(filename, "w");
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 if (!fp) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 ret = 0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 goto out;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 buf_setpos(buf, 0);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 do {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 len = fwrite(buf_getptr(buf, buf->len - buf->pos),
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 1, buf->len - buf->pos, fp);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 buf_incrpos(buf, len);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 } while (len > 0 && buf->len != buf->pos);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185
256
ac890087b8c1 * keyimport.c: fclose() the key file to make sure data gets written
Matt Johnston <matt@ucc.asn.au>
parents: 241
diff changeset
186 fclose(fp);
ac890087b8c1 * keyimport.c: fclose() the key file to make sure data gets written
Matt Johnston <matt@ucc.asn.au>
parents: 241
diff changeset
187
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 if (buf->pos != buf->len) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 ret = 0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 } else {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 ret = 1;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 out:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 buf_free(buf);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 return ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 /* ----------------------------------------------------------------------
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 * Helper routines. (The base64 ones are defined in sshpubk.c.)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 #define isbase64(c) ( ((c) >= 'A' && (c) <= 'Z') || \
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
204 ((c) >= 'a' && (c) <= 'z') || \
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
205 ((c) >= '0' && (c) <= '9') || \
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
206 (c) == '+' || (c) == '/' || (c) == '=' \
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
207 )
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 /* cpl has to be less than 100 */
1459
06d52bcb8094 Pointer parameter could be declared as pointing to const
Francois Perrad <francois.perrad@gadz.org>
parents: 1395
diff changeset
210 static void base64_encode_fp(FILE * fp, const unsigned char *data,
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 int datalen, int cpl)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212 {
1094
c45d65392c1a Fix pointer differ in signess warnings [-Werror=pointer-sign]
Gaël PORTAY <gael.portay@gmail.com>
parents: 1089
diff changeset
213 unsigned char out[100];
1250
2bb4c662d1c2 more hard tab
Francois Perrad <francois.perrad@gadz.org>
parents: 1124
diff changeset
214 int n;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 unsigned long outlen;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 int rawcpl;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 rawcpl = cpl * 3 / 4;
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 87
diff changeset
218 dropbear_assert((unsigned int)cpl < sizeof(out));
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219
1250
2bb4c662d1c2 more hard tab
Francois Perrad <francois.perrad@gadz.org>
parents: 1124
diff changeset
220 while (datalen > 0) {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 n = (datalen < rawcpl ? datalen : rawcpl);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 outlen = sizeof(out);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 base64_encode(data, n, out, &outlen);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 data += n;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 datalen -= n;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 fwrite(out, 1, outlen, fp);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 fputc('\n', fp);
1250
2bb4c662d1c2 more hard tab
Francois Perrad <francois.perrad@gadz.org>
parents: 1124
diff changeset
228 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 /*
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 * Read an ASN.1/BER identifier and length pair.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 * Flags are a combination of the #defines listed below.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 * Returns -1 if unsuccessful; otherwise returns the number of
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 * bytes used out of the source data.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 /* ASN.1 tag classes. */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 #define ASN1_CLASS_UNIVERSAL (0 << 6)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 #define ASN1_CLASS_APPLICATION (1 << 6)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 #define ASN1_CLASS_CONTEXT_SPECIFIC (2 << 6)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 #define ASN1_CLASS_PRIVATE (3 << 6)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 #define ASN1_CLASS_MASK (3 << 6)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 /* Primitive versus constructed bit. */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 #define ASN1_CONSTRUCTED (1 << 5)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 static int ber_read_id_len(void *source, int sourcelen,
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
250 int *id, int *length, int *flags)
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 {
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
252 unsigned char *p = (unsigned char *) source;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
254 if (sourcelen == 0)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
255 return -1;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
257 *flags = (*p & 0xE0);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
258 if ((*p & 0x1F) == 0x1F) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
259 *id = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
260 while (*p & 0x80) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
261 p++, sourcelen--;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
262 if (sourcelen == 0)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
263 return -1;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
264 *id = (*id << 7) | (*p & 0x7F);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
265 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
266 p++, sourcelen--;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
267 } else {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
268 *id = *p & 0x1F;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
269 p++, sourcelen--;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 }
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
271
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
272 if (sourcelen == 0)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
273 return -1;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
275 if (*p & 0x80) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
276 unsigned len;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
277 int n = *p & 0x7F;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
278 p++, sourcelen--;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
279 if (sourcelen < n)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
280 return -1;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
281 len = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
282 while (n--)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
283 len = (len << 8) | (*p++);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
284 sourcelen -= n;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
285 *length = toint(len);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
286 } else {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
287 *length = *p;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
288 p++, sourcelen--;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
289 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
291 if (*length < 0) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
292 printf("Negative ASN.1 length\n");
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
293 return -1;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
294 }
1307
ad9c40aca3bc add length checks for ecc too
Matt Johnston <matt@ucc.asn.au>
parents: 1306
diff changeset
295
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
296 return p - (unsigned char *) source;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299 /*
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 * Write an ASN.1/BER identifier and length pair. Returns the
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301 * number of bytes consumed. Assumes dest contains enough space.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302 * Will avoid writing anything if dest is NULL, but still return
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
303 * amount of space required.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
304 */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305 static int ber_write_id_len(void *dest, int id, int length, int flags)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
306 {
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
307 unsigned char *d = (unsigned char *)dest;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
308 int len = 0;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
310 if (id <= 30) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
311 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
312 * Identifier is one byte.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
313 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
314 len++;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
315 if (d) *d++ = id | flags;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
316 } else {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
317 int n;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
318 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
319 * Identifier is multiple bytes: the first byte is 11111
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
320 * plus the flags, and subsequent bytes encode the value of
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
321 * the identifier, 7 bits at a time, with the top bit of
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
322 * each byte 1 except the last one which is 0.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
323 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
324 len++;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
325 if (d) *d++ = 0x1F | flags;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
326 for (n = 1; (id >> (7*n)) > 0; n++)
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 continue; /* count the bytes */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
328 while (n--) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
329 len++;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
330 if (d) *d++ = (n ? 0x80 : 0) | ((id >> (7*n)) & 0x7F);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
331 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
332 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
333
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
334 if (length < 128) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
335 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
336 * Length is one byte.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
337 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
338 len++;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
339 if (d) *d++ = length;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
340 } else {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
341 int n;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
342 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
343 * Length is multiple bytes. The first is 0x80 plus the
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
344 * number of subsequent bytes, and the subsequent bytes
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
345 * encode the actual length.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
346 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
347 for (n = 1; (length >> (8*n)) > 0; n++)
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
348 continue; /* count the bytes */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
349 len++;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
350 if (d) *d++ = 0x80 | n;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
351 while (n--) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
352 len++;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
353 if (d) *d++ = (length >> (8*n)) & 0xFF;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
354 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
355 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
356
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
357 return len;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
358 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
359
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
360
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
361 /* Simple structure to point to an mp-int within a blob. */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
362 struct mpint_pos { void *start; int bytes; };
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
363
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
364 /* ----------------------------------------------------------------------
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365 * Code to read and write OpenSSH private keys.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
366 */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
367
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
368 enum { OSSH_DSA, OSSH_RSA, OSSH_EC, OSSH_PKEY };
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
369 struct openssh_key {
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
370 int type;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
371 int encrypted;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
372 char iv[32];
1907
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
373 /* keyblob is publickey1 onwards (ref OpenSSH PROTOCOL.key) */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
374 unsigned char *keyblob;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
375 unsigned int keyblob_len, keyblob_size;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
376 };
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
377
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
378 static struct openssh_key *load_openssh_key(const char *filename)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
379 {
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
380 struct openssh_key *ret;
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
381 buffer *buf = NULL;
340
454a34b2dfd1 Fixes from Erik Hovland:
Matt Johnston <matt@ucc.asn.au>
parents: 256
diff changeset
382 FILE *fp = NULL;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
383 char buffer[256];
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
384 char *errmsg = NULL, *p = NULL;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
385 int headers_done;
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
386 unsigned long len;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
387
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
388 ret = (struct openssh_key*)m_malloc(sizeof(struct openssh_key));
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
389 ret->keyblob = NULL;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
390 ret->keyblob_len = ret->keyblob_size = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
391 ret->encrypted = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
392 memset(ret->iv, 0, sizeof(ret->iv));
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
393
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
394 if (strlen(filename) == 1 && filename[0] == '-') {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
395 fp = stdin;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
396 } else {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
397 fp = fopen(filename, "r");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
398 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
399 if (!fp) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
400 errmsg = "Unable to open key file";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
401 goto error;
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
402 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
403 if (!fgets(buffer, sizeof(buffer), fp) ||
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
404 0 != strncmp(buffer, "-----BEGIN ", 11) ||
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
405 0 != strcmp(buffer+strlen(buffer)-17, "PRIVATE KEY-----\n")) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
406 errmsg = "File does not begin with OpenSSH key header";
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
407 goto error;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
408 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
409 if (!strcmp(buffer, "-----BEGIN RSA PRIVATE KEY-----\n"))
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
410 ret->type = OSSH_RSA;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
411 else if (!strcmp(buffer, "-----BEGIN DSA PRIVATE KEY-----\n"))
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
412 ret->type = OSSH_DSA;
793
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 491
diff changeset
413 else if (!strcmp(buffer, "-----BEGIN EC PRIVATE KEY-----\n"))
70625eed40c9 A bit of work on ecdsa for host/auth keys
Matt Johnston <matt@ucc.asn.au>
parents: 491
diff changeset
414 ret->type = OSSH_EC;
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
415 else if (!strcmp(buffer, "-----BEGIN OPENSSH PRIVATE KEY-----\n"))
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
416 ret->type = OSSH_PKEY;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
417 else {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
418 errmsg = "Unrecognised key type";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
419 goto error;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
420 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
421
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
422 headers_done = 0;
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
423 buf = buf_new(0);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
424 while (1) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
425 if (!fgets(buffer, sizeof(buffer), fp)) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
426 errmsg = "Unexpected end of file";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
427 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
428 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
429 if (0 == strncmp(buffer, "-----END ", 9) &&
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
430 0 == strcmp(buffer+strlen(buffer)-17, "PRIVATE KEY-----\n"))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
431 break; /* done */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
432 if ((p = strchr(buffer, ':')) != NULL) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
433 if (headers_done) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
434 errmsg = "Header found in body of key data";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
435 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
436 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
437 *p++ = '\0';
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
438 while (*p && isspace((unsigned char)*p)) p++;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
439 if (!strcmp(buffer, "Proc-Type")) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
440 if (p[0] != '4' || p[1] != ',') {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
441 errmsg = "Proc-Type is not 4 (only 4 is supported)";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
442 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
443 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
444 p += 2;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
445 if (!strcmp(p, "ENCRYPTED\n"))
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
446 ret->encrypted = 1;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
447 } else if (!strcmp(buffer, "DEK-Info")) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
448 int i, j;
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
449
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
450 if (strncmp(p, "DES-EDE3-CBC,", 13)) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
451 errmsg = "Ciphers other than DES-EDE3-CBC not supported";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
452 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
453 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
454 p += 13;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
455 for (i = 0; i < 8; i++) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
456 if (1 != sscanf(p, "%2x", &j))
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
457 break;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
458 ret->iv[i] = j;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
459 p += 2;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
460 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
461 if (i < 8) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
462 errmsg = "Expected 16-digit iv in DEK-Info";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
463 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
464 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
465 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
466 } else {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
467 headers_done = 1;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
468 len = strlen(buffer);
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
469 buf = buf_resize(buf, buf->size + len);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
470 buf_putbytes(buf, buffer, len);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
471 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
472 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
473
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
474 if (buf && buf->len) {
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
475 ret->keyblob_size = ret->keyblob_len + buf->len*4/3 + 256;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
476 ret->keyblob = (unsigned char*)m_realloc(ret->keyblob, ret->keyblob_size);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
477 len = ret->keyblob_size;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
478 if (base64_decode((const unsigned char *)buf->data, buf->len,
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
479 ret->keyblob, &len) != CRYPT_OK){
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
480 errmsg = "Error decoding base64";
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
481 goto error;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
482 }
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
483 ret->keyblob_len = len;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
484 }
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
485
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
486 if (ret->type == OSSH_PKEY) {
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
487 if (ret->keyblob_len < OSSH_PKEY_BLOBLEN ||
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
488 memcmp(ret->keyblob, OSSH_PKEY_BLOB, OSSH_PKEY_BLOBLEN)) {
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
489 errmsg = "Error decoding OpenSSH key";
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
490 goto error;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
491 }
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
492 ret->keyblob_len -= OSSH_PKEY_BLOBLEN;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
493 memmove(ret->keyblob, ret->keyblob + OSSH_PKEY_BLOBLEN, ret->keyblob_len);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
494 }
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
495
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
496 if (ret->keyblob_len == 0 || !ret->keyblob) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
497 errmsg = "Key body not present";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
498 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
499 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
500
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
501 if (ret->encrypted && ret->keyblob_len % 8 != 0) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
502 errmsg = "Encrypted key blob is not a multiple of cipher block size";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
503 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
504 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
505
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
506 if (buf) {
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
507 buf_burn(buf);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
508 buf_free(buf);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
509 }
1045
31727a8abd4b Use m_burn rather than memset
Thorsten Horstmann <thorsten.horstmann@web.de>
parents: 1038
diff changeset
510 m_burn(buffer, sizeof(buffer));
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
511 return ret;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
512
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
513 error:
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
514 if (buf) {
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
515 buf_burn(buf);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
516 buf_free(buf);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
517 }
1045
31727a8abd4b Use m_burn rather than memset
Thorsten Horstmann <thorsten.horstmann@web.de>
parents: 1038
diff changeset
518 m_burn(buffer, sizeof(buffer));
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
519 if (ret) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
520 if (ret->keyblob) {
1045
31727a8abd4b Use m_burn rather than memset
Thorsten Horstmann <thorsten.horstmann@web.de>
parents: 1038
diff changeset
521 m_burn(ret->keyblob, ret->keyblob_size);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
522 m_free(ret->keyblob);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
523 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
524 m_free(ret);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
525 }
340
454a34b2dfd1 Fixes from Erik Hovland:
Matt Johnston <matt@ucc.asn.au>
parents: 256
diff changeset
526 if (fp) {
454a34b2dfd1 Fixes from Erik Hovland:
Matt Johnston <matt@ucc.asn.au>
parents: 256
diff changeset
527 fclose(fp);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
528 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
529 if (errmsg) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
530 fprintf(stderr, "Error: %s\n", errmsg);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
531 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
532 return NULL;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
533 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
534
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
535 static int openssh_encrypted(const char *filename)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
536 {
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
537 struct openssh_key *key = load_openssh_key(filename);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
538 int ret;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
539
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
540 if (!key)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
541 return 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
542 ret = key->encrypted;
1045
31727a8abd4b Use m_burn rather than memset
Thorsten Horstmann <thorsten.horstmann@web.de>
parents: 1038
diff changeset
543 m_burn(key->keyblob, key->keyblob_size);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
544 m_free(key->keyblob);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
545 m_free(key);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
546 return ret;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
547 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
548
1459
06d52bcb8094 Pointer parameter could be declared as pointing to const
Francois Perrad <francois.perrad@gadz.org>
parents: 1395
diff changeset
549 static sign_key *openssh_read(const char *filename, const char * UNUSED(passphrase))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550 {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551 struct openssh_key *key;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
552 unsigned char *p;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
553 int ret, id, len, flags;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554 int i, num_integers = 0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 sign_key *retval = NULL;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
556 char *errmsg;
1119
845922d73e9c Turn modptr local variable into unsigned char *
Gaël PORTAY <gael.portay@gmail.com>
parents: 1094
diff changeset
557 unsigned char *modptr = NULL;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
558 int modlen = -9999;
846
b298bb438625 refactor key generation, make it generate as required.
Matt Johnston <matt@ucc.asn.au>
parents: 845
diff changeset
559 enum signkey_type type;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
561 sign_key *retkey;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
562 buffer * blobbuf = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
563
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
564 retkey = new_sign_key();
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
565
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
566 key = load_openssh_key(filename);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
567
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
568 if (!key)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
569 return NULL;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
571 if (key->encrypted) {
1908
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
572 errmsg = "Encrypted keys are not supported. Please convert with ssh-keygen first";
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573 goto error;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
574 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
576 /*
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
577 * Now we have a decrypted key blob, which contains OpenSSH
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
578 * encoded private key. We must now untangle the OpenSSH format.
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
579 */
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
580 if (key->type == OSSH_PKEY) {
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
581 blobbuf = buf_new(key->keyblob_len);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
582 buf_putbytes(blobbuf, key->keyblob, key->keyblob_len);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
583 buf_setpos(blobbuf, 0);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
584
1907
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
585 /* limit length of public key blob */
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
586 len = buf_getint(blobbuf);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
587
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
588 type = DROPBEAR_SIGNKEY_ANY;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
589 if (buf_get_pub_key(blobbuf, retkey, &type)
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
590 != DROPBEAR_SUCCESS) {
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
591 errmsg = "Error parsing OpenSSH key";
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
592 goto ossh_error;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
593 }
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
594
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
595 /* restore full length */
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
596 buf_setlen(blobbuf, key->keyblob_len);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
597
1907
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
598 /* length of private key part. we can discard it */
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
599 buf_getint(blobbuf);
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
600
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
601 /* discard checkkey1 */
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
602 buf_getint(blobbuf);
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
603 /* discard checkkey2 */
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
604 buf_getint(blobbuf);
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
605
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
606 if (type != DROPBEAR_SIGNKEY_NONE) {
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
607 retkey->type = type;
1908
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
608 #if DROPBEAR_RSA
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
609 if (type == DROPBEAR_SIGNKEY_RSA) {
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
610 if (buf_get_rsa_priv_ossh(blobbuf, retkey)
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
611 == DROPBEAR_SUCCESS) {
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
612 errmsg = NULL;
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
613 retval = retkey;
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
614 goto error;
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
615 } else {
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
616 errmsg = "Error parsing OpenSSH RSA key";
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
617 goto ossh_error;
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
618 }
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
619 }
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
620 #endif
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
621 #if DROPBEAR_ED25519
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
622 if (type == DROPBEAR_SIGNKEY_ED25519) {
1907
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
623 if (buf_get_ed25519_priv_ossh(blobbuf, retkey)
1908
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
624 == DROPBEAR_SUCCESS) {
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
625 errmsg = NULL;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
626 retval = retkey;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
627 goto error;
1908
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
628 } else {
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
629 errmsg = "Error parsing OpenSSH ed25519 key";
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
630 goto ossh_error;
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
631 }
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
632 }
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
633 #endif
1911
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
634 #if DROPBEAR_ECDSA
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
635 if (signkey_is_ecdsa(type)) {
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
636 if (buf_get_ecdsa_priv_ossh(blobbuf, retkey)
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
637 == DROPBEAR_SUCCESS) {
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
638 errmsg = NULL;
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
639 retval = retkey;
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
640 goto error;
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
641 } else {
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
642 errmsg = "Error parsing OpenSSH ed25519 key";
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
643 goto ossh_error;
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
644 }
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
645 }
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
646 #endif
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
647 }
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
648
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
649 errmsg = "Unsupported OpenSSH key type";
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
650 ossh_error:
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
651 sign_key_free(retkey);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
652 retkey = NULL;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
653 goto error;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
654 }
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
655
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
656 /*
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
657 * Now we have a decrypted key blob, which contains an ASN.1
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
658 * encoded private key. We must now untangle the ASN.1.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
659 *
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
660 * We expect the whole key blob to be formatted as a SEQUENCE
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
661 * (0x30 followed by a length code indicating that the rest of
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
662 * the blob is part of the sequence). Within that SEQUENCE we
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
663 * expect to see a bunch of INTEGERs. What those integers mean
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
664 * depends on the key type:
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
665 *
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
666 * - For RSA, we expect the integers to be 0, n, e, d, p, q,
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
667 * dmp1, dmq1, iqmp in that order. (The last three are d mod
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
668 * (p-1), d mod (q-1), inverse of q mod p respectively.)
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
669 *
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
670 * - For DSA, we expect them to be 0, p, q, g, y, x in that
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
671 * order.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
672 */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
673
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
674 p = key->keyblob;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
675
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
676 /* Expect the SEQUENCE header. Take its absence as a failure to decrypt. */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
677 ret = ber_read_id_len(p, key->keyblob_len, &id, &len, &flags);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
678 p += ret;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
679 if (ret < 0 || id != 16 || len < 0 ||
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
680 key->keyblob+key->keyblob_len-p < len) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
681 errmsg = "ASN.1 decoding failure";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
682 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
683 }
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
684
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
685 /* Expect a load of INTEGERs. */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
686 if (key->type == OSSH_RSA)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
687 num_integers = 9;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
688 else if (key->type == OSSH_DSA)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
689 num_integers = 6;
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
690 else if (key->type == OSSH_EC)
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
691 num_integers = 1;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
692
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
693 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
694 * Space to create key blob in.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
695 */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
696 blobbuf = buf_new(3000);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
697
1295
750ec4ec4cbe Convert #ifdef to #if, other build changes
Matt Johnston <matt@ucc.asn.au>
parents: 1294
diff changeset
698 #if DROPBEAR_DSS
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
699 if (key->type == OSSH_DSA) {
1122
aaf576b27a10 Merge pull request #13 from gazoo74/fix-warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1119
diff changeset
700 buf_putstring(blobbuf, "ssh-dss", 7);
836
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
701 retkey->type = DROPBEAR_SIGNKEY_DSS;
935
25692c60479e Fix compiling with ECDSA and DSS disabled
Matt Johnston <matt@ucc.asn.au>
parents: 867
diff changeset
702 }
25692c60479e Fix compiling with ECDSA and DSS disabled
Matt Johnston <matt@ucc.asn.au>
parents: 867
diff changeset
703 #endif
1295
750ec4ec4cbe Convert #ifdef to #if, other build changes
Matt Johnston <matt@ucc.asn.au>
parents: 1294
diff changeset
704 #if DROPBEAR_RSA
935
25692c60479e Fix compiling with ECDSA and DSS disabled
Matt Johnston <matt@ucc.asn.au>
parents: 867
diff changeset
705 if (key->type == OSSH_RSA) {
1122
aaf576b27a10 Merge pull request #13 from gazoo74/fix-warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1119
diff changeset
706 buf_putstring(blobbuf, "ssh-rsa", 7);
836
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
707 retkey->type = DROPBEAR_SIGNKEY_RSA;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
708 }
935
25692c60479e Fix compiling with ECDSA and DSS disabled
Matt Johnston <matt@ucc.asn.au>
parents: 867
diff changeset
709 #endif
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
710
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
711 for (i = 0; i < num_integers; i++) {
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
712 ret = ber_read_id_len(p, key->keyblob+key->keyblob_len-p,
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
713 &id, &len, &flags);
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
714 p += ret;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
715 if (ret < 0 || id != 2 || len < 0 ||
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
716 key->keyblob+key->keyblob_len-p < len) {
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
717 errmsg = "ASN.1 decoding failure";
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
718 goto error;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
719 }
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
720
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
721 if (i == 0) {
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
722 /* First integer is a version indicator */
991
4f65c867fc99 Fix variables may be uninitialized.
Like Ma <likemartinma@gmail.com>
parents: 935
diff changeset
723 int expected = -1;
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
724 switch (key->type) {
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
725 case OSSH_RSA:
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
726 case OSSH_DSA:
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
727 expected = 0;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
728 break;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
729 case OSSH_EC:
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
730 expected = 1;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
731 break;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
732 }
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
733 if (len != 1 || p[0] != expected) {
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
734 errmsg = "Version number mismatch";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
735 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
736 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
737 } else if (key->type == OSSH_RSA) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
738 /*
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
739 * OpenSSH key order is n, e, d, p, q, dmp1, dmq1, iqmp
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
740 * but we want e, n, d, p, q
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
741 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
742 if (i == 1) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
743 /* Save the details for after we deal with number 2. */
1119
845922d73e9c Turn modptr local variable into unsigned char *
Gaël PORTAY <gael.portay@gmail.com>
parents: 1094
diff changeset
744 modptr = p;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
745 modlen = len;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
746 } else if (i >= 2 && i <= 5) {
1122
aaf576b27a10 Merge pull request #13 from gazoo74/fix-warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1119
diff changeset
747 buf_putstring(blobbuf, (const char*)p, len);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
748 if (i == 2) {
1122
aaf576b27a10 Merge pull request #13 from gazoo74/fix-warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1119
diff changeset
749 buf_putstring(blobbuf, (const char*)modptr, modlen);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
750 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
751 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
752 } else if (key->type == OSSH_DSA) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
753 /*
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
754 * OpenSSH key order is p, q, g, y, x,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
755 * we want the same.
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
756 */
1122
aaf576b27a10 Merge pull request #13 from gazoo74/fix-warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1119
diff changeset
757 buf_putstring(blobbuf, (const char*)p, len);
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
758 }
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
759
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
760 /* Skip past the number. */
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
761 p += len;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
762 }
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
763
1295
750ec4ec4cbe Convert #ifdef to #if, other build changes
Matt Johnston <matt@ucc.asn.au>
parents: 1294
diff changeset
764 #if DROPBEAR_ECDSA
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
765 if (key->type == OSSH_EC) {
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
766 unsigned char* private_key_bytes = NULL;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
767 int private_key_len = 0;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
768 unsigned char* public_key_bytes = NULL;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
769 int public_key_len = 0;
807
75509065db53 have separate ecdsa keys for each size
Matt Johnston <matt@ucc.asn.au>
parents: 806
diff changeset
770 ecc_key *ecc = NULL;
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
771 const struct dropbear_ecc_curve *curve = NULL;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
772
857
c19acba28590 use oldstyle comments
Matt Johnston <matt@ucc.asn.au>
parents: 849
diff changeset
773 /* See SEC1 v2, Appendix C.4 */
c19acba28590 use oldstyle comments
Matt Johnston <matt@ucc.asn.au>
parents: 849
diff changeset
774 /* OpenSSL (so OpenSSH) seems to include the optional parts. */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
775
857
c19acba28590 use oldstyle comments
Matt Johnston <matt@ucc.asn.au>
parents: 849
diff changeset
776 /* privateKey OCTET STRING, */
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
777 ret = ber_read_id_len(p, key->keyblob+key->keyblob_len-p,
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
778 &id, &len, &flags);
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
779 p += ret;
857
c19acba28590 use oldstyle comments
Matt Johnston <matt@ucc.asn.au>
parents: 849
diff changeset
780 /* id==4 for octet string */
1395
77c0d57a4410 fix indentation
Francois Perrad <francois.perrad@gadz.org>
parents: 1316
diff changeset
781 if (ret < 0 || id != 4 || len < 0 ||
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
782 key->keyblob+key->keyblob_len-p < len) {
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
783 errmsg = "ASN.1 decoding failure";
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
784 goto error;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
785 }
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
786 private_key_bytes = p;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
787 private_key_len = len;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
788 p += len;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
789
857
c19acba28590 use oldstyle comments
Matt Johnston <matt@ucc.asn.au>
parents: 849
diff changeset
790 /* parameters [0] ECDomainParameters {{ SECGCurveNames }} OPTIONAL, */
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
791 ret = ber_read_id_len(p, key->keyblob+key->keyblob_len-p,
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
792 &id, &len, &flags);
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
793 p += ret;
857
c19acba28590 use oldstyle comments
Matt Johnston <matt@ucc.asn.au>
parents: 849
diff changeset
794 /* id==0 */
1395
77c0d57a4410 fix indentation
Francois Perrad <francois.perrad@gadz.org>
parents: 1316
diff changeset
795 if (ret < 0 || id != 0 || len < 0) {
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
796 errmsg = "ASN.1 decoding failure";
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
797 goto error;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
798 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
799
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
800 ret = ber_read_id_len(p, key->keyblob+key->keyblob_len-p,
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
801 &id, &len, &flags);
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
802 p += ret;
857
c19acba28590 use oldstyle comments
Matt Johnston <matt@ucc.asn.au>
parents: 849
diff changeset
803 /* id==6 for object */
1395
77c0d57a4410 fix indentation
Francois Perrad <francois.perrad@gadz.org>
parents: 1316
diff changeset
804 if (ret < 0 || id != 6 || len < 0 ||
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
805 key->keyblob+key->keyblob_len-p < len) {
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
806 errmsg = "ASN.1 decoding failure";
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
807 goto error;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
808 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
809
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 846
diff changeset
810 if (0) {}
1295
750ec4ec4cbe Convert #ifdef to #if, other build changes
Matt Johnston <matt@ucc.asn.au>
parents: 1294
diff changeset
811 #if DROPBEAR_ECC_256
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 846
diff changeset
812 else if (len == sizeof(OID_SEC256R1_BLOB)
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
813 && memcmp(p, OID_SEC256R1_BLOB, len) == 0) {
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
814 retkey->type = DROPBEAR_SIGNKEY_ECDSA_NISTP256;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
815 curve = &ecc_curve_nistp256;
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 846
diff changeset
816 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
817 #endif
1295
750ec4ec4cbe Convert #ifdef to #if, other build changes
Matt Johnston <matt@ucc.asn.au>
parents: 1294
diff changeset
818 #if DROPBEAR_ECC_384
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 846
diff changeset
819 else if (len == sizeof(OID_SEC384R1_BLOB)
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
820 && memcmp(p, OID_SEC384R1_BLOB, len) == 0) {
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
821 retkey->type = DROPBEAR_SIGNKEY_ECDSA_NISTP384;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
822 curve = &ecc_curve_nistp384;
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 846
diff changeset
823 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
824 #endif
1295
750ec4ec4cbe Convert #ifdef to #if, other build changes
Matt Johnston <matt@ucc.asn.au>
parents: 1294
diff changeset
825 #if DROPBEAR_ECC_521
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 846
diff changeset
826 else if (len == sizeof(OID_SEC521R1_BLOB)
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
827 && memcmp(p, OID_SEC521R1_BLOB, len) == 0) {
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
828 retkey->type = DROPBEAR_SIGNKEY_ECDSA_NISTP521;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
829 curve = &ecc_curve_nistp521;
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 846
diff changeset
830 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
831 #endif
847
f4bb964c8678 Add '-R' for delayed hostkey option
Matt Johnston <matt@ucc.asn.au>
parents: 846
diff changeset
832 else {
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
833 errmsg = "Unknown ECC key type";
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
834 goto error;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
835 }
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
836 p += len;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
837
857
c19acba28590 use oldstyle comments
Matt Johnston <matt@ucc.asn.au>
parents: 849
diff changeset
838 /* publicKey [1] BIT STRING OPTIONAL */
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
839 ret = ber_read_id_len(p, key->keyblob+key->keyblob_len-p,
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
840 &id, &len, &flags);
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
841 p += ret;
857
c19acba28590 use oldstyle comments
Matt Johnston <matt@ucc.asn.au>
parents: 849
diff changeset
842 /* id==1 */
1395
77c0d57a4410 fix indentation
Francois Perrad <francois.perrad@gadz.org>
parents: 1316
diff changeset
843 if (ret < 0 || id != 1 || len < 0) {
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
844 errmsg = "ASN.1 decoding failure";
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
845 goto error;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
846 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
847
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
848 ret = ber_read_id_len(p, key->keyblob+key->keyblob_len-p,
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
849 &id, &len, &flags);
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
850 p += ret;
857
c19acba28590 use oldstyle comments
Matt Johnston <matt@ucc.asn.au>
parents: 849
diff changeset
851 /* id==3 for bit string */
1395
77c0d57a4410 fix indentation
Francois Perrad <francois.perrad@gadz.org>
parents: 1316
diff changeset
852 if (ret < 0 || id != 3 || len < 0 ||
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
853 key->keyblob+key->keyblob_len-p < len) {
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
854 errmsg = "ASN.1 decoding failure";
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
855 goto error;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
856 }
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
857 public_key_bytes = p+1;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
858 public_key_len = len-1;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
859 p += len;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
860
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
861 buf_putbytes(blobbuf, public_key_bytes, public_key_len);
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
862 ecc = buf_get_ecc_raw_pubkey(blobbuf, curve);
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
863 if (!ecc) {
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
864 errmsg = "Error parsing ECC key";
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
865 goto error;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
866 }
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
867 m_mp_alloc_init_multi((mp_int**)&ecc->k, NULL);
1692
1051e4eea25a Update LibTomMath to 1.2.0 (#84)
Steffen Jaeckel <s@jaeckel.eu>
parents: 1674
diff changeset
868 if (mp_from_ubin(ecc->k, private_key_bytes, private_key_len)
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
869 != MP_OKAY) {
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
870 errmsg = "Error parsing ECC key";
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
871 goto error;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
872 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
873
841
d4ce5269a439 Fix specifying a keysize for key generation, fix key name arguments
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
874 *signkey_key_ptr(retkey, retkey->type) = ecc;
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
875 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
876 #endif /* DROPBEAR_ECDSA */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
877
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
878 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
879 * Now put together the actual key. Simplest way to do this is
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
880 * to assemble our own key blobs and feed them to the createkey
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
881 * functions; this is a bit faffy but it does mean we get all
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
882 * the sanity checks for free.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
883 */
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
884 if (key->type == OSSH_RSA || key->type == OSSH_DSA) {
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
885 buf_setpos(blobbuf, 0);
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
886 type = DROPBEAR_SIGNKEY_ANY;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
887 if (buf_get_priv_key(blobbuf, retkey, &type)
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
888 != DROPBEAR_SUCCESS) {
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
889 errmsg = "unable to create key structure";
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
890 sign_key_free(retkey);
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
891 retkey = NULL;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
892 goto error;
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
893 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
894 }
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
895
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
896 errmsg = NULL; /* no error */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
897 retval = retkey;
806
71e7d31f7671 hackish ECC import code from OpenSSH
Matt Johnston <matt@ucc.asn.au>
parents: 793
diff changeset
898
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
899 error:
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
900 if (blobbuf) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
901 buf_burn(blobbuf);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
902 buf_free(blobbuf);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
903 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
904 m_burn(key->keyblob, key->keyblob_size);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
905 m_free(key->keyblob);
1002
97d1e54941fd When clearing the memory of 'key' in function openssh_read(), only the size
Christian Engelmayer <cengelma@gmx.at>
parents: 991
diff changeset
906 m_burn(key, sizeof(*key));
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
907 m_free(key);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
908 if (errmsg) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
909 fprintf(stderr, "Error: %s\n", errmsg);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
910 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
911 return retval;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
912 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
913
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
914 static int openssh_write(const char *filename, sign_key *key,
1459
06d52bcb8094 Pointer parameter could be declared as pointing to const
Francois Perrad <francois.perrad@gadz.org>
parents: 1395
diff changeset
915 const char *passphrase)
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
916 {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
917 buffer * keyblob = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
918 buffer * extrablob = NULL; /* used for calculated values to write */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
919 unsigned char *outblob = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
920 int outlen = -9999;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
921 struct mpint_pos numbers[9];
991
4f65c867fc99 Fix variables may be uninitialized.
Like Ma <likemartinma@gmail.com>
parents: 935
diff changeset
922 int nnumbers = -1, pos = 0, len = 0, seqlen, i;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
923 char *header = NULL, *footer = NULL;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
924 char zero[1];
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
925 int ret = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
926 FILE *fp;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
927
1295
750ec4ec4cbe Convert #ifdef to #if, other build changes
Matt Johnston <matt@ucc.asn.au>
parents: 1294
diff changeset
928 #if DROPBEAR_DSS
1911
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
929 if (key->type == DROPBEAR_SIGNKEY_DSS) {
836
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
930 /*
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
931 * Fetch the key blobs.
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
932 */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
933 keyblob = buf_new(3000);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
934 buf_put_priv_key(keyblob, key, key->type);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
935
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
936 buf_setpos(keyblob, 0);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
937 /* skip the "ssh-rsa" or "ssh-dss" header */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
938 buf_incrpos(keyblob, buf_getint(keyblob));
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
939
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
940 /*
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
941 * Find the sequence of integers to be encoded into the OpenSSH
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
942 * key blob, and also decide on the header line.
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
943 */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
944 numbers[0].start = zero; numbers[0].bytes = 1; zero[0] = '\0';
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
945
1499
2d450c1056e3 options: Complete the transition to numeric toggles (`#if')
Michael Witten <mfwitten@gmail.com>
parents: 1459
diff changeset
946 #if DROPBEAR_DSS
836
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
947 if (key->type == DROPBEAR_SIGNKEY_DSS) {
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
948
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
949 /* p */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
950 numbers[1].bytes = buf_getint(keyblob);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
951 numbers[1].start = buf_getptr(keyblob, numbers[1].bytes);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
952 buf_incrpos(keyblob, numbers[1].bytes);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
953
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
954 /* q */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
955 numbers[2].bytes = buf_getint(keyblob);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
956 numbers[2].start = buf_getptr(keyblob, numbers[2].bytes);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
957 buf_incrpos(keyblob, numbers[2].bytes);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
958
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
959 /* g */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
960 numbers[3].bytes = buf_getint(keyblob);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
961 numbers[3].start = buf_getptr(keyblob, numbers[3].bytes);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
962 buf_incrpos(keyblob, numbers[3].bytes);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
963
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
964 /* y */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
965 numbers[4].bytes = buf_getint(keyblob);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
966 numbers[4].start = buf_getptr(keyblob, numbers[4].bytes);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
967 buf_incrpos(keyblob, numbers[4].bytes);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
968
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
969 /* x */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
970 numbers[5].bytes = buf_getint(keyblob);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
971 numbers[5].start = buf_getptr(keyblob, numbers[5].bytes);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
972 buf_incrpos(keyblob, numbers[5].bytes);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
973
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
974 nnumbers = 6;
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
975 header = "-----BEGIN DSA PRIVATE KEY-----\n";
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
976 footer = "-----END DSA PRIVATE KEY-----\n";
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
977 }
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
978 #endif /* DROPBEAR_DSS */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
979
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
980 /*
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
981 * Now count up the total size of the ASN.1 encoded integers,
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
982 * so as to determine the length of the containing SEQUENCE.
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
983 */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
984 len = 0;
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
985 for (i = 0; i < nnumbers; i++) {
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
986 len += ber_write_id_len(NULL, 2, numbers[i].bytes, 0);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
987 len += numbers[i].bytes;
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
988 }
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
989 seqlen = len;
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
990 /* Now add on the SEQUENCE header. */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
991 len += ber_write_id_len(NULL, 16, seqlen, ASN1_CONSTRUCTED);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
992 /* Round up to the cipher block size, ensuring we have at least one
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
993 * byte of padding (see below). */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
994 outlen = len;
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
995 if (passphrase)
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
996 outlen = (outlen+8) &~ 7;
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
997
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
998 /*
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
999 * Now we know how big outblob needs to be. Allocate it.
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
1000 */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
1001 outblob = (unsigned char*)m_malloc(outlen);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1002
836
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
1003 /*
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
1004 * And write the data into it.
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
1005 */
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
1006 pos = 0;
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
1007 pos += ber_write_id_len(outblob+pos, 16, seqlen, ASN1_CONSTRUCTED);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
1008 for (i = 0; i < nnumbers; i++) {
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
1009 pos += ber_write_id_len(outblob+pos, 2, numbers[i].bytes, 0);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
1010 memcpy(outblob+pos, numbers[i].start, numbers[i].bytes);
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
1011 pos += numbers[i].bytes;
d7d9f1612d51 writing out openssh ecc keys works
Matt Johnston <matt@ucc.asn.au>
parents: 807
diff changeset
1012 }
1908
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1013 } /* end DSS handling */
1911
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
1014 #endif /* DROPBEAR_DSS */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1015
1908
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1016 if (0
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1017 #if DROPBEAR_RSA
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1018 || key->type == DROPBEAR_SIGNKEY_RSA
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1019 #endif
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1020 #if DROPBEAR_ED25519
1908
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1021 || key->type == DROPBEAR_SIGNKEY_ED25519
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1022 #endif
1911
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
1023 #if DROPBEAR_ECDSA
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
1024 || signkey_is_ecdsa(key->type)
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
1025 #endif
1908
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1026 ) {
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1027 buffer *buf = buf_new(3200);
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1028 keyblob = buf_new(3000);
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1029 extrablob = buf_new(3100);
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1030
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1031 /* private key blob w/o header */
1908
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1032 #if DROPBEAR_RSA
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1033 if (key->type == DROPBEAR_SIGNKEY_RSA) {
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1034 buf_put_rsa_priv_ossh(keyblob, key);
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1035 }
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1036 #endif
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1037 #if DROPBEAR_ED25519
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1038 if (key->type == DROPBEAR_SIGNKEY_ED25519) {
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1039 buf_put_ed25519_priv_ossh(keyblob, key);
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1040 }
eadd023fde4d Support RSA OpenSSH new format in dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1907
diff changeset
1041 #endif
1911
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
1042 #if DROPBEAR_ECDSA
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
1043 if (signkey_is_ecdsa(key->type)) {
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
1044 buf_put_ecdsa_priv_ossh(keyblob, key);
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
1045 }
ced53051e200 Add ecdsa OpenSSH format for dropbearconvert
Matt Johnston <matt@ucc.asn.au>
parents: 1908
diff changeset
1046 #endif
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1047
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1048 /* header */
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1049 buf_putbytes(buf, OSSH_PKEY_BLOB, OSSH_PKEY_BLOBLEN);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1050
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1051 /* public key */
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1052 buf_put_pub_key(buf, key, key->type);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1053
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1054 /* private key */
1907
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
1055 buf_putint(extrablob, 0); /* checkint 1 */
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
1056 buf_putint(extrablob, 0); /* checkint 2 */
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
1057 /* raw openssh private key */
3e0aacf0a4f3 Fix ed25519 dropbear to openssh conversion
Matt Johnston <matt@ucc.asn.au>
parents: 1754
diff changeset
1058 buf_putbytes(extrablob, keyblob->data, keyblob->len);
1659
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1059 /* comment */
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1060 buf_putstring(extrablob, "", 0);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1061 /* padding to cipher block length */
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1062 len = (extrablob->len+8) & ~7;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1063 for (i = 1; len - extrablob->len > 0; i++)
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1064 buf_putbyte(extrablob, i);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1065 buf_setpos(extrablob, 0);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1066 buf_putbytes(extrablob, "\0\0\0\0\0\0\0\0", 8);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1067 buf_putbufstring(buf, extrablob);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1068
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1069 outlen = len = pos = buf->len;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1070 outblob = (unsigned char*)m_malloc(outlen);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1071 memcpy(outblob, buf->data, buf->len);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1072
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1073 buf_burn(buf);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1074 buf_free(buf);
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1075 buf = NULL;
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1076
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1077 header = "-----BEGIN OPENSSH PRIVATE KEY-----\n";
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1078 footer = "-----END OPENSSH PRIVATE KEY-----\n";
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1079 }
d32bcb5c557d Add Ed25519 support (#91)
Vladislav Grishenko <themiron@users.noreply.github.com>
parents: 1638
diff changeset
1080
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1081 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1082 * Padding on OpenSSH keys is deterministic. The number of
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1083 * padding bytes is always more than zero, and always at most
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1084 * the cipher block length. The value of each padding byte is
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1085 * equal to the number of padding bytes. So a plaintext that's
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1086 * an exact multiple of the block size will be padded with 08
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1087 * 08 08 08 08 08 08 08 (assuming a 64-bit block cipher); a
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1088 * plaintext one byte less than a multiple of the block size
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1089 * will be padded with just 01.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1090 *
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1091 * This enables the OpenSSL key decryption function to strip
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1092 * off the padding algorithmically and return the unpadded
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1093 * plaintext to the next layer: it looks at the final byte, and
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1094 * then expects to find that many bytes at the end of the data
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1095 * with the same value. Those are all removed and the rest is
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1096 * returned.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1097 */
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 87
diff changeset
1098 dropbear_assert(pos == len);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1099 while (pos < outlen) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1100 outblob[pos++] = outlen - len;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1101 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1102
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1103 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1104 * Encrypt the key.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1105 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1106 if (passphrase) {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1107 fprintf(stderr, "Encrypted keys aren't supported currently\n");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1108 goto error;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1109 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1110
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1111 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1112 * And save it. We'll use Unix line endings just in case it's
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1113 * subsequently transferred in binary mode.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1114 */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1115 if (strlen(filename) == 1 && filename[0] == '-') {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1116 fp = stdout;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1117 } else {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1118 fp = fopen(filename, "wb"); /* ensure Unix line endings */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1119 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1120 if (!fp) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1121 fprintf(stderr, "Failed opening output file\n");
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1122 goto error;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1123 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1124 fputs(header, fp);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1125 base64_encode_fp(fp, outblob, outlen, 64);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1126 fputs(footer, fp);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1127 fclose(fp);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1128 ret = 1;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1129
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1130 error:
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1131 if (outblob) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1132 memset(outblob, 0, outlen);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1133 m_free(outblob);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1134 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1135 if (keyblob) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1136 buf_burn(keyblob);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1137 buf_free(keyblob);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1138 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1139 if (extrablob) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1140 buf_burn(extrablob);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1141 buf_free(extrablob);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1142 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1143 return ret;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1144 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1145
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1146 #if 0
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1147 /* XXX TODO ssh.com stuff isn't going yet */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1148
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1149 /* ----------------------------------------------------------------------
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1150 * Code to read ssh.com private keys.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1151 */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1152
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1153 /*
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1154 * The format of the base64 blob is largely ssh2-packet-formatted,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1155 * except that mpints are a bit different: they're more like the
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1156 * old ssh1 mpint. You have a 32-bit bit count N, followed by
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1157 * (N+7)/8 bytes of data.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1158 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1159 * So. The blob contains:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1160 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1161 * - uint32 0x3f6ff9eb (magic number)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1162 * - uint32 size (total blob size)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1163 * - string key-type (see below)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1164 * - string cipher-type (tells you if key is encrypted)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1165 * - string encrypted-blob
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1166 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1167 * (The first size field includes the size field itself and the
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1168 * magic number before it. All other size fields are ordinary ssh2
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1169 * strings, so the size field indicates how much data is to
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1170 * _follow_.)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1171 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1172 * The encrypted blob, once decrypted, contains a single string
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1173 * which in turn contains the payload. (This allows padding to be
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1174 * added after that string while still making it clear where the
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1175 * real payload ends. Also it probably makes for a reasonable
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1176 * decryption check.)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1177 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1178 * The payload blob, for an RSA key, contains:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1179 * - mpint e
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1180 * - mpint d
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1181 * - mpint n (yes, the public and private stuff is intermixed)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1182 * - mpint u (presumably inverse of p mod q)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1183 * - mpint p (p is the smaller prime)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1184 * - mpint q (q is the larger)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1185 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1186 * For a DSA key, the payload blob contains:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1187 * - uint32 0
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1188 * - mpint p
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1189 * - mpint g
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1190 * - mpint q
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1191 * - mpint y
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1192 * - mpint x
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1193 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1194 * Alternatively, if the parameters are `predefined', that
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1195 * (0,p,g,q) sequence can be replaced by a uint32 1 and a string
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1196 * containing some predefined parameter specification. *shudder*,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1197 * but I doubt we'll encounter this in real life.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1198 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1199 * The key type strings are ghastly. The RSA key I looked at had a
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1200 * type string of
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1201 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1202 * `if-modn{sign{rsa-pkcs1-sha1},encrypt{rsa-pkcs1v2-oaep}}'
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1203 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1204 * and the DSA key wasn't much better:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1205 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1206 * `dl-modp{sign{dsa-nist-sha1},dh{plain}}'
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1207 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1208 * It isn't clear that these will always be the same. I think it
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1209 * might be wise just to look at the `if-modn{sign{rsa' and
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1210 * `dl-modp{sign{dsa' prefixes.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1211 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1212 * Finally, the encryption. The cipher-type string appears to be
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1213 * either `none' or `3des-cbc'. Looks as if this is SSH2-style
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1214 * 3des-cbc (i.e. outer cbc rather than inner). The key is created
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1215 * from the passphrase by means of yet another hashing faff:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1216 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1217 * - first 16 bytes are MD5(passphrase)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1218 * - next 16 bytes are MD5(passphrase || first 16 bytes)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1219 * - if there were more, they'd be MD5(passphrase || first 32),
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1220 * and so on.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1221 */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1222
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1223 #define SSHCOM_MAGIC_NUMBER 0x3f6ff9eb
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1224
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1225 struct sshcom_key {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1226 char comment[256]; /* allowing any length is overkill */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1227 unsigned char *keyblob;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1228 int keyblob_len, keyblob_size;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1229 };
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1230
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1231 static struct sshcom_key *load_sshcom_key(const char *filename)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1232 {
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1233 struct sshcom_key *ret;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1234 FILE *fp;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1235 char buffer[256];
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1236 int len;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1237 char *errmsg, *p;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1238 int headers_done;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1239 char base64_bit[4];
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1240 int base64_chars = 0;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1241
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1242 ret = snew(struct sshcom_key);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1243 ret->comment[0] = '\0';
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1244 ret->keyblob = NULL;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1245 ret->keyblob_len = ret->keyblob_size = 0;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1246
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1247 fp = fopen(filename, "r");
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1248 if (!fp) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1249 errmsg = "Unable to open key file";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1250 goto error;
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
1251 }
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1252 if (!fgets(buffer, sizeof(buffer), fp) ||
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1253 0 != strcmp(buffer, "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----\n")) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1254 errmsg = "File does not begin with ssh.com key header";
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
1255 goto error;
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1256 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1257
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1258 headers_done = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1259 while (1) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1260 if (!fgets(buffer, sizeof(buffer), fp)) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1261 errmsg = "Unexpected end of file";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1262 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1263 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1264 if (!strcmp(buffer, "---- END SSH2 ENCRYPTED PRIVATE KEY ----\n"))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1265 break; /* done */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1266 if ((p = strchr(buffer, ':')) != NULL) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1267 if (headers_done) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1268 errmsg = "Header found in body of key data";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1269 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1270 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1271 *p++ = '\0';
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1272 while (*p && isspace((unsigned char)*p)) p++;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1273 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1274 * Header lines can end in a trailing backslash for
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1275 * continuation.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1276 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1277 while ((len = strlen(p)) > (int)(sizeof(buffer) - (p-buffer) -1) ||
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1278 p[len-1] != '\n' || p[len-2] == '\\') {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1279 if (len > (int)((p-buffer) + sizeof(buffer)-2)) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1280 errmsg = "Header line too long to deal with";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1281 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1282 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1283 if (!fgets(p+len-2, sizeof(buffer)-(p-buffer)-(len-2), fp)) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1284 errmsg = "Unexpected end of file";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1285 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1286 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1287 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1288 p[strcspn(p, "\n")] = '\0';
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1289 if (!strcmp(buffer, "Comment")) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1290 /* Strip quotes in comment if present. */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1291 if (p[0] == '"' && p[strlen(p)-1] == '"') {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1292 p++;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1293 p[strlen(p)-1] = '\0';
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1294 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1295 strncpy(ret->comment, p, sizeof(ret->comment));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1296 ret->comment[sizeof(ret->comment)-1] = '\0';
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1297 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1298 } else {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1299 headers_done = 1;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1300
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1301 p = buffer;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1302 while (isbase64(*p)) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1303 base64_bit[base64_chars++] = *p;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1304 if (base64_chars == 4) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1305 unsigned char out[3];
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1306
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1307 base64_chars = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1309 len = base64_decode_atom(base64_bit, out);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1310
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1311 if (len <= 0) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1312 errmsg = "Invalid base64 encoding";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1313 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1314 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1315
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1316 if (ret->keyblob_len + len > ret->keyblob_size) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1317 ret->keyblob_size = ret->keyblob_len + len + 256;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1318 ret->keyblob = sresize(ret->keyblob, ret->keyblob_size,
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1319 unsigned char);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1320 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1321
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1322 memcpy(ret->keyblob + ret->keyblob_len, out, len);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1323 ret->keyblob_len += len;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1324 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1325
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1326 p++;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1327 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1328 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1329 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1330
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1331 if (ret->keyblob_len == 0 || !ret->keyblob) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1332 errmsg = "Key body not present";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1333 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1334 }
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
1335
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1336 return ret;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1337
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1338 error:
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1339 if (ret) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1340 if (ret->keyblob) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1341 memset(ret->keyblob, 0, ret->keyblob_size);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1342 m_free(ret->keyblob);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1343 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1344 memset(ret, 0, sizeof(*ret));
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1345 m_free(ret);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1346 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1347 return NULL;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1348 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1349
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1350 int sshcom_encrypted(const char *filename, char **comment)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1351 {
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1352 struct sshcom_key *key = load_sshcom_key(filename);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1353 int pos, len, answer;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1354
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1355 *comment = NULL;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1356 if (!key)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1357 return 0;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1358
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1359 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1360 * Check magic number.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1361 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1362 if (GET_32BIT(key->keyblob) != 0x3f6ff9eb)
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1363 return 0; /* key is invalid */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1364
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1365 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1366 * Find the cipher-type string.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1367 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1368 answer = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1369 pos = 8;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1370 if (key->keyblob_len < pos+4)
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1371 goto done; /* key is far too short */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1372 len = toint(GET_32BIT(key->keyblob + pos));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1373 if (len < 0 || len > key->keyblob_len - pos - 4)
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1374 goto done; /* key is far too short */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1375 pos += 4 + len; /* skip key type */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1376 len = toint(GET_32BIT(key->keyblob + pos)); /* find cipher-type length */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1377 if (len < 0 || len > key->keyblob_len - pos - 4)
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1378 goto done; /* cipher type string is incomplete */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1379 if (len != 4 || 0 != memcmp(key->keyblob + pos + 4, "none", 4))
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1380 answer = 1;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1381
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1382 done:
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1383 *comment = dupstr(key->comment);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1384 memset(key->keyblob, 0, key->keyblob_size);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1385 m_free(key->keyblob);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1386 memset(key, 0, sizeof(*key));
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1387 m_free(key);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1388 return answer;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1389 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1390
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1391 static int sshcom_read_mpint(void *data, int len, struct mpint_pos *ret)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1392 {
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1393 unsigned bits, bytes;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1394 unsigned char *d = (unsigned char *) data;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1395
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1396 if (len < 4)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1397 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1398 bits = GET_32BIT(d);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1399
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1400 bytes = (bits + 7) / 8;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1401 if (len < 4+bytes)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1402 goto error;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1403
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1404 ret->start = d + 4;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1405 ret->bytes = bytes;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1406 return bytes+4;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1407
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1408 error:
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1409 ret->start = NULL;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1410 ret->bytes = -1;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1411 return len; /* ensure further calls fail as well */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1412 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1413
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1414 static int sshcom_put_mpint(void *target, void *data, int len)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1415 {
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1416 unsigned char *d = (unsigned char *)target;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1417 unsigned char *i = (unsigned char *)data;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1418 int bits = len * 8 - 1;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1419
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1420 while (bits > 0) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1421 if (*i & (1 << (bits & 7)))
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1422 break;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1423 if (!(bits-- & 7))
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1424 i++, len--;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1425 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1426
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1427 PUT_32BIT(d, bits+1);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1428 memcpy(d+4, i, len);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1429 return len+4;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1430 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1431
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1432 sign_key *sshcom_read(const char *filename, char *passphrase)
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1433 {
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1434 struct sshcom_key *key = load_sshcom_key(filename);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1435 char *errmsg;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1436 int pos, len;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1437 const char prefix_rsa[] = "if-modn{sign{rsa";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1438 const char prefix_dsa[] = "dl-modp{sign{dsa";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1439 enum { RSA, DSA } type;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1440 int encrypted;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1441 char *ciphertext;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1442 int cipherlen;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1443 struct ssh2_userkey *ret = NULL, *retkey;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1444 const struct ssh_signkey *alg;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1445 unsigned char *blob = NULL;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1446 int blobsize = 0, publen, privlen;
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
1447
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1448 if (!key)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1449 return NULL;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1450
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1451 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1452 * Check magic number.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1453 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1454 if (GET_32BIT(key->keyblob) != SSHCOM_MAGIC_NUMBER) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1455 errmsg = "Key does not begin with magic number";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1456 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1457 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1458
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1459 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1460 * Determine the key type.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1461 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1462 pos = 8;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1463 if (key->keyblob_len < pos+4 ||
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1464 (len = GET_32BIT(key->keyblob + pos)) > key->keyblob_len - pos - 4) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1465 errmsg = "Key blob does not contain a key type string";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1466 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1467 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1468 if (len > sizeof(prefix_rsa) - 1 &&
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1469 !memcmp(key->keyblob+pos+4, prefix_rsa, sizeof(prefix_rsa) - 1)) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1470 type = RSA;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1471 } else if (len > sizeof(prefix_dsa) - 1 &&
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1472 !memcmp(key->keyblob+pos+4, prefix_dsa, sizeof(prefix_dsa) - 1)) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1473 type = DSA;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1474 } else {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1475 errmsg = "Key is of unknown type";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1476 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1477 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1478 pos += 4+len;
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
1479
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1480 /*
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1481 * Determine the cipher type.
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1482 */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1483 if (key->keyblob_len < pos+4 ||
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1484 (len = GET_32BIT(key->keyblob + pos)) > key->keyblob_len - pos - 4) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1485 errmsg = "Key blob does not contain a cipher type string";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1486 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1487 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1488 if (len == 4 && !memcmp(key->keyblob+pos+4, "none", 4))
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1489 encrypted = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1490 else if (len == 8 && !memcmp(key->keyblob+pos+4, "3des-cbc", 8))
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1491 encrypted = 1;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1492 else {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1493 errmsg = "Key encryption is of unknown type";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1494 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1495 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1496 pos += 4+len;
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
1497
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1498 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1499 * Get hold of the encrypted part of the key.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1500 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1501 if (key->keyblob_len < pos+4 ||
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1502 (len = GET_32BIT(key->keyblob + pos)) > key->keyblob_len - pos - 4) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1503 errmsg = "Key blob does not contain actual key data";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1504 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1505 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1506 ciphertext = (char *)key->keyblob + pos + 4;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1507 cipherlen = len;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1508 if (cipherlen == 0) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1509 errmsg = "Length of key data is zero";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1510 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1511 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1512
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1513 /*
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1514 * Decrypt it if necessary.
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1515 */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1516 if (encrypted) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1517 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1518 * Derive encryption key from passphrase and iv/salt:
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1519 *
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1520 * - let block A equal MD5(passphrase)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1521 * - let block B equal MD5(passphrase || A)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1522 * - block C would be MD5(passphrase || A || B) and so on
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1523 * - encryption key is the first N bytes of A || B
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1524 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1525 struct MD5Context md5c;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1526 unsigned char keybuf[32], iv[8];
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1527
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1528 if (cipherlen % 8 != 0) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1529 errmsg = "Encrypted part of key is not a multiple of cipher block"
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1530 " size";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1531 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1532 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1533
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1534 MD5Init(&md5c);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1535 MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1536 MD5Final(keybuf, &md5c);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1537
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1538 MD5Init(&md5c);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1539 MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1540 MD5Update(&md5c, keybuf, 16);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1541 MD5Final(keybuf+16, &md5c);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1542
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1543 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1544 * Now decrypt the key blob.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1545 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1546 memset(iv, 0, sizeof(iv));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1547 des3_decrypt_pubkey_ossh(keybuf, iv, (unsigned char *)ciphertext,
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1548 cipherlen);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1549
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1550 memset(&md5c, 0, sizeof(md5c));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1551 memset(keybuf, 0, sizeof(keybuf));
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1552
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1553 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1554 * Hereafter we return WRONG_PASSPHRASE for any parsing
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1555 * error. (But only if we've just tried to decrypt it!
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1556 * Returning WRONG_PASSPHRASE for an unencrypted key is
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1557 * automatic doom.)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1558 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1559 if (encrypted)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1560 ret = SSH2_WRONG_PASSPHRASE;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1561 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1562
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1563 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1564 * Strip away the containing string to get to the real meat.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1565 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1566 len = toint(GET_32BIT(ciphertext));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1567 if (len < 0 || len > cipherlen-4) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1568 errmsg = "containing string was ill-formed";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1569 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1570 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1571 ciphertext += 4;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1572 cipherlen = len;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1573
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1574 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1575 * Now we break down into RSA versus DSA. In either case we'll
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1576 * construct public and private blobs in our own format, and
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1577 * end up feeding them to alg->createkey().
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1578 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1579 blobsize = cipherlen + 256;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1580 blob = snewn(blobsize, unsigned char);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1581 privlen = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1582 if (type == RSA) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1583 struct mpint_pos n, e, d, u, p, q;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1584 int pos = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1585 pos += sshcom_read_mpint(ciphertext+pos, cipherlen-pos, &e);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1586 pos += sshcom_read_mpint(ciphertext+pos, cipherlen-pos, &d);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1587 pos += sshcom_read_mpint(ciphertext+pos, cipherlen-pos, &n);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1588 pos += sshcom_read_mpint(ciphertext+pos, cipherlen-pos, &u);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1589 pos += sshcom_read_mpint(ciphertext+pos, cipherlen-pos, &p);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1590 pos += sshcom_read_mpint(ciphertext+pos, cipherlen-pos, &q);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1591 if (!q.start) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1592 errmsg = "key data did not contain six integers";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1593 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1594 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1595
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1596 alg = &ssh_rsa;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1597 pos = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1598 pos += put_string(blob+pos, "ssh-rsa", 7);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1599 pos += put_mp(blob+pos, e.start, e.bytes);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1600 pos += put_mp(blob+pos, n.start, n.bytes);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1601 publen = pos;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1602 pos += put_string(blob+pos, d.start, d.bytes);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1603 pos += put_mp(blob+pos, q.start, q.bytes);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1604 pos += put_mp(blob+pos, p.start, p.bytes);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1605 pos += put_mp(blob+pos, u.start, u.bytes);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1606 privlen = pos - publen;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1607 } else if (type == DSA) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1608 struct mpint_pos p, q, g, x, y;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1609 int pos = 4;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1610 if (GET_32BIT(ciphertext) != 0) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1611 errmsg = "predefined DSA parameters not supported";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1612 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1613 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1614 pos += sshcom_read_mpint(ciphertext+pos, cipherlen-pos, &p);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1615 pos += sshcom_read_mpint(ciphertext+pos, cipherlen-pos, &g);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1616 pos += sshcom_read_mpint(ciphertext+pos, cipherlen-pos, &q);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1617 pos += sshcom_read_mpint(ciphertext+pos, cipherlen-pos, &y);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1618 pos += sshcom_read_mpint(ciphertext+pos, cipherlen-pos, &x);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1619 if (!x.start) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1620 errmsg = "key data did not contain five integers";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1621 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1622 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1623
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1624 alg = &ssh_dss;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1625 pos = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1626 pos += put_string(blob+pos, "ssh-dss", 7);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1627 pos += put_mp(blob+pos, p.start, p.bytes);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1628 pos += put_mp(blob+pos, q.start, q.bytes);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1629 pos += put_mp(blob+pos, g.start, g.bytes);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1630 pos += put_mp(blob+pos, y.start, y.bytes);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1631 publen = pos;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1632 pos += put_mp(blob+pos, x.start, x.bytes);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1633 privlen = pos - publen;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1634 } else
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1635 return NULL;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1636
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 87
diff changeset
1637 dropbear_assert(privlen > 0); /* should have bombed by now if not */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1638
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1639 retkey = snew(struct ssh2_userkey);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1640 retkey->alg = alg;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1641 retkey->data = alg->createkey(blob, publen, blob+publen, privlen);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1642 if (!retkey->data) {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1643 m_free(retkey);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1644 errmsg = "unable to create key data structure";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1645 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1646 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1647 retkey->comment = dupstr(key->comment);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1648
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1649 errmsg = NULL; /* no error */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1650 ret = retkey;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1651
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1652 error:
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1653 if (blob) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1654 memset(blob, 0, blobsize);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1655 m_free(blob);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1656 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1657 memset(key->keyblob, 0, key->keyblob_size);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1658 m_free(key->keyblob);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1659 memset(key, 0, sizeof(*key));
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1660 m_free(key);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1661 return ret;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1662 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1663
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1664 int sshcom_write(const char *filename, sign_key *key,
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1665 char *passphrase)
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1666 {
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1667 unsigned char *pubblob, *privblob;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1668 int publen, privlen;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1669 unsigned char *outblob;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1670 int outlen;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1671 struct mpint_pos numbers[6];
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1672 int nnumbers, initial_zero, pos, lenpos, i;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1673 char *type;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1674 char *ciphertext;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1675 int cipherlen;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1676 int ret = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1677 FILE *fp;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1678
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1679 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1680 * Fetch the key blobs.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1681 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1682 pubblob = key->alg->public_blob(key->data, &publen);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1683 privblob = key->alg->private_blob(key->data, &privlen);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1684 outblob = NULL;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1685
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1686 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1687 * Find the sequence of integers to be encoded into the OpenSSH
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1688 * key blob, and also decide on the header line.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1689 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1690 if (key->alg == &ssh_rsa) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1691 int pos;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1692 struct mpint_pos n, e, d, p, q, iqmp;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1693
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1694 pos = 4 + GET_32BIT(pubblob);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1695 pos += ssh2_read_mpint(pubblob+pos, publen-pos, &e);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1696 pos += ssh2_read_mpint(pubblob+pos, publen-pos, &n);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1697 pos = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1698 pos += ssh2_read_mpint(privblob+pos, privlen-pos, &d);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1699 pos += ssh2_read_mpint(privblob+pos, privlen-pos, &p);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1700 pos += ssh2_read_mpint(privblob+pos, privlen-pos, &q);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1701 pos += ssh2_read_mpint(privblob+pos, privlen-pos, &iqmp);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1702
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 87
diff changeset
1703 dropbear_assert(e.start && iqmp.start); /* can't go wrong */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1704
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1705 numbers[0] = e;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1706 numbers[1] = d;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1707 numbers[2] = n;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1708 numbers[3] = iqmp;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1709 numbers[4] = q;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1710 numbers[5] = p;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1711
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1712 nnumbers = 6;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1713 initial_zero = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1714 type = "if-modn{sign{rsa-pkcs1-sha1},encrypt{rsa-pkcs1v2-oaep}}";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1715 } else if (key->alg == &ssh_dss) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1716 int pos;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1717 struct mpint_pos p, q, g, y, x;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1718
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1719 pos = 4 + GET_32BIT(pubblob);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1720 pos += ssh2_read_mpint(pubblob+pos, publen-pos, &p);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1721 pos += ssh2_read_mpint(pubblob+pos, publen-pos, &q);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1722 pos += ssh2_read_mpint(pubblob+pos, publen-pos, &g);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1723 pos += ssh2_read_mpint(pubblob+pos, publen-pos, &y);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1724 pos = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1725 pos += ssh2_read_mpint(privblob+pos, privlen-pos, &x);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1726
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 87
diff changeset
1727 dropbear_assert(y.start && x.start); /* can't go wrong */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1728
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1729 numbers[0] = p;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1730 numbers[1] = g;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1731 numbers[2] = q;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1732 numbers[3] = y;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1733 numbers[4] = x;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1734
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1735 nnumbers = 5;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1736 initial_zero = 1;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1737 type = "dl-modp{sign{dsa-nist-sha1},dh{plain}}";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1738 } else {
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 87
diff changeset
1739 dropbear_assert(0); /* zoinks! */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1740 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1741
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1742 /*
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1743 * Total size of key blob will be somewhere under 512 plus
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1744 * combined length of integers. We'll calculate the more
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1745 * precise size as we construct the blob.
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1746 */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1747 outlen = 512;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1748 for (i = 0; i < nnumbers; i++)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1749 outlen += 4 + numbers[i].bytes;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1750 outblob = snewn(outlen, unsigned char);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1751
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1752 /*
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1753 * Create the unencrypted key blob.
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1754 */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1755 pos = 0;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1756 PUT_32BIT(outblob+pos, SSHCOM_MAGIC_NUMBER); pos += 4;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1757 pos += 4; /* length field, fill in later */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1758 pos += put_string(outblob+pos, type, strlen(type));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1759 {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1760 char *ciphertype = passphrase ? "3des-cbc" : "none";
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1761 pos += put_string(outblob+pos, ciphertype, strlen(ciphertype));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1762 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1763 lenpos = pos; /* remember this position */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1764 pos += 4; /* encrypted-blob size */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1765 pos += 4; /* encrypted-payload size */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1766 if (initial_zero) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1767 PUT_32BIT(outblob+pos, 0);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1768 pos += 4;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1769 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1770 for (i = 0; i < nnumbers; i++)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1771 pos += sshcom_put_mpint(outblob+pos,
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1772 numbers[i].start, numbers[i].bytes);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1773 /* Now wrap up the encrypted payload. */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1774 PUT_32BIT(outblob+lenpos+4, pos - (lenpos+8));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1775 /* Pad encrypted blob to a multiple of cipher block size. */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1776 if (passphrase) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1777 int padding = -(pos - (lenpos+4)) & 7;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1778 while (padding--)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1779 outblob[pos++] = random_byte();
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1780 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1781 ciphertext = (char *)outblob+lenpos+4;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1782 cipherlen = pos - (lenpos+4);
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 87
diff changeset
1783 dropbear_assert(!passphrase || cipherlen % 8 == 0);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1784 /* Wrap up the encrypted blob string. */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1785 PUT_32BIT(outblob+lenpos, cipherlen);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1786 /* And finally fill in the total length field. */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1787 PUT_32BIT(outblob+4, pos);
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
1788
241
c5d3ef11155f * use own assertions which should get logged properly
Matt Johnston <matt@ucc.asn.au>
parents: 87
diff changeset
1789 dropbear_assert(pos < outlen);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1790
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1791 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1792 * Encrypt the key.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1793 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1794 if (passphrase) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1795 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1796 * Derive encryption key from passphrase and iv/salt:
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1797 *
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1798 * - let block A equal MD5(passphrase)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1799 * - let block B equal MD5(passphrase || A)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1800 * - block C would be MD5(passphrase || A || B) and so on
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1801 * - encryption key is the first N bytes of A || B
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1802 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1803 struct MD5Context md5c;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1804 unsigned char keybuf[32], iv[8];
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1805
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1806 MD5Init(&md5c);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1807 MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1808 MD5Final(keybuf, &md5c);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1809
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1810 MD5Init(&md5c);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1811 MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1812 MD5Update(&md5c, keybuf, 16);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1813 MD5Final(keybuf+16, &md5c);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1814
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1815 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1816 * Now decrypt the key blob.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1817 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1818 memset(iv, 0, sizeof(iv));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1819 des3_encrypt_pubkey_ossh(keybuf, iv, (unsigned char *)ciphertext,
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1820 cipherlen);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1821
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1822 memset(&md5c, 0, sizeof(md5c));
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1823 memset(keybuf, 0, sizeof(keybuf));
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1824 }
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
1825
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1826 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1827 * And save it. We'll use Unix line endings just in case it's
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1828 * subsequently transferred in binary mode.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1829 */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1830 fp = fopen(filename, "wb"); /* ensure Unix line endings */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1831 if (!fp)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1832 goto error;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1833 fputs("---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----\n", fp);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1834 fprintf(fp, "Comment: \"");
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1835 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1836 * Comment header is broken with backslash-newline if it goes
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1837 * over 70 chars. Although it's surrounded by quotes, it
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1838 * _doesn't_ escape backslashes or quotes within the string.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1839 * Don't ask me, I didn't design it.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1840 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1841 {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1842 int slen = 60; /* starts at 60 due to "Comment: " */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1843 char *c = key->comment;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1844 while ((int)strlen(c) > slen) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1845 fprintf(fp, "%.*s\\\n", slen, c);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1846 c += slen;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1847 slen = 70; /* allow 70 chars on subsequent lines */
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1848 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1849 fprintf(fp, "%s\"\n", c);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1850 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1851 base64_encode_fp(fp, outblob, pos, 70);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1852 fputs("---- END SSH2 ENCRYPTED PRIVATE KEY ----\n", fp);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1853 fclose(fp);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1854 ret = 1;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1855
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1856 error:
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1857 if (outblob) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1858 memset(outblob, 0, outlen);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1859 m_free(outblob);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1860 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1861 if (privblob) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1862 memset(privblob, 0, privlen);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1863 m_free(privblob);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1864 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1865 if (pubblob) {
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1866 memset(pubblob, 0, publen);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1867 m_free(pubblob);
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1868 }
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1869 return ret;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1870 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1871 #endif /* ssh.com stuff disabled */
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
1872
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
1873 /* From PuTTY misc.c */
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
1874 static int toint(unsigned u)
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
1875 {
1308
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1876 /*
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1877 * Convert an unsigned to an int, without running into the
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1878 * undefined behaviour which happens by the strict C standard if
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1879 * the value overflows. You'd hope that sensible compilers would
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1880 * do the sensible thing in response to a cast, but actually I
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1881 * don't trust modern compilers not to do silly things like
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1882 * assuming that _obviously_ you wouldn't have caused an overflow
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1883 * and so they can elide an 'if (i < 0)' test immediately after
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1884 * the cast.
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1885 *
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1886 * Sensible compilers ought of course to optimise this entire
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1887 * function into 'just return the input value'!
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1888 */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1889 if (u <= (unsigned)INT_MAX)
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1890 return (int)u;
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1891 else if (u >= (unsigned)INT_MIN) /* wrap in cast _to_ unsigned is OK */
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1892 return INT_MIN + (int)(u - (unsigned)INT_MIN);
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1893 else
8678e2cc1e53 make indenting consistent
Matt Johnston <matt@ucc.asn.au>
parents: 1307
diff changeset
1894 return INT_MIN; /* fallback; should never occur on binary machines */
1306
34e6127ef02e merge fixes from PuTTY import.c
Matt Johnston <matt@ucc.asn.au>
parents: 1250
diff changeset
1895 }