0001: package org.bouncycastle.jce.provider;
0002:
0003: import org.bouncycastle.asn1.ASN1EncodableVector;
0004: import org.bouncycastle.asn1.ASN1InputStream;
0005: import org.bouncycastle.asn1.ASN1OctetString;
0006: import org.bouncycastle.asn1.ASN1Sequence;
0007: import org.bouncycastle.asn1.DERInteger;
0008: import org.bouncycastle.asn1.DERNull;
0009: import org.bouncycastle.asn1.DERObjectIdentifier;
0010: import org.bouncycastle.asn1.DEROctetString;
0011: import org.bouncycastle.asn1.DEROutputStream;
0012: import org.bouncycastle.asn1.DERSequence;
0013: import org.bouncycastle.asn1.cryptopro.GOST3410PublicKeyAlgParameters;
0014: import org.bouncycastle.asn1.misc.IDEACBCPar;
0015: import org.bouncycastle.asn1.oiw.ElGamalParameter;
0016: import org.bouncycastle.asn1.pkcs.DHParameter;
0017: import org.bouncycastle.asn1.pkcs.PKCS12PBEParams;
0018: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
0019: import org.bouncycastle.asn1.pkcs.RC2CBCParameter;
0020: import org.bouncycastle.asn1.pkcs.RSAESOAEPparams;
0021: import org.bouncycastle.asn1.pkcs.RSASSAPSSparams;
0022: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
0023: import org.bouncycastle.asn1.x509.DSAParameter;
0024: import org.bouncycastle.jce.spec.ElGamalParameterSpec;
0025: import org.bouncycastle.jce.spec.GOST3410ParameterSpec;
0026: import org.bouncycastle.jce.spec.GOST3410PublicKeyParameterSetSpec;
0027: import org.bouncycastle.jce.spec.IESParameterSpec;
0028:
0029: import javax.crypto.spec.DHParameterSpec;
0030: import javax.crypto.spec.IvParameterSpec;
0031: import javax.crypto.spec.OAEPParameterSpec;
0032: import javax.crypto.spec.PBEParameterSpec;
0033: import javax.crypto.spec.PSource;
0034: import javax.crypto.spec.RC2ParameterSpec;
0035: import java.io.ByteArrayOutputStream;
0036: import java.io.IOException;
0037: import java.security.AlgorithmParametersSpi;
0038: import java.security.spec.AlgorithmParameterSpec;
0039: import java.security.spec.DSAParameterSpec;
0040: import java.security.spec.InvalidParameterSpecException;
0041: import java.security.spec.MGF1ParameterSpec;
0042: import java.security.spec.PSSParameterSpec;
0043:
0044: public abstract class JDKAlgorithmParameters extends
0045: AlgorithmParametersSpi {
0046: protected boolean isASN1FormatString(String format) {
0047: return format == null || format.equals("ASN.1");
0048: }
0049:
0050: protected AlgorithmParameterSpec engineGetParameterSpec(
0051: Class paramSpec) throws InvalidParameterSpecException {
0052: if (paramSpec == null) {
0053: throw new NullPointerException(
0054: "argument to getParameterSpec must not be null");
0055: }
0056:
0057: return localEngineGetParameterSpec(paramSpec);
0058: }
0059:
0060: protected abstract AlgorithmParameterSpec localEngineGetParameterSpec(
0061: Class paramSpec) throws InvalidParameterSpecException;
0062:
0063: public static class IVAlgorithmParameters extends
0064: JDKAlgorithmParameters {
0065: private byte[] iv;
0066:
0067: protected byte[] engineGetEncoded() throws IOException {
0068: return engineGetEncoded("ASN.1");
0069: }
0070:
0071: protected byte[] engineGetEncoded(String format)
0072: throws IOException {
0073: if (isASN1FormatString(format)) {
0074: return new DEROctetString(engineGetEncoded("RAW"))
0075: .getEncoded();
0076: }
0077:
0078: if (format.equals("RAW")) {
0079: byte[] tmp = new byte[iv.length];
0080:
0081: System.arraycopy(iv, 0, tmp, 0, iv.length);
0082: return tmp;
0083: }
0084:
0085: return null;
0086: }
0087:
0088: protected AlgorithmParameterSpec localEngineGetParameterSpec(
0089: Class paramSpec) throws InvalidParameterSpecException {
0090: if (paramSpec == IvParameterSpec.class) {
0091: return new IvParameterSpec(iv);
0092: }
0093:
0094: throw new InvalidParameterSpecException(
0095: "unknown parameter spec passed to IV parameters object.");
0096: }
0097:
0098: protected void engineInit(AlgorithmParameterSpec paramSpec)
0099: throws InvalidParameterSpecException {
0100: if (!(paramSpec instanceof IvParameterSpec)) {
0101: throw new InvalidParameterSpecException(
0102: "IvParameterSpec required to initialise a IV parameters algorithm parameters object");
0103: }
0104:
0105: this .iv = ((IvParameterSpec) paramSpec).getIV();
0106: }
0107:
0108: protected void engineInit(byte[] params) throws IOException {
0109: //
0110: // check that we don't have a DER encoded octet string
0111: //
0112: if ((params.length % 8) != 0 && params[0] == 0x04
0113: && params[1] == params.length - 2) {
0114: ASN1InputStream aIn = new ASN1InputStream(params);
0115: ASN1OctetString oct = (ASN1OctetString) aIn
0116: .readObject();
0117:
0118: params = oct.getOctets();
0119: }
0120:
0121: this .iv = new byte[params.length];
0122:
0123: System.arraycopy(params, 0, iv, 0, iv.length);
0124: }
0125:
0126: protected void engineInit(byte[] params, String format)
0127: throws IOException {
0128: if (isASN1FormatString(format)) {
0129: ASN1InputStream aIn = new ASN1InputStream(params);
0130:
0131: try {
0132: ASN1OctetString oct = (ASN1OctetString) aIn
0133: .readObject();
0134:
0135: engineInit(oct.getOctets());
0136: } catch (Exception e) {
0137: throw new IOException("Exception decoding: " + e);
0138: }
0139:
0140: return;
0141: }
0142:
0143: if (format.equals("RAW")) {
0144: engineInit(params);
0145: return;
0146: }
0147:
0148: throw new IOException(
0149: "Unknown parameters format in IV parameters object");
0150: }
0151:
0152: protected String engineToString() {
0153: return "IV Parameters";
0154: }
0155: }
0156:
0157: public static class IDEAAlgorithmParameters extends
0158: JDKAlgorithmParameters {
0159: private byte[] iv;
0160:
0161: protected byte[] engineGetEncoded() throws IOException {
0162: return engineGetEncoded("ASN.1");
0163: }
0164:
0165: protected byte[] engineGetEncoded(String format)
0166: throws IOException {
0167: if (isASN1FormatString(format)) {
0168: return new IDEACBCPar(engineGetEncoded("RAW"))
0169: .getEncoded();
0170: }
0171:
0172: if (format.equals("RAW")) {
0173: byte[] tmp = new byte[iv.length];
0174:
0175: System.arraycopy(iv, 0, tmp, 0, iv.length);
0176: return tmp;
0177: }
0178:
0179: return null;
0180: }
0181:
0182: protected AlgorithmParameterSpec localEngineGetParameterSpec(
0183: Class paramSpec) throws InvalidParameterSpecException {
0184: if (paramSpec == IvParameterSpec.class) {
0185: return new IvParameterSpec(iv);
0186: }
0187:
0188: throw new InvalidParameterSpecException(
0189: "unknown parameter spec passed to IV parameters object.");
0190: }
0191:
0192: protected void engineInit(AlgorithmParameterSpec paramSpec)
0193: throws InvalidParameterSpecException {
0194: if (!(paramSpec instanceof IvParameterSpec)) {
0195: throw new InvalidParameterSpecException(
0196: "IvParameterSpec required to initialise a IV parameters algorithm parameters object");
0197: }
0198:
0199: this .iv = ((IvParameterSpec) paramSpec).getIV();
0200: }
0201:
0202: protected void engineInit(byte[] params) throws IOException {
0203: this .iv = new byte[params.length];
0204:
0205: System.arraycopy(params, 0, iv, 0, iv.length);
0206: }
0207:
0208: protected void engineInit(byte[] params, String format)
0209: throws IOException {
0210: if (format.equals("RAW")) {
0211: engineInit(params);
0212: return;
0213: }
0214: if (format.equals("ASN.1")) {
0215: ASN1InputStream aIn = new ASN1InputStream(params);
0216: IDEACBCPar oct = new IDEACBCPar((ASN1Sequence) aIn
0217: .readObject());
0218:
0219: engineInit(oct.getIV());
0220: return;
0221: }
0222:
0223: throw new IOException(
0224: "Unknown parameters format in IV parameters object");
0225: }
0226:
0227: protected String engineToString() {
0228: return "IDEA Parameters";
0229: }
0230: }
0231:
0232: public static class RC2AlgorithmParameters extends
0233: JDKAlgorithmParameters {
0234: private short[] table = { 0xbd, 0x56, 0xea, 0xf2, 0xa2, 0xf1,
0235: 0xac, 0x2a, 0xb0, 0x93, 0xd1, 0x9c, 0x1b, 0x33, 0xfd,
0236: 0xd0, 0x30, 0x04, 0xb6, 0xdc, 0x7d, 0xdf, 0x32, 0x4b,
0237: 0xf7, 0xcb, 0x45, 0x9b, 0x31, 0xbb, 0x21, 0x5a, 0x41,
0238: 0x9f, 0xe1, 0xd9, 0x4a, 0x4d, 0x9e, 0xda, 0xa0, 0x68,
0239: 0x2c, 0xc3, 0x27, 0x5f, 0x80, 0x36, 0x3e, 0xee, 0xfb,
0240: 0x95, 0x1a, 0xfe, 0xce, 0xa8, 0x34, 0xa9, 0x13, 0xf0,
0241: 0xa6, 0x3f, 0xd8, 0x0c, 0x78, 0x24, 0xaf, 0x23, 0x52,
0242: 0xc1, 0x67, 0x17, 0xf5, 0x66, 0x90, 0xe7, 0xe8, 0x07,
0243: 0xb8, 0x60, 0x48, 0xe6, 0x1e, 0x53, 0xf3, 0x92, 0xa4,
0244: 0x72, 0x8c, 0x08, 0x15, 0x6e, 0x86, 0x00, 0x84, 0xfa,
0245: 0xf4, 0x7f, 0x8a, 0x42, 0x19, 0xf6, 0xdb, 0xcd, 0x14,
0246: 0x8d, 0x50, 0x12, 0xba, 0x3c, 0x06, 0x4e, 0xec, 0xb3,
0247: 0x35, 0x11, 0xa1, 0x88, 0x8e, 0x2b, 0x94, 0x99, 0xb7,
0248: 0x71, 0x74, 0xd3, 0xe4, 0xbf, 0x3a, 0xde, 0x96, 0x0e,
0249: 0xbc, 0x0a, 0xed, 0x77, 0xfc, 0x37, 0x6b, 0x03, 0x79,
0250: 0x89, 0x62, 0xc6, 0xd7, 0xc0, 0xd2, 0x7c, 0x6a, 0x8b,
0251: 0x22, 0xa3, 0x5b, 0x05, 0x5d, 0x02, 0x75, 0xd5, 0x61,
0252: 0xe3, 0x18, 0x8f, 0x55, 0x51, 0xad, 0x1f, 0x0b, 0x5e,
0253: 0x85, 0xe5, 0xc2, 0x57, 0x63, 0xca, 0x3d, 0x6c, 0xb4,
0254: 0xc5, 0xcc, 0x70, 0xb2, 0x91, 0x59, 0x0d, 0x47, 0x20,
0255: 0xc8, 0x4f, 0x58, 0xe0, 0x01, 0xe2, 0x16, 0x38, 0xc4,
0256: 0x6f, 0x3b, 0x0f, 0x65, 0x46, 0xbe, 0x7e, 0x2d, 0x7b,
0257: 0x82, 0xf9, 0x40, 0xb5, 0x1d, 0x73, 0xf8, 0xeb, 0x26,
0258: 0xc7, 0x87, 0x97, 0x25, 0x54, 0xb1, 0x28, 0xaa, 0x98,
0259: 0x9d, 0xa5, 0x64, 0x6d, 0x7a, 0xd4, 0x10, 0x81, 0x44,
0260: 0xef, 0x49, 0xd6, 0xae, 0x2e, 0xdd, 0x76, 0x5c, 0x2f,
0261: 0xa7, 0x1c, 0xc9, 0x09, 0x69, 0x9a, 0x83, 0xcf, 0x29,
0262: 0x39, 0xb9, 0xe9, 0x4c, 0xff, 0x43, 0xab };
0263:
0264: private short[] ekb = { 0x5d, 0xbe, 0x9b, 0x8b, 0x11, 0x99,
0265: 0x6e, 0x4d, 0x59, 0xf3, 0x85, 0xa6, 0x3f, 0xb7, 0x83,
0266: 0xc5, 0xe4, 0x73, 0x6b, 0x3a, 0x68, 0x5a, 0xc0, 0x47,
0267: 0xa0, 0x64, 0x34, 0x0c, 0xf1, 0xd0, 0x52, 0xa5, 0xb9,
0268: 0x1e, 0x96, 0x43, 0x41, 0xd8, 0xd4, 0x2c, 0xdb, 0xf8,
0269: 0x07, 0x77, 0x2a, 0xca, 0xeb, 0xef, 0x10, 0x1c, 0x16,
0270: 0x0d, 0x38, 0x72, 0x2f, 0x89, 0xc1, 0xf9, 0x80, 0xc4,
0271: 0x6d, 0xae, 0x30, 0x3d, 0xce, 0x20, 0x63, 0xfe, 0xe6,
0272: 0x1a, 0xc7, 0xb8, 0x50, 0xe8, 0x24, 0x17, 0xfc, 0x25,
0273: 0x6f, 0xbb, 0x6a, 0xa3, 0x44, 0x53, 0xd9, 0xa2, 0x01,
0274: 0xab, 0xbc, 0xb6, 0x1f, 0x98, 0xee, 0x9a, 0xa7, 0x2d,
0275: 0x4f, 0x9e, 0x8e, 0xac, 0xe0, 0xc6, 0x49, 0x46, 0x29,
0276: 0xf4, 0x94, 0x8a, 0xaf, 0xe1, 0x5b, 0xc3, 0xb3, 0x7b,
0277: 0x57, 0xd1, 0x7c, 0x9c, 0xed, 0x87, 0x40, 0x8c, 0xe2,
0278: 0xcb, 0x93, 0x14, 0xc9, 0x61, 0x2e, 0xe5, 0xcc, 0xf6,
0279: 0x5e, 0xa8, 0x5c, 0xd6, 0x75, 0x8d, 0x62, 0x95, 0x58,
0280: 0x69, 0x76, 0xa1, 0x4a, 0xb5, 0x55, 0x09, 0x78, 0x33,
0281: 0x82, 0xd7, 0xdd, 0x79, 0xf5, 0x1b, 0x0b, 0xde, 0x26,
0282: 0x21, 0x28, 0x74, 0x04, 0x97, 0x56, 0xdf, 0x3c, 0xf0,
0283: 0x37, 0x39, 0xdc, 0xff, 0x06, 0xa4, 0xea, 0x42, 0x08,
0284: 0xda, 0xb4, 0x71, 0xb0, 0xcf, 0x12, 0x7a, 0x4e, 0xfa,
0285: 0x6c, 0x1d, 0x84, 0x00, 0xc8, 0x7f, 0x91, 0x45, 0xaa,
0286: 0x2b, 0xc2, 0xb1, 0x8f, 0xd5, 0xba, 0xf2, 0xad, 0x19,
0287: 0xb2, 0x67, 0x36, 0xf7, 0x0f, 0x0a, 0x92, 0x7d, 0xe3,
0288: 0x9d, 0xe9, 0x90, 0x3e, 0x23, 0x27, 0x66, 0x13, 0xec,
0289: 0x81, 0x15, 0xbd, 0x22, 0xbf, 0x9f, 0x7e, 0xa9, 0x51,
0290: 0x4b, 0x4c, 0xfb, 0x02, 0xd3, 0x70, 0x86, 0x31, 0xe7,
0291: 0x3b, 0x05, 0x03, 0x54, 0x60, 0x48, 0x65, 0x18, 0xd2,
0292: 0xcd, 0x5f, 0x32, 0x88, 0x0e, 0x35, 0xfd };
0293:
0294: private byte[] iv;
0295: private int parameterVersion = 58;
0296:
0297: protected byte[] engineGetEncoded() {
0298: byte[] tmp = new byte[iv.length];
0299:
0300: System.arraycopy(iv, 0, tmp, 0, iv.length);
0301: return tmp;
0302: }
0303:
0304: protected byte[] engineGetEncoded(String format)
0305: throws IOException {
0306: if (isASN1FormatString(format)) {
0307: if (parameterVersion == -1) {
0308: return new RC2CBCParameter(engineGetEncoded())
0309: .getEncoded();
0310: } else {
0311: return new RC2CBCParameter(parameterVersion,
0312: engineGetEncoded()).getEncoded();
0313: }
0314: }
0315:
0316: if (format.equals("RAW")) {
0317: return engineGetEncoded();
0318: }
0319:
0320: return null;
0321: }
0322:
0323: protected AlgorithmParameterSpec localEngineGetParameterSpec(
0324: Class paramSpec) throws InvalidParameterSpecException {
0325: if (paramSpec == RC2ParameterSpec.class) {
0326: if (parameterVersion != -1) {
0327: if (parameterVersion < 256) {
0328: return new RC2ParameterSpec(
0329: ekb[parameterVersion], iv);
0330: } else {
0331: return new RC2ParameterSpec(parameterVersion,
0332: iv);
0333: }
0334: }
0335: }
0336:
0337: if (paramSpec == IvParameterSpec.class) {
0338: return new IvParameterSpec(iv);
0339: }
0340:
0341: throw new InvalidParameterSpecException(
0342: "unknown parameter spec passed to RC2 parameters object.");
0343: }
0344:
0345: protected void engineInit(AlgorithmParameterSpec paramSpec)
0346: throws InvalidParameterSpecException {
0347: if (paramSpec instanceof IvParameterSpec) {
0348: this .iv = ((IvParameterSpec) paramSpec).getIV();
0349: } else if (paramSpec instanceof RC2ParameterSpec) {
0350: int effKeyBits = ((RC2ParameterSpec) paramSpec)
0351: .getEffectiveKeyBits();
0352: if (effKeyBits != -1) {
0353: if (effKeyBits < 256) {
0354: parameterVersion = table[effKeyBits];
0355: } else {
0356: parameterVersion = effKeyBits;
0357: }
0358: }
0359:
0360: this .iv = ((RC2ParameterSpec) paramSpec).getIV();
0361: } else {
0362: throw new InvalidParameterSpecException(
0363: "IvParameterSpec or RC2ParameterSpec required to initialise a RC2 parameters algorithm parameters object");
0364: }
0365: }
0366:
0367: protected void engineInit(byte[] params) throws IOException {
0368: this .iv = new byte[params.length];
0369:
0370: System.arraycopy(params, 0, iv, 0, iv.length);
0371: }
0372:
0373: protected void engineInit(byte[] params, String format)
0374: throws IOException {
0375: if (isASN1FormatString(format)) {
0376: ASN1InputStream aIn = new ASN1InputStream(params);
0377: RC2CBCParameter p = RC2CBCParameter.getInstance(aIn
0378: .readObject());
0379:
0380: if (p.getRC2ParameterVersion() != null) {
0381: parameterVersion = p.getRC2ParameterVersion()
0382: .intValue();
0383: }
0384:
0385: iv = p.getIV();
0386:
0387: return;
0388: }
0389:
0390: if (format.equals("RAW")) {
0391: engineInit(params);
0392: return;
0393: }
0394:
0395: throw new IOException(
0396: "Unknown parameters format in IV parameters object");
0397: }
0398:
0399: protected String engineToString() {
0400: return "RC2 Parameters";
0401: }
0402: }
0403:
0404: public static class PKCS12PBE extends JDKAlgorithmParameters {
0405: PKCS12PBEParams params;
0406:
0407: protected byte[] engineGetEncoded() {
0408: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
0409: DEROutputStream dOut = new DEROutputStream(bOut);
0410:
0411: try {
0412: dOut.writeObject(params);
0413: } catch (IOException e) {
0414: throw new RuntimeException("Oooops! " + e.toString());
0415: }
0416:
0417: return bOut.toByteArray();
0418: }
0419:
0420: protected byte[] engineGetEncoded(String format) {
0421: if (isASN1FormatString(format)) {
0422: return engineGetEncoded();
0423: }
0424:
0425: return null;
0426: }
0427:
0428: protected AlgorithmParameterSpec localEngineGetParameterSpec(
0429: Class paramSpec) throws InvalidParameterSpecException {
0430: if (paramSpec == PBEParameterSpec.class) {
0431: return new PBEParameterSpec(params.getIV(), params
0432: .getIterations().intValue());
0433: }
0434:
0435: throw new InvalidParameterSpecException(
0436: "unknown parameter spec passed to PKCS12 PBE parameters object.");
0437: }
0438:
0439: protected void engineInit(AlgorithmParameterSpec paramSpec)
0440: throws InvalidParameterSpecException {
0441: if (!(paramSpec instanceof PBEParameterSpec)) {
0442: throw new InvalidParameterSpecException(
0443: "PBEParameterSpec required to initialise a PKCS12 PBE parameters algorithm parameters object");
0444: }
0445:
0446: PBEParameterSpec pbeSpec = (PBEParameterSpec) paramSpec;
0447:
0448: this .params = new PKCS12PBEParams(pbeSpec.getSalt(),
0449: pbeSpec.getIterationCount());
0450: }
0451:
0452: protected void engineInit(byte[] params) throws IOException {
0453: ASN1InputStream aIn = new ASN1InputStream(params);
0454:
0455: this .params = PKCS12PBEParams.getInstance(aIn.readObject());
0456: }
0457:
0458: protected void engineInit(byte[] params, String format)
0459: throws IOException {
0460: if (isASN1FormatString(format)) {
0461: engineInit(params);
0462: return;
0463: }
0464:
0465: throw new IOException(
0466: "Unknown parameters format in PKCS12 PBE parameters object");
0467: }
0468:
0469: protected String engineToString() {
0470: return "PKCS12 PBE Parameters";
0471: }
0472: }
0473:
0474: public static class DH extends JDKAlgorithmParameters {
0475: DHParameterSpec currentSpec;
0476:
0477: /**
0478: * Return the PKCS#3 ASN.1 structure DHParameter.
0479: * <p>
0480: * <pre>
0481: * DHParameter ::= SEQUENCE {
0482: * prime INTEGER, -- p
0483: * base INTEGER, -- g
0484: * privateValueLength INTEGER OPTIONAL}
0485: * </pre>
0486: */
0487: protected byte[] engineGetEncoded() {
0488: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
0489: DEROutputStream dOut = new DEROutputStream(bOut);
0490: DHParameter dhP = new DHParameter(currentSpec.getP(),
0491: currentSpec.getG(), currentSpec.getL());
0492:
0493: try {
0494: dOut.writeObject(dhP);
0495: dOut.close();
0496: } catch (IOException e) {
0497: throw new RuntimeException(
0498: "Error encoding DHParameters");
0499: }
0500:
0501: return bOut.toByteArray();
0502: }
0503:
0504: protected byte[] engineGetEncoded(String format) {
0505: if (isASN1FormatString(format)) {
0506: return engineGetEncoded();
0507: }
0508:
0509: return null;
0510: }
0511:
0512: protected AlgorithmParameterSpec localEngineGetParameterSpec(
0513: Class paramSpec) throws InvalidParameterSpecException {
0514: if (paramSpec == DHParameterSpec.class) {
0515: return currentSpec;
0516: }
0517:
0518: throw new InvalidParameterSpecException(
0519: "unknown parameter spec passed to DH parameters object.");
0520: }
0521:
0522: protected void engineInit(AlgorithmParameterSpec paramSpec)
0523: throws InvalidParameterSpecException {
0524: if (!(paramSpec instanceof DHParameterSpec)) {
0525: throw new InvalidParameterSpecException(
0526: "DHParameterSpec required to initialise a Diffie-Hellman algorithm parameters object");
0527: }
0528:
0529: this .currentSpec = (DHParameterSpec) paramSpec;
0530: }
0531:
0532: protected void engineInit(byte[] params) throws IOException {
0533: ASN1InputStream aIn = new ASN1InputStream(params);
0534:
0535: try {
0536: DHParameter dhP = new DHParameter((ASN1Sequence) aIn
0537: .readObject());
0538:
0539: if (dhP.getL() != null) {
0540: currentSpec = new DHParameterSpec(dhP.getP(), dhP
0541: .getG(), dhP.getL().intValue());
0542: } else {
0543: currentSpec = new DHParameterSpec(dhP.getP(), dhP
0544: .getG());
0545: }
0546: } catch (ClassCastException e) {
0547: throw new IOException(
0548: "Not a valid DH Parameter encoding.");
0549: } catch (ArrayIndexOutOfBoundsException e) {
0550: throw new IOException(
0551: "Not a valid DH Parameter encoding.");
0552: }
0553: }
0554:
0555: protected void engineInit(byte[] params, String format)
0556: throws IOException {
0557: if (isASN1FormatString(format)) {
0558: engineInit(params);
0559: } else {
0560: throw new IOException("Unknown parameter format "
0561: + format);
0562: }
0563: }
0564:
0565: protected String engineToString() {
0566: return "Diffie-Hellman Parameters";
0567: }
0568: }
0569:
0570: public static class DSA extends JDKAlgorithmParameters {
0571: DSAParameterSpec currentSpec;
0572:
0573: /**
0574: * Return the X.509 ASN.1 structure DSAParameter.
0575: * <p>
0576: * <pre>
0577: * DSAParameter ::= SEQUENCE {
0578: * prime INTEGER, -- p
0579: * subprime INTEGER, -- q
0580: * base INTEGER, -- g}
0581: * </pre>
0582: */
0583: protected byte[] engineGetEncoded() {
0584: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
0585: DEROutputStream dOut = new DEROutputStream(bOut);
0586: DSAParameter dsaP = new DSAParameter(currentSpec.getP(),
0587: currentSpec.getQ(), currentSpec.getG());
0588:
0589: try {
0590: dOut.writeObject(dsaP);
0591: dOut.close();
0592: } catch (IOException e) {
0593: throw new RuntimeException(
0594: "Error encoding DSAParameters");
0595: }
0596:
0597: return bOut.toByteArray();
0598: }
0599:
0600: protected byte[] engineGetEncoded(String format) {
0601: if (isASN1FormatString(format)) {
0602: return engineGetEncoded();
0603: }
0604:
0605: return null;
0606: }
0607:
0608: protected AlgorithmParameterSpec localEngineGetParameterSpec(
0609: Class paramSpec) throws InvalidParameterSpecException {
0610: if (paramSpec == DSAParameterSpec.class) {
0611: return currentSpec;
0612: }
0613:
0614: throw new InvalidParameterSpecException(
0615: "unknown parameter spec passed to DSA parameters object.");
0616: }
0617:
0618: protected void engineInit(AlgorithmParameterSpec paramSpec)
0619: throws InvalidParameterSpecException {
0620: if (!(paramSpec instanceof DSAParameterSpec)) {
0621: throw new InvalidParameterSpecException(
0622: "DSAParameterSpec required to initialise a DSA algorithm parameters object");
0623: }
0624:
0625: this .currentSpec = (DSAParameterSpec) paramSpec;
0626: }
0627:
0628: protected void engineInit(byte[] params) throws IOException {
0629: ASN1InputStream aIn = new ASN1InputStream(params);
0630:
0631: try {
0632: DSAParameter dsaP = new DSAParameter((ASN1Sequence) aIn
0633: .readObject());
0634:
0635: currentSpec = new DSAParameterSpec(dsaP.getP(), dsaP
0636: .getQ(), dsaP.getG());
0637: } catch (ClassCastException e) {
0638: throw new IOException(
0639: "Not a valid DSA Parameter encoding.");
0640: } catch (ArrayIndexOutOfBoundsException e) {
0641: throw new IOException(
0642: "Not a valid DSA Parameter encoding.");
0643: }
0644: }
0645:
0646: protected void engineInit(byte[] params, String format)
0647: throws IOException {
0648: if (isASN1FormatString(format)
0649: || format.equalsIgnoreCase("X.509")) {
0650: engineInit(params);
0651: } else {
0652: throw new IOException("Unknown parameter format "
0653: + format);
0654: }
0655: }
0656:
0657: protected String engineToString() {
0658: return "DSA Parameters";
0659: }
0660: }
0661:
0662: public static class GOST3410 extends JDKAlgorithmParameters {
0663: GOST3410ParameterSpec currentSpec;
0664:
0665: /**
0666: * Return the X.509 ASN.1 structure GOST3410Parameter.
0667: * <p>
0668: * <pre>
0669: * GOST3410Parameter ::= SEQUENCE {
0670: * prime INTEGER, -- p
0671: * subprime INTEGER, -- q
0672: * base INTEGER, -- a}
0673: * </pre>
0674: */
0675: protected byte[] engineGetEncoded() {
0676: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
0677: DEROutputStream dOut = new DEROutputStream(bOut);
0678: GOST3410PublicKeyAlgParameters gost3410P = new GOST3410PublicKeyAlgParameters(
0679: new DERObjectIdentifier(currentSpec
0680: .getPublicKeyParamSetOID()),
0681: new DERObjectIdentifier(currentSpec
0682: .getDigestParamSetOID()),
0683: new DERObjectIdentifier(currentSpec
0684: .getEncryptionParamSetOID()));
0685:
0686: try {
0687: dOut.writeObject(gost3410P);
0688: dOut.close();
0689: } catch (IOException e) {
0690: throw new RuntimeException(
0691: "Error encoding GOST3410Parameters");
0692: }
0693:
0694: return bOut.toByteArray();
0695: }
0696:
0697: protected byte[] engineGetEncoded(String format) {
0698: if (isASN1FormatString(format)
0699: || format.equalsIgnoreCase("X.509")) {
0700: return engineGetEncoded();
0701: }
0702:
0703: return null;
0704: }
0705:
0706: protected AlgorithmParameterSpec localEngineGetParameterSpec(
0707: Class paramSpec) throws InvalidParameterSpecException {
0708: if (paramSpec == GOST3410PublicKeyParameterSetSpec.class) {
0709: return currentSpec;
0710: }
0711:
0712: throw new InvalidParameterSpecException(
0713: "unknown parameter spec passed to GOST3410 parameters object.");
0714: }
0715:
0716: protected void engineInit(AlgorithmParameterSpec paramSpec)
0717: throws InvalidParameterSpecException {
0718: if (!(paramSpec instanceof GOST3410ParameterSpec)) {
0719: throw new InvalidParameterSpecException(
0720: "GOST3410ParameterSpec required to initialise a GOST3410 algorithm parameters object");
0721: }
0722:
0723: this .currentSpec = (GOST3410ParameterSpec) paramSpec;
0724: }
0725:
0726: protected void engineInit(byte[] params) throws IOException {
0727: ASN1InputStream dIn = new ASN1InputStream(params);
0728:
0729: try {
0730: GOST3410PublicKeyAlgParameters gost3410P = new GOST3410PublicKeyAlgParameters(
0731: (ASN1Sequence) dIn.readObject());
0732:
0733: currentSpec = new GOST3410ParameterSpec(gost3410P
0734: .getPublicKeyParamSet().getId(), gost3410P
0735: .getDigestParamSet().getId(), gost3410P
0736: .getEncryptionParamSet().getId());
0737: } catch (ClassCastException e) {
0738: throw new IOException(
0739: "Not a valid GOST3410 Parameter encoding.");
0740: } catch (ArrayIndexOutOfBoundsException e) {
0741: throw new IOException(
0742: "Not a valid GOST3410 Parameter encoding.");
0743: }
0744: }
0745:
0746: protected void engineInit(byte[] params, String format)
0747: throws IOException {
0748: if (isASN1FormatString(format)
0749: || format.equalsIgnoreCase("X.509")) {
0750: engineInit(params);
0751: } else {
0752: throw new IOException("Unknown parameter format "
0753: + format);
0754: }
0755: }
0756:
0757: protected String engineToString() {
0758: return "GOST3410 Parameters";
0759: }
0760: }
0761:
0762: public static class ElGamal extends JDKAlgorithmParameters {
0763: ElGamalParameterSpec currentSpec;
0764:
0765: /**
0766: * Return the X.509 ASN.1 structure ElGamalParameter.
0767: * <p>
0768: * <pre>
0769: * ElGamalParameter ::= SEQUENCE {
0770: * prime INTEGER, -- p
0771: * base INTEGER, -- g}
0772: * </pre>
0773: */
0774: protected byte[] engineGetEncoded() {
0775: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
0776: DEROutputStream dOut = new DEROutputStream(bOut);
0777: ElGamalParameter elP = new ElGamalParameter(currentSpec
0778: .getP(), currentSpec.getG());
0779:
0780: try {
0781: dOut.writeObject(elP);
0782: dOut.close();
0783: } catch (IOException e) {
0784: throw new RuntimeException(
0785: "Error encoding ElGamalParameters");
0786: }
0787:
0788: return bOut.toByteArray();
0789: }
0790:
0791: protected byte[] engineGetEncoded(String format) {
0792: if (isASN1FormatString(format)
0793: || format.equalsIgnoreCase("X.509")) {
0794: return engineGetEncoded();
0795: }
0796:
0797: return null;
0798: }
0799:
0800: protected AlgorithmParameterSpec localEngineGetParameterSpec(
0801: Class paramSpec) throws InvalidParameterSpecException {
0802: if (paramSpec == ElGamalParameterSpec.class) {
0803: return currentSpec;
0804: } else if (paramSpec == DHParameterSpec.class) {
0805: return new DHParameterSpec(currentSpec.getP(),
0806: currentSpec.getG());
0807: }
0808:
0809: throw new InvalidParameterSpecException(
0810: "unknown parameter spec passed to ElGamal parameters object.");
0811: }
0812:
0813: protected void engineInit(AlgorithmParameterSpec paramSpec)
0814: throws InvalidParameterSpecException {
0815: if (!(paramSpec instanceof ElGamalParameterSpec)
0816: && !(paramSpec instanceof DHParameterSpec)) {
0817: throw new InvalidParameterSpecException(
0818: "DHParameterSpec required to initialise a ElGamal algorithm parameters object");
0819: }
0820:
0821: if (paramSpec instanceof ElGamalParameterSpec) {
0822: this .currentSpec = (ElGamalParameterSpec) paramSpec;
0823: } else {
0824: DHParameterSpec s = (DHParameterSpec) paramSpec;
0825:
0826: this .currentSpec = new ElGamalParameterSpec(s.getP(), s
0827: .getG());
0828: }
0829: }
0830:
0831: protected void engineInit(byte[] params) throws IOException {
0832: ASN1InputStream aIn = new ASN1InputStream(params);
0833:
0834: try {
0835: ElGamalParameter elP = new ElGamalParameter(
0836: (ASN1Sequence) aIn.readObject());
0837:
0838: currentSpec = new ElGamalParameterSpec(elP.getP(), elP
0839: .getG());
0840: } catch (ClassCastException e) {
0841: throw new IOException(
0842: "Not a valid ElGamal Parameter encoding.");
0843: } catch (ArrayIndexOutOfBoundsException e) {
0844: throw new IOException(
0845: "Not a valid ElGamal Parameter encoding.");
0846: }
0847: }
0848:
0849: protected void engineInit(byte[] params, String format)
0850: throws IOException {
0851: if (isASN1FormatString(format)
0852: || format.equalsIgnoreCase("X.509")) {
0853: engineInit(params);
0854: } else {
0855: throw new IOException("Unknown parameter format "
0856: + format);
0857: }
0858: }
0859:
0860: protected String engineToString() {
0861: return "ElGamal Parameters";
0862: }
0863: }
0864:
0865: public static class IES extends JDKAlgorithmParameters {
0866: IESParameterSpec currentSpec;
0867:
0868: /**
0869: * in the abscence of a standard way of doing it this will do for
0870: * now...
0871: */
0872: protected byte[] engineGetEncoded() {
0873: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
0874: DEROutputStream dOut = new DEROutputStream(bOut);
0875:
0876: try {
0877: ASN1EncodableVector v = new ASN1EncodableVector();
0878:
0879: v.add(new DEROctetString(currentSpec.getDerivationV()));
0880: v.add(new DEROctetString(currentSpec.getEncodingV()));
0881: v.add(new DERInteger(currentSpec.getMacKeySize()));
0882:
0883: dOut.writeObject(new DERSequence(v));
0884: dOut.close();
0885: } catch (IOException e) {
0886: throw new RuntimeException(
0887: "Error encoding IESParameters");
0888: }
0889:
0890: return bOut.toByteArray();
0891: }
0892:
0893: protected byte[] engineGetEncoded(String format) {
0894: if (isASN1FormatString(format)
0895: || format.equalsIgnoreCase("X.509")) {
0896: return engineGetEncoded();
0897: }
0898:
0899: return null;
0900: }
0901:
0902: protected AlgorithmParameterSpec localEngineGetParameterSpec(
0903: Class paramSpec) throws InvalidParameterSpecException {
0904: if (paramSpec == IESParameterSpec.class) {
0905: return currentSpec;
0906: }
0907:
0908: throw new InvalidParameterSpecException(
0909: "unknown parameter spec passed to ElGamal parameters object.");
0910: }
0911:
0912: protected void engineInit(AlgorithmParameterSpec paramSpec)
0913: throws InvalidParameterSpecException {
0914: if (!(paramSpec instanceof IESParameterSpec)) {
0915: throw new InvalidParameterSpecException(
0916: "IESParameterSpec required to initialise a IES algorithm parameters object");
0917: }
0918:
0919: this .currentSpec = (IESParameterSpec) paramSpec;
0920: }
0921:
0922: protected void engineInit(byte[] params) throws IOException {
0923: ASN1InputStream aIn = new ASN1InputStream(params);
0924:
0925: try {
0926: ASN1Sequence s = (ASN1Sequence) aIn.readObject();
0927:
0928: this .currentSpec = new IESParameterSpec(
0929: ((ASN1OctetString) s.getObjectAt(0))
0930: .getOctets(), ((ASN1OctetString) s
0931: .getObjectAt(0)).getOctets(),
0932: ((DERInteger) s.getObjectAt(0)).getValue()
0933: .intValue());
0934: } catch (ClassCastException e) {
0935: throw new IOException(
0936: "Not a valid IES Parameter encoding.");
0937: } catch (ArrayIndexOutOfBoundsException e) {
0938: throw new IOException(
0939: "Not a valid IES Parameter encoding.");
0940: }
0941: }
0942:
0943: protected void engineInit(byte[] params, String format)
0944: throws IOException {
0945: if (isASN1FormatString(format)
0946: || format.equalsIgnoreCase("X.509")) {
0947: engineInit(params);
0948: } else {
0949: throw new IOException("Unknown parameter format "
0950: + format);
0951: }
0952: }
0953:
0954: protected String engineToString() {
0955: return "IES Parameters";
0956: }
0957: }
0958:
0959: public static class OAEP extends JDKAlgorithmParameters {
0960: OAEPParameterSpec currentSpec;
0961:
0962: /**
0963: * Return the PKCS#1 ASN.1 structure RSA-ES-OAEP-params.
0964: */
0965: protected byte[] engineGetEncoded() {
0966: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
0967: DEROutputStream dOut = new DEROutputStream(bOut);
0968: AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier(
0969: JCEDigestUtil.getOID(currentSpec
0970: .getDigestAlgorithm()), new DERNull());
0971: MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec) currentSpec
0972: .getMGFParameters();
0973: AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier(
0974: PKCSObjectIdentifiers.id_mgf1,
0975: new AlgorithmIdentifier(JCEDigestUtil
0976: .getOID(mgfSpec.getDigestAlgorithm()),
0977: new DERNull()));
0978: PSource.PSpecified pSource = (PSource.PSpecified) currentSpec
0979: .getPSource();
0980: AlgorithmIdentifier pSourceAlgorithm = new AlgorithmIdentifier(
0981: PKCSObjectIdentifiers.id_pSpecified,
0982: new DEROctetString(pSource.getValue()));
0983: RSAESOAEPparams oaepP = new RSAESOAEPparams(hashAlgorithm,
0984: maskGenAlgorithm, pSourceAlgorithm);
0985:
0986: try {
0987: dOut.writeObject(oaepP);
0988: dOut.close();
0989: } catch (IOException e) {
0990: throw new RuntimeException(
0991: "Error encoding OAEPParameters");
0992: }
0993:
0994: return bOut.toByteArray();
0995: }
0996:
0997: protected byte[] engineGetEncoded(String format) {
0998: if (isASN1FormatString(format)
0999: || format.equalsIgnoreCase("X.509")) {
1000: return engineGetEncoded();
1001: }
1002:
1003: return null;
1004: }
1005:
1006: protected AlgorithmParameterSpec localEngineGetParameterSpec(
1007: Class paramSpec) throws InvalidParameterSpecException {
1008: if (paramSpec == OAEPParameterSpec.class
1009: && currentSpec != null) {
1010: return currentSpec;
1011: }
1012:
1013: throw new InvalidParameterSpecException(
1014: "unknown parameter spec passed to OAEP parameters object.");
1015: }
1016:
1017: protected void engineInit(AlgorithmParameterSpec paramSpec)
1018: throws InvalidParameterSpecException {
1019: if (!(paramSpec instanceof OAEPParameterSpec)) {
1020: throw new InvalidParameterSpecException(
1021: "OAEPParameterSpec required to initialise an OAEP algorithm parameters object");
1022: }
1023:
1024: this .currentSpec = (OAEPParameterSpec) paramSpec;
1025: }
1026:
1027: protected void engineInit(byte[] params) throws IOException {
1028: ASN1InputStream aIn = new ASN1InputStream(params);
1029:
1030: try {
1031: RSAESOAEPparams oaepP = new RSAESOAEPparams(
1032: (ASN1Sequence) aIn.readObject());
1033:
1034: currentSpec = new OAEPParameterSpec(oaepP
1035: .getHashAlgorithm().getObjectId().getId(),
1036: oaepP.getMaskGenAlgorithm().getObjectId()
1037: .getId(), new MGF1ParameterSpec(
1038: AlgorithmIdentifier.getInstance(
1039: oaepP.getMaskGenAlgorithm()
1040: .getParameters())
1041: .getObjectId().getId()),
1042: new PSource.PSpecified(ASN1OctetString
1043: .getInstance(
1044: oaepP.getPSourceAlgorithm()
1045: .getParameters())
1046: .getOctets()));
1047: } catch (ClassCastException e) {
1048: throw new IOException(
1049: "Not a valid OAEP Parameter encoding.");
1050: } catch (ArrayIndexOutOfBoundsException e) {
1051: throw new IOException(
1052: "Not a valid OAEP Parameter encoding.");
1053: }
1054: }
1055:
1056: protected void engineInit(byte[] params, String format)
1057: throws IOException {
1058: if (format.equalsIgnoreCase("X.509")
1059: || format.equalsIgnoreCase("ASN.1")) {
1060: engineInit(params);
1061: } else {
1062: throw new IOException("Unknown parameter format "
1063: + format);
1064: }
1065: }
1066:
1067: protected String engineToString() {
1068: return "OAEP Parameters";
1069: }
1070: }
1071:
1072: public static class PSS extends JDKAlgorithmParameters {
1073: PSSParameterSpec currentSpec;
1074:
1075: /**
1076: * Return the PKCS#1 ASN.1 structure RSA-ES-OAEP-params.
1077: */
1078: protected byte[] engineGetEncoded() throws IOException {
1079: PSSParameterSpec pssSpec = currentSpec;
1080: AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier(
1081: JCEDigestUtil.getOID(pssSpec.getDigestAlgorithm()),
1082: new DERNull());
1083: MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec) pssSpec
1084: .getMGFParameters();
1085: AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier(
1086: PKCSObjectIdentifiers.id_mgf1,
1087: new AlgorithmIdentifier(JCEDigestUtil
1088: .getOID(mgfSpec.getDigestAlgorithm()),
1089: new DERNull()));
1090: RSASSAPSSparams pssP = new RSASSAPSSparams(hashAlgorithm,
1091: maskGenAlgorithm, new DERInteger(pssSpec
1092: .getSaltLength()), new DERInteger(pssSpec
1093: .getTrailerField()));
1094:
1095: return pssP.getEncoded("DER");
1096: }
1097:
1098: protected byte[] engineGetEncoded(String format)
1099: throws IOException {
1100: if (format.equalsIgnoreCase("X.509")
1101: || format.equalsIgnoreCase("ASN.1")) {
1102: return engineGetEncoded();
1103: }
1104:
1105: return null;
1106: }
1107:
1108: protected AlgorithmParameterSpec localEngineGetParameterSpec(
1109: Class paramSpec) throws InvalidParameterSpecException {
1110: if (paramSpec == PSSParameterSpec.class
1111: && currentSpec != null) {
1112: return currentSpec;
1113: }
1114:
1115: throw new InvalidParameterSpecException(
1116: "unknown parameter spec passed to PSS parameters object.");
1117: }
1118:
1119: protected void engineInit(AlgorithmParameterSpec paramSpec)
1120: throws InvalidParameterSpecException {
1121: if (!(paramSpec instanceof PSSParameterSpec)) {
1122: throw new InvalidParameterSpecException(
1123: "PSSParameterSpec required to initialise an PSS algorithm parameters object");
1124: }
1125:
1126: this .currentSpec = (PSSParameterSpec) paramSpec;
1127: }
1128:
1129: protected void engineInit(byte[] params) throws IOException {
1130: ASN1InputStream aIn = new ASN1InputStream(params);
1131:
1132: try {
1133: RSASSAPSSparams pssP = new RSASSAPSSparams(
1134: (ASN1Sequence) aIn.readObject());
1135:
1136: currentSpec = new PSSParameterSpec(pssP
1137: .getHashAlgorithm().getObjectId().getId(), pssP
1138: .getMaskGenAlgorithm().getObjectId().getId(),
1139: new MGF1ParameterSpec(AlgorithmIdentifier
1140: .getInstance(
1141: pssP.getMaskGenAlgorithm()
1142: .getParameters())
1143: .getObjectId().getId()), pssP
1144: .getSaltLength().getValue().intValue(),
1145: pssP.getTrailerField().getValue().intValue());
1146: } catch (ClassCastException e) {
1147: throw new IOException(
1148: "Not a valid PSS Parameter encoding.");
1149: } catch (ArrayIndexOutOfBoundsException e) {
1150: throw new IOException(
1151: "Not a valid PSS Parameter encoding.");
1152: }
1153: }
1154:
1155: protected void engineInit(byte[] params, String format)
1156: throws IOException {
1157: if (isASN1FormatString(format)
1158: || format.equalsIgnoreCase("X.509")) {
1159: engineInit(params);
1160: } else {
1161: throw new IOException("Unknown parameter format "
1162: + format);
1163: }
1164: }
1165:
1166: protected String engineToString() {
1167: return "PSS Parameters";
1168: }
1169: }
1170: }
|