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:com.google.u2f.server.impl.U2FServerReferenceImpl.java

License:Open Source License

/**
 * Parses a transport extension from an attestation certificate and returns
 * a List of HardwareFeatures supported by the security key. The specification of
 * the HardwareFeatures in the certificate should match their internal definition in
 * device_auth.proto//  w  ww .ja v  a2s . c om
 *
 * <p>The expected transport extension value is a BIT STRING containing the enabled
 * transports:
 *
 *  <p>FIDOU2FTransports ::= BIT STRING {
 *       bluetoothRadio(0), -- Bluetooth Classic
 *       bluetoothLowEnergyRadio(1),
 *       uSB(2),
 *       nFC(3)
 *     }
 *
 *   <p>Note that the BIT STRING must be wrapped in an OCTET STRING.
 *   An extension that encodes BT, BLE, and NFC then looks as follows:
 *
 *   <p>SEQUENCE (2 elem)
 *      OBJECT IDENTIFIER 1.3.6.1.4.1.45724.2.1.1
 *      OCTET STRING (1 elem)
 *        BIT STRING (4 bits) 1101
 *
 * @param cert the certificate to parse for extension
 * @return the supported transports as a List of HardwareFeatures or null if no extension
 * was found
 */
public static List<Transports> parseTransportsExtension(X509Certificate cert)
        throws CertificateParsingException {
    byte[] extValue = cert.getExtensionValue(TRANSPORT_EXTENSION_OID);
    LinkedList<Transports> transportsList = new LinkedList<Transports>();
    if (extValue == null) {
        // No transports extension found.
        return null;
    }

    ASN1InputStream ais = new ASN1InputStream(extValue);
    ASN1Object asn1Object;
    // Read out the OctetString
    try {
        asn1Object = ais.readObject();
        ais.close();
    } catch (IOException e) {
        throw new CertificateParsingException("Not able to read object in transports extenion", e);
    }

    if (asn1Object == null || !(asn1Object instanceof DEROctetString)) {
        throw new CertificateParsingException("No Octet String found in transports extension");
    }
    DEROctetString octet = (DEROctetString) asn1Object;

    // Read out the BitString
    ais = new ASN1InputStream(octet.getOctets());
    try {
        asn1Object = ais.readObject();
        ais.close();
    } catch (IOException e) {
        throw new CertificateParsingException("Not able to read object in transports extension", e);
    }
    if (asn1Object == null || !(asn1Object instanceof DERBitString)) {
        throw new CertificateParsingException("No BitString found in transports extension");
    }
    DERBitString bitString = (DERBitString) asn1Object;

    byte[] values = bitString.getBytes();
    BitSet bitSet = BitSet.valueOf(values);

    // We might have more defined transports than used by the extension
    for (int i = 0; i < BITS_IN_A_BYTE; i++) {
        if (bitSet.get(BITS_IN_A_BYTE - i - 1)) {
            transportsList.add(Transports.values()[i]);
        }
    }
    return transportsList;
}

From source file:com.guardtime.asn1.Accuracy.java

License:Apache License

/**
 * Parses a DER-encoded {@code Accuracy} out from the given input stream.
 *
 * @param in/*from w  w w .j  av  a 2  s .co m*/
 *            the input stream to read data from.
 * @return the {@code Accuracy} object.
 * @throws Asn1FormatException
 *             if the data read from {@code in} does not represent a valid
 *             {@code Accuracy} object.
 * @throws IOException
 *             if {@code in} throws one.
 */
public static Accuracy getInstance(InputStream in) throws Asn1FormatException, IOException {
    if (in == null) {
        throw new IllegalArgumentException("invalid input stream: null");
    }

    try {
        ASN1Object obj = new ASN1InputStream(in).readObject();
        return new Accuracy(obj);
    } catch (IOException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("accuracy has invalid format", e);
        } else {
            throw e;
        }
    } catch (IllegalArgumentException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("accuracy has invalid format", e);
        } else {
            throw e;
        }
    }
}

From source file:com.guardtime.asn1.CertToken.java

License:Apache License

