Example usage for org.bouncycastle.asn1 DERTaggedObject getEncoded

List of usage examples for org.bouncycastle.asn1 DERTaggedObject getEncoded

Introduction

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

Prototype

public byte[] getEncoded() throws IOException 

Source Link

Document

Return the default BER or DER encoding for this object.

Usage

From source file:com.hierynomus.spnego.NegTokenTarg.java

License:Apache License

protected void writeGss(Buffer<?> buffer, ASN1EncodableVector negToken) throws IOException {
    DERTaggedObject negotiationToken = new DERTaggedObject(true, 0x01, new DERSequence(negToken));

    buffer.putRawBytes(negotiationToken.getEncoded());
}

From source file:com.viettel.hqmc.DAO.FilesDAO.java

private static String getOcspUrl(X509Certificate certificate) throws Exception {
    byte[] octetBytes = certificate.getExtensionValue(X509Extension.authorityInfoAccess.getId());
    DLSequence dlSequence = null;/*  www .jav  a 2s.c  o m*/
    ASN1Encodable asn1Encodable = null;
    try {
        ASN1Primitive fromExtensionValue = X509ExtensionUtil.fromExtensionValue(octetBytes);
        if (!(fromExtensionValue instanceof DLSequence)) {
            return null;
        }
        dlSequence = (DLSequence) fromExtensionValue;
        for (int i = 0; i < dlSequence.size(); i++) {
            asn1Encodable = dlSequence.getObjectAt(i);
            if (!(asn1Encodable instanceof DLSequence)) {
                break;
            }
        }
        if (!(asn1Encodable instanceof DLSequence)) {
            return null;
        }
        dlSequence = (DLSequence) asn1Encodable;
        for (int i = 0; i < dlSequence.size(); i++) {
            asn1Encodable = dlSequence.getObjectAt(i);
            if (asn1Encodable instanceof DERTaggedObject) {
                break;
            }
        }
        if (!(asn1Encodable instanceof DERTaggedObject)) {
            return null;
        }
        DERTaggedObject derTaggedObject = (DERTaggedObject) asn1Encodable;
        byte[] encoded = derTaggedObject.getEncoded();
        if (derTaggedObject.getTagNo() == 6) {
            int len = encoded[1];
            return new String(encoded, 2, len);
        }
    } catch (IOException ex) {
        LogUtil.addLog(ex);//binhnt sonar a160901
    }
    return null;
}

From source file:io.netty.example.ocsp.OcspUtils.java

License:Apache License

/**
 * Returns the OCSP responder {@link URI} or {@code null} if it doesn't have one.
 *//*  w  ww  .  j av a 2s  .c o m*/
public static URI ocspUri(X509Certificate certificate) throws IOException {
    byte[] value = certificate.getExtensionValue(Extension.authorityInfoAccess.getId());
    if (value == null) {
        return null;
    }

    ASN1Primitive authorityInfoAccess = X509ExtensionUtil.fromExtensionValue(value);
    if (!(authorityInfoAccess instanceof DLSequence)) {
        return null;
    }

    DLSequence aiaSequence = (DLSequence) authorityInfoAccess;
    DERTaggedObject taggedObject = findObject(aiaSequence, OCSP_RESPONDER_OID, DERTaggedObject.class);
    if (taggedObject == null) {
        return null;
    }

    if (taggedObject.getTagNo() != BERTags.OBJECT_IDENTIFIER) {
        return null;
    }

    byte[] encoded = taggedObject.getEncoded();
    int length = (int) encoded[1] & 0xFF;
    String uri = new String(encoded, 2, length, CharsetUtil.UTF_8);
    return URI.create(uri);
}

From source file:org.jboss.as.test.integration.security.common.negotiation.KerberosTestUtils.java

License:Open Source License

/**
 * Generates SPNEGO response (to a "select mechanism challenge") with given bytes as the ticket for selected mechanism.
 *
 * @param ticket//w w  w . j  av a2  s .com
 * @return ASN.1 encoded SPNEGO response
 */
