Example usage for org.bouncycastle.asn1 DLSequence getObjects

List of usage examples for org.bouncycastle.asn1 DLSequence getObjects

Introduction

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

Prototype

public Enumeration getObjects() 

Source Link

Usage

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

License:Open Source License

/**
 * Interpreta um dado do tipo otherName. Obs. O JDK 5.0 no tem classes que
 * lidem com um dado do tipo OtherName.  necessrio usar o BouncyCastle.
 * /*from  w ww . j a  v  a2  s. com*/
 * @param encoded
 *            O dado em ASN.1.
 * @return Um par contendo o OID e o contedo.
 */
public static Pair<ASN1ObjectIdentifier, String> getOtherName(DLSequence sequence) throws IOException {

    String conteudo = "";
    @SuppressWarnings("rawtypes")
    Enumeration en = sequence.getObjects();
    ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) en.nextElement();
    ASN1TaggedObject taggedObject = (ASN1TaggedObject) en.nextElement();

    ASN1Primitive obj = taggedObject.getObject();
    if (obj instanceof ASN1String) { // Certificados antigos SERASA -
        // incorretos
        conteudo = ((ASN1String) obj).getString();
    } else if (obj instanceof ASN1OctetString) { // Certificados corretos
        conteudo = new String(((ASN1OctetString) obj).getOctets(), "ISO-8859-1");
    }

    return new Pair<ASN1ObjectIdentifier, String>(oid, conteudo);
}

From source file:jcifs.pac.kerberos.KerberosApRequest.java

License:Open Source License

public KerberosApRequest(byte[] token, KerberosKey[] keys) throws PACDecodingException {
    if (token.length <= 0)
        throw new PACDecodingException("Empty kerberos ApReq");

    DLSequence sequence;
    try {//from  w  w  w  .j  a  v  a 2s.com
        try (ASN1InputStream stream = new ASN1InputStream(new ByteArrayInputStream(token))) {
            sequence = ASN1Util.as(DLSequence.class, stream);
        }
    } catch (IOException e) {
        throw new PACDecodingException("Malformed Kerberos Ticket", e);
    }

    Enumeration<?> fields = sequence.getObjects();
    while (fields.hasMoreElements()) {
        ASN1TaggedObject tagged = ASN1Util.as(ASN1TaggedObject.class, fields.nextElement());
        switch (tagged.getTagNo()) {
        case 0:
            ASN1Integer pvno = ASN1Util.as(ASN1Integer.class, tagged);
            if (!pvno.getValue().equals(new BigInteger(KerberosConstants.KERBEROS_VERSION))) {
                throw new PACDecodingException("Invalid kerberos version");
            }
            break;
        case 1:
            ASN1Integer msgType = ASN1Util.as(ASN1Integer.class, tagged);
            if (!msgType.getValue().equals(new BigInteger(KerberosConstants.KERBEROS_AP_REQ)))
                throw new PACDecodingException("Invalid kerberos request");
            break;
        case 2:
            DERBitString bitString = ASN1Util.as(DERBitString.class, tagged);
            this.apOptions = bitString.getBytes()[0];
            break;
        case 3:
            DERApplicationSpecific derTicket = ASN1Util.as(DERApplicationSpecific.class, tagged);
            if (!derTicket.isConstructed())
                throw new PACDecodingException("Malformed Kerberos Ticket");
            this.ticket = new KerberosTicket(derTicket.getContents(), this.apOptions, keys);
            break;
        case 4:
            // Let's ignore this for now
            break;
        default:
            throw new PACDecodingException("Invalid field in kerberos ticket");
        }
    }
}

From source file:jcifs.pac.kerberos.KerberosEncData.java

License:Open Source License

