Java org.bouncycastle.asn1.ASN1EncodableVector 代码实例

・19 分钟阅读

以下是展示如何使用org.bouncycastle.asn1.ASN1EncodableVector的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


private ASN1Encodable buildACCertsExtensionContent(
  EnumSet<ACGenerationProperties> properties) {
  ASN1EncodableVector issuerCertsContainer = new ASN1EncodableVector();
  if (properties
    .contains(ACGenerationProperties.INCLUDE_EMPTY_AC_CERTS_EXTENSION))
    issuerCertsContainer.add(new DERSequence());
  else
    issuerCertsContainer.add(new DERSequence(
      getCertAsDEREncodable(aaCredential.getCertificate())));
  return new DERSequence(issuerCertsContainer);
}
 

实例 2


private ASN1Encodable buildFQANsAttributeContent(List<String> fqans,
  GeneralName policyAuthorityInfo) {
  ASN1EncodableVector container = new ASN1EncodableVector();
  ASN1EncodableVector encodedFQANs = new ASN1EncodableVector();
  // Policy authority info
  DERTaggedObject pai = new DERTaggedObject(0, policyAuthorityInfo);
  container.add(pai);
  for (String s : fqans)
    encodedFQANs.add(new DEROctetString(s.getBytes()));
  container.add(new DERSequence(encodedFQANs));
  return new DERSequence(container);
}
 

实例 3


/**
 * Added by Aiken Sam, 2006-11-15, modifed by Martin Brunecky 07/12/2007
 * to start with the timeStampToken (signedData 1.2.840.113549.1.7.2).
 * Token is the TSA response without response status, which is usually
 * handled by the (vendor supplied) TSA request/response interface).
 * @param timeStampToken byte[] - time stamp token, DER encoded signedData
 * @return ASN1EncodableVector
 * @throws IOException
 */
private ASN1EncodableVector buildUnauthenticatedAttributes(byte[] timeStampToken)  throws IOException {
    if (timeStampToken == null)
        return null;
    // @todo: move this together with the rest of the defintions
    String ID_TIME_STAMP_TOKEN = "1.2.840.113549.1.9.16.2.14"; // RFC 3161 id-aa-timeStampToken
    ASN1InputStream tempstream = new ASN1InputStream(new ByteArrayInputStream(timeStampToken));
    ASN1EncodableVector unauthAttributes = new ASN1EncodableVector();
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(new DERObjectIdentifier(ID_TIME_STAMP_TOKEN)); // id-aa-timeStampToken
    ASN1Sequence seq = (ASN1Sequence) tempstream.readObject();
    v.add(new DERSet(seq));
    unauthAttributes.add(new DERSequence(v));
    return unauthAttributes;
 }
 

实例 4


static ASN1Set createBerSetFromList(List derObjects)
{
    ASN1EncodableVector v = new ASN1EncodableVector();
    for (Iterator it = derObjects.iterator(); it.hasNext();)
    {
        v.add((DEREncodable)it.next());
    }
    return new BERSet(v);
}
 

实例 5


static ASN1Set createDerSetFromList(List derObjects)
{
    ASN1EncodableVector v = new ASN1EncodableVector();
    for (Iterator it = derObjects.iterator(); it.hasNext();)
    {
        v.add((DEREncodable)it.next());
    }
    return new DERSet(v);
}
 

实例 6


/**
 * This outputs the key in PKCS1v2 format.
 * <pre>
 *      RSAPublicKey ::= SEQUENCE {
 *                          modulus INTEGER, -- n
 *                          publicExponent INTEGER, -- e
 *                      }
 * </pre>
 * <p>
 */
public DERObject toASN1Object()
{
    ASN1EncodableVector  v = new ASN1EncodableVector();
    v.add(new DERInteger(getModulus()));
    v.add(new DERInteger(getPublicExponent()));
    return new DERSequence(v);
}
 

实例 7


private ASN1Primitive getSingleValuedSignedAttribute(
    ASN1ObjectIdentifier attrOID, String printableName)
    throws CMSException
{
    AttributeTable unsignedAttrTable = this.getUnsignedAttributes();
    if (unsignedAttrTable != null
        && unsignedAttrTable.getAll(attrOID).size() > 0)
    {
        throw new CMSException("The " + printableName
            + " attribute MUST NOT be an unsigned attribute");
    }
    AttributeTable signedAttrTable = this.getSignedAttributes();
    if (signedAttrTable == null)
    {
        return null;
    }
    ASN1EncodableVector v = signedAttrTable.getAll(attrOID);
    switch (v.size())
    {
        case 0:
            return null;
        case 1:
        {
            Attribute t = (Attribute)v.get(0);
            ASN1Set attrValues = t.getAttrValues();
            if (attrValues.size() != 1)
            {
                throw new CMSException("A " + printableName
                    + " attribute MUST have a single attribute value");
            }
            return attrValues.getObjectAt(0).toASN1Primitive();
        }
        default:
            throw new CMSException("The SignedAttributes in a signerInfo MUST NOT include multiple instances of the "
                + printableName + " attribute");
    }
}
 

