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.DERNull; 8 import org.bouncycastle.asn1.DERObject; 9 import org.bouncycastle.asn1.DEROctetString; 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 RSAESOAEPparams 16 extends ASN1Encodable 17 { 18 private AlgorithmIdentifier hashAlgorithm; 19 private AlgorithmIdentifier maskGenAlgorithm; 20 private AlgorithmIdentifier pSourceAlgorithm; 21 22 // BEGIN android-changed 23 public final static AlgorithmIdentifier DEFAULT_HASH_ALGORITHM = new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, DERNull.INSTANCE); 24 // END android-changed 25 public final static AlgorithmIdentifier DEFAULT_MASK_GEN_FUNCTION = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, DEFAULT_HASH_ALGORITHM); 26 public final static AlgorithmIdentifier DEFAULT_P_SOURCE_ALGORITHM = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(new byte[0])); 27 28 public static RSAESOAEPparams getInstance( 29 Object obj) 30 { 31 if (obj instanceof RSAESOAEPparams) 32 { 33 return (RSAESOAEPparams)obj; 34 } 35 else if (obj instanceof ASN1Sequence) 36 { 37 return new RSAESOAEPparams((ASN1Sequence)obj); 38 } 39 40 throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName()); 41 } 42 43 /** 44 * The default version 45 */ 46 public RSAESOAEPparams() 47 { 48 hashAlgorithm = DEFAULT_HASH_ALGORITHM; 49 maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION; 50 pSourceAlgorithm = DEFAULT_P_SOURCE_ALGORITHM; 51 } 52 53 public RSAESOAEPparams( 54 AlgorithmIdentifier hashAlgorithm, 55 AlgorithmIdentifier maskGenAlgorithm, 56 AlgorithmIdentifier pSourceAlgorithm) 57 { 58 this.hashAlgorithm = hashAlgorithm; 59 this.maskGenAlgorithm = maskGenAlgorithm; 60 this.pSourceAlgorithm = pSourceAlgorithm; 61 } 62 63 public RSAESOAEPparams( 64 ASN1Sequence seq) 65 { 66 hashAlgorithm = DEFAULT_HASH_ALGORITHM; 67 maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION; 68 pSourceAlgorithm = DEFAULT_P_SOURCE_ALGORITHM; 69 70 for (int i = 0; i != seq.size(); i++) 71 { 72 ASN1TaggedObject o = (ASN1TaggedObject)seq.getObjectAt(i); 73 74 switch (o.getTagNo()) 75 { 76 case 0: 77 hashAlgorithm = AlgorithmIdentifier.getInstance(o, true); 78 break; 79 case 1: 80 maskGenAlgorithm = AlgorithmIdentifier.getInstance(o, true); 81 break; 82 case 2: 83 pSourceAlgorithm = AlgorithmIdentifier.getInstance(o, true); 84 break; 85 default: 86 throw new IllegalArgumentException("unknown tag"); 87 } 88 } 89 } 90 91 public AlgorithmIdentifier getHashAlgorithm() 92 { 93 return hashAlgorithm; 94 } 95 96 public AlgorithmIdentifier getMaskGenAlgorithm() 97 { 98 return maskGenAlgorithm; 99 } 100 101 public AlgorithmIdentifier getPSourceAlgorithm() 102 { 103 return pSourceAlgorithm; 104 } 105 106 /** 107 * <pre> 108 * RSAES-OAEP-params ::= SEQUENCE { 109 * hashAlgorithm [0] OAEP-PSSDigestAlgorithms DEFAULT sha1, 110 * maskGenAlgorithm [1] PKCS1MGFAlgorithms DEFAULT mgf1SHA1, 111 * pSourceAlgorithm [2] PKCS1PSourceAlgorithms DEFAULT pSpecifiedEmpty 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 * PKCS1MGFAlgorithms ALGORITHM-IDENTIFIER ::= { 122 * { OID id-mgf1 PARAMETERS OAEP-PSSDigestAlgorithms }, 123 * ... -- Allows for future expansion -- 124 * } 125 * PKCS1PSourceAlgorithms ALGORITHM-IDENTIFIER ::= { 126 * { OID id-pSpecified PARAMETERS OCTET STRING }, 127 * ... -- Allows for future expansion -- 128 * } 129 * </pre> 130 * @return the asn1 primitive representing the parameters. 131 */ 132 public DERObject toASN1Object() 133 { 134 ASN1EncodableVector v = new ASN1EncodableVector(); 135 136 if (!hashAlgorithm.equals(DEFAULT_HASH_ALGORITHM)) 137 { 138 v.add(new DERTaggedObject(true, 0, hashAlgorithm)); 139 } 140 141 if (!maskGenAlgorithm.equals(DEFAULT_MASK_GEN_FUNCTION)) 142 { 143 v.add(new DERTaggedObject(true, 1, maskGenAlgorithm)); 144 } 145 146 if (!pSourceAlgorithm.equals(DEFAULT_P_SOURCE_ALGORITHM)) 147 { 148 v.add(new DERTaggedObject(true, 2, pSourceAlgorithm)); 149 } 150 151 return new DERSequence(v); 152 } 153 } 154