public KerberosEncData(byte[] token, Key key) throws PACDecodingException {
    ASN1InputStream stream = new ASN1InputStream(new ByteArrayInputStream(token));
    DERApplicationSpecific derToken;//w ww .j  a v  a2s.c om
    try {
        derToken = ASN1Util.as(DERApplicationSpecific.class, stream);
        if (!derToken.isConstructed())
            throw new PACDecodingException("Malformed kerberos ticket");
        stream.close();
    } catch (IOException e) {
        throw new PACDecodingException("Malformed kerberos ticket", e);
    }

    stream = new ASN1InputStream(new ByteArrayInputStream(derToken.getContents()));
    DLSequence sequence;
    try {
        sequence = ASN1Util.as(DLSequence.class, stream);
        stream.close();
    } catch (IOException e) {
        throw new PACDecodingException("Malformed kerberos ticket", e);
    }

    Enumeration<?> fields = sequence.getObjects();
    while (fields.hasMoreElements()) {
        ASN1TaggedObject tagged = ASN1Util.as(ASN1TaggedObject.class, fields);

        switch (tagged.getTagNo()) {
        case 0: // Ticket Flags
            break;
        case 1: // Key
            break;
        case 2: // Realm
            DERGeneralString derRealm = ASN1Util.as(DERGeneralString.class, tagged);
            this.userRealm = derRealm.getString();
            break;
        case 3: // Principal
            DLSequence principalSequence = ASN1Util.as(DLSequence.class, tagged);
            DLSequence nameSequence = ASN1Util.as(DLSequence.class,
                    ASN1Util.as(DERTaggedObject.class, principalSequence, 1));

            StringBuilder nameBuilder = new StringBuilder();
            Enumeration<?> parts = nameSequence.getObjects();
            while (parts.hasMoreElements()) {
                Object part = parts.nextElement();
                DERGeneralString stringPart = ASN1Util.as(DERGeneralString.class, part);
                nameBuilder.append(stringPart.getString());
                if (parts.hasMoreElements())
                    nameBuilder.append('/');
            }
            this.userPrincipalName = nameBuilder.toString();
            break;
        case 4: // Transited Encoding
            break;
        case 5: // Kerberos Time
            // DERGeneralizedTime derTime = KerberosUtil.readAs(tagged,
            // DERGeneralizedTime.class);
            break;
        case 6: // Kerberos Time
            // DERGeneralizedTime derTime = KerberosUtil.readAs(tagged,
            // DERGeneralizedTime.class);
            break;
        case 7: // Kerberos Time
            // DERGeneralizedTime derTime = KerberosUtil.readAs(tagged,
            // DERGeneralizedTime.class);
            break;
        case 8: // Kerberos Time
            // DERGeneralizedTime derTime = KerberosUtil.readAs(tagged,
            // DERGeneralizedTime.class);
            break;
        case 9: // Host Addresses
            DLSequence adressesSequence = ASN1Util.as(DLSequence.class, tagged);
            Enumeration<?> adresses = adressesSequence.getObjects();
            while (adresses.hasMoreElements()) {
                DLSequence addressSequence = ASN1Util.as(DLSequence.class, adresses);
                ASN1Integer addressType = ASN1Util.as(ASN1Integer.class, addressSequence, 0);
                DEROctetString addressOctets = ASN1Util.as(DEROctetString.class, addressSequence, 1);

                this.userAddresses = new ArrayList<>();
                if (addressType.getValue().intValue() == KerberosConstants.AF_INTERNET) {
                    InetAddress userAddress = null;
                    try {
                        userAddress = InetAddress.getByAddress(addressOctets.getOctets());
                    } catch (UnknownHostException e) {
                    }
                    this.userAddresses.add(userAddress);
                }
            }
            break;
        case 10: // Authorization Data
            DLSequence authSequence = ASN1Util.as(DLSequence.class, tagged);

            this.userAuthorizations = new ArrayList<>();
            Enumeration<?> authElements = authSequence.getObjects();
            while (authElements.hasMoreElements()) {
                DLSequence authElement = ASN1Util.as(DLSequence.class, authElements);
                ASN1Integer authType = ASN1Util.as(ASN1Integer.class,
                        ASN1Util.as(DERTaggedObject.class, authElement, 0));
                DEROctetString authData = ASN1Util.as(DEROctetString.class,
                        ASN1Util.as(DERTaggedObject.class, authElement, 1));

                this.userAuthorizations.addAll(
                        KerberosAuthData.parse(authType.getValue().intValue(), authData.getOctets(), key));
            }
            break;
        default:
            throw new PACDecodingException("Unknown field " + tagged.getTagNo());
        }
    }
}

From source file:jcifs.pac.kerberos.KerberosRelevantAuthData.java

License:Open Source License

