This repository was archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathRSASSHKeyPair.java
More file actions
268 lines (234 loc) · 10.4 KB
/
Copy pathRSASSHKeyPair.java
File metadata and controls
268 lines (234 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package co.krypt.krypton.crypto;
import android.security.keystore.KeyInfo;
import android.security.keystore.KeyProperties;
import android.support.annotation.NonNull;
import android.support.v4.util.Pair;
import android.util.Base64;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Signature;
import java.security.SignatureException;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import co.krypt.krypton.exception.CryptoException;
import co.krypt.krypton.pgp.PGPException;
import co.krypt.krypton.pgp.packet.HashAlgorithm;
import co.krypt.krypton.pgp.packet.MPInt;
import co.krypt.krypton.pgp.packet.RSASignature;
import co.krypt.krypton.pgp.packet.UnsupportedHashAlgorithmException;
import co.krypt.krypton.pgp.publickey.PublicKeyAlgorithm;
import co.krypt.krypton.pgp.publickey.PublicKeyData;
import co.krypt.krypton.pgp.publickey.PublicKeyPacketAttributes;
import co.krypt.krypton.pgp.publickey.RSAPublicKeyData;
/**
* Created by Kevin King on 11/30/16.
* Copyright 2016. KryptCo, Inc.
*/
public class RSASSHKeyPair implements SSHKeyPairI {
private static final String TAG = "RSASSHKeyPair";
// Prevent too many concurrent Keystore operations from happening at one time
// (hard limit is 15 https://android.googlesource.com/platform/system/security/+/1f76969bd8b6179f256dafb938bb458bc997c23d%5E!/ )
private static ExecutorService threadPool = Executors.newFixedThreadPool(4);
private final @NonNull KeyPair keyPair;
// PGP public key attribute
public final long created;
RSASSHKeyPair(@NonNull KeyPair keyPair, long created) {
this.keyPair = keyPair;
this.created = created;
}
public String publicKeyDERBase64() {
return Base64.encodeToString(keyPair.getPublic().getEncoded(), Base64.DEFAULT);
}
public byte[] publicKeySSHWireFormat() throws InvalidKeyException, IOException {
if (!(keyPair.getPublic() instanceof RSAPublicKey)) {
throw new InvalidKeyException("Only RSA Supported");
}
RSAPublicKey rsaPub = (RSAPublicKey) keyPair.getPublic();
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(SSHWire.encode("ssh-rsa".getBytes()));
out.write(SSHWire.encode(rsaPub.getPublicExponent().toByteArray()));
out.write(SSHWire.encode(rsaPub.getModulus().toByteArray()));
return out.toByteArray();
}
public byte[] publicKeyFingerprint() throws CryptoException {
try {
return SHA256.digest(publicKeySSHWireFormat());
} catch (InvalidKeyException | IOException e) {
e.printStackTrace();
throw new CryptoException(e);
}
}
public byte[] signDigest(String digest, byte[] data) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, CryptoException, NoSuchProviderException, InvalidKeySpecException {
try {
return threadPool.submit(() -> signDigestJob(digest, data)).get();
} catch (InterruptedException | ExecutionException e) {
throw new CryptoException(e);
}
}
private byte[] signDigestJob(String digest, byte[] data) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, CryptoException, NoSuchProviderException, InvalidKeySpecException {
long start = System.currentTimeMillis();
byte[] signature;
Pair<Signature, byte[]> signerAndData = getSignerAndPrepareData(digest, data);
Signature signer = signerAndData.first;
data = signerAndData.second;
signer.initSign(keyPair.getPrivate());
signer.update(data);
signature = signer.sign();
long stop = System.currentTimeMillis();
Log.d(TAG, "signature took " + String.valueOf((stop - start) / 1000.0) + " seconds");
return signature;
}
public byte[] signDigestAppendingPubkey(byte[] data, String algo) throws CryptoException {
try {
ByteArrayOutputStream dataWithPubkey = new ByteArrayOutputStream();
dataWithPubkey.write(data);
dataWithPubkey.write(SSHWire.encode(publicKeySSHWireFormat()));
byte[] signaturePayload = dataWithPubkey.toByteArray();
String digest = getDigestForAlgo(algo);
return signDigest(digest, signaturePayload);
} catch (IOException | NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException | SignatureException | InvalidKeySpecException e) {
e.printStackTrace();
throw new CryptoException(e);
}
}
private String getDigestForAlgo(String algo) throws CryptoException {
switch (algo) {
case "ssh-rsa":
return KeyProperties.DIGEST_SHA1;
case "rsa-sha2-256":
return KeyProperties.DIGEST_SHA256;
case "rsa-sha2-512":
return KeyProperties.DIGEST_SHA512;
default:
throw new CryptoException("unsupported algo: " + algo);
}
}
private String getDigestForPGPHashAlgorithm(HashAlgorithm hash) throws UnsupportedHashAlgorithmException {
switch (hash) {
case MD5:
throw new UnsupportedHashAlgorithmException();
case RIPE_MD160:
throw new UnsupportedHashAlgorithmException();
case SHA1:
return KeyProperties.DIGEST_SHA1;
case SHA256:
return KeyProperties.DIGEST_SHA256;
case SHA384:
throw new UnsupportedHashAlgorithmException();
case SHA512:
return KeyProperties.DIGEST_SHA512;
case SHA224:
throw new UnsupportedHashAlgorithmException();
}
throw new UnsupportedHashAlgorithmException();
}
public Pair<Signature, byte[]> getSignerAndPrepareData(String digest, byte[] data) throws CryptoException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
KeyFactory factory = KeyFactory.getInstance(keyPair.getPrivate().getAlgorithm(), "AndroidKeyStore");
KeyInfo keyInfo;
keyInfo = factory.getKeySpec(keyPair.getPrivate(), KeyInfo.class);
Signature signer;
if (Arrays.asList(keyInfo.getDigests()).contains(digest)) {
switch (digest) {
case KeyProperties.DIGEST_SHA1:
signer = Signature.getInstance("SHA1withRSA");
break;
case KeyProperties.DIGEST_SHA256:
signer = Signature.getInstance("SHA256withRSA");
break;
case KeyProperties.DIGEST_SHA512:
signer = Signature.getInstance("SHA512withRSA");
break;
default:
throw new CryptoException("Unsupported digest: " + digest);
}
} else {
// fall back to NONEwithRSA for backwards compatibility
signer = Signature.getInstance("NONEwithRSA");
switch (digest) {
case KeyProperties.DIGEST_SHA1:
data = SHA1.digestPrependingOID(data);
break;
case KeyProperties.DIGEST_SHA256:
data = SHA256.digestPrependingOID(data);
break;
case KeyProperties.DIGEST_SHA512:
data = SHA512.digestPrependingOID(data);
break;
default:
throw new CryptoException("Unsupported digest: " + digest);
}
}
return new Pair<>(signer, data);
}
public boolean verifyDigest(String digest, byte[] signature, byte[] data) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, CryptoException, InvalidKeySpecException, NoSuchProviderException {
Pair<Signature, byte[]> signerAndData = getSignerAndPrepareData(digest, data);
Signature s = signerAndData.first;
data = signerAndData.second;
s.initVerify(keyPair.getPublic());
s.update(data);
return s.verify(signature);
}
@Override
public PublicKeyData pgpPublicKeyData() {
RSAPublicKey rsaPub = (RSAPublicKey) keyPair.getPublic();
byte[] n = rsaPub.getModulus().toByteArray();
byte[] e = rsaPub.getPublicExponent().toByteArray();
return new RSAPublicKeyData(
new MPInt(
n
),
new MPInt(
e
)
);
}
@Override
public PublicKeyPacketAttributes pgpPublicKeyPacketAttributes() {
return new PublicKeyPacketAttributes(
created,
PublicKeyAlgorithm.RSA_SIGN_ONLY
);
}
@Override
public co.krypt.krypton.pgp.packet.Signature pgpSign(HashAlgorithm hash, byte[] data) throws PGPException, NoSuchAlgorithmException, CryptoException, SignatureException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {
byte[] signatureBytes = signDigest(getDigestForPGPHashAlgorithm(hash), data);
return new RSASignature(
new MPInt(
signatureBytes
)
);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RSASSHKeyPair that = (RSASSHKeyPair) o;
return publicKeyDERBase64().equals(that.publicKeyDERBase64());
}
public boolean isKeyStoredInSecureHardware() {
try {
KeyInfo keyInfo;
KeyFactory factory = KeyFactory.getInstance(keyPair.getPrivate().getAlgorithm(), "AndroidKeyStore");
keyInfo = factory.getKeySpec(keyPair.getPrivate(), KeyInfo.class);
return keyInfo.isInsideSecureHardware();
} catch (InvalidKeySpecException e) {
// Not an Android KeyStore key.
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
return false;
}
}