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.DERNull;
008: import org.bouncycastle.asn1.DERObject;
009: import org.bouncycastle.asn1.DEROctetString;
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 RSAESOAEPparams extends ASN1Encodable {
016: private AlgorithmIdentifier hashAlgorithm;
017: private AlgorithmIdentifier maskGenAlgorithm;
018: private AlgorithmIdentifier pSourceAlgorithm;
019:
020: public final static AlgorithmIdentifier DEFAULT_HASH_ALGORITHM = new AlgorithmIdentifier(
021: OIWObjectIdentifiers.idSHA1, new DERNull());
022: public final static AlgorithmIdentifier DEFAULT_MASK_GEN_FUNCTION = new AlgorithmIdentifier(
023: PKCSObjectIdentifiers.id_mgf1, DEFAULT_HASH_ALGORITHM);
024: public final static AlgorithmIdentifier DEFAULT_P_SOURCE_ALGORITHM = new AlgorithmIdentifier(
025: PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(
026: new byte[0]));
027:
028: public static RSAESOAEPparams getInstance(Object obj) {
029: if (obj instanceof RSAESOAEPparams) {
030: return (RSAESOAEPparams) obj;
031: } else if (obj instanceof ASN1Sequence) {
032: return new RSAESOAEPparams((ASN1Sequence) obj);
033: }
034:
035: throw new IllegalArgumentException("unknown object in factory");
036: }
037:
038: /**
039: * The default version
040: */
041: public RSAESOAEPparams() {
042: hashAlgorithm = DEFAULT_HASH_ALGORITHM;
043: maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION;
044: pSourceAlgorithm = DEFAULT_P_SOURCE_ALGORITHM;
045: }
046:
047: public RSAESOAEPparams(AlgorithmIdentifier hashAlgorithm,
048: AlgorithmIdentifier maskGenAlgorithm,
049: AlgorithmIdentifier pSourceAlgorithm) {
050: this .hashAlgorithm = hashAlgorithm;
051: this .maskGenAlgorithm = maskGenAlgorithm;
052: this .pSourceAlgorithm = pSourceAlgorithm;
053: }
054:
055: public RSAESOAEPparams(ASN1Sequence seq) {
056: hashAlgorithm = DEFAULT_HASH_ALGORITHM;
057: maskGenAlgorithm = DEFAULT_MASK_GEN_FUNCTION;
058: pSourceAlgorithm = DEFAULT_P_SOURCE_ALGORITHM;
059:
060: for (int i = 0; i != seq.size(); i++) {
061: ASN1TaggedObject o = (ASN1TaggedObject) seq.getObjectAt(i);
062:
063: switch (o.getTagNo()) {
064: case 0:
065: hashAlgorithm = AlgorithmIdentifier
066: .getInstance(o, true);
067: break;
068: case 1:
069: maskGenAlgorithm = AlgorithmIdentifier.getInstance(o,
070: true);
071: break;
072: case 2:
073: pSourceAlgorithm = AlgorithmIdentifier.getInstance(o,
074: true);
075: break;
076: default:
077: throw new IllegalArgumentException("unknown tag");
078: }
079: }
080: }
081:
082: public AlgorithmIdentifier getHashAlgorithm() {
083: return hashAlgorithm;
084: }
085:
086: public AlgorithmIdentifier getMaskGenAlgorithm() {
087: return maskGenAlgorithm;
088: }
089:
090: public AlgorithmIdentifier getPSourceAlgorithm() {
091: return pSourceAlgorithm;
092: }
093:
094: /**
095: * <pre>
096: * RSAES-OAEP-params ::= SEQUENCE {
097: * hashAlgorithm [0] OAEP-PSSDigestAlgorithms DEFAULT sha1,
098: * maskGenAlgorithm [1] PKCS1MGFAlgorithms DEFAULT mgf1SHA1,
099: * pSourceAlgorithm [2] PKCS1PSourceAlgorithms DEFAULT pSpecifiedEmpty
100: * }
101: *
102: * OAEP-PSSDigestAlgorithms ALGORITHM-IDENTIFIER ::= {
103: * { OID id-sha1 PARAMETERS NULL }|
104: * { OID id-sha256 PARAMETERS NULL }|
105: * { OID id-sha384 PARAMETERS NULL }|
106: * { OID id-sha512 PARAMETERS NULL },
107: * ... -- Allows for future expansion --
108: * }
109: * PKCS1MGFAlgorithms ALGORITHM-IDENTIFIER ::= {
110: * { OID id-mgf1 PARAMETERS OAEP-PSSDigestAlgorithms },
111: * ... -- Allows for future expansion --
112: * }
113: * PKCS1PSourceAlgorithms ALGORITHM-IDENTIFIER ::= {
114: * { OID id-pSpecified PARAMETERS OCTET STRING },
115: * ... -- Allows for future expansion --
116: * }
117: * </pre>
118: * @return the asn1 primitive representing the parameters.
119: */
120: public DERObject toASN1Object() {
121: ASN1EncodableVector v = new ASN1EncodableVector();
122:
123: if (!hashAlgorithm.equals(DEFAULT_HASH_ALGORITHM)) {
124: v.add(new DERTaggedObject(true, 0, hashAlgorithm));
125: }
126:
127: if (!maskGenAlgorithm.equals(DEFAULT_MASK_GEN_FUNCTION)) {
128: v.add(new DERTaggedObject(true, 1, maskGenAlgorithm));
129: }
130:
131: if (!pSourceAlgorithm.equals(DEFAULT_P_SOURCE_ALGORITHM)) {
132: v.add(new DERTaggedObject(true, 2, pSourceAlgorithm));
133: }
134:
135: return new DERSequence(v);
136: }
137: }
|