use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class X509Certificate method getExtensionWithOID.
private ASN1Object getExtensionWithOID(String oid, Class<?> c) {
try {
Extensions es = certificate.tbsCertificate.extensions;
for (int i = 0; i < es.getCount(); i++) {
Extension e = (Extension) es.getComponent(i);
if (e.extnID.getString().equals(oid)) {
ASN1DER der = new ASN1DER();
ByteArrayInputStream ba = new ByteArrayInputStream(e.extnValue.getRaw());
ASN1Object obj = (ASN1Object) c.newInstance();
der.decode(ba, obj);
return obj;
}
}
} catch (Throwable t) {
}
return null;
}
use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class PKCS12KeyStore method extractPrivateKey.
private static PrivateKey extractPrivateKey(EncryptedPrivateKeyInfo keyBag, char[] password) throws NoSuchAlgorithmException, UnrecoverableKeyException {
new ASN1DER();
PKCS12PbeParams params = (PKCS12PbeParams) keyBag.encryptionAlgorithm.parameters.getValue();
String alg = keyBag.encryptionAlgorithm.algorithmName();
byte[] enc = keyBag.encryptedData.getRaw();
byte[] salt = params.salt.getRaw();
int iterations = params.iterations.getValue().intValue();
byte[] dec = new byte[enc.length];
doCipher(Cipher.DECRYPT_MODE, password, enc, enc.length, dec, salt, iterations, alg);
return extractPrivateKey(dec);
}
use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class PKCS12KeyStore method engineLoad.
public void engineLoad(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException {
try {
ASN1DER ber = new ASN1DER();
PFX pfx = new PFX();
ber.decode(stream, pfx);
if (password == null) {
password = new char[0];
}
checkMac(pfx, password);
AuthenticatedSafe authSafe = new AuthenticatedSafe();
ASN1OctetString data = pfx.getDataContent();
ByteArrayInputStream ba = new ByteArrayInputStream(data.getRaw());
ber.decode(ba, authSafe);
for (int i = 0; i < authSafe.getCount(); i++) {
ContentInfo ci = authSafe.getContentInfo(i);
String cit = ci.contentType.getString();
if (cit.equals("1.2.840.113549.1.7.1")) {
data = (ASN1OctetString) ci.content.getValue();
processSafeContents(data.getRaw());
} else if (cit.equals("1.2.840.113549.1.7.6")) {
EncryptedData ed = (EncryptedData) ci.content.getValue();
String alg = ed.encryptedContentInfo.contentEncryptionAlgorithm.algorithmName();
byte[] enc = ed.encryptedContentInfo.encryptedContent.getRaw();
PKCS12PbeParams params = (PKCS12PbeParams) ed.encryptedContentInfo.contentEncryptionAlgorithm.parameters.getValue();
byte[] salt = params.salt.getRaw();
int iterations = params.iterations.getValue().intValue();
byte[] dec = new byte[enc.length];
doCipher(Cipher.DECRYPT_MODE, password, enc, enc.length, dec, salt, iterations, alg);
processSafeContents(dec);
} else {
throw new IOException("ContentInfo type not supported: " + cit);
}
}
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class DSAWithSHA1 method verify.
protected boolean verify(byte[] signature, byte[] data) {
DSAPublicKey key = (DSAPublicKey) publicKey;
DSAParams parm = key.getParams();
BigInteger y = key.getY();
BigInteger p = parm.getP();
BigInteger q = parm.getQ();
BigInteger g = parm.getG();
DSASIG sign = new DSASIG();
try {
ASN1DER der = new ASN1DER();
ByteArrayInputStream dec = new ByteArrayInputStream(signature);
der.decode(dec, sign);
} catch (IOException e) {
// This should not happen
System.err.println("DSAWithSHA1.verify: " + e);
return false;
}
return DSAAlgorithm.verify(y, p, q, g, sign.r.getValue(), sign.s.getValue(), data);
}
use of com.mindbright.asn1.ASN1DER in project SpringRemote by HaleyWang.
the class DSAWithSHA1 method sign.
protected byte[] sign(byte[] data) {
DSAPrivateKey key = (DSAPrivateKey) privateKey;
DSAParams parm = key.getParams();
BigInteger x = key.getX();
BigInteger p = parm.getP();
BigInteger q = parm.getQ();
BigInteger g = parm.getG();
BigInteger[] sign = DSAAlgorithm.sign(x, p, q, g, data);
if (sign == null || sign.length != 2) {
return null;
}
BigInteger r = sign[0];
BigInteger s = sign[1];
// Encode
DSASIG dsasig = new DSASIG(r, s);
ByteArrayOutputStream enc = new ByteArrayOutputStream(128);
ASN1DER der = new ASN1DER();
try {
der.encode(enc, dsasig);
} catch (IOException e) {
// This should not happen
}
return enc.toByteArray();
}
Aggregations