public KerberosRelevantAuthData(byte[] token, Key key) throws PACDecodingException {
    DLSequence authSequence;
    try {/*  w  w  w . j  a  v a2  s  . c o m*/
        try (ASN1InputStream stream = new ASN1InputStream(new ByteArrayInputStream(token))) {
            authSequence = ASN1Util.as(DLSequence.class, stream);
        }
    } catch (IOException e) {
        throw new PACDecodingException("Malformed kerberos ticket", e);
    }

    this.authorizations = new ArrayList<>();
    Enumeration<?> authElements = authSequence.getObjects();
    while (authElements.hasMoreElements()) {
        DLSequence authElement = ASN1Util.as(DLSequence.class, authElements);
        ASN1Integer authType = ASN1Util.as(ASN1Integer.class,
                ASN1Util.as(DERTaggedObject.class, authElement, 0));
        DEROctetString authData = ASN1Util.as(DEROctetString.class,
                ASN1Util.as(DERTaggedObject.class, authElement, 1));

        this.authorizations
                .addAll(KerberosAuthData.parse(authType.getValue().intValue(), authData.getOctets(), key));
    }
}

From source file:jcifs.pac.kerberos.KerberosTicket.java

License:Open Source License

public KerberosTicket(byte[] token, byte apOptions, KerberosKey[] keys) throws PACDecodingException {
    if (token.length <= 0)
        throw new PACDecodingException("Empty kerberos ticket");

    DLSequence sequence;
    try {/*  w w  w  . j av a 2  s. co m*/
        try (ASN1InputStream stream = new ASN1InputStream(new ByteArrayInputStream(token))) {
            sequence = ASN1Util.as(DLSequence.class, stream);
        }
    } catch (IOException e) {
        throw new PACDecodingException("Malformed kerberos ticket", e);
    }

    Enumeration<?> fields = sequence.getObjects();
    while (fields.hasMoreElements()) {
        ASN1TaggedObject tagged = ASN1Util.as(ASN1TaggedObject.class, fields);
        switch (tagged.getTagNo()) {
        case 0:// Kerberos version
            ASN1Integer tktvno = ASN1Util.as(ASN1Integer.class, tagged);
            if (!tktvno.getValue().equals(new BigInteger(KerberosConstants.KERBEROS_VERSION))) {
                throw new PACDecodingException("Invalid kerberos version " + tktvno);
            }
            break;
        case 1:// Realm
            DERGeneralString derRealm = ASN1Util.as(DERGeneralString.class, tagged);
            this.serverRealm = derRealm.getString();
            break;
        case 2:// Principal
            DLSequence principalSequence = ASN1Util.as(DLSequence.class, tagged);
            DLSequence nameSequence = ASN1Util.as(DLSequence.class,
                    ASN1Util.as(DERTaggedObject.class, principalSequence, 1));

            StringBuilder nameBuilder = new StringBuilder();
            Enumeration<?> parts = nameSequence.getObjects();
            while (parts.hasMoreElements()) {
                Object part = parts.nextElement();
                DERGeneralString stringPart = ASN1Util.as(DERGeneralString.class, part);
                nameBuilder.append(stringPart.getString());
                if (parts.hasMoreElements())
                    nameBuilder.append('/');
            }
            this.serverPrincipalName = nameBuilder.toString();
            break;
        case 3:// Encrypted part
            DLSequence encSequence = ASN1Util.as(DLSequence.class, tagged);
            ASN1Integer encType = ASN1Util.as(ASN1Integer.class,
                    ASN1Util.as(DERTaggedObject.class, encSequence, 0));
            DEROctetString encOctets = ASN1Util.as(DEROctetString.class,
                    ASN1Util.as(DERTaggedObject.class, encSequence, 2));
            byte[] crypt = encOctets.getOctets();

            if (keys == null) {
                try {
                    keys = new KerberosCredentials().getKeys();
                } catch (LoginException e) {
                    throw new PACDecodingException("Login failure", e);
                }
            }

            KerberosKey serverKey = null;
            for (KerberosKey key : keys) {
                if (key.getKeyType() == encType.getValue().intValue())
                    serverKey = key;
            }

            if (serverKey == null) {
                throw new PACDecodingException("Kerberos key not found for eType " + encType.getValue());
            }

            try {
                byte[] decrypted = KerberosEncData.decrypt(crypt, serverKey, serverKey.getKeyType());
                this.encData = new KerberosEncData(decrypted, serverKey);
            } catch (GeneralSecurityException e) {
                throw new PACDecodingException("Decryption failed " + serverKey.getKeyType(), e);
            }
            break;
        default:
            throw new PACDecodingException("Unrecognized field " + tagged.getTagNo());
        }
    }

}

