Example usage for org.bouncycastle.asn1 DEROctetString getOctets

List of usage examples for org.bouncycastle.asn1 DEROctetString getOctets

Introduction

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

Prototype

public byte[] getOctets() 

Source Link

Document

Return the content of the OCTET STRING as a byte array.

Usage

From source file:be.fedict.trust.ocsp.OcspTrustLinker.java

License:Open Source License

private URI getAccessLocation(X509Certificate certificate, ASN1ObjectIdentifier accessMethod)
        throws IOException, URISyntaxException {
    byte[] authInfoAccessExtensionValue = certificate.getExtensionValue(Extension.authorityInfoAccess.getId());
    if (null == authInfoAccessExtensionValue) {
        return null;
    }/*w w w . j  ava2  s.c  om*/
    AuthorityInformationAccess authorityInformationAccess;
    DEROctetString oct = (DEROctetString) (new ASN1InputStream(
            new ByteArrayInputStream(authInfoAccessExtensionValue)).readObject());
    authorityInformationAccess = AuthorityInformationAccess
            .getInstance(new ASN1InputStream(oct.getOctets()).readObject());
    AccessDescription[] accessDescriptions = authorityInformationAccess.getAccessDescriptions();
    for (AccessDescription accessDescription : accessDescriptions) {
        LOG.debug("access method: " + accessDescription.getAccessMethod());
        boolean correctAccessMethod = accessDescription.getAccessMethod().equals(accessMethod);
        if (!correctAccessMethod) {
            continue;
        }
        GeneralName gn = accessDescription.getAccessLocation();
        if (gn.getTagNo() != GeneralName.uniformResourceIdentifier) {
            LOG.debug("not a uniform resource identifier");
            continue;
        }
        DERIA5String str = DERIA5String.getInstance(gn.getName());
        String accessLocation = str.getString();
        LOG.debug("access location: " + accessLocation);
        URI uri = toURI(accessLocation);
        LOG.debug("access location URI: " + uri);
        return uri;
    }
    return null;
}

From source file:be.fedict.trust.service.bean.HarvesterMDB.java

License:Open Source License

private BigInteger getCrlNumber(X509CRL crl) {
    byte[] crlNumberExtensionValue = crl.getExtensionValue("2.5.29.20");
    if (null == crlNumberExtensionValue) {
        return null;
    }/*from w ww .  j  a  v  a  2  s . c  o  m*/
    try {
        DEROctetString octetString = (DEROctetString) (new ASN1InputStream(
                new ByteArrayInputStream(crlNumberExtensionValue)).readObject());
        byte[] octets = octetString.getOctets();
        DERInteger integer = (DERInteger) new ASN1InputStream(octets).readObject();
        return integer.getPositiveValue();
    } catch (IOException e) {
        throw new RuntimeException("IO error: " + e.getMessage(), e);
    }
}

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

License:Open Source License

public static byte[] extractSignature(byte[] sign) throws Exception {
    byte[] ret = null;
    ASN1InputStream is = new ASN1InputStream(new ByteArrayInputStream(sign));
    ASN1Primitive topLevel = is.readObject();
    LOG.debug("top level:" + topLevel.getClass().getName());

    if (topLevel instanceof org.bouncycastle.asn1.DLSequence) {
        DLSequence topLevelDLS = (DLSequence) topLevel;
        if (topLevelDLS.size() == 2) {
            ASN1Encodable level1 = topLevelDLS.getObjectAt(1);
            LOG.debug("level1:" + level1.getClass().getName());
            if (level1 instanceof org.bouncycastle.asn1.DERTaggedObject) {
                DERTaggedObject level1TO = (DERTaggedObject) level1;
                ASN1Primitive level2 = level1TO.getObject();
                LOG.debug("level2:" + level2.getClass().getName());
                if (level2 instanceof org.bouncycastle.asn1.DERSequence) {
                    DERSequence level2DS = (DERSequence) level2;
                    LOG.debug("level2 len:" + level2DS.size());
                    ASN1Encodable level3_4 = level2DS.getObjectAt(level2DS.size() - 1);
                    LOG.debug("level3_4:" + level3_4.getClass().getName());
                    if (level3_4 instanceof org.bouncycastle.asn1.DERSet) {
                        DERSet level3_4DS = (DERSet) level3_4;
                        ASN1Encodable level3_4_0 = level3_4DS.getObjectAt(0);
                        LOG.debug("level3_4_0:" + level3_4_0.getClass().getName());
                        if (level3_4_0 instanceof org.bouncycastle.asn1.DERSequence) {
                            DERSequence level3_4_0DS = (DERSequence) level3_4_0;
                            LOG.debug("level3_4_0DS len:" + level3_4_0DS.size());
                            ASN1Encodable signature = level3_4_0DS.getObjectAt(level3_4_0DS.size() - 1);
                            LOG.debug("signature:" + signature.getClass().getName());
                            if (signature instanceof org.bouncycastle.asn1.DEROctetString) {
                                DEROctetString signDOS = (DEROctetString) signature;
                                ret = signDOS.getOctets();
                            }//from   www  . j a v  a2  s .c o m
                        } else {
                            throw new Exception("DER enconding error");
                        }

                    } else {
                        throw new Exception("DER enconding error");
                    }
                } else {
                    throw new Exception("DER enconding error");
                }

            } else {
                throw new Exception("DER enconding error");
            }
        } else {
            throw new Exception("DER enconding error");
        }

    } else {
        throw new Exception("DER enconding error");
    }

    return ret;
}

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

