Search in sources :

Example 51 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project ddf by codice.

the class SignerCondition method getIdentifyFromBytes.

// this code was grabbed online and should be correct
// we don't have any tests for this so be wary of changing this code
private String getIdentifyFromBytes(byte[] itemBytes) {
    try (ASN1InputStream decoder = new ASN1InputStream(itemBytes)) {
        ASN1Encodable encoded = decoder.readObject();
        encoded = ((DERSequence) encoded).getObjectAt(1);
        encoded = ((DERTaggedObject) encoded).getObject();
        encoded = ((DERTaggedObject) encoded).getObject();
        return ((DERUTF8String) encoded).getString();
    } catch (IOException e) {
        return "";
    }
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) IOException(java.io.IOException)

Example 52 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project LinLong-Java by zhenwei1108.

the class CMSTimeStampedGenerator method setMetaData.

/**
 * Set the MetaData for the generated message.
 *
 * @param hashProtected true if the MetaData should be included in first imprint calculation,
 *                      false otherwise.
 * @param fileName      optional file name, may be null.
 * @param mediaType     optional media type, may be null.
 * @param attributes    optional attributes, may be null.
 */
public void setMetaData(boolean hashProtected, String fileName, String mediaType, Attributes attributes) {
    ASN1UTF8String asn1FileName = null;
    if (fileName != null) {
        asn1FileName = new DERUTF8String(fileName);
    }
    ASN1IA5String asn1MediaType = null;
    if (mediaType != null) {
        asn1MediaType = new DERIA5String(mediaType);
    }
    setMetaData(hashProtected, asn1FileName, asn1MediaType, attributes);
}
Also used : DERUTF8String(com.github.zhenwei.core.asn1.DERUTF8String) DERIA5String(com.github.zhenwei.core.asn1.DERIA5String) ASN1UTF8String(com.github.zhenwei.core.asn1.ASN1UTF8String) ASN1IA5String(com.github.zhenwei.core.asn1.ASN1IA5String)

Example 53 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project LinLong-Java by zhenwei1108.

the class OERInputStream method parse.

