Search in sources :

Example 11 with ASN1DER

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;
}
Also used : ASN1DER(com.mindbright.asn1.ASN1DER) ByteArrayInputStream(java.io.ByteArrayInputStream) ASN1Object(com.mindbright.asn1.ASN1Object)

Example 12 with ASN1DER

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);
}
Also used : ASN1DER(com.mindbright.asn1.ASN1DER) PKCS12PbeParams(com.mindbright.security.pkcs12.PKCS12PbeParams) ASN1OctetString(com.mindbright.asn1.ASN1OctetString) ASN1CharString(com.mindbright.asn1.ASN1CharString)

Example 13 with ASN1DER

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;
    }
}
Also used : ASN1OctetString(com.mindbright.asn1.ASN1OctetString) PFX(com.mindbright.security.pkcs12.PFX) ASN1DER(com.mindbright.asn1.ASN1DER) ByteArrayInputStream(java.io.ByteArrayInputStream) ContentInfo(com.mindbright.security.pkcs7.ContentInfo) PKCS12PbeParams(com.mindbright.security.pkcs12.PKCS12PbeParams) AuthenticatedSafe(com.mindbright.security.pkcs12.AuthenticatedSafe) ASN1OctetString(com.mindbright.asn1.ASN1OctetString) ASN1CharString(com.mindbright.asn1.ASN1CharString) EncryptedData(com.mindbright.security.pkcs7.EncryptedData) IOException(java.io.IOException)

Example 14 with ASN1DER

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);
}
Also used : ASN1DER(com.mindbright.asn1.ASN1DER) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) DSAParams(java.security.interfaces.DSAParams) IOException(java.io.IOException) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 15 with ASN1DER

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();
}
Also used : ASN1DER(com.mindbright.asn1.ASN1DER) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) DSAParams(java.security.interfaces.DSAParams) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

ASN1DER (com.mindbright.asn1.ASN1DER)15 IOException (java.io.IOException)13 ByteArrayInputStream (java.io.ByteArrayInputStream)10 BigInteger (java.math.BigInteger)7 ASN1OctetString (com.mindbright.asn1.ASN1OctetString)5 SignatureException (java.security.SignatureException)5 ASN1CharString (com.mindbright.asn1.ASN1CharString)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 EncryptedPrivateKeyInfo (com.mindbright.security.pkcs8.EncryptedPrivateKeyInfo)3 KeyFactory (java.security.KeyFactory)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 DSAParams (java.security.interfaces.DSAParams)3 ASN1Object (com.mindbright.asn1.ASN1Object)2 PKCS12PbeParams (com.mindbright.security.pkcs12.PKCS12PbeParams)2 GeneralSecurityException (java.security.GeneralSecurityException)2 InvalidKeyException (java.security.InvalidKeyException)2 UnrecoverableKeyException (java.security.UnrecoverableKeyException)2 CertificateException (java.security.cert.CertificateException)2 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)2 DSAPublicKey (java.security.interfaces.DSAPublicKey)2