License:Open Source License

public static Map<String, String> createSanMap(byte[] extensionValue, int index) {
    Map<String, String> ret = new HashMap<String, String>();
    try {// ww  w  . j a  v a 2s . c  om
        if (extensionValue == null) {
            return null;
        }
        ASN1InputStream oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(extensionValue));
        ASN1Primitive derObjCP = oAsnInStream.readObject();
        DLSequence derSeq = (DLSequence) derObjCP;
        // int seqLen = derSeq.size();
        ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) derSeq.getObjectAt(0);
        String sanOid = oid.getId();

        DERTaggedObject derTO = (DERTaggedObject) derSeq.getObjectAt(1);
        // int tag = derTO.getTagNo();
        ASN1Primitive derObjA = derTO.getObject();

        DERTaggedObject derTO2 = (DERTaggedObject) derObjA;
        // int tag2 = derTO2.getTagNo();
        ASN1Primitive derObjB = derTO2.getObject();
        String contentStr = "";
        if (derObjB instanceof DEROctetString) {
            DEROctetString derOCStr = (DEROctetString) derObjB;
            contentStr = new String(derOCStr.getOctets(), "UTF8");
        } else if (derObjB instanceof DERPrintableString) {
            DERPrintableString derOCStr = (DERPrintableString) derObjB;
            contentStr = new String(derOCStr.getOctets(), "UTF8");
        } else {
            System.err.println("FORMAT OF SAN: UNRECOGNIZED -> " + derObjB.getClass().getCanonicalName());
        }
        LOG.debug(sanOid + " -> " + contentStr);

        String value = "";
        String name = "";

        if (sanOid.compareTo(PF_PF_ID) == 0 || sanOid.compareTo(PJ_PF_ID) == 0) {
            value = contentStr.substring(BIRTH_DATE_INI, BIRTH_DATE_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.BIRTH_DATE_D, index);
                ret.put(name, value);
            }

            value = contentStr.substring(CPF_INI, CPF_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.CPF_D, index);
                ret.put(name, value);
            }

            value = contentStr.substring(PIS_INI, PIS_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.PIS_D, index);
                ret.put(name, value);
            }

            value = contentStr.substring(RG_INI, RG_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.RG_D, index);
                ret.put(name, value);
            }

            int rgOrgUfLen = RG_ORG_UF_LEN > contentStr.length() ? contentStr.length() : RG_ORG_UF_LEN;
            if (rgOrgUfLen > RG_ORG_UF_INI) {
                value = contentStr.substring(RG_ORG_UF_INI, rgOrgUfLen);

                String rgOrg = value.substring(0, value.length() - 2);
                String rgUf = value.substring(value.length() - 2, value.length());
                if (isValidValue(rgOrg)) {
                    name = String.format(CertConstants.RG_ORG_D, index);
                    ret.put(name, rgOrg);
                }
                if (isValidValue(rgUf)) {
                    name = String.format(CertConstants.RG_UF_D, index);
                    ret.put(name, rgUf);
                }
            }

        } else if (sanOid.compareTo(PERSON_NAME_OID) == 0) {
            value = contentStr;
            if (isValidValue(value)) {
                name = String.format(CertConstants.PERSON_NAME_D, index);
                ret.put(name, value);
            }

        } else if (sanOid.compareTo(CNPJ_OID) == 0) {
            name = String.format(CERT_TYPE_FMT, index);
            ret.put(name, ICP_BRASIL_PJ);
            value = contentStr;
            if (isValidValue(value)) {
                name = String.format(CertConstants.CNPJ_D, index);
                ret.put(name, value);
            }

        } else if (sanOid.compareTo(ELEITOR_OID) == 0) {
            name = String.format(CERT_TYPE_FMT, index);
            ret.put(name, ICP_BRASIL_PF);
            value = contentStr.substring(ELEITOR_INI, ELEITOR_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.ELEITOR_D, index);
                ret.put(name, value);
            }

            int zonaLen = ZONA_LEN > contentStr.length() ? contentStr.length() : ZONA_LEN;
            if (zonaLen > ZONA_LEN) {

                value = contentStr.substring(ZONA_INI, zonaLen);
                if (isValidValue(value)) {
                    name = String.format(CertConstants.ZONA_D, index);
                    ret.put(name, value);
                }
            }

            int secaoLen = SECAO_LEN > contentStr.length() ? contentStr.length() : SECAO_LEN;
            if (secaoLen > SECAO_LEN) {
                value = contentStr.substring(SECAO_INI, SECAO_LEN);
                if (isValidValue(value)) {
                    name = String.format(CertConstants.SECAO_D, index);
                    ret.put(name, value);
                }
            }

        } else if (sanOid.compareTo(PF_PF_INSS_OID) == 0 || sanOid.compareTo(PJ_PF_INSS_OID) == 0) {
            value = contentStr.substring(INSS_INI, INSS_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.INSS_D, index);
                ret.put(name, value);
            }

        } else if (sanOid.compareTo(OAB_OID) == 0) {
            value = contentStr.substring(OAB_REG_INI, OAB_REG_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.OAB_REG_D, index);
                ret.put(name, value);
            }
            value = contentStr.substring(OAB_UF_INI, OAB_UF_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.OAB_UF_D, index);
                ret.put(name, value);
            }

        } else if (sanOid.startsWith(PROFESSIONAL_OID)) {
            value = contentStr;
            if (isValidValue(value)) {
                name = String.format(CertConstants.PROFESSIONAL_D, index);
                ret.put(name, value);
            }
        } else if (sanOid.startsWith(UPN)) {
            value = contentStr;
            if (isValidValue(value)) {
                name = String.format(CertConstants.UPN_D, index);
                ret.put(name, value);
            }
        } else {
            System.err.println("SAN:OTHER NAME NOT RECOGNIZED");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return ret;
}

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  www . j av  a  2s . 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 {//from   w w  w. j ava2  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;
    }/* w  w w.j a v  a2 s .  com*/
    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 ww .ja  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:br.gov.frameworkdemoiselle.certificate.extension.BasicCertificate.java

