001: package org.bouncycastle.asn1.pkcs;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1Sequence;
006: import org.bouncycastle.asn1.ASN1TaggedObject;
007: import org.bouncycastle.asn1.DERInteger;
008: import org.bouncycastle.asn1.DERNull;
009: import org.bouncycastle.asn1.DERObject;
010: import org.bouncycastle.asn1.DERSequence;
011: import org.bouncycastle.asn1.DERTaggedObject;
012: import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
013: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
014:
015: public class RSASSAPSSparams extends ASN1Encodable {
016: private AlgorithmIdentifier hashAlgorithm;
017: private AlgorithmIdentifier maskGenAlgorithm;
018: private DERInteger saltLength;
019: private DERInteger trailerField;
020:
021: public final static AlgorithmIdentifier DEFAULT_HASH_ALGORITHM = new AlgorithmIdentifier(
022: OIWObjectIdentifiers.idSHA1, new DERNull());
023: public final static AlgorithmIdentifier DEFAULT_MASK_GEN_FUNCTION = new AlgorithmIdentifier(
024: PKCSObjectIdentifiers.id_mgf1, DEFAULT_HASH_ALGORITHM);
025: public final static DERInteger DEFAULT_SALT_LENGTH = new DERInteger(
026: 20);
027: public final static DERInteger DEFAULT_TRAILER_FIELD = new DERInteger(
028: 1);
029:
030: public static RSASSAPSSparams getInstance(Object obj) {
031: if (obj instanceof RSASSAPSSparams) {
032: return (RSASSAPSSparams) obj;
033: } else if (obj instanceof ASN1Sequence) {
034: return new RSASSAPSSparams((ASN1Sequence) obj);
035: }
036:
037: throw new IllegalArgumentException("unknown object in factory");
038: }
039:
040: /**
041: * The default version
042: */
043: public RSASSAPSSparams() {
044: hashAlgorithm = DEFAULT_HASH_ALGORITHM;
045: maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION;
046: saltLength = DEFAULT_SALT_LENGTH;
047: trailerField = DEFAULT_TRAILER_FIELD;
048: }
049:
050: public RSASSAPSSparams(AlgorithmIdentifier hashAlgorithm,
051: AlgorithmIdentifier maskGenAlgorithm,
052: DERInteger saltLength, DERInteger trailerField) {
053: this .hashAlgorithm = hashAlgorithm;
054: this .maskGenAlgorithm = maskGenAlgorithm;
055: this .saltLength = saltLength;
056: this .trailerField = trailerField;
057: }
058:
059: public RSASSAPSSparams(ASN1Sequence seq) {
060: hashAlgorithm = DEFAULT_HASH_ALGORITHM;
061: maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION;
062: saltLength = DEFAULT_SALT_LENGTH;
063: trailerField = DEFAULT_TRAILER_FIELD;
064:
065: for (int i = 0; i != seq.size(); i++) {
066: ASN1TaggedObject o = (ASN1TaggedObject) seq.getObjectAt(i);
067:
068: switch (o.getTagNo()) {
069: case 0:
070: hashAlgorithm = AlgorithmIdentifier
071: .getInstance(o, true);
072: break;
073: case 1:
074: maskGenAlgorithm = AlgorithmIdentifier.getInstance(o,
075: true);
076: break;
077: case 2:
078: saltLength = DERInteger.getInstance(o, true);
079: break;
080: case 3:
081: trailerField = DERInteger.getInstance(o, true);
082: break;
083: default:
084: throw new IllegalArgumentException("unknown tag");
085: }
086: }
087: }
088:
089: public AlgorithmIdentifier getHashAlgorithm() {
090: return hashAlgorithm;
091: }
092:
093: public AlgorithmIdentifier getMaskGenAlgorithm() {
094: return maskGenAlgorithm;
095: }
096:
097: public DERInteger getSaltLength() {
098: return saltLength;
099: }
100:
101: public DERInteger getTrailerField() {
102: return trailerField;
103: }
104:
105: /**
106: * <pre>
107: * RSASSA-PSS-params ::= SEQUENCE {
108: * hashAlgorithm [0] OAEP-PSSDigestAlgorithms DEFAULT sha1,
109: * maskGenAlgorithm [1] PKCS1MGFAlgorithms DEFAULT mgf1SHA1,
110: * saltLength [2] INTEGER DEFAULT 20,
111: * trailerField [3] TrailerField DEFAULT trailerFieldBC
112: * }
113: *
114: * OAEP-PSSDigestAlgorithms ALGORITHM-IDENTIFIER ::= {
115: * { OID id-sha1 PARAMETERS NULL }|
116: * { OID id-sha256 PARAMETERS NULL }|
117: * { OID id-sha384 PARAMETERS NULL }|
118: * { OID id-sha512 PARAMETERS NULL },
119: * ... -- Allows for future expansion --
120: * }
121: *
122: * PKCS1MGFAlgorithms ALGORITHM-IDENTIFIER ::= {
123: * { OID id-mgf1 PARAMETERS OAEP-PSSDigestAlgorithms },
124: * ... -- Allows for future expansion --
125: * }
126: *
127: * TrailerField ::= INTEGER { trailerFieldBC(1) }
128: * </pre>
129: * @return the asn1 primitive representing the parameters.
130: */
131: public DERObject toASN1Object() {
132: ASN1EncodableVector v = new ASN1EncodableVector();
133:
134: if (!hashAlgorithm.equals(DEFAULT_HASH_ALGORITHM)) {
135: v.add(new DERTaggedObject(true, 0, hashAlgorithm));
136: }
137:
138: if (!maskGenAlgorithm.equals(DEFAULT_MASK_GEN_FUNCTION)) {
139: v.add(new DERTaggedObject(true, 1, maskGenAlgorithm));
140: }
141:
142: if (!saltLength.equals(DEFAULT_SALT_LENGTH)) {
143: v.add(new DERTaggedObject(true, 2, saltLength));
144: }
145:
146: if (!trailerField.equals(DEFAULT_TRAILER_FIELD)) {
147: v.add(new DERTaggedObject(true, 3, trailerField));
148: }
149:
150: return new DERSequence(v);
151: }
152: }
|