public static byte[] generateSpnegoTokenResp(byte[] ticket) throws IOException {
    DEROctetString ourKerberosTicket = new DEROctetString(ticket);

    DERTaggedObject taggedNegState = new DERTaggedObject(0, new ASN1Enumerated(1)); // accept-incomplete
    DERTaggedObject taggedResponseToken = new DERTaggedObject(2, ourKerberosTicket);
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(taggedNegState);
    v.add(taggedResponseToken);
    DERSequence seqNegTokenResp = new DERSequence(v);
    DERTaggedObject taggedSpnego = new DERTaggedObject(1, seqNegTokenResp);
    return taggedSpnego.getEncoded();
}

From source file:pro.javacard.gp.GlobalPlatform.java

License:Open Source License

private void parse_select_response(byte[] fci) throws GPException {
    try (ASN1InputStream ais = new ASN1InputStream(fci)) {
        if (ais.available() > 0) {
            // Read FCI
            DERApplicationSpecific fcidata = (DERApplicationSpecific) ais.readObject();
            // FIXME System.out.println(ASN1Dump.dumpAsString(fcidata, true));
            if (fcidata.getApplicationTag() == 15) {
                ASN1Sequence s = ASN1Sequence.getInstance(fcidata.getObject(BERTags.SEQUENCE));
                for (ASN1Encodable e : Lists.newArrayList(s.iterator())) {
                    ASN1TaggedObject t = DERTaggedObject.getInstance(e);
                    if (t.getTagNo() == 4) {
                        // ISD AID
                        ASN1OctetString isdaid = DEROctetString.getInstance(t.getObject());
                        AID detectedAID = new AID(isdaid.getOctets());
                        if (sdAID == null) {
                            logger.debug("Auto-detected ISD AID: " + detectedAID);
                        }/*  w w  w .  jav  a 2 s .c  om*/
                        if (sdAID != null && !detectedAID.equals(sdAID)) {
                            giveStrictWarning("SD AID in FCI does not match the requested AID!");
                        }
                        this.sdAID = sdAID == null ? detectedAID : sdAID;
                    } else if (t.getTagNo() == 5) {
                        // Proprietary, usually a sequence
                        if (t.getObject() instanceof ASN1Sequence) {
                            ASN1Sequence prop = ASN1Sequence.getInstance(t.getObject());
                            for (ASN1Encodable enc : Lists.newArrayList(prop.iterator())) {
                                ASN1Primitive proptag = enc.toASN1Primitive();
                                if (proptag instanceof DERApplicationSpecific) {
                                    DERApplicationSpecific isddata = (DERApplicationSpecific) proptag;
                                    if (isddata.getApplicationTag() == 19) {
                                        spec = GPData.get_version_from_card_data(isddata.getEncoded());
                                        logger.debug("Auto-detected GP version: " + spec);
                                    }
                                } else if (proptag instanceof DERTaggedObject) {
                                    DERTaggedObject tag = (DERTaggedObject) proptag;
                                    if (tag.getTagNo() == 101) {
                                        setBlockSize(DEROctetString.getInstance(tag.getObject()));
                                    } else if (tag.getTagNo() == 110) {
                                        logger.debug("Lifecycle data (ignored): "
                                                + HexUtils.bin2hex(tag.getObject().getEncoded()));
                                    } else {
                                        logger.info("Unknown/unhandled tag in FCI proprietary data: "
                                                + HexUtils.bin2hex(tag.getEncoded()));
                                    }
                                } else {
                                    throw new GPException("Unknown data from card: "
                                            + HexUtils.bin2hex(proptag.getEncoded()));
                                }
                            }
                        } else {
                            // Except Feitian cards which have a plain nested tag
                            if (t.getObject() instanceof DERTaggedObject) {
                                DERTaggedObject tag = (DERTaggedObject) t.getObject();
                                if (tag.getTagNo() == 101) {
                                    setBlockSize(DEROctetString.getInstance(tag.getObject()));
                                } else {
                                    logger.info("Unknown/unhandled tag in FCI proprietary data: "
                                            + HexUtils.bin2hex(tag.getEncoded()));
                                }
                            }
                        }
                    } else {
                        logger.info("Unknown/unhandled tag in FCI: " + HexUtils.bin2hex(t.getEncoded()));
                    }
                }
            } else {
                throw new GPException("Unknown data from card: " + HexUtils.bin2hex(fci));
            }
        }
    } catch (IOException | ClassCastException e) {
        throw new GPException("Invalid data: " + e.getMessage(), e);
    }

}