License:Open Source License

/**
 * Returns the AuthorityKeyIdentifier extension value on String format.<br>
 * Otherwise, returns <b>null</b>.<br>
 *
 * @return String//w  w  w.j  a v  a2  s .  c o  m
 * @throws IOException
 */
public String getAuthorityKeyIdentifier() throws IOException {
    // TODO - Precisa validar este metodo com a RFC
    DERSequence seq = (DERSequence) getExtensionValue(X509Extensions.AuthorityKeyIdentifier.getId());
    if (seq == null || seq.size() == 0) {
        return null;
    }
    DERTaggedObject tag = (DERTaggedObject) seq.getObjectAt(0);
    DEROctetString oct = (DEROctetString) DEROctetString.getInstance(tag);

    return toString(oct.getOctets());
}

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

License:Open Source License

/**
 * Returns the SubjectKeyIdentifier extension value on String format.<br>
 * Otherwise, returns <b>null</b>.<br>
 *
 * @return String/* w  w w.ja  va2  s .c  o m*/
 * @throws java.io.IOException
 */
public String getSubjectKeyIdentifier() throws IOException {
    // TODO - Precisa validar este metodo com a RFC
    DEROctetString oct = (DEROctetString) getExtensionValue(X509Extensions.SubjectKeyIdentifier.getId());
    if (oct == null) {
        return null;
    }

    return toString(oct.getOctets());
}