use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class X509Certificate method getPublicKey.
public PublicKey getPublicKey() {
SubjectPublicKeyInfo spki = certificate.tbsCertificate.subjectPublicKeyInfo;
String alg = spki.algorithm.algorithmName().toUpperCase();
ASN1DER der = new ASN1DER();
if (alg.startsWith("RSA")) {
RSAPublicKey rsa = new RSAPublicKey();
ByteArrayInputStream ba = new ByteArrayInputStream(spki.subjectPublicKey.getBitArray());
try {
der.decode(ba, rsa);
} catch (Exception e) {
throw new Error("Internal error decoding SubjectPublicKeyInfo.subjectPublicKey: " + e.getMessage());
}
try {
KeyFactory keyFact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(rsa.modulus.getValue(), rsa.publicExponent.getValue());
return keyFact.generatePublic(pubSpec);
} catch (Exception e) {
throw new Error("Error creating RSA key: " + e.getMessage());
}
} else if (alg.startsWith("DSA")) {
DSAPublicKey dsa = new DSAPublicKey();
ByteArrayInputStream ba = new ByteArrayInputStream(spki.subjectPublicKey.getBitArray());
try {
der.decode(ba, dsa);
} catch (Exception e) {
throw new Error("Internal error decoding SubjectPublicKeyInfo.subjectPublicKey: " + e.getMessage());
}
BigInteger y = dsa.getValue();
DSAParams dsaParams = (DSAParams) spki.algorithm.parameters.getValue();
BigInteger p = dsaParams.p.getValue();
BigInteger q = dsaParams.q.getValue();
BigInteger g = dsaParams.g.getValue();
try {
KeyFactory dsaKeyFact = KeyFactory.getInstance("DSA");
DSAPublicKeySpec dsaPubSpec = new DSAPublicKeySpec(y, p, q, g);
return dsaKeyFact.generatePublic(dsaPubSpec);
} catch (Exception e) {
throw new Error("Error creating DSA key: " + e.getMessage());
}
} else {
throw new Error("Internal error decoding publicKey: unknown algorithm");
}
}
use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class SSH2DSS method verify.
public boolean verify(byte[] sigBlob, byte[] data) throws SSH2SignatureException {
try {
byte[] sigRaw = decodeSignature(sigBlob);
signature.update(data);
int slen = sigRaw.length / 2;
byte[] ra = new byte[slen];
byte[] sa = new byte[slen];
System.arraycopy(sigRaw, 0, ra, 0, slen);
System.arraycopy(sigRaw, slen, sa, 0, slen);
BigInteger r = new BigInteger(1, ra);
BigInteger s = new BigInteger(1, sa);
DSASIG dsasig = new DSASIG(r, s);
ByteArrayOutputStream enc = new ByteArrayOutputStream(128);
ASN1DER der = new ASN1DER();
try {
der.encode(enc, dsasig);
} catch (IOException ioe) {
throw new SSH2SignatureException("DER encode failed: " + ioe.getMessage());
}
return signature.verify(enc.toByteArray());
} catch (SignatureException e) {
throw new SSH2SignatureException("Error in " + algorithm + " verify: " + e.getMessage());
}
}
use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class SSH2ECDSASHA2NIST method verify.
public boolean verify(byte[] sigBlob, byte[] data) throws SSH2SignatureException {
try {
signature.update(data);
byte[] sigRaw = decodeSignature(sigBlob);
SSH2DataBuffer buf = new SSH2DataBuffer(sigRaw.length);
buf.writeRaw(sigRaw);
BigInteger r = buf.readBigInt();
BigInteger s = buf.readBigInt();
SSH2DSS.DSASIG dsasig = new SSH2DSS.DSASIG(r, s);
ByteArrayOutputStream enc = new ByteArrayOutputStream(128);
ASN1DER der = new ASN1DER();
try {
der.encode(enc, dsasig);
} catch (IOException ioe) {
throw new SSH2SignatureException("DER encode failed: " + ioe.getMessage());
}
return signature.verify(enc.toByteArray());
} catch (SignatureException e) {
throw new SSH2SignatureException("Error in " + algorithm + " verify: " + e.getMessage());
}
}
use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class PKCS12KeyStore method extractPrivateKey.
/*
* !!! TODO generalize handling and move to pkcs8 or pkcs1
*/
public static PrivateKey extractPrivateKey(byte[] berPrivateKeyInfo) throws UnrecoverableKeyException {
ASN1DER ber = new ASN1DER();
ByteArrayInputStream ba = new ByteArrayInputStream(berPrivateKeyInfo);
PrivateKeyInfo pki = new PrivateKeyInfo();
try {
ber.decode(ba, pki);
boolean isrsakey = true;
try {
String alg = pki.privateKeyAlgorithm.algorithmName().toLowerCase();
if (alg.indexOf("dsa") >= 0)
isrsakey = false;
} catch (Throwable t) {
}
ba = new ByteArrayInputStream(pki.privateKey.getRaw());
if (isrsakey) {
com.mindbright.security.pkcs1.RSAPrivateKey rsa = new com.mindbright.security.pkcs1.RSAPrivateKey();
ber.decode(ba, 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();
RSAPrivateCrtKeySpec prvSpec = new RSAPrivateCrtKeySpec(n, e, d, p, q, pe, qe, u);
KeyFactory keyFact = KeyFactory.getInstance("RSA");
return keyFact.generatePrivate(prvSpec);
}
BigInteger x = null;
try {
// Normally, we should have just one ASN.1 integer here...
ASN1Integer dsax = new ASN1Integer();
ber.decode(ba, dsax);
x = dsax.getValue();
} catch (Throwable t) {
}
if (x == null) {
// ... but Mozilla returns SEQUENCE { y? INTEGER, x INTEGER }
DSAyx dsayx = new DSAyx();
ber.decode(new ByteArrayInputStream(pki.privateKey.getRaw()), dsayx);
x = dsayx.x.getValue();
}
com.mindbright.security.pkcs1.DSAParams params = (com.mindbright.security.pkcs1.DSAParams) pki.privateKeyAlgorithm.parameters.getValue();
DSAPrivateKeySpec prvSpec = new DSAPrivateKeySpec(x, params.p.getValue(), params.q.getValue(), params.g.getValue());
KeyFactory keyFact = KeyFactory.getInstance("DSA");
return keyFact.generatePrivate(prvSpec);
} catch (Exception e) {
throw new UnrecoverableKeyException(e.getMessage());
}
}
use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class PKCS12KeyStore method processSafeContents.
private void processSafeContents(byte[] scBer) throws IOException {
ByteArrayInputStream ba = new ByteArrayInputStream(scBer);
SafeContents sc = new SafeContents();
ASN1DER ber = new ASN1DER();
ber.decode(ba, sc);
for (int j = 0; j < sc.getCount(); j++) {
SafeBag safeBag = sc.getSafeBag(j);
String friendlyName = getAttribute(safeBag, "1.2.840.113549.1.9.20");
String localKeyId = getAttribute(safeBag, "1.2.840.113549.1.9.21");
if (friendlyName != null) {
if (localKeyId != null) {
name2id.put(friendlyName, localKeyId);
}
if (!aliases.contains(friendlyName)) {
aliases.addElement(friendlyName);
}
} else if (localKeyId != null) {
name2id.put(localKeyId, localKeyId);
if (!aliases.contains(localKeyId)) {
aliases.addElement(localKeyId);
}
}
switch(safeBag.getBagType()) {
case SafeBag.TYPE_PKCS8_SHROUDED_KEYBAG:
EncryptedPrivateKeyInfo keyBag = (EncryptedPrivateKeyInfo) safeBag.bagValue.getValue();
privateKeys.put(localKeyId, keyBag);
break;
case SafeBag.TYPE_CERTBAG:
CertBag cb = (CertBag) safeBag.bagValue.getValue();
byte[] derCert = ((ASN1OctetString) cb.certValue.getValue()).getRaw();
if (localKeyId == null) {
/*
* Trusted certs don't have a localKeyId
*/
localKeyId = friendlyName;
} else {
certificates.put(localKeyId, derCert);
}
break;
default:
throw new IOException("SafeBag type not supported: " + safeBag.bagId.getString());
}
}
}
Aggregations