Search in sources :

Example 56 with ASN1Set

use of com.unboundid.asn1.ASN1Set in project LinLong-Java by zhenwei1108.

the class X509Attribute method getValues.

public ASN1Encodable[] getValues() {
    ASN1Set s = attr.getAttrValues();
    ASN1Encodable[] values = new ASN1Encodable[s.size()];
    for (int i = 0; i != s.size(); i++) {
        values[i] = (ASN1Encodable) s.getObjectAt(i);
    }
    return values;
}
Also used : ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable)

Example 57 with ASN1Set

use of com.unboundid.asn1.ASN1Set in project LinLong-Java by zhenwei1108.

the class TSPUtil method getSignatureTimestamps.

/**
 * Fetches the signature time-stamp attributes from a SignerInformation object. Checks that the
 * MessageImprint for each time-stamp matches the signature field. (see RFC 3161 Appendix A).
 *
 * @param signerInfo      a SignerInformation to search for time-stamps
 * @param digCalcProvider provider for digest calculators
 * @return a collection of TimeStampToken objects
 * @throws TSPValidationException
 */
public static Collection getSignatureTimestamps(SignerInformation signerInfo, DigestCalculatorProvider digCalcProvider) throws TSPValidationException {
    List timestamps = new ArrayList();
    AttributeTable unsignedAttrs = signerInfo.getUnsignedAttributes();
    if (unsignedAttrs != null) {
        ASN1EncodableVector allTSAttrs = unsignedAttrs.getAll(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken);
        for (int i = 0; i < allTSAttrs.size(); ++i) {
            Attribute tsAttr = (Attribute) allTSAttrs.get(i);
            ASN1Set tsAttrValues = tsAttr.getAttrValues();
            for (int j = 0; j < tsAttrValues.size(); ++j) {
                try {
                    ContentInfo contentInfo = ContentInfo.getInstance(tsAttrValues.getObjectAt(j));
                    TimeStampToken timeStampToken = new TimeStampToken(contentInfo);
                    TimeStampTokenInfo tstInfo = timeStampToken.getTimeStampInfo();
                    DigestCalculator digCalc = digCalcProvider.get(tstInfo.getHashAlgorithm());
                    OutputStream dOut = digCalc.getOutputStream();
                    dOut.write(signerInfo.getSignature());
                    dOut.close();
                    byte[] expectedDigest = digCalc.getDigest();
                    if (!Arrays.constantTimeAreEqual(expectedDigest, tstInfo.getMessageImprintDigest())) {
                        throw new TSPValidationException("Incorrect digest in message imprint");
                    }
                    timestamps.add(timeStampToken);
                } catch (OperatorCreationException e) {
                    throw new TSPValidationException("Unknown hash algorithm specified in timestamp");
                } catch (Exception e) {
                    throw new TSPValidationException("Timestamp could not be parsed");
                }
            }
        }
    }
    return timestamps;
}
Also used : Attribute(com.github.zhenwei.pkix.util.asn1.cms.Attribute) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) AttributeTable(com.github.zhenwei.pkix.util.asn1.cms.AttributeTable) DigestCalculator(com.github.zhenwei.pkix.operator.DigestCalculator) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) IOException(java.io.IOException) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) ContentInfo(com.github.zhenwei.pkix.util.asn1.cms.ContentInfo) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) ArrayList(java.util.ArrayList) List(java.util.List) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException)

Example 58 with ASN1Set

use of com.unboundid.asn1.ASN1Set in project LinLong-Java by zhenwei1108.

the class PKCS12SafeBag method getAttributes.

public Attribute[] getAttributes() {
    ASN1Set attrs = safeBag.getBagAttributes();
    if (attrs == null) {
        return null;
    }
    Attribute[] attributes = new Attribute[attrs.size()];
    for (int i = 0; i != attrs.size(); i++) {
        attributes[i] = Attribute.getInstance(attrs.getObjectAt(i));
    }
    return attributes;
}
Also used : ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) Attribute(com.github.zhenwei.core.asn1.pkcs.Attribute)

Example 59 with ASN1Set

use of com.unboundid.asn1.ASN1Set in project LinLong-Java by zhenwei1108.

the class OEROutputStream method write.

