001: package org.bouncycastle.cms;
002:
003: import org.bouncycastle.asn1.DERNull;
004: import org.bouncycastle.asn1.DERObjectIdentifier;
005: import org.bouncycastle.asn1.DEROctetString;
006: import org.bouncycastle.asn1.DERSet;
007: import org.bouncycastle.asn1.cms.Attribute;
008: import org.bouncycastle.asn1.cms.AttributeTable;
009: import org.bouncycastle.asn1.cms.CMSAttributes;
010: import org.bouncycastle.asn1.cms.Time;
011:
012: import java.util.Date;
013: import java.util.Hashtable;
014: import java.util.Map;
015:
016: /**
017: * Default signed attributes generator.
018: */
019: public class DefaultSignedAttributeTableGenerator implements
020: CMSAttributeTableGenerator {
021: private final Hashtable table;
022:
023: /**
024: * Initialise to use all defaults
025: */
026: public DefaultSignedAttributeTableGenerator() {
027: table = new Hashtable();
028: }
029:
030: /**
031: * Initialise with some extra attributes or overrides.
032: *
033: * @param attributeTable initial attribute table to use.
034: */
035: public DefaultSignedAttributeTableGenerator(
036: AttributeTable attributeTable) {
037: if (attributeTable != null) {
038: table = attributeTable.toHashtable();
039: } else {
040: table = new Hashtable();
041: }
042: }
043:
044: /**
045: * Create a standard attribute table from the passed in parameters - this will
046: * normally include contentType, signingTime, and messageDigest. If the constructor
047: * using an AttributeTable was used, entries in it for contentType, signingTime, and
048: * messageDigest will override the generated ones.
049: *
050: * @param parameters source parameters for table generation.
051: *
052: * @return a filled in Hashtable of attributes.
053: */
054: protected Hashtable createStandardAttributeTable(Map parameters) {
055: Hashtable std = (Hashtable) table.clone();
056:
057: if (table.containsKey(CMSAttributes.contentType)) {
058: std.put(CMSAttributes.contentType, table
059: .get(CMSAttributes.contentType));
060: } else {
061: Attribute attr = new Attribute(
062: CMSAttributes.contentType,
063: new DERSet(
064: (DERObjectIdentifier) parameters
065: .get(CMSAttributeTableGenerator.CONTENT_TYPE)));
066: std.put(attr.getAttrType(), attr);
067: }
068:
069: if (table.containsKey(CMSAttributes.signingTime)) {
070: std.put(CMSAttributes.signingTime, table
071: .get(CMSAttributes.signingTime));
072: } else {
073: Attribute attr = new Attribute(CMSAttributes.signingTime,
074: new DERSet(new Time(new Date())));
075: std.put(attr.getAttrType(), attr);
076: }
077:
078: if (table.containsKey(CMSAttributes.messageDigest)) {
079: std.put(CMSAttributes.messageDigest, table
080: .get(CMSAttributes.messageDigest));
081: } else {
082: byte[] hash = (byte[]) parameters
083: .get(CMSAttributeTableGenerator.DIGEST);
084: Attribute attr;
085:
086: if (hash != null) {
087: attr = new Attribute(CMSAttributes.messageDigest,
088: new DERSet(new DEROctetString(hash)));
089: } else {
090: attr = new Attribute(CMSAttributes.messageDigest,
091: new DERSet(new DERNull()));
092: }
093:
094: std.put(attr.getAttrType(), attr);
095: }
096:
097: return std;
098: }
099:
100: /**
101: * @param parameters source parameters
102: * @return the populated attribute table
103: */
104: public AttributeTable getAttributes(Map parameters) {
105: return new AttributeTable(
106: createStandardAttributeTable(parameters));
107: }
108: }
|