From source file:org.cesecore.certificates.certificate.certextensions.BasicCertificateExtensionTest.java

License:Open Source License

@Test
public void test10SequencedExtension() throws Exception {
    Properties props = new Properties();
    props.put("id1.property.encoding", "DERUTF8STRING "); // Also test that we ignore spaces in the end here
    props.put("id1.property.nvalues", "3");
    props.put("id1.property.value1", "foo1");
    props.put("id1.property.value2", "foo2");
    props.put("id1.property.value3", "foo3");

    BasicCertificateExtension baseExt = new BasicCertificateExtension();
    baseExt.init(1, "1.2.3", false, props);

    ASN1Encodable value = getObject(baseExt.getValueEncoded(null, null, null, null, null, null));
    assertTrue(value.getClass().toString(), value instanceof DLSequence);
    DLSequence seq = (DLSequence) value;
    assertEquals(3, seq.size());/*  ww w  .  j av a2  s  .co m*/
    @SuppressWarnings("unchecked")
    Enumeration<ASN1Encodable> e = seq.getObjects();
    int i = 1;
    while (e.hasMoreElements()) {
        ASN1Encodable v = e.nextElement();
        assertTrue(v.getClass().toString(), v instanceof DERUTF8String);
        String str = ((DERUTF8String) v).getString();
        log.info(str);
        assertEquals(str, "foo" + i++);
    }
}

From source file:org.cesecore.certificates.certificate.certextensions.BasicCertificateExtensionTest.java

License:Open Source License

/**
 * Test with dynamic=true and and a static value specified where nvalues are used.
 *
 * The static values should be used if no value was specified in ExtendedInformation.
 * The values from ExtendedInformation should be used if present.
 *//*from ww w  .ja va  2  s .  co  m*/
@SuppressWarnings("unchecked")
@Test
public void test15DynamicTrueStaticNvalues() throws Exception {
    Properties props = new Properties();
    props.put("id1.property.encoding", "DERPRINTABLESTRING");
    props.put("id1.property.dynamic", "true");
    props.put("id1.property.nvalues", "3");
    props.put("id1.property.value1", "The static value 1");
    props.put("id1.property.value2", "The static value 2");
    props.put("id1.property.value3", "The static value 3");
    BasicCertificateExtension baseExt = new BasicCertificateExtension();
    baseExt.init(1, "1.2.3", false, props);
    EndEntityInformation userData = new EndEntityInformation();
    userData.setExtendedinformation(new ExtendedInformation());

    // Without value in userdata, the static values is used
    ASN1InputStream in = new ASN1InputStream(
            new ByteArrayInputStream(baseExt.getValueEncoded(userData, null, null, null, null, null)));
    ASN1Encodable value = in.readObject();
    assertTrue(value.getClass().toString(), value instanceof DLSequence);
    DLSequence seq = (DLSequence) value;
    assertEquals(3, seq.size());
    Enumeration<ASN1Encodable> e = seq.getObjects();
    int i = 1;
    while (e.hasMoreElements()) {
        ASN1Encodable v = e.nextElement();
        assertTrue(v.getClass().toString(), v instanceof DERPrintableString);
        String str = ((DERPrintableString) v).getString();
        assertEquals(str, "The static value " + i++);
    }

    // With values in userdata, that values is used
    userData.getExtendedinformation().setExtensionData("1.2.3.value1", "A dynamic value 1");
    userData.getExtendedinformation().setExtensionData("1.2.3.value2", "A dynamic value 2");
    userData.getExtendedinformation().setExtensionData("1.2.3.value3", "A dynamic value 3");
    in = new ASN1InputStream(
            new ByteArrayInputStream(baseExt.getValueEncoded(userData, null, null, null, null, null)));
    value = in.readObject();
    assertTrue(value.getClass().toString(), value instanceof DLSequence);
    seq = (DLSequence) value;
    assertEquals(3, seq.size());
    e = seq.getObjects();
    i = 1;
    while (e.hasMoreElements()) {
        ASN1Encodable v = (ASN1Encodable) e.nextElement();
        assertTrue(v.getClass().toString(), v instanceof DERPrintableString);
        String str = ((DERPrintableString) v).getString();
        assertEquals(str, "A dynamic value " + i++);
    }
}