Example usage for org.bouncycastle.asn1 ASN1InputStream ASN1InputStream

List of usage examples for org.bouncycastle.asn1 ASN1InputStream ASN1InputStream

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 ASN1InputStream ASN1InputStream.

Prototype

public ASN1InputStream(byte[] input) 

Source Link

Document

Create an ASN1InputStream based on the input byte array.

Usage

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static byte[] getAKI(byte[] extensionValue, int index) {
    byte[] ret = null;
    try {//from w w w .  ja v a2  s. c  o  m
        if (extensionValue == null) {
            return null;
        }
        ASN1InputStream oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(extensionValue));
        ASN1Primitive derObjCP = oAsnInStream.readObject();
        DEROctetString dosCP = (DEROctetString) derObjCP;
        byte[] cpOctets = dosCP.getOctets();
        ASN1InputStream oAsnInStream2 = new ASN1InputStream(new ByteArrayInputStream(cpOctets));
        ASN1Primitive derObj2 = oAsnInStream2.readObject();
        // derObj2 = oAsnInStream2.readObject();
        DLSequence derSeq = (DLSequence) derObj2;
        int seqLen = derSeq.size();
        // for(int i = 0; i < seqLen; i++){
        ASN1Encodable derObj3 = derSeq.getObjectAt(0);
        DERTaggedObject derTO = (DERTaggedObject) derObj3;
        int tag = derTO.getTagNo();
        boolean empty = derTO.isEmpty();
        ASN1Primitive derObj4 = derTO.getObject();
        DEROctetString ocStr4 = (DEROctetString) derObj4;
        ret = ocStr4.getOctets();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return ret;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static Map<String, String> getAIAComplete(byte[] ext) throws UnsupportedEncodingException {
    Map<String, String> ret = new HashMap<String, String>();
    try {/*ww w. ja  v  a  2  s.  co  m*/
        if (ext == null)
            return null;
        ASN1InputStream oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(ext));
        ASN1Primitive derObjAIA = oAsnInStream.readObject();
        DEROctetString dosAia = (DEROctetString) derObjAIA;
        byte[] aiaExtOctets = dosAia.getOctets();

        // ------------ level 2
        ASN1InputStream oAsnInStream2 = new ASN1InputStream(new ByteArrayInputStream(aiaExtOctets));
        ASN1Primitive derObj2 = oAsnInStream2.readObject();
        DLSequence aiaDLSeq = (DLSequence) derObj2;
        ASN1Encodable[] aiaAsArray = aiaDLSeq.toArray();
        for (ASN1Encodable next : aiaAsArray) {
            DLSequence aiaDLSeq2 = (DLSequence) next;
            ASN1Encodable[] aiaAsArray2 = aiaDLSeq2.toArray();
            // oid = 0 / content = 1
            ASN1Encodable aiaOidEnc = aiaAsArray2[0];
            ASN1ObjectIdentifier aiaOid = (ASN1ObjectIdentifier) aiaOidEnc;
            String idStr = aiaOid.getId();
            // if (idStr.compareTo("1.3.6.1.5.5.7.48.2") == 0) {
            ASN1Encodable aiaContent = aiaAsArray2[1];
            DERTaggedObject aiaDTO = (DERTaggedObject) aiaContent;
            ASN1Primitive aiaObj = aiaDTO.getObject();
            DEROctetString aiaDOS = (DEROctetString) aiaObj;
            byte[] aiaOC = aiaDOS.getOctets();
            ret.put(idStr, new String(aiaOC));
            // break;
            // }
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return ret;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static Map<String, String> getCertPolicies(byte[] certPols, int index)
        throws CertificateParsingException, IOException {
    Map<String, String> ret = new HashMap<String, String>();
    if (certPols == null) {
        return null;
    }/* www.  ja v  a2s.c om*/
    ASN1InputStream oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(certPols));
    ASN1Primitive derObjCP = oAsnInStream.readObject();
    DEROctetString dosCP = (DEROctetString) derObjCP;
    byte[] cpOctets = dosCP.getOctets();
    ASN1InputStream oAsnInStream2 = new ASN1InputStream(new ByteArrayInputStream(cpOctets));
    ASN1Primitive derObj2 = oAsnInStream2.readObject();
    DLSequence dlCP = (DLSequence) derObj2;
    int seqLen = dlCP.size();
    for (int i = 0; i < seqLen; i++) {
        ASN1Encodable nextObj = dlCP.getObjectAt(i);
        DLSequence dlCP2 = (DLSequence) nextObj;
        // for(int j = 0; j < dlCP2.size(); j++){
        ASN1Encodable nextObj2 = dlCP2.getObjectAt(0);
        ASN1ObjectIdentifier pcOID = (ASN1ObjectIdentifier) nextObj2;
        ret.put(String.format(CERT_POL_OID, index), pcOID.toString());
        if (pcOID.toString().startsWith(ICP_BRASIL_PC_PREFIX_OID)) {

            ret.put(String.format(CertConstants.CERT_USAGE_D, index), getCertUsage(pcOID.toString()));
        }

        if (dlCP2.size() == 2) {
            nextObj2 = dlCP2.getObjectAt(1);

            ASN1Encodable nextObj3 = null;
            if (nextObj2 instanceof DLSequence) {
                DLSequence dlCP3 = (DLSequence) nextObj2;
                nextObj3 = dlCP3.getObjectAt(0);
            } else if (nextObj2 instanceof DERSequence) {
                DERSequence dlCP3 = (DERSequence) nextObj2;
                if (dlCP3.size() > 1) {
                    nextObj3 = dlCP3.getObjectAt(0);
                }

            }
            if (nextObj3 != null) {
                DLSequence dlCP4 = (DLSequence) nextObj3;
                ASN1Encodable nextObj4a = dlCP4.getObjectAt(0);
                ASN1Encodable nextObj4b = dlCP4.getObjectAt(1);

                ret.put(String.format(CERT_POL_QUALIFIER, index), nextObj4b.toString());
            }
        }
    }
    return ret;

}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static List<String> getCrlDistributionPoints(byte[] crldpExt)
        throws CertificateParsingException, IOException {
    if (crldpExt == null) {
        return new ArrayList<String>();
    }/*  w  w  w. j  a  v  a2s .  c  o  m*/
    ASN1InputStream oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(crldpExt));
    ASN1Primitive derObjCrlDP = oAsnInStream.readObject();
    DEROctetString dosCrlDP = (DEROctetString) derObjCrlDP;
    byte[] crldpExtOctets = dosCrlDP.getOctets();
    ASN1InputStream oAsnInStream2 = new ASN1InputStream(new ByteArrayInputStream(crldpExtOctets));
    ASN1Primitive derObj2 = oAsnInStream2.readObject();
    CRLDistPoint distPoint = CRLDistPoint.getInstance(derObj2);
    List<String> crlUrls = new ArrayList<String>();
    for (DistributionPoint dp : distPoint.getDistributionPoints()) {
        DistributionPointName dpn = dp.getDistributionPoint();
        // Look for URIs in fullName
        if (dpn != null && dpn.getType() == DistributionPointName.FULL_NAME) {
            GeneralName[] genNames = GeneralNames.getInstance(dpn.getName()).getNames();
            // Look for an URI
            for (int j = 0; j < genNames.length; j++) {
                if (genNames[j].getTagNo() == GeneralName.uniformResourceIdentifier) {
                    String url = DERIA5String.getInstance(genNames[j].getName()).getString();
                    crlUrls.add(url);
                }
            }
        }
    }
    return crlUrls;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

private static ASN1Primitive getObject(String oid, byte[] ext) throws CRLException {
    try {//from   w ww .  ja v  a 2 s  .  c  o  m
        ASN1InputStream aIn = new ASN1InputStream(ext);
        ASN1OctetString octs = (ASN1OctetString) aIn.readObject();

        aIn = new ASN1InputStream(octs.getOctets());
        return aIn.readObject();
    } catch (Exception e) {

        throw new CRLException("exception processing extension " + oid, e); //$NON-NLS-1$
    }
}

From source file:br.gov.frameworkdemoiselle.certificate.extension.BasicCertificate.java

License:Open Source License

/**
 * Returns a List of URL for Certificate Revocation List. Must have on or
 * more<br>//from   ww  w.j  a v  a  2  s.  c om
 * Otherwise, returns <b>null</b>.<br>
 *
 * @return String
 * @throws IOException
 */
public List<String> getCRLDistributionPoint() throws IOException {

    List<String> lcrS = new ArrayList<String>();
    DERObject derObj = getExtensionValue(X509Extensions.CRLDistributionPoints.getId());
    if (derObj == null) {
        return null;
    }
    CRLDistPoint crlDistPoint = CRLDistPoint.getInstance(derObj);
    DistributionPoint[] dp = crlDistPoint.getDistributionPoints();
    for (int i = 0; i < dp.length; i++) {
        DERSequence seq = (DERSequence) new ASN1InputStream(
                dp[i].getDistributionPoint().getName().getDEREncoded()).readObject();
        DERTaggedObject tag = (DERTaggedObject) seq.getObjectAt(0);
        try {
            ASN1OctetString oct = DEROctetString.getInstance(tag);
            lcrS.add(new String(oct.getOctets()));
        } catch (Exception e) {
            // No  um objeto com informao de DistributionPoint
        }

    }
    return lcrS;
}

From source file:br.gov.frameworkdemoiselle.certificate.extension.BasicCertificate.java

License:Open Source License

/**
 * Returns the DERObject for the informed OID<br>
 * atraves do OID.<br>/*from   w  w  w.  ja  va 2s  . co  m*/
 *
 * @param oid
 * @return DERObject
 * @see DERObject
 */
public DERObject getExtensionValue(String oid) {
    byte[] extvalue = certificate.getExtensionValue(oid);
    if (extvalue == null) {
        return null;
    }
    try {
        DEROctetString oct = (DEROctetString) (new ASN1InputStream(extvalue).readObject());
        return (new ASN1InputStream(oct.getOctets()).readObject());
    } catch (IOException ex) {
        LOGGER.info(ex.getMessage());
    }
    return null;
}

From source file:br.gov.frameworkdemoiselle.certificate.oid.OIDGeneric.java

License:Open Source License

/**
 * Instance for object.//from w w w  .  j  ava2s.c o m
 *
 * @param data -> byte array with certificate content.
 * @return Object GenericOID
 * @throws IOException
 * @throws Exception
 */
public static OIDGeneric getInstance(byte[] data) throws IOException, Exception {
    ASN1InputStream is = new ASN1InputStream(data);
    DERSequence sequence = (DERSequence) is.readObject();
    DERObjectIdentifier objectIdentifier = (DERObjectIdentifier) sequence.getObjectAt(0);
    DERTaggedObject tag = (DERTaggedObject) sequence.getObjectAt(1);
    DEROctetString octetString = null;
    DERPrintableString printableString = null;
    DERUTF8String utf8String = null;
    DERIA5String ia5String = null;

    try {
        octetString = (DEROctetString) DEROctetString.getInstance(tag);
    } catch (Exception ex) {
        try {
            printableString = DERPrintableString.getInstance(tag);
        } catch (Exception e1) {
            try {
                utf8String = DERUTF8String.getInstance(tag);
            } catch (Exception e2) {
                ia5String = DERIA5String.getInstance(tag);
            }
        }
    }

    String className = "br.gov.frameworkdemoiselle.certificate.oid.OID_"
            + objectIdentifier.getId().replaceAll("[.]", "_");
    OIDGeneric oidGenerico;
    try {
        oidGenerico = (OIDGeneric) Class.forName(className).newInstance();
    } catch (InstantiationException e) {
        throw new Exception("Can not instace class '" + className + "'.", e);
    } catch (IllegalAccessException e) {
        throw new Exception("Was not possible instace class '" + className + "'.", e);
    } catch (ClassNotFoundException e) {
        oidGenerico = new OIDGeneric();
    }

    oidGenerico.setOid(objectIdentifier.getId());

    if (octetString != null) {
        oidGenerico.setData(new String(octetString.getOctets()));
    } else if (printableString != null) {
        oidGenerico.setData(printableString.getString());
    } else if (utf8String != null) {
        oidGenerico.setData(utf8String.getString());
    } else {
        oidGenerico.setData(ia5String.getString());
    }
    oidGenerico.initialize();
    return oidGenerico;
}

From source file:br.gov.frameworkdemoiselle.certificate.signer.util.ValidadorUtil.java

License:Open Source License

public static void validate(X509Certificate certificate) {
    /*//from w w  w. j av a  2 s  . c  o  m
     * Assinaturas digitais geradas segundo esta Poltica de Assinatura
     * devero ser criadas com chave privada associada ao certificado
     * ICP-Brasil * tipo A1 (do OID 2.16.76.1.2.1.1 ao OID
     * 2.16.76.1.2.1.100), tipo A2 (do OID 2.16.76.1.2.2.1 ao OID
     * 2.16.76.1.2.2.100), do tipo A3 (do OID 2.16.76.1.2.3.1 ao OID
     * 2.16.76.1.2.3.100) e do tipo A4 (do OID 2.16.76.1.2.4.1 ao OID
     * 2.16.76.1.2.4.100), conforme definido em DOC-ICP-04.
     */

    try {
        byte[] val1 = certificate.getExtensionValue("2.5.29.32");
        ASN1InputStream ans1InputStream = new ASN1InputStream(new ByteArrayInputStream(val1));
        DERObject derObject = ans1InputStream.readObject();
        ans1InputStream.close();
        DEROctetString derOctetString = (DEROctetString) derObject;
        byte[] val2 = derOctetString.getOctets();
        ASN1InputStream asn1InputStream2 = new ASN1InputStream(new ByteArrayInputStream(val2));
        DERObject derObject2 = asn1InputStream2.readObject();
        asn1InputStream2.close();
        DERSequence derSequence = (DERSequence) derObject2;
        DERSequence derObject3 = (DERSequence) derSequence.getObjectAt(0).getDERObject();
        DERObjectIdentifier objectIdentifier = (DERObjectIdentifier) derObject3.getObjectAt(0);
        String identificador = objectIdentifier.toString();

        if (!(identificador.startsWith("2.16.76.1.2.1.") || identificador.startsWith("2.16.76.1.2.2.")
                || identificador.startsWith("2.16.76.1.2.3.") || identificador.startsWith("2.16.76.1.2.4."))) {
            throw new SignerException("O OID no corresponde a uma Poltica de Certificado.");
        }

        int sufixo = Integer.parseInt(identificador.substring(identificador.lastIndexOf(".") + 1));
        if (sufixo < 1 || sufixo > 100) {
            throw new SignerException("O certificado deve ser do tipo A1, A2, A3 ou A4.");
        }

    } catch (Throwable error) {
        throw new SignerException(
                "A assinaturas digital deve ser criada com chave privada associada ao certificado ICP-Brasil tipo A1, A2, A3 ou A4",
                error);
    }
}

From source file:br.gov.jfrj.siga.cd.AssinaturaDigital.java

License:Open Source License

/**
 * Read an existing PKCS#7 object from a DER encoded byte array
 *//* w w w .  j  a  v  a2s . c o  m*/
protected static org.bouncycastle.asn1.pkcs.SignedData pkcs7SignedData(byte[] in) {
    ASN1InputStream din = new ASN1InputStream(new ByteArrayInputStream(in));

    //
    // Basic checks to make sure it's a PKCS#7 SignedData Object
    //
    ASN1Primitive pkcs;

    try {
        pkcs = din.readObject();
    } catch (IOException e) {
        throw new SecurityException("can't decode PKCS7SignedData object");
    } finally {
        try {
            din.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    if (!(pkcs instanceof ASN1Sequence)) {
        throw new SecurityException("Not a valid PKCS#7 object - not a sequence");
    }

    ContentInfo content = ContentInfo.getInstance(pkcs);

    org.bouncycastle.asn1.pkcs.SignedData data = org.bouncycastle.asn1.pkcs.SignedData
            .getInstance(content.getContent());

    return data;
}