/**
 * Parses a DER-encoded {@code CertToken} out from the given input stream.
 *
 * @param in// w w  w.  j  a v  a2 s .c om
 *            the input stream to read data from.
 * @return the {@code CertToken} object.
 * @throws Asn1FormatException
 *             if the data read from {@code in} does not represent a valid
 *             {@code CertToken} object.
 * @throws IOException
 *             if {@code in} throws one.
 */
public static CertToken getInstance(InputStream in) throws Asn1FormatException, IOException {
    if (in == null) {
        throw new IllegalArgumentException("invalid input stream: null");
    }

    try {
        ASN1Object obj = new ASN1InputStream(in).readObject();
        return new CertToken(obj);
    } catch (IOException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("cert token has invalid format", e);
        } else {
            throw e;
        }
    } catch (IllegalArgumentException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("cert token has invalid format", e);
        } else {
            throw e;
        }
    }
}

From source file:com.guardtime.asn1.CertTokenResponse.java

License:Apache License

/**
 * Parses a DER-encoded {@code CertTokenResponse} out from the given input
 * stream./*from w  ww  .  j a  v  a 2  s  .c  o  m*/
 *
 * @param in
 *            the input stream to read data from.
 * @return the {@code CertTokenResponse} object.
 * @throws Asn1FormatException
 *             if the data read from {@code in} does not represent a valid
 *             {@code CertTokenResponse} object.
 * @throws IOException
 *             if {@code in} throws one.
 */
public static CertTokenResponse getInstance(InputStream in) throws Asn1FormatException, IOException {
    if (in == null) {
        throw new IllegalArgumentException("invalid input stream: null");
    }

    try {
        ASN1Object obj = new ASN1InputStream(in).readObject();
        return new CertTokenResponse(obj);
    } catch (IOException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("cert token response has invalid format", e);
        } else {
            throw e;
        }
    } catch (IllegalArgumentException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("cert token response has invalid format", e);
        } else {
            throw e;
        }
    }
}

From source file:com.guardtime.asn1.ContentInfo.java

License:Apache License

/**
 * Parses a DER-encoded {@code ContentInfo} out from the given input stream.
 *
 * @param in//from w w w.  ja v a  2  s  .com
 *            the input stream to read data from.
 * @return the {@code ContentInfo} object.
 * @throws Asn1FormatException
 *             if the data read from {@code in} does not represent a valid
 *             {@code ContentInfo} object.
 * @throws IOException
 *             if {@code in} throws one.
 */
public static ContentInfo getInstance(InputStream in) throws Asn1FormatException, IOException {
    if (in == null) {
        throw new IllegalArgumentException("invalid input stream: null");
    }

    try {
        ASN1Object obj = new ASN1InputStream(in).readObject();
        return new ContentInfo(obj);
    } catch (IOException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("content info has invalid format", e);
        } else {
            throw e;
        }
    } catch (IllegalArgumentException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("content info has invalid format", e);
        } else {
            throw e;
        }
    }
}

From source file:com.guardtime.asn1.MessageImprint.java

License:Apache License

/**
 * Parses a DER-encoded {@code MessageImprint} out from the given input stream.
 *
 * @param in//from   w w w.  ja  v  a2  s  . com
 *            the input stream to read data from.
 * @return the {@code MessageImprint} object.
 * @throws Asn1FormatException
 *             if the data read from {@code in} does not represent a valid
 *             {@code MessageImprint} object.
 * @throws IOException
 *             if {@code in} throws one.
 */
public static MessageImprint getInstance(InputStream in) throws Asn1FormatException, IOException {
    if (in == null) {
        throw new IllegalArgumentException("invalid input stream: null");
    }

    try {
        ASN1Object obj = new ASN1InputStream(in).readObject();
        return new MessageImprint(obj);
    } catch (IOException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("message imprint has invalid format", e);
        } else {
            throw e;
        }
    } catch (IllegalArgumentException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("message imprint has invalid format", e);
        } else {
            throw e;
        }
    }
}

From source file:com.guardtime.asn1.PublishedData.java