实例 8


private ASN1Primitive getSingleValuedSignedAttribute(
    ASN1ObjectIdentifier attrOID, String printableName)
    throws CMSException
{
    AttributeTable unsignedAttrTable = this.getUnsignedAttributes();
    if (unsignedAttrTable != null
        && unsignedAttrTable.getAll(attrOID).size() > 0)
    {
        throw new CMSException("The " + printableName
            + " attribute MUST NOT be an unsigned attribute");
    }
    AttributeTable signedAttrTable = this.getSignedAttributes();
    if (signedAttrTable == null)
    {
        return null;
    }
    ASN1EncodableVector v = signedAttrTable.getAll(attrOID);
    switch (v.size())
    {
        case 0:
            return null;
        case 1:
        {
            Attribute t = (Attribute)v.get(0);
            ASN1Set attrValues = t.getAttrValues();
            if (attrValues.size() != 1)
            {
                throw new CMSException("A " + printableName
                    + " attribute MUST have a single attribute value");
            }
            return attrValues.getObjectAt(0).toASN1Primitive();
        }
        default:
            throw new CMSException("The SignedAttributes in a signerInfo MUST NOT include multiple instances of the "
                + printableName + " attribute");
    }
}
 

实例 9


private static ASN1Sequence createSubjectInfoAccess(
        final Extensions requestExtensions,
        final Map<ASN1ObjectIdentifier, Set<GeneralNameMode>> modes)
throws BadCertTemplateException {
    ASN1Encodable extValue = requestExtensions.getExtensionParsedValue(
            Extension.subjectInfoAccess);
    if (extValue == null) {
        return null;
    }
    ASN1Sequence reqSeq = ASN1Sequence.getInstance(extValue);
    int size = reqSeq.size();
    if (modes == null) {
        return reqSeq;
    }
    ASN1EncodableVector v = new ASN1EncodableVector();
    for (int i = 0; i < size; i++) {
        AccessDescription ad = AccessDescription.getInstance(reqSeq.getObjectAt(i));
        ASN1ObjectIdentifier accessMethod = ad.getAccessMethod();
        if (accessMethod == null) {
            accessMethod = X509Certprofile.OID_ZERO;
        }
        Set<GeneralNameMode> generalNameModes = modes.get(accessMethod);
        if (generalNameModes == null) {
            throw new BadCertTemplateException("subjectInfoAccess.accessMethod "
                    + accessMethod.getId() + " is not allowed");
        }
        GeneralName accessLocation = createGeneralName(ad.getAccessLocation(),
                generalNameModes);
        v.add(new AccessDescription(accessMethod, accessLocation));
    } // end for
    return v.size() > 0
            ? new DERSequence(v)
            : null;
}
 

实例 10


private List<TimestampToken> createTimestamps(final ASN1ObjectIdentifier attrType, final TimestampType timestampType, final ArchiveTimestampType archiveTimestampType) {
                final List<TimestampToken> timestampTokenList = new ArrayList<TimestampToken>();
                final AttributeTable attributes = attrType.equals(id_aa_ets_contentTimestamp) ? signerInformation.getSignedAttributes() : signerInformation.getUnsignedAttributes();
                if (attributes != null) {
                        final ASN1EncodableVector allAttributes = attributes.getAll(attrType);
                        for (int ii = 0; ii < allAttributes.size(); ii++) {
                                final Attribute attribute = (Attribute) allAttributes.get(ii);
                                final ASN1Set attrValues = attribute.getAttrValues();
                                for (final ASN1Encodable value : attrValues.toArray()) {
                                        if (value instanceof DEROctetString){
                                                LOG.warn("Illegal content for timestamp (OID : " + attrType + ") : OCTET STRING is not allowed !");
                                        } else {
                                                try {
                                                        byte[] encoded = value.toASN1Primitive().getEncoded();
                                                        final CMSSignedData signedData = new CMSSignedData(encoded);
                                                        final TimeStampToken token = new TimeStampToken(signedData);
                                                        final TimestampToken timestampToken = new TimestampToken(token, timestampType, certPool);
                                                        timestampToken.setArchiveTimestampType(archiveTimestampType);
                                                        timestampTokenList.add(timestampToken);
                                                } catch (Exception e) {
                                                        throw new DSSException(e);
                                                }
                                        }
                                }
                        }
                }
                return timestampTokenList;
        }
 

实例 11


