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:de.tsenger.animamea.iso7816.DO8E.java

License:Open Source License

public void fromByteArray(byte[] encodedData) {
    ASN1InputStream asn1in = new ASN1InputStream(encodedData);
    try {/* ww w. j av  a 2s  .  c  o  m*/
        to = (DERTaggedObject) asn1in.readObject();
        asn1in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    DEROctetString ocs = (DEROctetString) to.getObject();
    data = ocs.getOctets();
}

From source file:de.tsenger.animamea.iso7816.DO97.java

License:Open Source License

public void fromByteArray(byte[] encodedData) {
    ASN1InputStream asn1in = new ASN1InputStream(encodedData);
    try {/*from w w w .j  a v  a 2s .  c  o  m*/
        to = (DERTaggedObject) asn1in.readObject();
        asn1in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    DEROctetString ocs = (DEROctetString) to.getObject();
    data = ocs.getOctets();

}

From source file:de.tsenger.animamea.iso7816.DO99.java

License:Open Source License

public void fromByteArray(byte[] encodedData) {
    ASN1InputStream asn1in = new ASN1InputStream(encodedData);
    try {/*  w  w w.  j av a  2s .co  m*/
        to = (DERTaggedObject) asn1in.readObject();
        asn1in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    DEROctetString ocs = (DEROctetString) to.getObject();
    data = ocs.getOctets();

}

From source file:de.tsenger.animamea.Operator.java

License:Open Source License

private static SecurityInfos decodeEFCardSecurity(byte[] data) throws IOException, CertificateException,
        NoSuchProviderException, CMSException, OperatorCreationException {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    ASN1Sequence asnSeq = (ASN1Sequence) ASN1Sequence.fromByteArray(data);
    ContentInfo contentInfo = ContentInfo.getInstance(asnSeq);
    DERSequence derSeq = (DERSequence) contentInfo.getContent();

    System.out.println("ContentType: " + contentInfo.getContentType().toString());
    SignedData cardSecurity = SignedData.getInstance(derSeq);

    //Get SecurityInfos
    ContentInfo encapContentInfo = cardSecurity.getEncapContentInfo();
    DEROctetString octString = (DEROctetString) encapContentInfo.getContent();
    SecurityInfos si = new SecurityInfos();
    si.decode(octString.getOctets());

    return si;/*from   ww  w.  ja  va 2  s . co m*/
}

From source file:de.tsenger.sandbox.CardSecurityParser.java

License:Open Source License

/**
 * @param args/*from w  ww .j a  v a  2  s  . co  m*/
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    byte[] efcsBytes = readBinaryFile("/home/tsenger/Desktop/EFCardSecurity.bin");
    ASN1Sequence asnSeq = (ASN1Sequence) ASN1Sequence.fromByteArray(efcsBytes);
    ContentInfo contentInfo = ContentInfo.getInstance(asnSeq);
    System.out.println(contentInfo.getContentType());
    DERSequence derSeq = (DERSequence) contentInfo.getContent();
    System.out.println(HexString.bufferToHex(derSeq.getEncoded(null)));
    SignedData signedData = SignedData.getInstance(derSeq);
    System.out.println("CMSVersion: " + signedData.getVersion().getValue().intValue());
    ContentInfo contentInfo2 = signedData.getEncapContentInfo();
    System.out.println(contentInfo2.getContentType());
    DEROctetString octString = (DEROctetString) contentInfo2.getContent();
    System.out.println("OctetString:\n" + HexString.bufferToHex(octString.getEncoded(null)));
    System.out.println("OctetString:\n" + HexString.bufferToHex(octString.getOctets()));

    SecurityInfos si = new SecurityInfos();
    si.decode(octString.getOctets());
    System.out.println(si);

    byte[] parameter = si.getChipAuthenticationPublicKeyInfoList().get(0).getPublicKey().getPublicKey();
    System.out.println(HexString.bufferToHex(parameter));
    System.out.println("Key Referenz: " + si.getChipAuthenticationPublicKeyInfoList().get(0).getKeyId());
    System.out.println("CA OID: "
            + si.getChipAuthenticationPublicKeyInfoList().get(0).getPublicKey().getAlgorithm().getAlgorithm());

}

From source file:demo.sts.provider.cert.CRLVerifier.java

License:Apache License

/**
 * Extracts all CRL distribution point URLs from the
 * "CRL Distribution Point" extension in a X.509 certificate. If CRL
 * distribution point extension is unavailable, returns an empty list.
 *//*  w ww  .  ja v  a  2 s  .co  m*/
public static List<String> getCrlDistributionPoints(X509Certificate cert)
        throws CertificateParsingException, IOException {
    byte[] crldpExt = cert.getExtensionValue(X509Extensions.CRLDistributionPoints.getId());
    if (crldpExt == null) {
        return new ArrayList<String>();
    }
    ASN1InputStream oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(crldpExt));
    DERObject derObjCrlDP = oAsnInStream.readObject();
    DEROctetString dosCrlDP = (DEROctetString) derObjCrlDP;
    byte[] crldpExtOctets = dosCrlDP.getOctets();
    ASN1InputStream oAsnInStream2 = new ASN1InputStream(new ByteArrayInputStream(crldpExtOctets));
    DERObject 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:ec.gov.informatica.firmadigital.FirmaDigital.java

License:Open Source License

/**
 * <code> crearDatosUsuario </code>
 * //ww w .  jav  a  2s  . c  o m
 * @param signingCert
 * @return Esta funcion llena los datos del usuario encontrados en el
 *         certificado
 */
public DatosUsuario crearDatosUsuario(X509Certificate signingCert) {
    // System.out.println("Libreria: Esta en crearDatosUsuario : ");

    // System.out.println(" Antigua Infra probando Datos User CEDULA: " +
    // signingCert.getExtensionValue("1.2.3.4.1"));
    // System.out.println(" Nueva Infra probando Datos User CEDULA: " +
    // (signingCert.getExtensionValue("1.3.6.1.4.1.37947.3.1")));

    /* **************************************************************************************************************
     * No existe la posibilidad de que se realice una firma si el
     * certificado no tiene el campo de cedula, por esta razon el campo
     * cedula ser el validador del tipo de infraestructura que fue creado
     * el certificado
     * ********************************************************
     * ******************************************************
     */
    DatosUsuario datosUsuario = new DatosUsuario();

    if (signingCert.getExtensionValue("1.2.3.4.1") != null) { // esta sobre
        // la
        // antigua
        // infraestructura
        System.out.println("- Certificado generado con OIDS de antigua infraestructura BCE ");
        datosUsuario.setCedula(new String(signingCert.getExtensionValue("1.2.3.4.1")).trim());

        if (signingCert.getExtensionValue("1.2.3.4.2") != null) {
            datosUsuario.setNombre(new String(signingCert.getExtensionValue("1.2.3.4.2")).trim());
        }
        if (signingCert.getExtensionValue("1.2.3.4.3") != null) {
            String txtApellido = new String(signingCert.getExtensionValue("1.2.3.4.3")).trim();
            if (signingCert.getExtensionValue("1.2.3.4.4") != null) {
                txtApellido = txtApellido + " " + new String(signingCert.getExtensionValue("1.2.3.4.4")).trim();
            }
            datosUsuario.setApellido(txtApellido);
        }
        if (signingCert.getExtensionValue("1.2.3.4.6") != null) {
            datosUsuario.setInstitucion(new String(signingCert.getExtensionValue("1.2.3.4.6")).trim());
        }
        if (signingCert.getExtensionValue("1.2.3.4.5") != null) {
            datosUsuario.setCargo(new String(signingCert.getExtensionValue("1.2.3.4.5")).trim());
        }

        if (signingCert.getSerialNumber() != null) {
            datosUsuario.setSerial(signingCert.getSerialNumber().toString());
        }
    } else if (signingCert.getExtensionValue("1.3.6.1.4.1.37947.3.1") != null) { // esta
        // sobre
        // la
        // nueva
        // infraestructura
        System.out.println("- Certificado generado con OIDS de nueva infraestructura BCE");
        datosUsuario.setCedula(new String(signingCert.getExtensionValue("1.3.6.1.4.1.37947.3.1")).trim());

        if (signingCert.getExtensionValue("1.3.6.1.4.1.37947.3.2") != null) {
            datosUsuario.setNombre(new String(signingCert.getExtensionValue("1.3.6.1.4.1.37947.3.2")).trim());
        }
        if (signingCert.getExtensionValue("1.3.6.1.4.1.37947.3.3") != null) {
            String txtApellido = new String(signingCert.getExtensionValue("1.3.6.1.4.1.37947.3.3")).trim();
            if (signingCert.getExtensionValue("1.3.6.1.4.1.37947.3.4") != null) {
                txtApellido = txtApellido + " "
                        + new String(signingCert.getExtensionValue("1.3.6.1.4.1.37947.3.4")).trim();
            }
            datosUsuario.setApellido(txtApellido);
        }
        if (signingCert.getExtensionValue("1.3.6.1.4.1.37947.3.6") != null) {
            datosUsuario
                    .setInstitucion(new String(signingCert.getExtensionValue("1.3.6.1.4.1.37947.3.6")).trim());
        }
        if (signingCert.getExtensionValue("1.3.6.1.4.1.37947.3.5") != null) {
            datosUsuario.setCargo(new String(signingCert.getExtensionValue("1.3.6.1.4.1.37947.3.5")).trim());
        }

        if (signingCert.getSerialNumber() != null) {
            datosUsuario.setSerial(signingCert.getSerialNumber().toString());
        }
    } else {
        System.out.println("- Certificado generado con OIDS de infraestructura securityData");
        datosUsuario.setCedula(new String(signingCert.getExtensionValue("1.3.6.1.4.1.37746.3.1")).trim());

        if (signingCert.getExtensionValue("1.3.6.1.4.1.37746.3.2") != null) {
            datosUsuario.setNombre(new String(signingCert.getExtensionValue("1.3.6.1.4.1.37746.3.2")).trim());
        }
        if (signingCert.getExtensionValue("1.3.6.1.4.1.37746.3.3") != null) {
            String txtApellido = new String(signingCert.getExtensionValue("1.3.6.1.4.1.37746.3.3")).trim();
            if (signingCert.getExtensionValue("1.3.6.1.4.1.37746.3.4") != null) {
                txtApellido = txtApellido + " "
                        + new String(signingCert.getExtensionValue("1.3.6.1.4.1.37746.3.4")).trim();
            }
            datosUsuario.setApellido(txtApellido);
        }
        if (signingCert.getExtensionValue("1.3.6.1.4.1.37746.3.6") != null) {
            datosUsuario
                    .setInstitucion(new String(signingCert.getExtensionValue("1.3.6.1.4.1.37746.3.6")).trim());
        }
        if (signingCert.getExtensionValue("1.3.6.1.4.1.37746.3.5") != null) {
            datosUsuario.setCargo(new String(signingCert.getExtensionValue("1.3.6.1.4.1.37746.3.5")).trim());
        }

        if (signingCert.getSerialNumber() != null) {
            datosUsuario.setSerial(signingCert.getSerialNumber().toString());
        }

    }

    if (signingCert.getExtensionValue("2.5.29.31") != null) {

        // Nuevo codigo validacion CRL
        byte[] val1 = signingCert.getExtensionValue("2.5.29.31");
        if (val1 == null) // esta parte se puede omitir o se lo puede dejar
                          // si se quiere tener un mayor control
        {
            if (signingCert.getSubjectDN().getName().equals(signingCert.getIssuerDN().getName())) {
                System.out.println(
                        "El certificado es un certificado raiz: " + signingCert.getSubjectDN().getName());
            } else {
                System.out.println("El certificado NO tiene punto de distribucin de CRL : "
                        + signingCert.getSubjectDN().getName());
            }
            // return Collections.emptyList();
        } else {
            // Obtiene dentro del certificado del token la lista de
            // distribucin CRL usada para consultar el LDAP del BCE.
            try {
                ASN1InputStream oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(val1));
                DERObject derObj = oAsnInStream.readObject();
                DEROctetString dos = (DEROctetString) derObj;
                byte[] val2 = dos.getOctets();
                ASN1InputStream oAsnInStream2 = new ASN1InputStream(new ByteArrayInputStream(val2));
                DERObject derObj2 = oAsnInStream2.readObject();
                List<String> urls = getDERValue(derObj2);

                for (int j = 0; j < urls.size(); j++) {
                    datosUsuario.setCrl(urls.get(7));
                }
                // datosUsuario.setCrl( new String( distrPoint.substring(
                // distrPoint.indexOf("U")+8,
                // distrPoint.indexOf("ldap") - 8 ) ).trim() );
                // //distrPoint.substring( distrPoint.indexOf("U")+8,
                // distrPoint.indexOf("U") + 12 )
                System.out.println("- Informacion contenida en el Certificado : > " + urls + "\n");// .println(urls);
            } catch (Exception e) {
                System.out.println("Error: " + e.getMessage());
                e.printStackTrace();
            }
        } // fin else
          // Fin validacion CRL

        // System.out.println("Dist_point:" + distrPoint );
        // OJO : Esta validacion puede fallar si la lista de distribucion
        // crece a dos digitos . REVISAR
        // datosUsuario.setCrl( new String( distrPoint.substring(
        // distrPoint.indexOf("U")+8, distrPoint.indexOf("U")
        // + 12 ) ).trim() );
    }
    return datosUsuario;
}

From source file:ec.gov.informatica.firmadigital.FirmaDigital.java

License:Open Source License

/**
 * para parsear el objeto y te devuelve el listado con las urls de los
 * puntos de distribucin/*from   w  w w.ja  v a2s.c om*/
 * 
 * @param derObj
 * @return
 */
@SuppressWarnings("unchecked")
private List<String> getDERValue(DERObject derObj) {
    if (derObj instanceof DERSequence) {
        List<String> list = new LinkedList<String>();
        DERSequence seq = (DERSequence) derObj;
        Enumeration enumeracion = seq.getObjects();
        while (enumeracion.hasMoreElements()) {
            DERObject nestedObj = (DERObject) enumeracion.nextElement();
            List<String> appo = getDERValue(nestedObj);
            if (appo != null) {
                list.addAll(appo);
            }
        }
        return list;
    } else if (derObj instanceof DERTaggedObject) {
        DERTaggedObject derTag = (DERTaggedObject) derObj;
        if ((derTag.isExplicit() && !derTag.isEmpty()) || derTag.getObject() instanceof DERSequence) {
            DERObject nestedObj = derTag.getObject();
            List<String> ret = getDERValue(nestedObj);
            return ret;
        } else {
            DEROctetString derOct = (DEROctetString) derTag.getObject();
            String val = new String(derOct.getOctets());
            List<String> ret = new LinkedList<String>();
            ret.add(val);
            return ret;
        }
    } else if (derObj instanceof DERSet) {
        Enumeration enumSet = ((DERSet) derObj).getObjects();
        List<String> list = new LinkedList<String>();
        while (enumSet.hasMoreElements()) {
            DERObject nestedObj = (DERObject) enumSet.nextElement();
            List<String> appo = getDERValue(nestedObj);
            if (appo != null) {
                list.addAll(appo);
            }
        }
        return list;
    } else if (derObj instanceof DERObjectIdentifier) {
        DERObjectIdentifier derId = (DERObjectIdentifier) derObj;
        List<String> list = new LinkedList<String>();
        list.add(derId.getId());
        return list;
    } else if (derObj instanceof DERPrintableString) {
        // hemos localizado un par id-valor
        String valor = ((DERPrintableString) derObj).getString();
        List<String> list = new LinkedList<String>();
        list.add(valor);
        return list;
    } else {
        System.out.println("tipo de dato en ASN1 al recuperar las crls no es reconocido : " + derObj);
    }
    return null;
}

From source file:ec.gov.informatica.firmadigital.signature.BouncyCastleSignatureProcessor.java

License:Open Source License

/**
 *  <code> crearDatosUsuario </code>
 * @param signingCert/* w w  w .  ja  v  a 2 s .c om*/
 * @return
 * Esta funcion llena los datos del usuario encontrados en el certificado
 */
private DatosUsuario crearDatosUsuario(X509Certificate signingCert) {
    //        depuracionActual.info("Libreria: Esta en crearDatosUsuario : ");
    DatosUsuario datosUsuario = new DatosUsuario();
    if (signingCert.getExtensionValue("1.2.3.4.1") != null) {
        datosUsuario.setCedula(new String(signingCert.getExtensionValue("1.2.3.4.1")).trim());
    }
    if (signingCert.getExtensionValue("1.2.3.4.2") != null) {
        datosUsuario.setNombre(new String(signingCert.getExtensionValue("1.2.3.4.2")).trim());
    }
    if (signingCert.getExtensionValue("1.2.3.4.3") != null) {
        String txtNombre = new String(signingCert.getExtensionValue("1.2.3.4.3")).trim();
        if (signingCert.getExtensionValue("1.2.3.4.4") != null)
            txtNombre = txtNombre + " " + new String(signingCert.getExtensionValue("1.2.3.4.4")).trim();
        datosUsuario.setApellido(txtNombre);
    }
    if (signingCert.getExtensionValue("1.2.3.4.6") != null) {
        datosUsuario.setInstitucion(new String(signingCert.getExtensionValue("1.2.3.4.6")).trim());
    }
    if (signingCert.getExtensionValue("1.2.3.4.5") != null) {
        datosUsuario.setCargo(new String(signingCert.getExtensionValue("1.2.3.4.5")).trim());
    }

    if (signingCert.getSerialNumber() != null) {
        datosUsuario.setSerial(signingCert.getSerialNumber().toString());
    }

    if (signingCert.getExtensionValue("2.5.29.31") != null) {

        //Estas declaraciones buscan un atributo del Certificado (lista CRL) que permite buscar en revocados.
        byte[] val1 = signingCert.getExtensionValue("2.5.29.31");
        if (val1 == null) {
            if (signingCert.getSubjectDN().getName().equals(signingCert.getIssuerDN().getName())) {
                System.out.println(
                        "El certificado es un certificado raiz: " + signingCert.getSubjectDN().getName());
            } else {
                System.out.println("El certificado NO tiene punto de distribucin de CRL : "
                        + signingCert.getSubjectDN().getName());
            }
            //return Collections.emptyList();
        } else {
            //esta es la parte que deberas aumentar en si
            try {
                ASN1InputStream oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(val1));
                DERObject derObj = oAsnInStream.readObject();
                DEROctetString dos = (DEROctetString) derObj;
                byte[] val2 = dos.getOctets();
                ASN1InputStream oAsnInStream2 = new ASN1InputStream(new ByteArrayInputStream(val2));
                DERObject derObj2 = oAsnInStream2.readObject();
                List<String> urls = getDERValue(derObj2);

                for (int j = 0; j < urls.size(); j++) {
                    datosUsuario.setCrl(urls.get(7));
                }
                //                datosUsuario.setCrl( new String(   distrPoint.substring( distrPoint.indexOf("U")+8, distrPoint.indexOf("ldap") - 8 )     ).trim() );     //distrPoint.substring( distrPoint.indexOf("U")+8, distrPoint.indexOf("U") + 12 )
                System.out.println(urls);// .println(urls);
            } catch (Exception e) {
                System.out.println("Error: " + e.getMessage());
                e.printStackTrace();
            }
        } //fin else 

    }

    return datosUsuario;
}

