remove dependency on bcpkix-jdk15on

This commit is contained in:
Adrian Cole 2012-12-27 23:23:28 -08:00
parent ba156b7552
commit 207b3db41f
4 changed files with 30 additions and 22 deletions

View File

@ -51,7 +51,7 @@
<!-- Required for Pems.java to read and write public and private keys --> <!-- Required for Pems.java to read and write public and private keys -->
<dependency> <dependency>
<groupId>org.bouncycastle</groupId> <groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId> <artifactId>bcprov-jdk15on</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.ws.rs</groupId> <groupId>javax.ws.rs</groupId>

View File

@ -18,18 +18,24 @@
*/ */
package org.jclouds.crypto; package org.jclouds.crypto;
import static com.google.common.base.Joiner.on;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Splitter.fixedLength;
import static com.google.common.base.Throwables.propagate; import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.get;
import static com.google.common.collect.Iterables.size;
import static com.google.common.io.BaseEncoding.base64;
import static org.jclouds.crypto.CryptoStreams.base64; import static org.jclouds.crypto.CryptoStreams.base64;
import static org.jclouds.crypto.CryptoStreams.hex; import static org.jclouds.crypto.CryptoStreams.hex;
import static org.jclouds.crypto.CryptoStreams.md5; import static org.jclouds.crypto.CryptoStreams.md5;
import static org.jclouds.crypto.Pems.privateKeySpec; import static org.jclouds.crypto.Pems.privateKeySpec;
import static org.jclouds.util.Strings2.toStringAndClose;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringWriter;
import java.math.BigInteger; import java.math.BigInteger;
import java.security.KeyFactory; import java.security.KeyFactory;
import java.security.KeyPair; import java.security.KeyPair;
@ -44,16 +50,15 @@ import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec; import java.security.spec.RSAPublicKeySpec;
import java.util.Map; import java.util.Map;
import org.bouncycastle.openssl.PEMWriter; import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.jclouds.io.InputSuppliers; import org.jclouds.io.InputSuppliers;
import org.jclouds.util.Strings2;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Iterables;
import com.google.common.io.InputSupplier; import com.google.common.io.InputSupplier;
/** /**
@ -96,10 +101,10 @@ public class SshKeys {
public static RSAPublicKeySpec publicKeySpecFromOpenSSH(InputSupplier<? extends InputStream> supplier) public static RSAPublicKeySpec publicKeySpecFromOpenSSH(InputSupplier<? extends InputStream> supplier)
throws IOException { throws IOException {
InputStream stream = supplier.getInput(); InputStream stream = supplier.getInput();
Iterable<String> parts = Splitter.on(' ').split(Strings2.toStringAndClose(stream)); Iterable<String> parts = Splitter.on(' ').split(toStringAndClose(stream).trim());
checkArgument(Iterables.size(parts) >= 2 && "ssh-rsa".equals(Iterables.get(parts, 0)), checkArgument(size(parts) >= 2 && "ssh-rsa".equals(get(parts, 0)),
"bad format, should be: ssh-rsa AAAAB3..."); "bad format, should be: ssh-rsa AAAAB3...");
stream = new ByteArrayInputStream(CryptoStreams.base64(Iterables.get(parts, 1))); stream = new ByteArrayInputStream(base64().decode(get(parts, 1)));
String marker = new String(readLengthFirst(stream)); String marker = new String(readLengthFirst(stream));
checkArgument("ssh-rsa".equals(marker), "looking for marker ssh-rsa but got %s", marker); checkArgument("ssh-rsa".equals(marker), "looking for marker ssh-rsa but got %s", marker);
BigInteger publicExponent = new BigInteger(readLengthFirst(stream)); BigInteger publicExponent = new BigInteger(readLengthFirst(stream));
@ -158,18 +163,22 @@ public class SshKeys {
} }
public static String encodeAsPem(RSAPrivateKey key) { public static String encodeAsPem(RSAPrivateKey key) {
StringWriter stringWriter = new StringWriter(); String type = "RSA PRIVATE KEY";
PEMWriter pemFormatWriter = new PEMWriter(stringWriter); byte[] encoded = asn1Encode(checkNotNull(key, type));
StringBuilder builder = new StringBuilder();
builder.append("-----BEGIN ").append(type).append("-----").append('\n');
builder.append(on('\n').join(fixedLength(64).split(base64().encode(encoded)))).append('\n');
builder.append("-----END ").append(type).append("-----").append('\n');
return builder.toString();
}
private static byte[] asn1Encode(RSAPrivateKey key) {
try { try {
pemFormatWriter.writeObject(key); PrivateKeyInfo info = new PrivateKeyInfo((ASN1Sequence) ASN1Primitive.fromByteArray(key.getEncoded()));
pemFormatWriter.close(); return info.parsePrivateKey().toASN1Primitive().getEncoded();
} catch (IOException e) { } catch (IOException e) {
throw propagate(e); throw propagate(e);
} }
return stringWriter.toString();
// TODO: understand why pem isn't passing testCanGenerate where keys are
// checked to match.
// return pem(key.getEncoded(), PRIVATE_PKCS1_MARKER, 64);
} }
/** /**
@ -283,8 +292,7 @@ public class SshKeys {
*/ */
public static String sha1(RSAPrivateCrtKeySpec privateKey) { public static String sha1(RSAPrivateCrtKeySpec privateKey) {
try { try {
String sha1 = Joiner.on(":").join( String sha1 = on(':').join(fixedLength(2).split(
Splitter.fixedLength(2).split(
hex(CryptoStreams.sha1(KeyFactory.getInstance("RSA").generatePrivate(privateKey) hex(CryptoStreams.sha1(KeyFactory.getInstance("RSA").generatePrivate(privateKey)
.getEncoded())))); .getEncoded()))));
return sha1; return sha1;
@ -324,7 +332,7 @@ public class SshKeys {
*/ */
public static String fingerprint(BigInteger publicExponent, BigInteger modulus) { public static String fingerprint(BigInteger publicExponent, BigInteger modulus) {
byte[] keyBlob = keyBlob(publicExponent, modulus); byte[] keyBlob = keyBlob(publicExponent, modulus);
return Joiner.on(":").join(Splitter.fixedLength(2).split(hex(md5(keyBlob)))); return on(':').join(fixedLength(2).split(hex(md5(keyBlob))));
} }
public static byte[] keyBlob(BigInteger publicExponent, BigInteger modulus) { public static byte[] keyBlob(BigInteger publicExponent, BigInteger modulus) {

View File

@ -63,7 +63,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.bouncycastle</groupId> <groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId> <artifactId>bcprov-jdk15on</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -206,7 +206,7 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.bouncycastle</groupId> <groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId> <artifactId>bcprov-jdk15on</artifactId>
<version>1.47</version> <version>1.47</version>
</dependency> </dependency>
<dependency> <dependency>