use of io.churchkey.util.Pem in project churchkey by tomitribe.
the class OpenSSHPrivateKey method decode.
public static Key decode(final byte[] bytes) {
final Pem pem = Pem.parse(bytes);
try {
final KeyInput keyInput = new KeyInput(pem.getData());
assertString("Auth Magic", "openssh-key-v1", keyInput.readAuthMagic());
assertString("ciphername", "none", keyInput.readString());
assertString("kdfname", "none", keyInput.readString());
assertString("kdf", "", keyInput.readString());
assertInt("number of keys", 1, keyInput.readInt());
// Ignore the public key, it is repeated in the private key
final byte[] sshpublic = keyInput.readBytes();
keyInput.readInt();
// a random 32-bit int, repeated
keyInput.readInt();
keyInput.readInt();
final String keyType = keyInput.readString();
if ("ssh-rsa".equals(keyType)) {
return readRsaPrivateKey(keyInput);
}
if ("ssh-dss".equals(keyType)) {
return readPrivateDssKey(keyInput);
}
if ("ecdsa-sha2-nistp256".equals(keyType)) {
return readEcdsaPrivateKey(Curve.nistp256, keyInput);
}
if ("ecdsa-sha2-nistp384".equals(keyType)) {
return readEcdsaPrivateKey(Curve.nistp384, keyInput);
}
if ("ecdsa-sha2-nistp521".equals(keyType)) {
return readEcdsaPrivateKey(Curve.nistp521, keyInput);
}
throw new UnsupportedOperationException("Unsupported key type: " + keyType);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
} catch (IOException | InvalidKeySpecException e) {
throw new RuntimeException(e);
}
}
use of io.churchkey.util.Pem in project churchkey by tomitribe.
the class BeginPublicKey method encode.
public static byte[] encode(final Key key) {
final Pem.Builder pem = Pem.builder().type("PUBLIC KEY").wrap(64);
final java.security.Key value = key.getKey();
try {
if (value instanceof RSAPublicKey) {
final KeyFactory factory = KeyFactory.getInstance("RSA");
final X509EncodedKeySpec keySpec = factory.getKeySpec(value, X509EncodedKeySpec.class);
return pem.data(keySpec.getEncoded()).format().getBytes();
}
if (value instanceof DSAPublicKey) {
final KeyFactory factory = KeyFactory.getInstance("DSA");
final X509EncodedKeySpec keySpec = factory.getKeySpec(value, X509EncodedKeySpec.class);
return pem.data(keySpec.getEncoded()).format().getBytes();
}
if (value instanceof ECPublicKey) {
final KeyFactory factory = KeyFactory.getInstance("EC");
final X509EncodedKeySpec keySpec = factory.getKeySpec(value, X509EncodedKeySpec.class);
return pem.data(keySpec.getEncoded()).format().getBytes();
}
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("Unsupported key algorithm", e);
} catch (InvalidKeySpecException e) {
throw new IllegalStateException("Invalid Key Spec", e);
}
throw new UnsupportedOperationException("Unsupported key algorithm " + key.getAlgorithm());
}
use of io.churchkey.util.Pem in project churchkey by tomitribe.
the class FooTest method test3.
@Ignore
@Test
public void test3() throws Exception {
final Resource resource = Resource.resource(BeginPrivateKeyTest.class.getSimpleName());
final byte[] bytes = resource.bytes("openssl-rsaprivatekey-3072.pem");
final Pem pem = Pem.parse(bytes);
{
final DerParser d1 = new DerParser(pem.getData());
final Asn1Object d1o1 = d1.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d2 = new DerParser(d1o1.getValue());
final Asn1Object d2o1 = d2.readObject().assertType(Asn1Type.INTEGER);
final Asn1Object d2o2 = d2.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d3 = new DerParser(d2o2.getValue());
final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.OBJECT_IDENTIFIER);
final Asn1Object d3o2 = d3.readObject().assertType(Asn1Type.NULL);
}
final Asn1Object d2o3 = d2.readObject().assertType(OCTET_STRING);
{
final DerParser d3 = new DerParser(d2o3.getValue());
final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.SEQUENCE);
{
final DerParser d4 = new DerParser(d3o1.getValue());
final BigInteger version = d4.readBigInteger();
final RSAPrivateCrtKey privateKey = Rsa.Private.builder().modulus(d4.readBigInteger()).publicExponent(d4.readBigInteger()).privateExponent(d4.readBigInteger()).primeP(d4.readBigInteger()).primeQ(d4.readBigInteger()).primeExponentP(d4.readBigInteger()).primeExponentQ(d4.readBigInteger()).crtCoefficient(d4.readBigInteger()).build().toKey();
final Key key1 = new Key(privateKey, Key.Type.PRIVATE, RSA, Key.Format.PEM);
System.out.println(key1);
}
}
}
}
}
use of io.churchkey.util.Pem in project churchkey by tomitribe.
the class BeginEcParameters method decode.
public static Object decode(final byte[] bytes) throws IOException {
if (!Utils.startsWith("-----BEGIN EC PARAMETERS-----", bytes)) {
throw new IllegalArgumentException("Contents do not start with -----BEGIN EC PARAMETERS-----");
}
final Pem pem = Pem.parse(bytes);
final byte[] data = pem.getData();
final Asn1Type type = new DerParser(data).readObject().getType();
if (type == Asn1Type.SEQUENCE) {
return EcCurveParams.parse(data);
}
if (type == Asn1Type.OBJECT_IDENTIFIER) {
return EcCurveParams.parseOid(data);
}
throw new UnsupportedOperationException("Unexpected ASN1 type: " + type);
}
use of io.churchkey.util.Pem in project churchkey by tomitribe.
the class Asn1Dump method dump.
public static String dump(final byte[] bytes) throws IOException {
final File der = File.createTempFile("der", ".dump");
if (bytes[0] == '-' && bytes[1] == '-') {
final Pem pem = Pem.parse(bytes);
IO.copy(pem.getData(), der);
} else {
IO.copy(bytes, der);
}
final PrintString err = new PrintString();
final PrintString out = new PrintString();
final ProcessBuilder builder = new ProcessBuilder("openssl", "asn1parse", "-i", "-inform", "DER", "-in", der.getAbsolutePath(), "-dump");
final Process process = builder.start();
final Future<Pipe> o = Pipe.pipe(process.getInputStream(), out);
final Future<Pipe> e = Pipe.pipe(process.getErrorStream(), err);
try {
final int i = process.waitFor();
o.get();
e.get();
if (i != 0) {
throw new IllegalStateException("Exit code " + i + "\n" + err);
}
} catch (InterruptedException | ExecutionException exception) {
throw new IllegalStateException(exception);
} finally {
out.close();
err.close();
der.delete();
}
return out.toString();
}
Aggregations