1 | /******************************************************************************
|
---|
2 | *
|
---|
3 | * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
|
---|
4 | * www.mindbright.se, [email protected]
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | *****************************************************************************
|
---|
17 | * $Author: mats $
|
---|
18 | * $Date: 1999/02/23 07:05:11 $
|
---|
19 | * $Name: rel1-2-1 $
|
---|
20 | *****************************************************************************/
|
---|
21 | package mindbright.ssh;
|
---|
22 |
|
---|
23 | import java.io.*;
|
---|
24 | import java.math.BigInteger;
|
---|
25 |
|
---|
26 | import mindbright.security.*;
|
---|
27 |
|
---|
28 | public class SSHRSAKeyFile {
|
---|
29 |
|
---|
30 | //
|
---|
31 | //
|
---|
32 | int cipherType;
|
---|
33 | RSAPublicKey pubKey;
|
---|
34 | String fileComment;
|
---|
35 |
|
---|
36 | byte[] encrypted;
|
---|
37 |
|
---|
38 | final static String privFileId = "SSH PRIVATE KEY FILE FORMAT 1.1\n";
|
---|
39 |
|
---|
40 | static public void createKeyFile(KeyPair kp, String passwd, String name, String comment)
|
---|
41 | throws IOException {
|
---|
42 | RSAPrivateKey privKey = (RSAPrivateKey)kp.getPrivate();
|
---|
43 |
|
---|
44 | ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
|
---|
45 | SSHDataOutputStream dataOut = new SSHDataOutputStream(baos);
|
---|
46 |
|
---|
47 | byte[] c = new byte[2];
|
---|
48 | SSH.secureRandom().nextBytes(c);
|
---|
49 | dataOut.writeByte((int)c[0]);
|
---|
50 | dataOut.writeByte((int)c[1]);
|
---|
51 | dataOut.writeByte((int)c[0]);
|
---|
52 | dataOut.writeByte((int)c[1]);
|
---|
53 | dataOut.writeBigInteger(privKey.getD());
|
---|
54 | dataOut.writeBigInteger(privKey.getU());
|
---|
55 | dataOut.writeBigInteger(privKey.getP());
|
---|
56 | dataOut.writeBigInteger(privKey.getQ());
|
---|
57 |
|
---|
58 | byte[] encrypted = baos.toByteArray();
|
---|
59 | c = new byte[(8 - (encrypted.length % 8)) + encrypted.length];
|
---|
60 | System.arraycopy(encrypted, 0, c, 0, encrypted.length);
|
---|
61 | encrypted = c;
|
---|
62 |
|
---|
63 | int cipherType = SSH.CIPHER_DEFAULT;
|
---|
64 |
|
---|
65 | Cipher cipher = Cipher.getInstance(SSH.cipherClasses[cipherType][0]);
|
---|
66 | cipher.setKey(passwd);
|
---|
67 | encrypted = cipher.encrypt(encrypted);
|
---|
68 |
|
---|
69 | FileOutputStream fileOut = new FileOutputStream(name);
|
---|
70 | dataOut = new SSHDataOutputStream(fileOut);
|
---|
71 |
|
---|
72 | dataOut.writeBytes(privFileId);
|
---|
73 | dataOut.writeByte(0);
|
---|
74 |
|
---|
75 | dataOut.writeByte(cipherType);
|
---|
76 | dataOut.writeInt(0);
|
---|
77 | dataOut.writeInt(0);
|
---|
78 | dataOut.writeBigInteger(((RSAPublicKey)kp.getPublic()).getN());
|
---|
79 | dataOut.writeBigInteger(((RSAPublicKey)kp.getPublic()).getE());
|
---|
80 | dataOut.writeString(comment);
|
---|
81 |
|
---|
82 | dataOut.write(encrypted, 0, encrypted.length);
|
---|
83 | dataOut.close();
|
---|
84 | }
|
---|
85 |
|
---|
86 | public SSHRSAKeyFile(String name) throws IOException {
|
---|
87 | FileInputStream fileIn = new FileInputStream(name);
|
---|
88 | SSHDataInputStream dataIn = new SSHDataInputStream(fileIn);
|
---|
89 |
|
---|
90 | byte[] id = new byte[privFileId.length()];
|
---|
91 | dataIn.readFully(id);
|
---|
92 | String idStr = new String(id);
|
---|
93 | dataIn.readByte(); // Skip end-of-string (?!)
|
---|
94 |
|
---|
95 | if(!idStr.equals(privFileId))
|
---|
96 | throw new IOException("RSA key file corrupt");
|
---|
97 |
|
---|
98 | cipherType = dataIn.readByte();
|
---|
99 | if(SSH.cipherClasses[cipherType][0] == null)
|
---|
100 | throw new IOException("Ciphertype " + cipherType + " in key-file not supported");
|
---|
101 |
|
---|
102 | dataIn.readInt(); // Skip a reserved int
|
---|
103 |
|
---|
104 | dataIn.readInt(); // Skip bits... (!?)
|
---|
105 |
|
---|
106 | BigInteger n = dataIn.readBigInteger();
|
---|
107 | BigInteger e = dataIn.readBigInteger();
|
---|
108 | pubKey = new RSAPublicKey(e, n);
|
---|
109 |
|
---|
110 | fileComment = dataIn.readString();
|
---|
111 |
|
---|
112 | byte[] rest = new byte[8192];
|
---|
113 | int len = dataIn.read(rest);
|
---|
114 | dataIn.close();
|
---|
115 |
|
---|
116 | encrypted = new byte[len];
|
---|
117 | System.arraycopy(rest, 0, encrypted, 0, len);
|
---|
118 | }
|
---|
119 |
|
---|
120 | public String getComment() {
|
---|
121 | return fileComment;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public RSAPublicKey getPublic() {
|
---|
125 | return pubKey;
|
---|
126 | }
|
---|
127 |
|
---|
128 | public RSAPrivateKey getPrivate(String passwd) {
|
---|
129 | RSAPrivateKey privKey = null;
|
---|
130 |
|
---|
131 | Cipher cipher = Cipher.getInstance(SSH.cipherClasses[cipherType][0]);
|
---|
132 | cipher.setKey(passwd);
|
---|
133 | byte[] decrypted = cipher.decrypt(encrypted);
|
---|
134 | SSHDataInputStream dataIn = new SSHDataInputStream(new ByteArrayInputStream(decrypted));
|
---|
135 |
|
---|
136 | try {
|
---|
137 | byte c1 = dataIn.readByte();
|
---|
138 | byte c2 = dataIn.readByte();
|
---|
139 | byte c11 = dataIn.readByte();
|
---|
140 | byte c22 = dataIn.readByte();
|
---|
141 |
|
---|
142 | if(c1 != c11 || c2 != c22)
|
---|
143 | return null;
|
---|
144 |
|
---|
145 | BigInteger d = dataIn.readBigInteger();
|
---|
146 | BigInteger u = dataIn.readBigInteger();
|
---|
147 | BigInteger p = dataIn.readBigInteger();
|
---|
148 | BigInteger q = dataIn.readBigInteger();
|
---|
149 | dataIn.close();
|
---|
150 |
|
---|
151 | privKey = new RSAPrivateKey(pubKey.getE(), pubKey.getN(),
|
---|
152 | d, u, p, q);
|
---|
153 | } catch (IOException e) {
|
---|
154 | privKey = null;
|
---|
155 | }
|
---|
156 |
|
---|
157 | return privKey;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /* !!! DEBUG
|
---|
161 | public static void main(String[] argv) {
|
---|
162 | SSHRSAKeyFile file = null;
|
---|
163 |
|
---|
164 | try {
|
---|
165 | file = new SSHRSAKeyFile("/home/mats/.ssh/identity");
|
---|
166 | file.getPrivate("********");
|
---|
167 | } catch (Exception e) {
|
---|
168 | System.out.println("Error: " + e.toString());
|
---|
169 | }
|
---|
170 | System.out.println("Comment: " + file.fileComment);
|
---|
171 | }
|
---|
172 | */
|
---|
173 |
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|