Skip to content

Commit b1db0b3

Browse files
committed
WIP: CredentialParser changes
1 parent 612a819 commit b1db0b3

File tree

1 file changed

+40
-75
lines changed

1 file changed

+40
-75
lines changed

tools/tcg_rim_tool/src/main/java/hirs/swid/CredentialParser.java

Lines changed: 40 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
import org.bouncycastle.util.encoders.DecoderException;
1616

1717
import java.io.BufferedInputStream;
18-
import java.io.ByteArrayInputStream;
1918
import java.io.DataInputStream;
2019
import java.io.File;
2120
import java.io.FileInputStream;
2221
import java.io.FileNotFoundException;
2322
import java.io.FileReader;
2423
import java.io.IOException;
25-
import java.io.InputStream;
2624
import java.security.KeyFactory;
2725
import java.security.KeyPair;
2826
import java.security.KeyStore;
@@ -40,7 +38,8 @@
4038
import java.util.List;
4139

4240
/**
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.
4443
*/
4544
@Getter
4645
public class CredentialParser {
@@ -61,7 +60,13 @@ public class CredentialParser {
6160

6261
private PublicKey publicKey;
6362

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) {
6570
KeyStore.PrivateKeyEntry privateKeyEntry =
6671
parseKeystorePrivateKey(jksKeystore,
6772
SwidTagConstants.DEFAULT_PRIVATE_KEY_ALIAS,
@@ -71,7 +76,15 @@ public void parseJKSCredentials(String jksKeystore) {
7176
publicKey = certificate.getPublicKey();
7277
}
7378

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)
7588
throws Exception {
7689
certificate = parsePEMCertificates(certificateFile).get(0);
7790
if (certificate.getIssuerX500Principal().equals(certificate.getSubjectX500Principal())) {
@@ -81,41 +94,6 @@ public void parsePEMCredentials(String certificateFile, String privateKeyFile)
8194
publicKey = certificate.getPublicKey();
8295
}
8396

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-
11997
/**
12098
* This method returns the X509Certificate found in a PEM file.
12199
* Unchecked typcase warnings are suppressed because the CertificateFactory
@@ -125,7 +103,7 @@ public List<X509Certificate> parseCertsFromPEM(String certificateFile)
125103
* @return a list containing all X509Certificates extracted
126104
*/
127105
@SuppressWarnings("unchecked")
128-
private List<X509Certificate> parsePEMCertificates(String filename) {
106+
private List<X509Certificate> parsePEMCertificates(final String filename) {
129107
List<X509Certificate> certificates = null;
130108
FileInputStream fis = null;
131109
BufferedInputStream bis = null;
@@ -169,11 +147,13 @@ private List<X509Certificate> parsePEMCertificates(String filename) {
169147
* Algorithm argument is present to allow handling of multiple encryption algorithms,
170148
* but for now it is always RSA.
171149
*
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
174153
*/
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;
177157
FileInputStream fis = null;
178158
DataInputStream dis = null;
179159
String errorMessage = "";
@@ -187,7 +167,7 @@ private PrivateKey parsePEMPrivateKey(String filename, String algorithm) throws
187167

188168
String privateKeyStr = new String(key);
189169
if (privateKeyStr.contains(PKCS1_HEADER)) {
190-
privateKey = getPKCS1KeyPair(filename).getPrivate();
170+
privatePemKey = getPKCS1KeyPair(filename).getPrivate();
191171
} else if (privateKeyStr.contains(PKCS8_HEADER)) {
192172
privateKeyStr = privateKeyStr.replace(PKCS8_HEADER, "");
193173
privateKeyStr = privateKeyStr.replace(PKCS8_FOOTER, "");
@@ -196,7 +176,7 @@ private PrivateKey parsePEMPrivateKey(String filename, String algorithm) throws
196176
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decodedKey);
197177
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
198178

199-
privateKey = keyFactory.generatePrivate(spec);
179+
privatePemKey = keyFactory.generatePrivate(spec);
200180
}
201181
} catch (FileNotFoundException e) {
202182
errorMessage += "Unable to locate private key file: " + filename;
@@ -224,16 +204,16 @@ private PrivateKey parsePEMPrivateKey(String filename, String algorithm) throws
224204
}
225205
}
226206

227-
return privateKey;
207+
return privatePemKey;
228208
}
229209

230210
/**
231211
* This method reads a PKCS1 keypair from a PEM file.
232212
*
233213
* @param filename
234-
* @return
214+
* @return KeyPair representing the private and public keys parsed from the file.
235215
*/
236-
private KeyPair getPKCS1KeyPair(String filename) throws IOException {
216+
private KeyPair getPKCS1KeyPair(final String filename) throws IOException {
237217
Security.addProvider(new BouncyCastleProvider());
238218
PEMParser pemParser = new PEMParser(new FileReader(filename));
239219
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
@@ -250,29 +230,30 @@ private KeyPair getPKCS1KeyPair(String filename) throws IOException {
250230
* @param password
251231
* @return KeyStore.PrivateKeyEntry
252232
*/
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) {
255236
KeyStore keystore = null;
256-
KeyStore.PrivateKeyEntry privateKey = null;
237+
KeyStore.PrivateKeyEntry keystorePrivateKey = null;
257238
try {
258239
keystore = KeyStore.getInstance("JKS");
259240
keystore.load(new FileInputStream(keystoreFile), password.toCharArray());
260-
privateKey = (KeyStore.PrivateKeyEntry) keystore.getEntry(alias,
241+
keystorePrivateKey = (KeyStore.PrivateKeyEntry) keystore.getEntry(alias,
261242
new KeyStore.PasswordProtection(password.toCharArray()));
262243
} catch (FileNotFoundException e) {
263244
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) {
266247
e.printStackTrace();
267248
}
268249

269-
return privateKey;
250+
return keystorePrivateKey;
270251
}
271252

272253
/**
273254
* This method returns the authorityInfoAccess from an X509Certificate.
274255
*
275-
* @return
256+
* @return the AIA string parsed from the local certificate
276257
* @throws IOException
277258
*/
278259
public String getCertificateAuthorityInfoAccess() throws IOException {
@@ -305,22 +286,6 @@ public String getCertificateSubjectKeyIdentifier() throws IOException {
305286
if (extension != null && extension.length > 0) {
306287
decodedValue = JcaX509ExtensionUtils.parseExtensionValue(extension).toString();
307288
}
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
325290
}
326291
}

0 commit comments

Comments
 (0)