88 String mgfName = spec.getMGFAlgorithm();
89 if (!mgfName.equalsIgnoreCase("MGF1")) {
90 throw new InvalidParameterSpecException("Unsupported mgf " +
91 mgfName + "; MGF1 only");
92 }
93 AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();
94 if (!(mgfSpec instanceof MGF1ParameterSpec)) {
95 throw new InvalidParameterSpecException("Inappropriate mgf " +
96 "parameters; non-null MGF1ParameterSpec only");
97 }
98 this.mgfSpec = (MGF1ParameterSpec) mgfSpec;
99 PSource pSrc = spec.getPSource();
100 if (pSrc.getAlgorithm().equals("PSpecified")) {
101 p = ((PSource.PSpecified) pSrc).getValue();
102 } else {
103 throw new InvalidParameterSpecException("Unsupported pSource " +
104 pSrc.getAlgorithm() + "; PSpecified only");
105 }
106 }
107
108 private static String convertToStandardName(String internalName) {
109 if (internalName.equals("SHA")) {
110 return "SHA-1";
111 } else if (internalName.equals("SHA256")) {
112 return "SHA-256";
113 } else if (internalName.equals("SHA384")) {
114 return "SHA-384";
115 } else if (internalName.equals("SHA512")) {
116 return "SHA-512";
117 } else {
118 return internalName;
119 }
120 }
121
122 protected void engineInit(byte[] encoded)
123 throws IOException {
124 DerInputStream der = new DerInputStream(encoded);
125 mdName = "SHA-1";
126 mgfSpec = MGF1ParameterSpec.SHA1;
127 p = new byte[0];
128 DerValue[] datum = der.getSequence(3);
129 for (int i=0; i<datum.length; i++) {
130 DerValue data = datum[i];
131 if (data.isContextSpecific((byte) 0x00)) {
132 // hash algid
133 mdName = convertToStandardName(AlgorithmId.parse
134 (data.data.getDerValue()).getName());
135 } else if (data.isContextSpecific((byte) 0x01)) {
136 // mgf algid
137 AlgorithmId val = AlgorithmId.parse(data.data.getDerValue());
138 if (!val.getOID().equals((Object) OID_MGF1)) {
139 throw new IOException("Only MGF1 mgf is supported");
140 }
141 AlgorithmId params = AlgorithmId.parse(new DerValue(val.getEncodedParams()));
142 String mgfDigestName = convertToStandardName(params.getName());
143 if (mgfDigestName.equals("SHA-1")) {
144 mgfSpec = MGF1ParameterSpec.SHA1;
145 } else if (mgfDigestName.equals("SHA-224")) {
146 mgfSpec = new MGF1ParameterSpec("SHA-224");
147 } else if (mgfDigestName.equals("SHA-256")) {
148 mgfSpec = MGF1ParameterSpec.SHA256;
149 } else if (mgfDigestName.equals("SHA-384")) {
150 mgfSpec = MGF1ParameterSpec.SHA384;
151 } else if (mgfDigestName.equals("SHA-512")) {
152 mgfSpec = MGF1ParameterSpec.SHA512;
153 } else {
154 throw new IOException("Unrecognized message digest algorithm");
155 }
156 } else if (data.isContextSpecific((byte) 0x02)) {
157 // pSource algid
158 AlgorithmId val = AlgorithmId.parse(data.data.getDerValue());
159 if (!val.getOID().equals((Object) OID_PSpecified)) {
160 throw new IOException("Wrong OID for pSpecified");
161 }
162 DerInputStream dis = new DerInputStream(val.getEncodedParams());
|
88 String mgfName = spec.getMGFAlgorithm();
89 if (!mgfName.equalsIgnoreCase("MGF1")) {
90 throw new InvalidParameterSpecException("Unsupported mgf " +
91 mgfName + "; MGF1 only");
92 }
93 AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();
94 if (!(mgfSpec instanceof MGF1ParameterSpec)) {
95 throw new InvalidParameterSpecException("Inappropriate mgf " +
96 "parameters; non-null MGF1ParameterSpec only");
97 }
98 this.mgfSpec = (MGF1ParameterSpec) mgfSpec;
99 PSource pSrc = spec.getPSource();
100 if (pSrc.getAlgorithm().equals("PSpecified")) {
101 p = ((PSource.PSpecified) pSrc).getValue();
102 } else {
103 throw new InvalidParameterSpecException("Unsupported pSource " +
104 pSrc.getAlgorithm() + "; PSpecified only");
105 }
106 }
107
108 protected void engineInit(byte[] encoded)
109 throws IOException {
110 DerInputStream der = new DerInputStream(encoded);
111 mdName = "SHA-1";
112 mgfSpec = MGF1ParameterSpec.SHA1;
113 p = new byte[0];
114 DerValue[] datum = der.getSequence(3);
115 for (int i=0; i<datum.length; i++) {
116 DerValue data = datum[i];
117 if (data.isContextSpecific((byte) 0x00)) {
118 // hash algid
119 mdName = AlgorithmId.getStandardDigestName(AlgorithmId.parse
120 (data.data.getDerValue()).getName());
121 } else if (data.isContextSpecific((byte) 0x01)) {
122 // mgf algid
123 AlgorithmId val = AlgorithmId.parse(data.data.getDerValue());
124 if (!val.getOID().equals((Object) OID_MGF1)) {
125 throw new IOException("Only MGF1 mgf is supported");
126 }
127 AlgorithmId params = AlgorithmId.parse(new DerValue(val.getEncodedParams()));
128 String mgfDigestName = AlgorithmId.getStandardDigestName(
129 params.getName());
130 if (mgfDigestName.equals("SHA-1")) {
131 mgfSpec = MGF1ParameterSpec.SHA1;
132 } else if (mgfDigestName.equals("SHA-224")) {
133 mgfSpec = new MGF1ParameterSpec("SHA-224");
134 } else if (mgfDigestName.equals("SHA-256")) {
135 mgfSpec = MGF1ParameterSpec.SHA256;
136 } else if (mgfDigestName.equals("SHA-384")) {
137 mgfSpec = MGF1ParameterSpec.SHA384;
138 } else if (mgfDigestName.equals("SHA-512")) {
139 mgfSpec = MGF1ParameterSpec.SHA512;
140 } else {
141 throw new IOException("Unrecognized message digest algorithm");
142 }
143 } else if (data.isContextSpecific((byte) 0x02)) {
144 // pSource algid
145 AlgorithmId val = AlgorithmId.parse(data.data.getDerValue());
146 if (!val.getOID().equals((Object) OID_PSpecified)) {
147 throw new IOException("Wrong OID for pSpecified");
148 }
149 DerInputStream dis = new DerInputStream(val.getEncodedParams());
|