1 package org.bouncycastle.asn1.pkcs; 2 3 import org.bouncycastle.asn1.ASN1Encodable; 4 import org.bouncycastle.asn1.ASN1EncodableVector; 5 import org.bouncycastle.asn1.ASN1Sequence; 6 import org.bouncycastle.asn1.ASN1TaggedObject; 7 import org.bouncycastle.asn1.DERInteger; 8 import org.bouncycastle.asn1.DERNull; 9 import org.bouncycastle.asn1.DERObject; 10 import org.bouncycastle.asn1.DERSequence; 11 import org.bouncycastle.asn1.DERTaggedObject; 12 import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers; 13 import org.bouncycastle.asn1.x509.AlgorithmIdentifier; 14 15 public class RSASSAPSSparams 16 extends ASN1Encodable 17 { 18 private AlgorithmIdentifier hashAlgorithm; 19 private AlgorithmIdentifier maskGenAlgorithm; 20 private DERInteger saltLength; 21 private DERInteger trailerField; 22 23 // BEGIN android-changed 24 public final static AlgorithmIdentifier DEFAULT_HASH_ALGORITHM = new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, DERNull.INSTANCE); 25 // END android-changed 26 public final static AlgorithmIdentifier DEFAULT_MASK_GEN_FUNCTION = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, DEFAULT_HASH_ALGORITHM); 27 public final static DERInteger DEFAULT_SALT_LENGTH = new DERInteger(20); 28 public final static DERInteger DEFAULT_TRAILER_FIELD = new DERInteger(1); 29 30 public static RSASSAPSSparams getInstance( 31 Object obj) 32 { 33 if (obj == null || obj instanceof RSASSAPSSparams) 34 { 35 return (RSASSAPSSparams)obj; 36 } 37 else if (obj instanceof ASN1Sequence) 38 { 39 return new RSASSAPSSparams((ASN1Sequence)obj); 40 } 41 42 throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName()); 43 } 44 45 /** 46 * The default version 47 */ 48 public RSASSAPSSparams() 49 { 50 hashAlgorithm = DEFAULT_HASH_ALGORITHM; 51 maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION; 52 saltLength = DEFAULT_SALT_LENGTH; 53 trailerField = DEFAULT_TRAILER_FIELD; 54 } 55 56 public RSASSAPSSparams( 57 AlgorithmIdentifier hashAlgorithm, 58 AlgorithmIdentifier maskGenAlgorithm, 59 DERInteger saltLength, 60 DERInteger trailerField) 61 { 62 this.hashAlgorithm = hashAlgorithm; 63 this.maskGenAlgorithm = maskGenAlgorithm; 64 this.saltLength = saltLength; 65 this.trailerField = trailerField; 66 } 67 68 public RSASSAPSSparams( 69 ASN1Sequence seq) 70 { 71 hashAlgorithm = DEFAULT_HASH_ALGORITHM; 72 maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION; 73 saltLength = DEFAULT_SALT_LENGTH; 74 trailerField = DEFAULT_TRAILER_FIELD; 75 76 for (int i = 0; i != seq.size(); i++) 77 { 78 ASN1TaggedObject o = (ASN1TaggedObject)seq.getObjectAt(i); 79 80 switch (o.getTagNo()) 81 { 82 case 0: 83 hashAlgorithm = AlgorithmIdentifier.getInstance(o, true); 84 break; 85 case 1: 86 maskGenAlgorithm = AlgorithmIdentifier.getInstance(o, true); 87 break; 88 case 2: 89 saltLength = DERInteger.getInstance(o, true); 90 break; 91 case 3: 92 trailerField = DERInteger.getInstance(o, true); 93 break; 94 default: 95 throw new IllegalArgumentException("unknown tag"); 96 } 97 } 98 } 99 100 public AlgorithmIdentifier getHashAlgorithm() 101 { 102 return hashAlgorithm; 103 } 104 105 public AlgorithmIdentifier getMaskGenAlgorithm() 106 { 107 return maskGenAlgorithm; 108 } 109 110 public DERInteger getSaltLength() 111 { 112 return saltLength; 113 } 114 115 public DERInteger getTrailerField() 116 { 117 return trailerField; 118 } 119 120 /** 121 * <pre> 122 * RSASSA-PSS-params ::= SEQUENCE { 123 * hashAlgorithm [0] OAEP-PSSDigestAlgorithms DEFAULT sha1, 124 * maskGenAlgorithm [1] PKCS1MGFAlgorithms DEFAULT mgf1SHA1, 125 * saltLength [2] INTEGER DEFAULT 20, 126 * trailerField [3] TrailerField DEFAULT trailerFieldBC 127 * } 128 * 129 * OAEP-PSSDigestAlgorithms ALGORITHM-IDENTIFIER ::= { 130 * { OID id-sha1 PARAMETERS NULL }| 131 * { OID id-sha256 PARAMETERS NULL }| 132 * { OID id-sha384 PARAMETERS NULL }| 133 * { OID id-sha512 PARAMETERS NULL }, 134 * ... -- Allows for future expansion -- 135 * } 136 * 137 * PKCS1MGFAlgorithms ALGORITHM-IDENTIFIER ::= { 138 * { OID id-mgf1 PARAMETERS OAEP-PSSDigestAlgorithms }, 139 * ... -- Allows for future expansion -- 140 * } 141 * 142 * TrailerField ::= INTEGER { trailerFieldBC(1) } 143 * </pre> 144 * @return the asn1 primitive representing the parameters. 145 */ 146 public DERObject toASN1Object() 147 { 148 ASN1EncodableVector v = new ASN1EncodableVector(); 149 150 if (!hashAlgorithm.equals(DEFAULT_HASH_ALGORITHM)) 151 { 152 v.add(new DERTaggedObject(true, 0, hashAlgorithm)); 153 } 154 155 if (!maskGenAlgorithm.equals(DEFAULT_MASK_GEN_FUNCTION)) 156 { 157 v.add(new DERTaggedObject(true, 1, maskGenAlgorithm)); 158 } 159 160 if (!saltLength.equals(DEFAULT_SALT_LENGTH)) 161 { 162 v.add(new DERTaggedObject(true, 2, saltLength)); 163 } 164 165 if (!trailerField.equals(DEFAULT_TRAILER_FIELD)) 166 { 167 v.add(new DERTaggedObject(true, 3, trailerField)); 168 } 169 170 return new DERSequence(v); 171 } 172 } 173