-
Notifications
You must be signed in to change notification settings - Fork 8
/
MerlinTruststore.java
213 lines (167 loc) · 5.54 KB
/
MerlinTruststore.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
/*
* 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.KeyStore;
import java.security.cert.Certificate;
import java.util.Properties;
import org.openspcoop2.security.SecurityException;
import org.openspcoop2.utils.certificate.hsm.HSMManager;
/**
* MerlinTruststore
*
* @author Andrea Poli (apoli@link.it)
* @author $Author$
* @version $Rev$, $Date$
*/
public class MerlinTruststore implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private transient org.openspcoop2.utils.certificate.KeyStore ks = null;
private byte[] ksBytes;
private String tipoStore = null;
private String pathStore = null;
private String passwordStore = null;
private boolean hsm;
@Override
public String toString() {
StringBuilder bf = new StringBuilder();
bf.append("TrustStore (").append(this.tipoStore).append(") ").append(this.pathStore);
return bf.toString();
}
public MerlinTruststore(String propertyFilePath) throws SecurityException{
Properties propStore = StoreUtils.readProperties("PropertyFilePath", propertyFilePath);
initMerlinTruststoreEngine(propStore);
}
public MerlinTruststore(Properties propStore) throws SecurityException{
initMerlinTruststoreEngine(propStore);
}
private void initMerlinTruststoreEngine(Properties propStore) throws SecurityException{
try{
if(propStore==null){
throw new SecurityException("Properties per lo Store non indicato");
}
this.tipoStore = propStore.getProperty(KeystoreConstants.PROPERTY_KEYSTORE_TYPE);
if(this.tipoStore!=null){
this.tipoStore = this.tipoStore.trim();
}else{
this.tipoStore = KeyStore.getDefaultType();
}
this.pathStore = propStore.getProperty(KeystoreConstants.PROPERTY_KEYSTORE_PATH);
this.passwordStore = propStore.getProperty(KeystoreConstants.PROPERTY_KEYSTORE_PASSWORD);
init();
}catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
public MerlinTruststore(String pathStore,String tipoStore,String passwordStore) throws SecurityException{
this.pathStore = pathStore;
this.tipoStore = tipoStore;
this.passwordStore = passwordStore;
init();
}
public MerlinTruststore(byte[]bytesKeystore,String tipoStore,String passwordStore) throws SecurityException{
this.ksBytes = bytesKeystore;
this.tipoStore = tipoStore;
this.passwordStore = passwordStore;
init();
}
private void init() throws SecurityException{
try{
if(this.tipoStore==null){
throw new SecurityException("Tipo dello Store non indicato");
}
if(this.passwordStore==null){
throw new SecurityException("Password dello Store non indicata");
}
HSMManager hsmManager = HSMManager.getInstance();
if(hsmManager!=null) {
this.hsm = hsmManager.existsKeystoreType(this.tipoStore);
}
if(!this.hsm) {
if(this.ksBytes==null && this.pathStore==null){
throw new SecurityException("Path per lo Store non indicato");
}
if(this.ksBytes==null) {
this.ksBytes = StoreUtils.readContent("Path", this.pathStore);
}
}
this.initKS();
}catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
public boolean isHsm() {
return this.hsm;
}
private void checkInit() throws SecurityException{
if(this.ks==null) {
this.initKS();
}
}
private synchronized void initKS() throws SecurityException{
if(this.ks==null) {
try {
if(this.hsm) {
this.ks = HSMManager.getInstance().getKeystore(this.tipoStore);
}
else {
this.ks = new org.openspcoop2.utils.certificate.KeyStore(this.ksBytes, this.tipoStore, this.passwordStore);
// non utilizzabile in hsm, si ottiene errore: java.lang.UnsupportedOperationException: trusted certificates may only be set by token initialization application
// at jdk.crypto.cryptoki/sun.security.pkcs11.P11KeyStore.engineSetEntry(P11KeyStore.java:1022)
FixTrustAnchorsNotEmpty.addCertificate(this.ks.getKeystore());
}
}
catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
}
public Certificate getCertificate(String alias) throws SecurityException {
if(alias==null) {
throw new SecurityException("Alias non fornito");
}
this.checkInit(); // per ripristino da Serializable
try{
return this.ks.getCertificate(alias);
}catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
public org.openspcoop2.utils.certificate.KeyStore getTrustStore() throws SecurityException {
this.checkInit(); // per ripristino da Serializable
try{
return this.ks;
}catch(Exception e){
throw new SecurityException(e.getMessage(),e);
}
}
public String getTipoStore() {
return this.tipoStore;
}
public String getPathStore() {
return this.pathStore;
}
public String getPasswordStore() {
return this.passwordStore;
}
}