From source file:ec.gov.informatica.firmadigital.signature.BouncyCastleSignatureProcessor.java

License:Open Source License

/**
* para parsear el objeto y te devuelve el listado con las urls de los puntos de distribucin
        //from w w  w  . j a va 2s .  com
* @param derObj
* @return
*/

@SuppressWarnings("unchecked")
private List<String> getDERValue(DERObject derObj) {
    if (derObj instanceof DERSequence) {
        List<String> list = new LinkedList<String>();
        DERSequence seq = (DERSequence) derObj;
        Enumeration enumeracion = seq.getObjects();
        while (enumeracion.hasMoreElements()) {
            DERObject nestedObj = (DERObject) enumeracion.nextElement();
            List<String> appo = getDERValue(nestedObj);
            if (appo != null) {
                list.addAll(appo);
            }
        }
        return list;
    } else if (derObj instanceof DERTaggedObject) {
        DERTaggedObject derTag = (DERTaggedObject) derObj;
        if ((derTag.isExplicit() && !derTag.isEmpty()) || derTag.getObject() instanceof DERSequence) {
            DERObject nestedObj = derTag.getObject();
            List<String> ret = getDERValue(nestedObj);
            return ret;
        } else {
            DEROctetString derOct = (DEROctetString) derTag.getObject();
            String val = new String(derOct.getOctets());
            List<String> ret = new LinkedList<String>();
            ret.add(val);
            return ret;
        }
    } else if (derObj instanceof DERSet) {
        Enumeration enumSet = ((DERSet) derObj).getObjects();
        List<String> list = new LinkedList<String>();
        while (enumSet.hasMoreElements()) {
            DERObject nestedObj = (DERObject) enumSet.nextElement();
            List<String> appo = getDERValue(nestedObj);
            if (appo != null) {
                list.addAll(appo);
            }
        }
        return list;
    } else if (derObj instanceof DERObjectIdentifier) {
        DERObjectIdentifier derId = (DERObjectIdentifier) derObj;
        List<String> list = new LinkedList<String>();
        list.add(derId.getId());
        return list;
    } else if (derObj instanceof DERPrintableString) {
        // hemos localizado un par id-valor
        String valor = ((DERPrintableString) derObj).getString();
        List<String> list = new LinkedList<String>();
        list.add(valor);
        return list;
    } else {
        System.out.println("tipo de dato en ASN1 al recuperar las crls no es reconocido : " + derObj);
    }
    return null;
}