public void write(ASN1Encodable encodable, OERDefinition.Element oerElement) throws IOException {
    if (encodable == OEROptional.ABSENT) {
        return;
    } else if (encodable instanceof OEROptional) {
        write(((OEROptional) encodable).get(), oerElement);
        return;
    }
    encodable = encodable.toASN1Primitive();
    switch(oerElement.baseType) {
        case SEQ:
            {
                ASN1Sequence seq = ASN1Sequence.getInstance(encodable);
                // build mask.
                int j = 7;
                int mask = 0;
                if (oerElement.extensionsInDefinition) {
                    if (oerElement.hasPopulatedExtension()) {
                        mask |= bits[j];
                    }
                    j--;
                }
                for (int t = 0; t < oerElement.children.size(); t++) {
                    OERDefinition.Element childOERDescription = oerElement.children.get(t);
                    if (j < 0) {
                        out.write(mask);
                        j = 7;
                        mask = 0;
                    }
                    ASN1Encodable asn1EncodableChild = seq.getObjectAt(t);
                    if (childOERDescription.explicit && asn1EncodableChild instanceof OEROptional) {
                        // TODO call stack like definition error.
                        throw new IllegalStateException("absent sequence element that is required by oer definition");
                    }
                    if (!childOERDescription.explicit) {
                        ASN1Encodable obj = seq.getObjectAt(t);
                        if (childOERDescription.getDefaultValue() != null) {
                            if (obj instanceof OEROptional) {
                                if (((OEROptional) obj).isDefined()) {
                                    if (!((OEROptional) obj).get().equals(childOERDescription.defaultValue)) {
                                        mask |= bits[j];
                                    }
                                }
                            } else {
                                if (!childOERDescription.getDefaultValue().equals(obj)) {
                                    mask |= bits[j];
                                }
                            }
                        } else {
                            if (asn1EncodableChild != OEROptional.ABSENT) {
                                mask |= bits[j];
                            }
                        }
                        j--;
                    }
                }
                if (j != 7) {
                    out.write(mask);
                }
                // 
                for (int t = 0; t < oerElement.children.size(); t++) {
                    ASN1Encodable child = seq.getObjectAt(t);
                    OERDefinition.Element childOERElement = oerElement.children.get(t);
                    if (childOERElement.getDefaultValue() != null) {
                        if (childOERElement.getDefaultValue().equals(child)) {
                            continue;
                        }
                    }
                    write(child, childOERElement);
                }
                out.flush();
                debugPrint(oerElement.appendLabel(""));
            }
            break;
        case SEQ_OF:
            // 
            // Assume this comes in as a sequence.
            // 
            Enumeration e;
            if (encodable instanceof ASN1Set) {
                e = ((ASN1Set) encodable).getObjects();
                encodeQuantity(((ASN1Set) encodable).size());
            } else if (encodable instanceof ASN1Sequence) {
                e = ((ASN1Sequence) encodable).getObjects();
                encodeQuantity(((ASN1Sequence) encodable).size());
            } else {
                throw new IllegalStateException("encodable at for SEQ_OF is not a container");
            }
            while (e.hasMoreElements()) {
                Object o = e.nextElement();
                write((ASN1Encodable) o, oerElement.getFirstChid());
            }
            out.flush();
            debugPrint(oerElement.appendLabel(""));
            break;
        case CHOICE:
            {
                ASN1Primitive item = encodable.toASN1Primitive();
                BitBuilder bb = new BitBuilder();
                int tag;
                if (item instanceof ASN1ApplicationSpecific) {
                    // 
                    // Application specific tag prefix.
                    // 
                    tag = ((ASN1ApplicationSpecific) item).getApplicationTag();
                    bb.writeBit(0).writeBit(1);
                    item = ((ASN1ApplicationSpecific) item).getEnclosedObject();
                } else if (item instanceof ASN1TaggedObject) {
                    ASN1TaggedObject taggedObject = (ASN1TaggedObject) item;
                    // 
                    // Tag prefix.
                    // 
                    int tagClass = taggedObject.getTagClass();
                    bb.writeBit(tagClass & BERTags.CONTEXT_SPECIFIC).writeBit(tagClass & BERTags.APPLICATION);
                    tag = taggedObject.getTagNo();
                    item = taggedObject.getBaseObject().toASN1Primitive();
                } else {
                    throw new IllegalStateException("only support tagged objects");
                }
                // Small tag value encode in remaining bits
                if (tag <= 63) {
                    bb.writeBits(tag, 6);
                } else {
                    // Large tag value variant.
                    bb.writeBits(0xFF, 6);
                    // Encode as 7bit bytes where MSB indicated continuing byte.
                    bb.write7BitBytes(tag);
                }
                if (debugOutput != null) {
                    if (item instanceof ASN1ApplicationSpecific) {
                        debugPrint(oerElement.appendLabel("AS"));
                    } else if (item instanceof ASN1TaggedObject) {
                        debugPrint(oerElement.appendLabel("CS"));
                    }
                }
                // Save the header.
                bb.writeAndClear(out);
                write(item, oerElement.children.get(tag));
                out.flush();
                break;
            }
        case ENUM:
            {
                BigInteger ordinal;
                if (encodable instanceof ASN1Integer) {
                    ordinal = ASN1Integer.getInstance(encodable).getValue();
                } else {
                    ordinal = ASN1Enumerated.getInstance(encodable).getValue();
                }
                for (Iterator it = oerElement.children.iterator(); it.hasNext(); ) {
                    OERDefinition.Element child = (OERDefinition.Element) it.next();
                    // 
                    if (child.enumValue.equals(ordinal)) {
                        if (ordinal.compareTo(BigInteger.valueOf(127)) > 0) {
                            // Note 2 Section 11.4 of T-REC-X.696-201508-I!!PDF-E.pdf
                            byte[] val = ordinal.toByteArray();
                            int l = 0x80 | (val.length & 0xFF);
                            out.write(l);
                            out.write(val);
                        } else {
                            out.write(ordinal.intValue() & 0x7F);
                        }
                        out.flush();
                        debugPrint(oerElement.appendLabel(oerElement.rangeExpression()));
                        return;
                    }
                }
                throw new IllegalArgumentException("enum value " + ordinal + " " + Hex.toHexString(ordinal.toByteArray()) + " no in defined child list");
            }
        case INT:
            {
                ASN1Integer integer = ASN1Integer.getInstance(encodable);
                // >0 = positive and <0 = negative
                int intBytesForRange = oerElement.intBytesForRange();
                if (intBytesForRange > 0) {
                    // 
                    // For unsigned fixed length 1,2,4,8 byte integers.
                    // 
                    byte[] encoded = BigIntegers.asUnsignedByteArray(intBytesForRange, integer.getValue());
                    switch(intBytesForRange) {
                        case 1:
                        case 2:
                        case 4:
                        case 8:
                            out.write(encoded);
                            break;
                        default:
                            throw new IllegalStateException("unknown uint length " + intBytesForRange);
                    }
                } else if (intBytesForRange < 0) {
                    // 
                    // For twos compliment numbers of 1,2,4,8 bytes in encoded length.
                    // 
                    byte[] encoded;
                    BigInteger number = integer.getValue();
                    switch(intBytesForRange) {
                        case -1:
                            encoded = new byte[] { BigIntegers.byteValueExact(number) };
                            break;
                        case -2:
                            encoded = Pack.shortToBigEndian(BigIntegers.shortValueExact(number));
                            break;
                        case -4:
                            encoded = Pack.intToBigEndian(BigIntegers.intValueExact(number));
                            break;
                        case -8:
                            encoded = Pack.longToBigEndian(BigIntegers.longValueExact(number));
                            break;
                        default:
                            throw new IllegalStateException("unknown twos compliment length");
                    }
                    out.write(encoded);
                } else {
                    // Unbounded at one or both ends and needs length encoding.
                    byte[] encoded;
                    if (oerElement.isLowerRangeZero()) {
                        // Since we have already captured the fixed with unsigned ints.
                        // Everything is assumed unbounded we need to encode a length and write the value.
                        encoded = BigIntegers.asUnsignedByteArray(integer.getValue());
                    } else {
                        // Twos complement
                        encoded = integer.getValue().toByteArray();
                    }
                    // Deals with long and short forms.
                    encodeLength(encoded.length);
                    out.write(encoded);
                }
                debugPrint(oerElement.appendLabel(oerElement.rangeExpression()));
                out.flush();
            }
            break;
        case OCTET_STRING:
            {
                ASN1OctetString octets = ASN1OctetString.getInstance(encodable);
                byte[] bytes = octets.getOctets();
                if (oerElement.isFixedLength()) {
                    out.write(bytes);
                } else {
                    encodeLength(bytes.length);
                    out.write(bytes);
                }
                debugPrint(oerElement.appendLabel(oerElement.rangeExpression()));
                out.flush();
                break;
            }
        case UTF8_STRING:
            {
                ASN1UTF8String utf8 = ASN1UTF8String.getInstance(encodable);
                byte[] encoded = Strings.toUTF8ByteArray(utf8.getString());
                encodeLength(encoded.length);
                out.write(encoded);
                debugPrint(oerElement.appendLabel(""));
                out.flush();
                break;
            }
        case BIT_STRING:
            {
                DERBitString bitString = DERBitString.getInstance(encodable);
                byte[] bytes = bitString.getBytes();
                if (oerElement.isFixedLength()) {
                    out.write(bytes);
                    debugPrint(oerElement.appendLabel(oerElement.rangeExpression()));
                } else {
                    int padBits = bitString.getPadBits();
                    // 13.3.1
                    encodeLength(bytes.length + 1);
                    // 13.3.2
                    out.write(padBits);
                    // 13.3.3
                    out.write(bytes);
                    debugPrint(oerElement.appendLabel(oerElement.rangeExpression()));
                }
                out.flush();
            }
            break;
        case NULL:
            // Does not encode in OER.
            break;
        case EXTENSION:
            {
                ASN1OctetString octets = ASN1OctetString.getInstance(encodable);
                byte[] bytes = octets.getOctets();
                if (oerElement.isFixedLength()) {
                    out.write(bytes);
                } else {
                    encodeLength(bytes.length);
                    out.write(bytes);
                }
                debugPrint(oerElement.appendLabel(oerElement.rangeExpression()));
                out.flush();
                break;
            }
        case ENUM_ITEM:
            // Used to define options does not encode.
            break;
        case BOOLEAN:
            debugPrint(oerElement.label);
            ASN1Boolean asn1Boolean = ASN1Boolean.getInstance(encodable);
            if (asn1Boolean.isTrue()) {
                out.write(255);
            } else {
                out.write(0);
            }
            out.flush();
    }
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) Enumeration(java.util.Enumeration) ASN1UTF8String(com.github.zhenwei.core.asn1.ASN1UTF8String) ASN1TaggedObject(com.github.zhenwei.core.asn1.ASN1TaggedObject) ASN1ApplicationSpecific(com.github.zhenwei.core.asn1.ASN1ApplicationSpecific) DERBitString(com.github.zhenwei.core.asn1.DERBitString) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) Iterator(java.util.Iterator) BigInteger(java.math.BigInteger) ASN1TaggedObject(com.github.zhenwei.core.asn1.ASN1TaggedObject) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) ASN1Boolean(com.github.zhenwei.core.asn1.ASN1Boolean) ASN1Primitive(com.github.zhenwei.core.asn1.ASN1Primitive)