public ASN1Object parse(OERDefinition.Element element) throws Exception {
    switch(element.baseType) {
        case SEQ_OF:
            {
                int l = readLength().intLength();
                // This l is the number of bytes that holds the actual length of the sequence of.
                byte[] lenEnc = allocateArray(l);
                if (Streams.readFully(this, lenEnc) != lenEnc.length) {
                    throw new IOException("could not read all of count of seq-of values");
                }
                // 
                // The actual number of elements encoded into this seq-of.
                // 
                int j = BigIntegers.fromUnsignedByteArray(lenEnc).intValue();
                debugPrint(element.appendLabel("(len = " + j + ")"));
                ASN1EncodableVector avec = new ASN1EncodableVector();
                for (int n = 0; n < j; n++) {
                    avec.add(parse(element.children.get(0)));
                }
                return new DERSequence(avec);
            }
        case SEQ:
            {
                Sequence sequence = sequence(countOptionalChildTypes(element), element.hasDefaultChildren(), element.extensionsInDefinition);
                debugPrint(element.appendLabel(sequence.toString()));
                ASN1EncodableVector avec = new ASN1EncodableVector();
                for (int t = 0; t < element.children.size(); t++) {
                    OERDefinition.Element child = element.children.get(t);
                    if (child.explicit) {
                        // debugPrint(child.appendLabel("E[" + t + "]"));
                        avec.add(parse(child));
                    } else {
                        if (sequence.hasOptional(element.optionalOrDefaultChildrenInOrder().indexOf(child))) {
                            // debugPrint(child.appendLabel("O[" + t + "]"));
                            avec.add(OEROptional.getInstance(parse(child)));
                        } else {
                            if (child.getDefaultValue() != null) {
                                avec.add(child.defaultValue);
                                debugPrint("Using default.");
                            } else {
                                avec.add(absent(child));
                            }
                        }
                    }
                }
                return new DERSequence(avec);
            }
        case CHOICE:
            {
                Choice choice = choice();
                debugPrint(element.appendLabel(choice.toString()));
                if (choice.isContextSpecific()) {
                    OERDefinition.Element item = element.children.get(choice.getTag());
                    return new DERTaggedObject(choice.tag, parse(element.children.get(choice.getTag())));
                } else if (choice.isApplicationTagClass()) {
                    throw new IllegalStateException("Unimplemented tag type");
                } else if (choice.isPrivateTagClass()) {
                    throw new IllegalStateException("Unimplemented tag type");
                } else if (choice.isUniversalTagClass()) {
                    switch(choice.getTag()) {
                    }
                } else {
                    throw new IllegalStateException("Unimplemented tag type");
                }
            }
        case ENUM:
            {
                BigInteger bi = enumeration();
                debugPrint(element.appendLabel("ENUM(" + bi + ") = " + element.children.get(bi.intValue()).label));
                return new ASN1Enumerated(bi);
            }
        case INT:
            {
                byte[] data;
                BigInteger bi;
                // 
                // Special fixed width cases used for signed and unsigned 8/16/24/32/64 bit numbers.
                // 
                int bytesToRead = element.intBytesForRange();
                if (// Fixed width
                bytesToRead != 0) {
                    data = allocateArray(Math.abs(bytesToRead));
                    Streams.readFully(this, data);
                    switch(data.length) {
                        case 1:
                            bi = BigInteger.valueOf(data[0]);
                            break;
                        case 2:
                            bi = BigInteger.valueOf(Pack.bigEndianToShort(data, 0));
                            break;
                        case 4:
                            bi = BigInteger.valueOf(Pack.bigEndianToInt(data, 0));
                            break;
                        case 8:
                            bi = BigInteger.valueOf(Pack.bigEndianToLong(data, 0));
                            break;
                        default:
                            throw new IllegalStateException("Unknown size");
                    }
                } else if (// INTEGER(0 ... MAX) or INTEGER (0 ... n)
                element.isLowerRangeZero()) {
                    LengthInfo lengthInfo = readLength();
                    data = allocateArray(lengthInfo.intLength());
                    Streams.readFully(this, data);
                    if (data.length == 0) {
                        bi = BigInteger.ZERO;
                    } else {
                        bi = BigIntegers.fromUnsignedByteArray(data);
                    }
                } else {
                    // 
                    // Classic twos compliment.
                    // 
                    LengthInfo lengthInfo = readLength();
                    data = allocateArray(lengthInfo.intLength());
                    Streams.readFully(this, data);
                    if (data.length == 0) {
                        bi = BigInteger.ZERO;
                    } else {
                        bi = new BigInteger(data);
                    }
                }
                if (debugOutput != null) {
                    debugPrint(element.appendLabel("INTEGER(" + data.length + " " + bi.toString(16) + ")"));
                }
                return new ASN1Integer(bi);
            }
        case OCTET_STRING:
            {
                byte[] data;
                int readSize = 0;
                if (element.upperBound != null && element.upperBound.equals(element.lowerBound)) {
                    // Fixed length there is no range.
                    readSize = element.upperBound.intValue();
                } else {
                    readSize = readLength().intLength();
                }
                data = allocateArray(readSize);
                if (Streams.readFully(this, data) != readSize) {
                    throw new IOException("did not read all of " + element.label);
                }
                if (debugOutput != null) {
                    debugPrint(element.appendLabel("OCTET STRING (" + data.length + ") = " + Hex.toHexString(data, 0, Math.min(data.length, 32))));
                }
                return new DEROctetString(data);
            }
        case UTF8_STRING:
            {
                // 27.3 and 27.4 a length determinant followed by a number of octets.
                byte[] data = allocateArray(readLength().intLength());
                if (Streams.readFully(this, data) != data.length) {
                    throw new IOException("could not read all of utf 8 string");
                }
                String content = Strings.fromUTF8ByteArray(data);
                if (debugOutput != null) {
                    debugPrint(element.appendLabel("UTF8 String (" + data.length + ") = " + content));
                }
                return new DERUTF8String(content);
            }
        case BIT_STRING:
            {
                byte[] data;
                if (element.isFixedLength()) {
                    data = new byte[element.lowerBound.intValue() / 8];
                } else if (BigInteger.ZERO.compareTo(element.upperBound) > 0) {
                    // Fixed size.
                    data = allocateArray(element.upperBound.intValue() / 8);
                } else {
                    // Length defined.
                    data = allocateArray(readLength().intLength() / 8);
                }
                Streams.readFully(this, data);
                if (debugOutput != null) {
                    StringBuffer sb = new StringBuffer();
                    sb.append("BIT STRING(" + (data.length * 8) + ") = ");
                    for (int i = 0; i != data.length; i++) {
                        byte b = data[i];
                        for (int t = 0; t < 8; t++) {
                            sb.append((b & 0x80) > 0 ? "1" : "0");
                            b <<= 1;
                        }
                    }
                    debugPrint(element.appendLabel(sb.toString()));
                }
                return new DERBitString(data);
            }
        case NULL:
            debugPrint(element.appendLabel("NULL"));
            return DERNull.INSTANCE;
        case EXTENSION:
            LengthInfo li = readLength();
            byte[] value = new byte[li.intLength()];
            if (Streams.readFully(this, value) != li.intLength()) {
                throw new IOException("could not read all of count of open value in choice (...) ");
            }
            debugPrint("ext " + li.intLength() + " " + Hex.toHexString(value));
            return new DEROctetString(value);
    }
    throw new IllegalStateException("Unhandled type " + element.baseType);
}
Also used : DERUTF8String(com.github.zhenwei.core.asn1.DERUTF8String) DERTaggedObject(com.github.zhenwei.core.asn1.DERTaggedObject) DERBitString(com.github.zhenwei.core.asn1.DERBitString) IOException(java.io.IOException) DERSequence(com.github.zhenwei.core.asn1.DERSequence) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) DERUTF8String(com.github.zhenwei.core.asn1.DERUTF8String) DERBitString(com.github.zhenwei.core.asn1.DERBitString) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) DERSequence(com.github.zhenwei.core.asn1.DERSequence) ASN1Enumerated(com.github.zhenwei.core.asn1.ASN1Enumerated) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) BigInteger(java.math.BigInteger)

