use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class NetscapeKeyStore method engineGetKey.
public Key engineGetKey(String alias, char[] password) throws NoSuchAlgorithmException, UnrecoverableKeyException {
KeyEntry keyEntry = getKeyEntry(alias);
if (!passwordCheck(password)) {
throw new UnrecoverableKeyException("Invalid password");
}
if (keyEntry != null) {
try {
EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo();
ASN1DER ber = new ASN1DER();
ByteArrayInputStream ba = new ByteArrayInputStream(keyEntry.encryptedKey);
ber.decode(ba, epki);
byte[] enc = epki.encryptedData.getRaw();
byte[] dec = new byte[enc.length];
do3DESCipher(Cipher.DECRYPT_MODE, password, enc, 0, enc.length, dec, globalSalt(), keyEntry.salt);
ba = new ByteArrayInputStream(dec);
return PKCS12KeyStore.extractPrivateKey(dec);
} catch (IOException e) {
throw new UnrecoverableKeyException(e.getMessage());
}
}
return null;
}
use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class SSH2KeyPairFile method writeKeyPair.
public static byte[] writeKeyPair(ASCIIArmour armour, String password, SecureRandom random, KeyPair keyPair) throws SSH2FatalException {
ASN1Object pem;
PublicKey publicKey = keyPair.getPublic();
int headType;
if (publicKey instanceof DSAPublicKey) {
DSAPublicKey pubKey = (DSAPublicKey) keyPair.getPublic();
DSAPrivateKey prvKey = (DSAPrivateKey) keyPair.getPrivate();
DSAParams params = pubKey.getParams();
pem = new PEMDSAPrivate(0, params.getP(), params.getQ(), params.getG(), pubKey.getY(), prvKey.getX());
headType = TYPE_PEM_DSA;
} else if (publicKey instanceof RSAPublicKey) {
RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateCrtKey prvKey = (RSAPrivateCrtKey) keyPair.getPrivate();
pem = new PEMRSAPrivate(0, pubKey.getModulus(), pubKey.getPublicExponent(), prvKey.getPrivateExponent(), prvKey.getPrimeP(), prvKey.getPrimeQ(), prvKey.getCrtCoefficient());
headType = TYPE_PEM_RSA;
} else if (publicKey instanceof ECPublicKey) {
ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey prvKey = (ECPrivateKey) keyPair.getPrivate();
pem = new PEMECPrivate(pubKey, prvKey);
headType = TYPE_PEM_EC;
} else {
throw new SSH2FatalException("Unsupported key type: " + publicKey);
}
armour.setHeaderLine(BEGIN_PRV_KEY[headType]);
armour.setTailLine(END_PRV_KEY[headType]);
ByteArrayOutputStream enc = new ByteArrayOutputStream(128);
ASN1DER der = new ASN1DER();
try {
der.encode(enc, pem);
} catch (IOException e) {
throw new SSH2FatalException("Error while DER encoding");
}
byte[] keyBlob = enc.toByteArray();
if (password != null && password.length() > 0) {
byte[] iv = new byte[16];
random.setSeed(keyBlob);
for (int i = 0; i < iv.length; i++) {
byte[] r = new byte[1];
do {
random.nextBytes(r);
iv[i] = r[0];
} while (iv[i] == 0x00);
}
armour.setHeaderField(PRV_PROCTYPE, "4,ENCRYPTED");
armour.setHeaderField(PRV_DEKINFO, "AES-128-CBC," + HexDump.toString(iv).toUpperCase());
int encLen = (16 - (keyBlob.length % 16)) + keyBlob.length;
byte[] encBuf = new byte[encLen];
doCipher(Cipher.ENCRYPT_MODE, "AES/CBC/PKCS5Padding", password, keyBlob, keyBlob.length, encBuf, iv);
keyBlob = encBuf;
}
return keyBlob;
}
use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class SSH2KeyPairFile method readKeyPair.
public static KeyPair readKeyPair(ASCIIArmour armour, byte[] keyBlob, String password) throws SSH2Exception {
String procType = armour.getHeaderField(PRV_PROCTYPE);
if (procType != null && password != null) {
String dekInfo = armour.getHeaderField(PRV_DEKINFO);
if (dekInfo == null || !(dekInfo.startsWith("DES-EDE3-CBC,") || dekInfo.startsWith("AES-128-CBC,"))) {
throw new SSH2FatalException("Proc type not supported: " + procType);
}
boolean isdes = dekInfo.startsWith("DES");
dekInfo = dekInfo.substring(dekInfo.indexOf(',') + 1);
BigInteger dekI = new BigInteger(dekInfo, 16);
byte[] iv = dekI.toByteArray();
if (isdes) {
if (iv.length > 8) {
byte[] tmp = iv;
iv = new byte[8];
System.arraycopy(tmp, 1, iv, 0, 8);
}
} else {
if (iv.length > 16) {
byte[] tmp = iv;
iv = new byte[16];
System.arraycopy(tmp, 1, iv, 0, 16);
}
}
doCipher(Cipher.DECRYPT_MODE, isdes ? "DESEDE/CBC/PKCS5Padding" : "AES/CBC/PKCS5Padding", password, keyBlob, keyBlob.length, keyBlob, iv);
}
ByteArrayInputStream enc = new ByteArrayInputStream(keyBlob);
ASN1DER der = new ASN1DER();
KeySpec prvSpec = null;
KeySpec pubSpec = null;
String keyFactType = null;
String head = armour.getHeaderLine();
if (head.indexOf("DSA") != -1) {
keyFactType = "DSA";
} else if (head.indexOf("RSA") != -1) {
keyFactType = "RSA";
} else if (head.indexOf("EC") != -1) {
keyFactType = "EC";
}
try {
if ("DSA".equals(keyFactType)) {
PEMDSAPrivate dsa = new PEMDSAPrivate();
der.decode(enc, dsa);
BigInteger p, q, g, x, y;
p = dsa.p.getValue();
q = dsa.q.getValue();
g = dsa.g.getValue();
y = dsa.y.getValue();
x = dsa.x.getValue();
prvSpec = new DSAPrivateKeySpec(x, p, q, g);
pubSpec = new DSAPublicKeySpec(y, p, q, g);
} else if ("RSA".equals(keyFactType)) {
PEMRSAPrivate rsa = new PEMRSAPrivate();
der.decode(enc, rsa);
BigInteger n, e, d, p, q, pe, qe, u;
n = rsa.modulus.getValue();
e = rsa.publicExponent.getValue();
d = rsa.privateExponent.getValue();
p = rsa.prime1.getValue();
q = rsa.prime2.getValue();
pe = rsa.exponent1.getValue();
qe = rsa.exponent2.getValue();
u = rsa.coefficient.getValue();
prvSpec = new RSAPrivateCrtKeySpec(n, e, d, p, q, pe, qe, u);
pubSpec = new RSAPublicKeySpec(n, e);
} else if ("EC".equals(keyFactType)) {
PEMECPrivate ec = new PEMECPrivate();
der.decode(enc, ec);
String curve;
String curveid = ec.curveid.getString();
if (curveid.equals(EC_CURVE_SECP256R1_OID)) {
curve = EC_CURVE_SECP256R1_NAME;
} else if (curveid.equals(EC_CURVE_SECP384R1_OID)) {
curve = EC_CURVE_SECP384R1_NAME;
} else {
curve = EC_CURVE_SECP521R1_NAME;
}
ECParameterSpec ecspec = SSH2ECDSASHA2NIST.getParamsForCurve(curve);
byte[] privraw = ec.privateKey.getRaw();
byte[] privb = new byte[privraw.length + 1];
System.arraycopy(privraw, 0, privb, 1, privraw.length);
prvSpec = new ECPrivateKeySpec(new BigInteger(privb), ecspec);
byte[] pubb = ec.publicKey.getBitArray();
pubSpec = new ECPublicKeySpec(SSH2KEXECDHSHA2NIST.frombytes(pubb, ecspec.getCurve()), ecspec);
} else {
throw new SSH2FatalException("Unsupported key type: " + keyFactType);
}
} catch (IOException e) {
throw new SSH2AccessDeniedException("Invalid password or corrupt key blob");
}
try {
KeyFactory keyFact = Crypto.getKeyFactory(keyFactType);
return new KeyPair(keyFact.generatePublic(pubSpec), keyFact.generatePrivate(prvSpec));
} catch (Exception e) {
throw new SSH2FatalException("Error in readKeyPair: " + e);
}
}
use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class SSH2ECDSASHA2NIST method sign.
public byte[] sign(byte[] data) throws SSH2SignatureException {
try {
signature.update(data);
byte[] sigRaw = signature.sign();
try {
SSH2DSS.DSASIG sign = new SSH2DSS.DSASIG();
ASN1DER der = new ASN1DER();
ByteArrayInputStream dec = new ByteArrayInputStream(sigRaw);
der.decode(dec, sign);
SSH2DataBuffer buf = new SSH2DataBuffer(256);
buf.writeBigInt(sign.r.getValue());
buf.writeBigInt(sign.s.getValue());
sigRaw = buf.readRestRaw();
} catch (IOException ioe) {
throw new SSH2SignatureException("DER decode failed: " + ioe.getMessage());
}
return encodeSignature(sigRaw);
} catch (SignatureException e) {
throw new SSH2SignatureException("Error in " + algorithm + " sign: " + e.getMessage());
}
}
use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class SSH2DSS method sign.
public byte[] sign(byte[] data) throws SSH2SignatureException {
try {
signature.update(data);
byte[] sigRaw = signature.sign();
try {
DSASIG sign = new DSASIG();
ASN1DER der = new ASN1DER();
ByteArrayInputStream dec = new ByteArrayInputStream(sigRaw);
der.decode(dec, sign);
sigRaw = new byte[40];
byte[] tmp = unsignedBigIntToBytes(sign.r.getValue(), 20);
System.arraycopy(tmp, 0, sigRaw, 0, 20);
tmp = unsignedBigIntToBytes(sign.s.getValue(), 20);
System.arraycopy(tmp, 0, sigRaw, 20, 20);
} catch (IOException ioe) {
throw new SSH2SignatureException("DER decode failed: " + ioe.getMessage());
}
return encodeSignature(sigRaw);
} catch (SignatureException e) {
throw new SSH2SignatureException("Error in " + algorithm + " sign: " + e.getMessage());
}
}
Aggregations