15
15
import org .bouncycastle .util .encoders .DecoderException ;
16
16
17
17
import java .io .BufferedInputStream ;
18
- import java .io .ByteArrayInputStream ;
19
18
import java .io .DataInputStream ;
20
19
import java .io .File ;
21
20
import java .io .FileInputStream ;
22
21
import java .io .FileNotFoundException ;
23
22
import java .io .FileReader ;
24
23
import java .io .IOException ;
25
- import java .io .InputStream ;
26
24
import java .security .KeyFactory ;
27
25
import java .security .KeyPair ;
28
26
import java .security .KeyStore ;
40
38
import java .util .List ;
41
39
42
40
/**
43
- * This class parses private key, public key, and certificate for use in their respective java.security objects.
41
+ * This class parses private key, public key, and certificate for use in their
42
+ * respective java.security objects.
44
43
*/
45
44
@ Getter
46
45
public class CredentialParser {
@@ -61,7 +60,13 @@ public class CredentialParser {
61
60
62
61
private PublicKey publicKey ;
63
62
64
- public void parseJKSCredentials (String jksKeystore ) {
63
+ /**
64
+ * This method assigns the class-scoped PublicKey and PrivateKey objects with values
65
+ * from a Java keystore.
66
+ *
67
+ * @param jksKeystore filename of the Java keystore
68
+ */
69
+ public void parseJKSCredentials (final String jksKeystore ) {
65
70
KeyStore .PrivateKeyEntry privateKeyEntry =
66
71
parseKeystorePrivateKey (jksKeystore ,
67
72
SwidTagConstants .DEFAULT_PRIVATE_KEY_ALIAS ,
@@ -71,7 +76,15 @@ public void parseJKSCredentials(String jksKeystore) {
71
76
publicKey = certificate .getPublicKey ();
72
77
}
73
78
74
- public void parsePEMCredentials (String certificateFile , String privateKeyFile )
79
+ /**
80
+ * This method assigns the class-scoped PublicKey and PrivateKey objects with values
81
+ * from a PEM file.
82
+ *
83
+ * @param certificateFile filename of the certificate
84
+ * @param privateKeyFile filename of the private key
85
+ * @throws Exception if there is a problem with parsing the private key
86
+ */
87
+ public void parsePEMCredentials (final String certificateFile , final String privateKeyFile )
75
88
throws Exception {
76
89
certificate = parsePEMCertificates (certificateFile ).get (0 );
77
90
if (certificate .getIssuerX500Principal ().equals (certificate .getSubjectX500Principal ())) {
@@ -81,41 +94,6 @@ public void parsePEMCredentials(String certificateFile, String privateKeyFile)
81
94
publicKey = certificate .getPublicKey ();
82
95
}
83
96
84
- /**
85
- * This method extracts certificate bytes from a string. The bytes are assumed to be
86
- * PEM format, and a header and footer are concatenated with the input string to
87
- * facilitate proper parsing.
88
- *
89
- * @param pemString the input string
90
- * @return an X509Certificate created from the string
91
- * @throws CertificateException if instantiating the CertificateFactory errors
92
- */
93
- public X509Certificate parseCertFromPEMString (String pemString ) throws CertificateException {
94
- try {
95
- CertificateFactory factory = CertificateFactory .getInstance (X509 );
96
- InputStream inputStream = new ByteArrayInputStream ((CERTIFICATE_HEADER
97
- + System .lineSeparator ()
98
- + pemString
99
- + System .lineSeparator ()
100
- + CERTIFICATE_FOOTER ).getBytes ());
101
- return (X509Certificate ) factory .generateCertificate (inputStream );
102
- } catch (CertificateException e ) {
103
- throw e ;
104
- }
105
- }
106
-
107
- /**
108
- * This method returns the X509Certificate object from a PEM certificate file.
109
- *
110
- * @param certificateFile
111
- * @return
112
- * @throws FileNotFoundException
113
- */
114
- public List <X509Certificate > parseCertsFromPEM (String certificateFile )
115
- throws FileNotFoundException {
116
- return parsePEMCertificates (certificateFile );
117
- }
118
-
119
97
/**
120
98
* This method returns the X509Certificate found in a PEM file.
121
99
* Unchecked typcase warnings are suppressed because the CertificateFactory
@@ -125,7 +103,7 @@ public List<X509Certificate> parseCertsFromPEM(String certificateFile)
125
103
* @return a list containing all X509Certificates extracted
126
104
*/
127
105
@ SuppressWarnings ("unchecked" )
128
- private List <X509Certificate > parsePEMCertificates (String filename ) {
106
+ private List <X509Certificate > parsePEMCertificates (final String filename ) {
129
107
List <X509Certificate > certificates = null ;
130
108
FileInputStream fis = null ;
131
109
BufferedInputStream bis = null ;
@@ -169,11 +147,13 @@ private List<X509Certificate> parsePEMCertificates(String filename) {
169
147
* Algorithm argument is present to allow handling of multiple encryption algorithms,
170
148
* but for now it is always RSA.
171
149
*
172
- * @param filename
173
- * @return
150
+ * @param filename the nanme of the PEM file
151
+ * @param algorithm the encryption algorithm
152
+ * @return the PrivateKey object
174
153
*/
175
- private PrivateKey parsePEMPrivateKey (String filename , String algorithm ) throws Exception {
176
- PrivateKey privateKey = null ;
154
+ private PrivateKey parsePEMPrivateKey (final String filename ,
155
+ final String algorithm ) throws Exception {
156
+ PrivateKey privatePemKey = null ;
177
157
FileInputStream fis = null ;
178
158
DataInputStream dis = null ;
179
159
String errorMessage = "" ;
@@ -187,7 +167,7 @@ private PrivateKey parsePEMPrivateKey(String filename, String algorithm) throws
187
167
188
168
String privateKeyStr = new String (key );
189
169
if (privateKeyStr .contains (PKCS1_HEADER )) {
190
- privateKey = getPKCS1KeyPair (filename ).getPrivate ();
170
+ privatePemKey = getPKCS1KeyPair (filename ).getPrivate ();
191
171
} else if (privateKeyStr .contains (PKCS8_HEADER )) {
192
172
privateKeyStr = privateKeyStr .replace (PKCS8_HEADER , "" );
193
173
privateKeyStr = privateKeyStr .replace (PKCS8_FOOTER , "" );
@@ -196,7 +176,7 @@ private PrivateKey parsePEMPrivateKey(String filename, String algorithm) throws
196
176
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec (decodedKey );
197
177
KeyFactory keyFactory = KeyFactory .getInstance (algorithm );
198
178
199
- privateKey = keyFactory .generatePrivate (spec );
179
+ privatePemKey = keyFactory .generatePrivate (spec );
200
180
}
201
181
} catch (FileNotFoundException e ) {
202
182
errorMessage += "Unable to locate private key file: " + filename ;
@@ -224,16 +204,16 @@ private PrivateKey parsePEMPrivateKey(String filename, String algorithm) throws
224
204
}
225
205
}
226
206
227
- return privateKey ;
207
+ return privatePemKey ;
228
208
}
229
209
230
210
/**
231
211
* This method reads a PKCS1 keypair from a PEM file.
232
212
*
233
213
* @param filename
234
- * @return
214
+ * @return KeyPair representing the private and public keys parsed from the file.
235
215
*/
236
- private KeyPair getPKCS1KeyPair (String filename ) throws IOException {
216
+ private KeyPair getPKCS1KeyPair (final String filename ) throws IOException {
237
217
Security .addProvider (new BouncyCastleProvider ());
238
218
PEMParser pemParser = new PEMParser (new FileReader (filename ));
239
219
JcaPEMKeyConverter converter = new JcaPEMKeyConverter ().setProvider ("BC" );
@@ -250,29 +230,30 @@ private KeyPair getPKCS1KeyPair(String filename) throws IOException {
250
230
* @param password
251
231
* @return KeyStore.PrivateKeyEntry
252
232
*/
253
- private KeyStore .PrivateKeyEntry parseKeystorePrivateKey (String keystoreFile , String alias ,
254
- String password ) {
233
+ private KeyStore .PrivateKeyEntry parseKeystorePrivateKey (final String keystoreFile ,
234
+ final String alias ,
235
+ final String password ) {
255
236
KeyStore keystore = null ;
256
- KeyStore .PrivateKeyEntry privateKey = null ;
237
+ KeyStore .PrivateKeyEntry keystorePrivateKey = null ;
257
238
try {
258
239
keystore = KeyStore .getInstance ("JKS" );
259
240
keystore .load (new FileInputStream (keystoreFile ), password .toCharArray ());
260
- privateKey = (KeyStore .PrivateKeyEntry ) keystore .getEntry (alias ,
241
+ keystorePrivateKey = (KeyStore .PrivateKeyEntry ) keystore .getEntry (alias ,
261
242
new KeyStore .PasswordProtection (password .toCharArray ()));
262
243
} catch (FileNotFoundException e ) {
263
244
System .out .println ("Cannot locate keystore " + keystoreFile );
264
- } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableEntryException |
265
- CertificateException | IOException e ) {
245
+ } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableEntryException
246
+ | CertificateException | IOException e ) {
266
247
e .printStackTrace ();
267
248
}
268
249
269
- return privateKey ;
250
+ return keystorePrivateKey ;
270
251
}
271
252
272
253
/**
273
254
* This method returns the authorityInfoAccess from an X509Certificate.
274
255
*
275
- * @return
256
+ * @return the AIA string parsed from the local certificate
276
257
* @throws IOException
277
258
*/
278
259
public String getCertificateAuthorityInfoAccess () throws IOException {
@@ -305,22 +286,6 @@ public String getCertificateSubjectKeyIdentifier() throws IOException {
305
286
if (extension != null && extension .length > 0 ) {
306
287
decodedValue = JcaX509ExtensionUtils .parseExtensionValue (extension ).toString ();
307
288
}
308
- return decodedValue .substring (1 );//Drop the # at the beginning of the string
309
- }
310
-
311
- /**
312
- * This method returns the subjectKeyIdentifier from a given X509Certificate.
313
- *
314
- * @param certificate the cert to pull the subjectKeyIdentifier from
315
- * @return the String representation of the subjectKeyIdentifier
316
- * @throws IOException
317
- */
318
- public String getCertificateSubjectKeyIdentifier (X509Certificate certificate ) throws IOException {
319
- String decodedValue = null ;
320
- byte [] extension = certificate .getExtensionValue (Extension .subjectKeyIdentifier .getId ());
321
- if (extension != null && extension .length > 0 ) {
322
- decodedValue = JcaX509ExtensionUtils .parseExtensionValue (extension ).toString ();
323
- }
324
- return decodedValue .substring (1 );//Drop the # at the beginning of the string
289
+ return decodedValue .substring (1 ); //Drop the # at the beginning of the string
325
290
}
326
291
}
0 commit comments