-
Notifications
You must be signed in to change notification settings - Fork 8
/
MultiKeystore.java
290 lines (251 loc) · 9.94 KB
/
MultiKeystore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* GovWay - A customizable API Gateway
* https://govway.org
*
* Copyright (c) 2005-2024 Link.it srl (https://link.it).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.openspcoop2.security.keystore;
import java.io.Serializable;
import java.security.Key;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.openspcoop2.security.SecurityException;
import org.openspcoop2.utils.LoggerWrapperFactory;
import org.openspcoop2.utils.certificate.KeyStore;
import org.openspcoop2.utils.certificate.byok.BYOKRequestParams;
/**
* MultiKeystore
*
* @author Andrea Poli (apoli@link.it)
* @author $Author$
* @version $Rev$, $Date$
*/
public class MultiKeystore implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final String ALIASES = "aliases";
private static final String KEYSTORE_TYPE = ".keystore.type";
private static final String KEYSTORE_PATH = ".keystore.path";
private static final String KEYSTORE_PASSWORD = ".keystore.password";
private static final String KEY_ALIAS = ".key.alias";
private static final String KEY_PASSWORD = ".key.password";
private static final String KEY_VALUE = ".key.value";
private static final String KEY_ALGORITHM = ".key.algorithm";
private List<String> aliasesList = new ArrayList<>();
private Map<String, Serializable> keystores = new HashMap<>();
private Map<String, String> mappingAliasToKeystorePath = new HashMap<>();
private Map<String, String> mappingAliasToKeystorePassword = new HashMap<>();
private Map<String, String> mappingAliasToKeystoreType = new HashMap<>();
private Map<String, String> mappingAliasToKeyAlias = new HashMap<>();
private Map<String, String> mappingAliasToKeyPassword = new HashMap<>();
@Override
public String toString() {
StringBuilder bf = new StringBuilder();
bf.append("MultiKeystore ").append(this.aliasesList);
return bf.toString();
}
public MultiKeystore(String propertyFilePath) throws SecurityException{
this(propertyFilePath, null);
}
public MultiKeystore(String propertyFilePath, BYOKRequestParams requestParams) throws SecurityException{
try{
Properties multiProperties = StoreUtils.readProperties("MultiProperties", propertyFilePath);
String keyAliases = getProperty(multiProperties, MultiKeystore.ALIASES);
String [] tmp = keyAliases.split(",");
for (int i = 0; i < tmp.length; i++) {
if(!this.aliasesList.contains(tmp[i].trim()))
this.aliasesList.add(tmp[i].trim());
}
for (String alias : this.aliasesList) {
String keyAlias = getSafeKeyAlias(alias, multiProperties);
String keyPassword = getProperty(multiProperties, alias+MultiKeystore.KEY_PASSWORD);
String keyValue = multiProperties.getProperty(alias+MultiKeystore.KEY_VALUE);
if(keyValue!=null){
// Configurazione con keyValue fornita direttamente
String keyAlgorithm = getProperty(multiProperties, alias+MultiKeystore.KEY_ALGORITHM);
addSymmetricKeystore(alias, keyAlias, keyValue, keyAlgorithm, requestParams);
}
else{
// Configurazione con MerlinKeystore
String keystoreType = getProperty(multiProperties, alias+MultiKeystore.KEYSTORE_TYPE);
String keystorePath = getProperty(multiProperties, alias+MultiKeystore.KEYSTORE_PATH);
String keystorePassword = getProperty(multiProperties, alias+MultiKeystore.KEYSTORE_PASSWORD);
addMerlinKeystore(alias, keyAlias, keystoreType, keystorePath, keystorePassword, keyPassword, requestParams);
}
this.mappingAliasToKeyAlias.put(alias, keyAlias);
this.mappingAliasToKeyPassword.put(alias, keyPassword);
}
}catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
private String getSafeKeyAlias(String alias, Properties multiProperties) {
String keyAlias = null;
try{
keyAlias = getProperty(multiProperties, alias+MultiKeystore.KEY_ALIAS);
}catch(Exception e){
keyAlias = alias;
}
return keyAlias;
}
private void addSymmetricKeystore(String alias, String keyAlias, String keyValue, String keyAlgorithm, BYOKRequestParams requestParams) {
try {
this.keystores.put(alias, new SymmetricKeystore(keyAlias, keyValue, keyAlgorithm, requestParams));
}catch(Exception e) {
String idKeystore = "!!! Errore durante il caricamento del SymmetricKeystore !!! [keyAlias:"+keyAlias+"] ";
LoggerWrapperFactory.getLogger(MultiKeystore.class).error(idKeystore+e.getMessage(),e);
}
}
private void addMerlinKeystore(String alias, String keyAlias, String keystoreType, String keystorePath, String keystorePassword, String keyPassword, BYOKRequestParams requestParams) {
try {
this.keystores.put(alias, new MerlinKeystore(keystorePath, keystoreType, keystorePassword, keyPassword, requestParams));
this.mappingAliasToKeystoreType.put(alias, keystoreType);
this.mappingAliasToKeystorePath.put(alias, keystorePath);
this.mappingAliasToKeystorePassword.put(alias, keystorePassword);
}catch(Exception e) {
String idKeystore = "!!! Errore durante il caricamento del MerlinKeystore !!! [keyAlias:"+keyAlias+"] ";
LoggerWrapperFactory.getLogger(MultiKeystore.class).error(idKeystore+e.getMessage(),e);
}
}
private String getProperty(Properties multiProperties,String property)throws SecurityException{
if(!multiProperties.containsKey(property)){
throw new SecurityException("Proprieta' ["+property+"] non trovata nel MultiProperties file");
}
else{
return multiProperties.getProperty(property).trim();
}
}
private String getErrorAlias(String alias) {
return "Alias["+alias+"] non trovato nella configurazione MultiKeystore";
}
public Key getKey(String alias) throws SecurityException {
try{
if(!this.aliasesList.contains(alias)){
throw new SecurityException(getErrorAlias(alias));
}
Object keystore = this.keystores.get(alias);
if(keystore==null) {
throw new SecurityException("Non esiste un keystore per l'alias["+alias+"]; verificare che non sia avvenuti errori durante l'inizializzazione (MultiKeystore)");
}
if(keystore instanceof MerlinKeystore){
return ((MerlinKeystore)keystore).getKey(this.mappingAliasToKeyAlias.get(alias));
}
else if(keystore instanceof SymmetricKeystore){
return ((SymmetricKeystore)keystore).getKey();
}
else{
throw new SecurityException("Tipo di Keystore non supportato: "+keystore.getClass().getName());
}
}catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
public boolean existsAlias(String alias){
return this.aliasesList.contains(alias);
}
public KeyStore getKeyStore(String alias) throws SecurityException {
try{
if(!this.aliasesList.contains(alias)){
throw new SecurityException(getErrorAlias(alias));
}
Object keystore = this.keystores.get(alias);
if(keystore==null) {
throw new SecurityException("Non esiste un keystore per l'alias["+alias+"]; verificare che non sia avvenuti errori durante l'inizializzazione (MultiKeystore)");
}
if(keystore instanceof MerlinKeystore){
return ((MerlinKeystore)keystore).getKeyStore();
}
else if(keystore instanceof SymmetricKeystore){
return ((SymmetricKeystore)keystore).getKeyStore();
}
else{
throw new SecurityException("Tipo di Keystore non supportato: "+keystore.getClass().getName());
}
}catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
public String getKeyAlias(String alias) throws SecurityException {
try{
if(!this.aliasesList.contains(alias)){
throw new SecurityException(getErrorAlias(alias));
}
return this.mappingAliasToKeyAlias.get(alias);
}catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
public String getKeyPassword(String alias) throws SecurityException {
try{
if(!this.aliasesList.contains(alias)){
throw new SecurityException(getErrorAlias(alias));
}
return this.mappingAliasToKeyPassword.get(alias);
}catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
public String getKeystorePath(String alias) throws SecurityException {
try{
if(!this.aliasesList.contains(alias)){
throw new SecurityException(getErrorAlias(alias));
}
return this.mappingAliasToKeystorePath.get(alias);
}catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
public String getKeystorePassword(String alias) throws SecurityException {
try{
if(!this.aliasesList.contains(alias)){
throw new SecurityException(getErrorAlias(alias));
}
return this.mappingAliasToKeystorePassword.get(alias);
}catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
public String getKeystoreType(String alias) throws SecurityException {
try{
if(!this.aliasesList.contains(alias)){
throw new SecurityException(getErrorAlias(alias));
}
return this.mappingAliasToKeystoreType.get(alias);
}catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
public String getInternalConfigAlias(String keyAlias) throws SecurityException {
try{
if(!this.mappingAliasToKeyAlias.containsValue(keyAlias)){
throw new SecurityException(getErrorAlias(keyAlias));
}
for (Map.Entry<String,String> entry : this.mappingAliasToKeyAlias.entrySet()) {
if(entry.getValue().equals(keyAlias)) {
return entry.getKey();
}
}
throw new SecurityException(getErrorAlias(keyAlias));
}catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
}