1
1
/*
2
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
40
40
public class RSAUtil {
41
41
42
42
public enum KeyType {
43
- RSA ("RSA" ),
44
- PSS ("RSASSA-PSS" )
43
+ RSA ("RSA" , AlgorithmId . RSAEncryption_oid , null ),
44
+ PSS ("RSASSA-PSS" , AlgorithmId . RSASSA_PSS_oid , PSSParameterSpec . class )
45
45
;
46
46
47
- private final String algo ;
47
+ final String keyAlgo ;
48
+ final ObjectIdentifier oid ;
49
+ final Class <? extends AlgorithmParameterSpec > paramSpecCls ;
48
50
49
- KeyType (String keyAlgo ) {
50
- this .algo = keyAlgo ;
51
+ KeyType (String keyAlgo , ObjectIdentifier oid ,
52
+ Class <? extends AlgorithmParameterSpec > paramSpecCls ) {
53
+ this .keyAlgo = keyAlgo ;
54
+ this .oid = oid ;
55
+ this .paramSpecCls = paramSpecCls ;
51
56
}
52
- public String keyAlgo () {
53
- return algo ;
54
- }
55
- public static KeyType lookup (String name )
56
- throws InvalidKeyException , ProviderException {
57
- if (name == null ) {
58
- throw new InvalidKeyException ("Null key algorithm" );
59
- }
60
- for (KeyType kt : KeyType .values ()) {
61
- if (kt .keyAlgo ().equalsIgnoreCase (name )) {
62
- return kt ;
63
- }
57
+
58
+ public static KeyType lookup (String name ) throws ProviderException {
59
+
60
+ requireNonNull (name , "Key algorithm should not be null" );
61
+
62
+ // match loosely in order to work with 3rd party providers which
63
+ // may not follow the standard names
64
+ if (name .indexOf ("PSS" ) != -1 ) {
65
+ return PSS ;
66
+ } else if (name .indexOf ("RSA" ) != -1 ) {
67
+ return RSA ;
68
+ } else { // no match
69
+ throw new ProviderException ("Unsupported algorithm " + name );
64
70
}
65
- // no match
66
- throw new ProviderException ("Unsupported algorithm " + name );
67
71
}
68
72
}
69
73
70
- public static void checkParamsAgainstType (KeyType type ,
74
+ private static void requireNonNull (Object obj , String msg ) {
75
+ if (obj == null ) throw new ProviderException (msg );
76
+ }
77
+
78
+ public static AlgorithmParameterSpec checkParamsAgainstType (KeyType type ,
71
79
AlgorithmParameterSpec paramSpec ) throws ProviderException {
72
- switch (type ) {
73
- case RSA :
74
- if (paramSpec != null ) {
75
- throw new ProviderException ("null params expected for " +
76
- type .keyAlgo ());
77
- }
78
- break ;
79
- case PSS :
80
- if ((paramSpec != null ) &&
81
- !(paramSpec instanceof PSSParameterSpec )) {
82
- throw new ProviderException
83
- ("PSSParmeterSpec expected for " + type .keyAlgo ());
84
- }
85
- break ;
86
- default :
87
- throw new ProviderException
88
- ("Unsupported RSA algorithm " + type );
80
+
81
+ // currently no check for null parameter spec
82
+ // assumption is parameter spec is optional and can be null
83
+ if (paramSpec == null ) return null ;
84
+
85
+ Class <? extends AlgorithmParameterSpec > expCls = type .paramSpecCls ;
86
+ if (expCls == null ) {
87
+ throw new ProviderException ("null params expected for " +
88
+ type .keyAlgo );
89
+ } else if (!expCls .isInstance (paramSpec )) {
90
+ throw new ProviderException
91
+ (expCls + " expected for " + type .keyAlgo );
92
+ }
93
+ return paramSpec ;
94
+ }
95
+
96
+ public static AlgorithmParameters getParams (KeyType type ,
97
+ AlgorithmParameterSpec spec ) throws ProviderException {
98
+
99
+ if (spec == null ) return null ;
100
+
101
+ try {
102
+ AlgorithmParameters params =
103
+ AlgorithmParameters .getInstance (type .keyAlgo );
104
+ params .init (spec );
105
+ return params ;
106
+ } catch (NoSuchAlgorithmException | InvalidParameterSpecException ex ) {
107
+ throw new ProviderException (ex );
89
108
}
90
109
}
91
110
@@ -94,69 +113,53 @@ public static AlgorithmId createAlgorithmId(KeyType type,
94
113
95
114
checkParamsAgainstType (type , paramSpec );
96
115
97
- ObjectIdentifier oid = null ;
98
- AlgorithmParameters params = null ;
99
- try {
100
- switch (type ) {
101
- case RSA :
102
- oid = AlgorithmId .RSAEncryption_oid ;
103
- break ;
104
- case PSS :
105
- if (paramSpec != null ) {
106
- params = AlgorithmParameters .getInstance (type .keyAlgo ());
107
- params .init (paramSpec );
108
- }
109
- oid = AlgorithmId .RSASSA_PSS_oid ;
110
- break ;
111
- default :
112
- throw new ProviderException
113
- ("Unsupported RSA algorithm " + type );
114
- }
115
- AlgorithmId result ;
116
- if (params == null ) {
117
- result = new AlgorithmId (oid );
118
- } else {
119
- result = new AlgorithmId (oid , params );
120
- }
121
- return result ;
122
- } catch (NoSuchAlgorithmException | InvalidParameterSpecException e ) {
123
- // should not happen
124
- throw new ProviderException (e );
125
- }
116
+ ObjectIdentifier oid = type .oid ;
117
+ AlgorithmParameters params = getParams (type , paramSpec );
118
+ return new AlgorithmId (oid , params );
126
119
}
127
120
128
- public static AlgorithmParameterSpec getParamSpec (AlgorithmId algid )
129
- throws ProviderException {
130
- if (algid == null ) {
131
- throw new ProviderException ("AlgorithmId should not be null" );
121
+ public static AlgorithmParameterSpec getParamSpec (
122
+ AlgorithmParameters params ) throws ProviderException {
123
+
124
+ if (params == null ) return null ;
125
+
126
+ String algName = params .getAlgorithm ();
127
+
128
+ KeyType type = KeyType .lookup (algName );
129
+ Class <? extends AlgorithmParameterSpec > specCls = type .paramSpecCls ;
130
+ if (specCls == null ) {
131
+ throw new ProviderException ("No params accepted for " +
132
+ type .keyAlgo );
133
+ }
134
+ try {
135
+ return params .getParameterSpec (specCls );
136
+ } catch (InvalidParameterSpecException ex ) {
137
+ throw new ProviderException (ex );
132
138
}
133
- return getParamSpec (algid .getParameters ());
134
139
}
135
140
136
- public static AlgorithmParameterSpec getParamSpec ( AlgorithmParameters params )
141
+ public static Object [] getTypeAndParamSpec ( AlgorithmId algid )
137
142
throws ProviderException {
138
- if (params == null ) return null ;
139
143
144
+ requireNonNull (algid , "AlgorithmId should not be null" );
145
+
146
+ Object [] result = new Object [2 ];
147
+
148
+ String algName = algid .getName ();
140
149
try {
141
- String algName = params .getAlgorithm ();
142
- KeyType type = KeyType .lookup (algName );
143
- Class <? extends AlgorithmParameterSpec > specCls ;
144
- switch (type ) {
145
- case RSA :
146
- throw new ProviderException ("No params accepted for " +
147
- type .keyAlgo ());
148
- case PSS :
149
- specCls = PSSParameterSpec .class ;
150
- break ;
151
- default :
152
- throw new ProviderException ("Unsupported RSA algorithm: " + algName );
153
- }
154
- return params .getParameterSpec (specCls );
150
+ result [0 ] = KeyType .lookup (algName );
155
151
} catch (ProviderException pe ) {
156
- // pass it up
157
- throw pe ;
158
- } catch (Exception e ) {
159
- throw new ProviderException (e );
152
+ // accommodate RSA keys encoded with various RSA signature oids
153
+ // for backward compatibility
154
+ if (algName .indexOf ("RSA" ) != -1 ) {
155
+ result [0 ] = KeyType .RSA ;
156
+ } else {
157
+ // pass it up
158
+ throw pe ;
159
+ }
160
160
}
161
+
162
+ result [1 ] = getParamSpec (algid .getParameters ());
163
+ return result ;
161
164
}
162
165
}
0 commit comments