Example 60 with ASN1Set

use of com.unboundid.asn1.ASN1Set in project openkeystore by cyberphone.

the class RelativeDistinguishedName method toASN1.

/*
     * Get the ASN.1 representation of this RelativeDistinguishedName.
     */
public ASN1Set toASN1() {
    if (asn1Representation == null) {
        BaseASN1Object[] t = new BaseASN1Object[components.size()];
        Enumeration<String> e = components.keys();
        for (int i = 0; i < t.length; i++) {
            String attribute = e.nextElement();
            t[i] = new ASN1Sequence(new BaseASN1Object[] { new ASN1ObjectID(attribute), components.get(attribute) });
        }
        asn1Representation = new ASN1Set(t);
    }
    return asn1Representation;
}
Also used : ASN1Sequence(org.webpki.asn1.ASN1Sequence) ASN1Set(org.webpki.asn1.ASN1Set) BaseASN1Object(org.webpki.asn1.BaseASN1Object) ASN1ObjectID(org.webpki.asn1.ASN1ObjectID) ASN1IA5String(org.webpki.asn1.ASN1IA5String) ASN1PrintableString(org.webpki.asn1.ASN1PrintableString) ASN1UTF8String(org.webpki.asn1.ASN1UTF8String) ASN1String(org.webpki.asn1.ASN1String)

Aggregations

ASN1Set (org.bouncycastle.asn1.ASN1Set)67 ArrayList (java.util.ArrayList)51 ASN1Set (com.unboundid.asn1.ASN1Set)33 IOException (java.io.IOException)32 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)30 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)30 ASN1Set (com.github.zhenwei.core.asn1.ASN1Set)26 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)22 ASN1Element (com.unboundid.asn1.ASN1Element)21 NotNull (com.unboundid.util.NotNull)21 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)19 List (java.util.List)17 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)17 DEROctetString (org.bouncycastle.asn1.DEROctetString)16 Enumeration (java.util.Enumeration)14 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)14 OutputStream (java.io.OutputStream)12 Test (org.testng.annotations.Test)12 ASN1Enumerated (com.unboundid.asn1.ASN1Enumerated)11 X509Certificate (java.security.cert.X509Certificate)11