26 package sun.security.util; |
26 package sun.security.util; |
27 |
27 |
28 import java.io.IOException; |
28 import java.io.IOException; |
29 import java.security.*; |
29 import java.security.*; |
30 import java.security.spec.*; |
30 import java.security.spec.*; |
|
31 import java.util.Locale; |
31 import sun.security.rsa.RSAUtil; |
32 import sun.security.rsa.RSAUtil; |
32 import jdk.internal.access.SharedSecrets; |
33 import jdk.internal.access.SharedSecrets; |
33 |
34 |
34 /** |
35 /** |
35 * Utility class for Signature related operations. Currently used by various |
36 * Utility class for Signature related operations. Currently used by various |
72 // into an AlgorithmParameterSpec object. |
73 // into an AlgorithmParameterSpec object. |
73 public static AlgorithmParameterSpec getParamSpec(String sigName, |
74 public static AlgorithmParameterSpec getParamSpec(String sigName, |
74 AlgorithmParameters params) |
75 AlgorithmParameters params) |
75 throws ProviderException { |
76 throws ProviderException { |
76 |
77 |
77 sigName = checkName(sigName); |
78 sigName = checkName(sigName).toUpperCase(Locale.ENGLISH); |
78 AlgorithmParameterSpec paramSpec = null; |
79 AlgorithmParameterSpec paramSpec = null; |
79 if (params != null) { |
80 if (params != null) { |
80 if (sigName.toUpperCase().indexOf("RSA") == -1) { |
|
81 throw new ProviderException |
|
82 ("Unrecognized algorithm for signature parameters " + |
|
83 sigName); |
|
84 } |
|
85 // AlgorithmParameters.getAlgorithm() may returns oid if it's |
81 // AlgorithmParameters.getAlgorithm() may returns oid if it's |
86 // created during DER decoding. Convert to use the standard name |
82 // created during DER decoding. Convert to use the standard name |
87 // before passing it to RSAUtil |
83 // before passing it to RSAUtil |
88 if (params.getAlgorithm().indexOf(".") != -1) { |
84 if (params.getAlgorithm().indexOf(".") != -1) { |
89 try { |
85 try { |
91 params.getEncoded()); |
87 params.getEncoded()); |
92 } catch (IOException e) { |
88 } catch (IOException e) { |
93 throw new ProviderException(e); |
89 throw new ProviderException(e); |
94 } |
90 } |
95 } |
91 } |
96 paramSpec = RSAUtil.getParamSpec(params); |
92 |
|
93 if (sigName.indexOf("RSA") != -1) { |
|
94 paramSpec = RSAUtil.getParamSpec(params); |
|
95 } else if (sigName.indexOf("ECDSA") != -1) { |
|
96 try { |
|
97 paramSpec = params.getParameterSpec(ECParameterSpec.class); |
|
98 } catch (Exception e) { |
|
99 throw new ProviderException("Error handling EC parameters", e); |
|
100 } |
|
101 } else { |
|
102 throw new ProviderException |
|
103 ("Unrecognized algorithm for signature parameters " + |
|
104 sigName); |
|
105 } |
97 } |
106 } |
98 return paramSpec; |
107 return paramSpec; |
99 } |
108 } |
100 |
109 |
101 // Utility method for converting the specified parameter bytes into an |
110 // Utility method for converting the specified parameter bytes into an |
102 // AlgorithmParameterSpec object. |
111 // AlgorithmParameterSpec object. |
103 public static AlgorithmParameterSpec getParamSpec(String sigName, |
112 public static AlgorithmParameterSpec getParamSpec(String sigName, |
104 byte[] paramBytes) |
113 byte[] paramBytes) |
105 throws ProviderException { |
114 throws ProviderException { |
106 sigName = checkName(sigName); |
115 sigName = checkName(sigName).toUpperCase(Locale.ENGLISH); |
107 AlgorithmParameterSpec paramSpec = null; |
116 AlgorithmParameterSpec paramSpec = null; |
|
117 |
108 if (paramBytes != null) { |
118 if (paramBytes != null) { |
109 if (sigName.toUpperCase().indexOf("RSA") == -1) { |
119 if (sigName.indexOf("RSA") != -1) { |
|
120 AlgorithmParameters params = |
|
121 createAlgorithmParameters(sigName, paramBytes); |
|
122 paramSpec = RSAUtil.getParamSpec(params); |
|
123 } else if (sigName.indexOf("ECDSA") != -1) { |
|
124 try { |
|
125 Provider p = Signature.getInstance(sigName).getProvider(); |
|
126 paramSpec = ECUtil.getECParameterSpec(p, paramBytes); |
|
127 } catch (Exception e) { |
|
128 throw new ProviderException("Error handling EC parameters", e); |
|
129 } |
|
130 // ECUtil discards exception and returns null, so we need to check |
|
131 // the returned value |
|
132 if (paramSpec == null) { |
|
133 throw new ProviderException("Error handling EC parameters"); |
|
134 } |
|
135 } else { |
110 throw new ProviderException |
136 throw new ProviderException |
111 ("Unrecognized algorithm for signature parameters " + |
137 ("Unrecognized algorithm for signature parameters " + |
112 sigName); |
138 sigName); |
113 } |
139 } |
114 AlgorithmParameters params = |
|
115 createAlgorithmParameters(sigName, paramBytes); |
|
116 paramSpec = RSAUtil.getParamSpec(params); |
|
117 } |
140 } |
118 return paramSpec; |
141 return paramSpec; |
119 } |
142 } |
120 |
143 |
121 // Utility method for initializing the specified Signature object |
144 // Utility method for initializing the specified Signature object |