License:Apache License

/**
 * Parses a DER-encoded {@code PublishedData} out from the given input
 * stream./* w w  w .  j a v  a 2 s.  co m*/
 *
 * @param in
 *            the input stream to read data from.
 * @return the {@code PublishedData} object.
 * @throws Asn1FormatException
 *             if the data read from {@code in} does not represent a valid
 *             {@code PublishedData} object.
 * @throws IOException
 *             if {@code in} throws one.
 */
public static PublishedData getInstance(InputStream in) throws Asn1FormatException, IOException {
    if (in == null) {
        throw new IllegalArgumentException("invalid input stream: null");
    }

    try {
        ASN1Object obj = new ASN1InputStream(in).readObject();
        return new PublishedData(obj);
    } catch (IOException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("publiched data has invalid format", e);
        } else {
            throw e;
        }
    } catch (IllegalArgumentException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("published data has invalid format", e);
        } else {
            throw e;
        }
    }
}

From source file:com.guardtime.asn1.SignatureInfo.java

License:Apache License

/**
 * Parses a DER-encoded {@code SignatureInfo} out from the given input stream.
 *
 * @param in/*from  w  ww. j  a va2s .c o m*/
 *            the input stream to read data from.
 * @return the {@code SignatureInfo} object.
 * @throws Asn1FormatException
 *             if the data read from {@code in} does not represent a valid
 *             {@code SignatureInfo} object.
 * @throws IOException
 *             if {@code in} throws one.
 */
public static SignatureInfo getInstance(InputStream in) throws Asn1FormatException, IOException {
    if (in == null) {
        throw new IllegalArgumentException("invalid input stream: null");
    }

    try {
        ASN1Object obj = new ASN1InputStream(in).readObject();
        return new SignatureInfo(obj);
    } catch (IOException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("signature info has invalid format", e);
        } else {
            throw e;
        }
    } catch (IllegalArgumentException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("signature info has invalid format", e);
        } else {
            throw e;
        }
    }
}

From source file:com.guardtime.asn1.SignedData.java

License:Apache License

/**
 * Parses a DER-encoded {@code SignedData} out from the given input stream.
 *
 * @param in//from   ww  w .j  a  v a2  s  .  co  m
 *            the input stream to read data from.
 * @return the {@code SignedData} object.
 * @throws Asn1FormatException
 *             if the data read from {@code in} does not represent a valid
 *             {@code SignedData} object.
 * @throws IOException
 *             if {@code in} throws one.
 */
public static SignedData getInstance(InputStream in) throws Asn1FormatException, IOException {
    if (in == null) {
        throw new IllegalArgumentException("invalid input stream: null");
    }

    try {
        ASN1Object obj = new ASN1InputStream(in).readObject();
        return new SignedData(obj);
    } catch (IOException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("signed data has invalid format", e);
        } else {
            throw e;
        }
    } catch (IllegalArgumentException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("signed data has invalid format", e);
        } else {
            throw e;
        }
    }
}

From source file:com.guardtime.asn1.SignerInfo.java

License:Apache License

/**
 * Parses a DER-encoded {@code SignerInfo} out from the given input stream.
 *
 * @param in//from ww  w  .j  av a2s.com
 *            the input stream to read data from.
 * @return the {@code SignerInfo} object.
 * @throws Asn1FormatException
 *             if the data read from {@code in} does not represent a valid
 *             {@code SignerInfo} object.
 * @throws IOException
 *             if {@code in} throws one.
 */
public static SignerInfo getInstance(InputStream in) throws Asn1FormatException, IOException {
    if (in == null) {
        throw new IllegalArgumentException("invalid input stream: null");
    }

    try {
        ASN1Object obj = new ASN1InputStream(in).readObject();
        return new SignerInfo(obj);
    } catch (IOException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("signer info has invalid format", e);
        } else {
            throw e;
        }
    } catch (IllegalArgumentException e) {
        if (isAsnParserException(e)) {
            throw new Asn1FormatException("signer info has invalid format", e);
        } else {
            throw e;
        }
    }
}