/**
 * The field unsignedAttrsHashIndex is a sequence of octet strings. Each one contains the hash value of one
 * instance of Attribute within unsignedAttrs field of the SignerInfo. A hash value for every instance of
 * Attribute, as present at the time when the corresponding archive time-stamp is requested, shall be included in
 * unsignedAttrsHashIndex. No other hash values shall be included in this field.
 *
 * @param signerInformation
 * @return
 */
@SuppressWarnings("unchecked")
private ASN1Sequence getUnsignedAttributesHashIndex(SignerInformation signerInformation) throws DSSException {
        final ASN1EncodableVector unsignedAttributesHashIndex = new ASN1EncodableVector();
        AttributeTable unsignedAttributes = signerInformation.getUnsignedAttributes();
        final ASN1EncodableVector asn1EncodableVector = unsignedAttributes.toASN1EncodableVector();
        for (int i = 0; i < asn1EncodableVector.size(); i++) {
                final Attribute attribute = (Attribute) asn1EncodableVector.get(i);
                if (!excludedAttributesFromAtsHashIndex.contains(attribute.getAttrType())) {
                        final DEROctetString derOctetStringDigest = getAttributeDerOctetStringHash(attribute);
                        unsignedAttributesHashIndex.add(derOctetStringDigest);
                }
        }
        return new DERSequence(unsignedAttributesHashIndex);
}
 

实例 12


public AttributeTable(
    ASN1EncodableVector v)
{
    for (int i = 0; i != v.size(); i++)
    {
        Attribute   a = Attribute.getInstance(v.get(i));
        addAttribute(a.getAttrType(), a);
    }
}
 

实例 13


@Override
ASN1Encodable toASN1(final ThreadContext context) {
    if ( isInfiniteLength() ) return super.toASN1(context);
    if ( isSequence() ) {
        return new DERSequence( toASN1EncodableVector(context) );
    }
    if ( isSet() ) {
        return new DLSet( toASN1EncodableVector(context) ); // return new BERSet(values);
        //return ASN1Set.getInstance(toASN1TaggedObject(context), isExplicitTagging());
    }
    switch ( getTag(context) ) { // "raw" Constructive ?!?
    case OCTET_STRING:
        final ASN1EncodableVector values = toASN1EncodableVector(context);
        ASN1OctetString[] octets = new ASN1OctetString[ values.size() ];
        for ( int i = 0; i < values.size(); i++ ) {
            octets[i] = (ASN1OctetString) values.get(i).toASN1Primitive();
        }
        return new BEROctetString(octets);
    case SEQUENCE:
        return new DERSequence( toASN1EncodableVector(context) );
    case SET:
        return new DLSet( toASN1EncodableVector(context) ); // return new BERSet(values);
        //return ASN1Set.getInstance(toASN1TaggedObject(context), isExplicitTagging());
    }
    throw new UnsupportedOperationException( this.inspect().toString() );
}
 

实例 14


private byte[] bitStringToDER(final ThreadContext context) throws IOException {
    final ASN1EncodableVector values = toASN1EncodableVector(context);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    out.write(BERTags.CONSTRUCTED | BERTags.BIT_STRING);
    out.write(0x80); // infinite-length
    for ( int i = 0; i < values.size(); i++ ) {
        out.write( values.get(i).toASN1Primitive().getEncoded() );
    }
    out.write(0x00); out.write(0x00); // writeBEREnd
    return out.toByteArray();
}
 

实例 15


private List<TimestampToken> createTimestamps(final ASN1ObjectIdentifier attrType, final TimestampType timestampType, final ArchiveTimestampType archiveTimestampType) {
                final List<TimestampToken> timestampTokenList = new ArrayList<TimestampToken>();
                final AttributeTable attributes = attrType.equals(id_aa_ets_contentTimestamp) ? signerInformation.getSignedAttributes() : signerInformation.getUnsignedAttributes();
                if (attributes != null) {
                        final ASN1EncodableVector allAttributes = attributes.getAll(attrType);
                        for (int ii = 0; ii < allAttributes.size(); ii++) {
                                final Attribute attribute = (Attribute) allAttributes.get(ii);
                                final ASN1Set attrValues = attribute.getAttrValues();
                                for (final ASN1Encodable value : attrValues.toArray()) {
                                        try {
                                                final byte[] encoded = value.toASN1Primitive().getEncoded(); // getEncoded(ASN1Encoding.DER)
                                                final CMSSignedData signedData = new CMSSignedData(encoded);
                                                final TimeStampToken token = new TimeStampToken(signedData);
                                                final TimestampToken timestampToken = new TimestampToken(token, timestampType, certPool);
                                                timestampToken.setArchiveTimestampType(archiveTimestampType);
                                                timestampTokenList.add(timestampToken);
                                        } catch (Exception e) {
                                                throw new DSSException(e);
                                        }
                                }
                        }
                }
                return timestampTokenList;
        }
 
讨论
淘淘あ西西 profile image