Example 54 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project X-Road by nordic-institute.

the class DigestList method singleDigest.

/**
 * Encodes hash value as SingleDigest data structure.
 */
private static DERSequence singleDigest(String digestMethodUri, byte[] digest) throws Exception {
    DEROctetString digestValue = new DEROctetString(digest);
    DERUTF8String digestMethod = new DERUTF8String(digestMethodUri);
    DERSequence transforms = new DERSequence();
    return new DERSequence(new ASN1Encodable[] { digestValue, digestMethod, transforms });
}
Also used : DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DERSequence(org.bouncycastle.asn1.DERSequence) DEROctetString(org.bouncycastle.asn1.DEROctetString)

Example 55 with DERUTF8String

use of com.github.zhenwei.core.asn1.DERUTF8String in project Conversations by iNPUTmice.

the class XmppDomainVerifier method parseOtherName.

private static Pair<String, String> parseOtherName(byte[] otherName) {
    try {
        ASN1Primitive asn1Primitive = ASN1Primitive.fromByteArray(otherName);
        if (asn1Primitive instanceof DERTaggedObject) {
            ASN1Primitive inner = ((DERTaggedObject) asn1Primitive).getObject();
            if (inner instanceof DLSequence) {
                DLSequence sequence = (DLSequence) inner;
                if (sequence.size() >= 2 && sequence.getObjectAt(1) instanceof DERTaggedObject) {
                    String oid = sequence.getObjectAt(0).toString();
                    ASN1Primitive value = ((DERTaggedObject) sequence.getObjectAt(1)).getObject();
                    if (value instanceof DERUTF8String) {
                        return new Pair<>(oid, ((DERUTF8String) value).getString());
                    } else if (value instanceof DERIA5String) {
                        return new Pair<>(oid, ((DERIA5String) value).getString());
                    }
                }
            }
        }
        return null;
    } catch (IOException e) {
        return null;
    }
}
Also used : DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DERIA5String(org.bouncycastle.asn1.DERIA5String) DLSequence(org.bouncycastle.asn1.DLSequence) DERTaggedObject(org.bouncycastle.asn1.DERTaggedObject) DERIA5String(org.bouncycastle.asn1.DERIA5String) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) IOException(java.io.IOException) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) Pair(android.util.Pair)

Aggregations

DERUTF8String (org.bouncycastle.asn1.DERUTF8String)52 DERSequence (org.bouncycastle.asn1.DERSequence)28 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)20 DEROctetString (org.bouncycastle.asn1.DEROctetString)19 IOException (java.io.IOException)17 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)17 DERTaggedObject (org.bouncycastle.asn1.DERTaggedObject)17 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)15 DERIA5String (org.bouncycastle.asn1.DERIA5String)15 DERPrintableString (org.bouncycastle.asn1.DERPrintableString)12 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)11 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)11 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)10 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)10 DLSequence (org.bouncycastle.asn1.DLSequence)9 X500Name (org.bouncycastle.asn1.x500.X500Name)8 X509Certificate (java.security.cert.X509Certificate)7 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)7 Pair (android.util.Pair)5 Date (java.util.Date)5