Search in sources :

Example 91 with ASN1InputStream

use of org.gudy.bouncycastle.asn1.ASN1InputStream in project aion by aionnetwork.

the class ECDSASignature method decodeFromDER.

public static ECDSASignature decodeFromDER(byte[] bytes) {
    ASN1InputStream decoder = null;
    try {
        decoder = new ASN1InputStream(bytes);
        DLSequence seq = (DLSequence) decoder.readObject();
        if (seq == null) {
            throw new RuntimeException("Reached past end of ASN.1 stream.");
        }
        ASN1Integer r, s;
        try {
            r = (ASN1Integer) seq.getObjectAt(0);
            s = (ASN1Integer) seq.getObjectAt(1);
        } catch (ClassCastException e) {
            throw new IllegalArgumentException(e);
        }
        // http://r6.ca/blog/20111119T211504Z.html
        return new ECDSASignature(r.getPositiveValue(), s.getPositiveValue());
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (decoder != null) {
            try {
                decoder.close();
            } catch (IOException x) {
            }
        }
    }
}
Also used : ASN1InputStream(org.spongycastle.asn1.ASN1InputStream) DLSequence(org.spongycastle.asn1.DLSequence) ASN1Integer(org.spongycastle.asn1.ASN1Integer) IOException(java.io.IOException)

Example 92 with ASN1InputStream

use of org.gudy.bouncycastle.asn1.ASN1InputStream in project nuls by nuls-io.

the class SM2Utils method decrypt.

public static byte[] decrypt(byte[] privateKey, byte[] encryptedData) throws IOException {
    if (privateKey == null || privateKey.length == 0) {
        return null;
    }
    if (encryptedData == null || encryptedData.length == 0) {
        return null;
    }
    byte[] enc = new byte[encryptedData.length];
    System.arraycopy(encryptedData, 0, enc, 0, encryptedData.length);
    SM2 sm2 = SM2.Instance();
    BigInteger userD = new BigInteger(1, privateKey);
    ByteArrayInputStream bis = new ByteArrayInputStream(enc);
    ASN1InputStream dis = new ASN1InputStream(bis);
    DERObject derObj = dis.readObject();
    ASN1Sequence asn1 = (ASN1Sequence) derObj;
    DERInteger x = (DERInteger) asn1.getObjectAt(0);
    DERInteger y = (DERInteger) asn1.getObjectAt(1);
    ECPoint c1 = sm2.ecc_curve.createPoint(x.getValue(), y.getValue(), true);
    Cipher cipher = new Cipher();
    cipher.initDec(userD, c1);
    DEROctetString data = (DEROctetString) asn1.getObjectAt(3);
    enc = data.getOctets();
    cipher.decrypt(enc);
    byte[] c3 = new byte[32];
    cipher.dofinal(c3);
    return enc;
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) DERObject(org.bouncycastle.asn1.DERObject) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) ECPoint(org.bouncycastle.math.ec.ECPoint) DEROctetString(org.bouncycastle.asn1.DEROctetString) DERInteger(org.bouncycastle.asn1.DERInteger)

Example 93 with ASN1InputStream

use of org.gudy.bouncycastle.asn1.ASN1InputStream in project BiglyBT by BiglySoftware.

the class PEMReader method readRSAPublicKey.

private PublicKey readRSAPublicKey(String endMarker) throws IOException {
    ByteArrayInputStream bAIS = new ByteArrayInputStream(readBytes(endMarker));
    ASN1InputStream ais = new ASN1InputStream(bAIS);
    Object asnObject = ais.readObject();
    ASN1Sequence sequence = (ASN1Sequence) asnObject;
    RSAPublicKeyStructure rsaPubStructure = new RSAPublicKeyStructure(sequence);
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(rsaPubStructure.getModulus(), rsaPubStructure.getPublicExponent());
    try {
        KeyFactory keyFact = KeyFactory.getInstance("RSA", provider);
        return keyFact.generatePublic(keySpec);
    } catch (NoSuchProviderException e) {
        throw new IOException("can't find provider " + provider);
    } catch (Exception e) {
        throw new IOException("problem extracting key: " + e.toString());
    }
}
Also used : RSAPublicKeyStructure(org.gudy.bouncycastle.asn1.x509.RSAPublicKeyStructure)

Example 94 with ASN1InputStream

use of org.gudy.bouncycastle.asn1.ASN1InputStream in project BiglyBT by BiglySoftware.

the class PrincipalUtil method getIssuerX509Principal.

/**
 * return the issuer of the given CRL as an X509PrincipalObject.
 */
public static X509Principal getIssuerX509Principal(X509CRL crl) throws CRLException {
    try {
        ByteArrayInputStream bIn = new ByteArrayInputStream(crl.getTBSCertList());
        ASN1InputStream aIn = new ASN1InputStream(bIn);
        TBSCertList tbsCertList = new TBSCertList((ASN1Sequence) aIn.readObject());
        return new X509Principal(tbsCertList.getIssuer());
    } catch (IOException e) {
        throw new CRLException(e.toString());
    }
}
Also used : ASN1InputStream(org.gudy.bouncycastle.asn1.ASN1InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TBSCertList(org.gudy.bouncycastle.asn1.x509.TBSCertList) IOException(java.io.IOException) CRLException(java.security.cert.CRLException)

Example 95 with ASN1InputStream

use of org.gudy.bouncycastle.asn1.ASN1InputStream in project BiglyBT by BiglySoftware.

the class PrincipalUtil method getIssuerX509Principal.

/**
 * return the issuer of the given cert as an X509PrincipalObject.
 */
public static X509Principal getIssuerX509Principal(X509Certificate cert) throws CertificateEncodingException {
    try {
        ByteArrayInputStream bIn = new ByteArrayInputStream(cert.getTBSCertificate());
        ASN1InputStream aIn = new ASN1InputStream(bIn);
        TBSCertificateStructure tbsCert = new TBSCertificateStructure((ASN1Sequence) aIn.readObject());
        return new X509Principal(tbsCert.getIssuer());
    } catch (IOException e) {
        throw new CertificateEncodingException(e.toString());
    }
}
Also used : ASN1InputStream(org.gudy.bouncycastle.asn1.ASN1InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TBSCertificateStructure(org.gudy.bouncycastle.asn1.x509.TBSCertificateStructure) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException)

Aggregations

ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)99 IOException (java.io.IOException)81 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)34 ByteArrayInputStream (java.io.ByteArrayInputStream)28 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)22 BigInteger (java.math.BigInteger)20 ASN1InputStream (com.android.org.bouncycastle.asn1.ASN1InputStream)19 CertificateException (java.security.cert.CertificateException)19 X509Certificate (java.security.cert.X509Certificate)19 DEROctetString (org.bouncycastle.asn1.DEROctetString)19 CertificateParsingException (java.security.cert.CertificateParsingException)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)17 Enumeration (java.util.Enumeration)17 CertificateEncodingException (java.security.cert.CertificateEncodingException)16 InvalidKeyException (java.security.InvalidKeyException)14 CRLException (java.security.cert.CRLException)14 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)14 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)12 NoSuchProviderException (java.security.NoSuchProviderException)11 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)11