3
3
import burp .BurpExtender ;
4
4
import gui .CertificateTab ;
5
5
import helpers .FileHelper ;
6
+ import model .BurpCertificate ;
7
+ import model .BurpCertificateBuilder ;
8
+ import model .BurpCertificateExtension ;
9
+ import model .BurpCertificateStore ;
10
+ import model .ObjectIdentifier ;
11
+ import org .bouncycastle .asn1 .pkcs .PrivateKeyInfo ;
12
+ import org .bouncycastle .openssl .PEMKeyPair ;
13
+ import org .bouncycastle .openssl .PEMParser ;
14
+ import org .bouncycastle .openssl .jcajce .JcaPEMKeyConverter ;
15
+
6
16
import java .awt .Component ;
7
- import java .io .BufferedReader ;
8
17
import java .io .ByteArrayInputStream ;
9
18
import java .io .DataInputStream ;
10
19
import java .io .File ;
13
22
import java .io .IOException ;
14
23
import java .security .InvalidKeyException ;
15
24
import java .security .KeyFactory ;
16
- import java .security .KeyPair ;
17
25
import java .security .NoSuchAlgorithmException ;
18
26
import java .security .NoSuchProviderException ;
19
27
import java .security .PrivateKey ;
31
39
import java .util .LinkedList ;
32
40
import java .util .List ;
33
41
import java .util .Observable ;
34
- import model .BurpCertificate ;
35
- import model .BurpCertificateBuilder ;
36
- import model .BurpCertificateExtension ;
37
- import model .BurpCertificateStore ;
38
- import model .ObjectIdentifier ;
39
- import org .bouncycastle .openssl .PEMKeyPair ;
40
- import org .bouncycastle .openssl .PEMParser ;
41
- import org .bouncycastle .openssl .jcajce .JcaPEMKeyConverter ;
42
42
43
43
public class CertificateTabController extends Observable {
44
44
@@ -186,7 +186,7 @@ private void importExampleCertificates() {
186
186
if (fileHelper .startedFromJar ()) {
187
187
try {
188
188
BurpCertificate c1 = importCertificate (fileHelper .exportRessourceFromJar ("examples/certificate.pem" ));
189
- importPrivateKey (c1 , fileHelper .exportRessourceFromJar ("examples/private_key_rsa.pem" ));
189
+ importPrivateKeyPemFormat (c1 , fileHelper .exportRessourceFromJar ("examples/private_key_rsa.pem" ));
190
190
importCertificateChain (fileHelper .exportRessourceFromJar ("examples/example.org_chain.pem" ));
191
191
setCertificateDetails (c1 );
192
192
} catch (IOException e ) {
@@ -196,7 +196,7 @@ private void importExampleCertificates() {
196
196
}
197
197
} else {
198
198
BurpCertificate c1 = importCertificate ("src/main/resources/examples/certificate.pem" );
199
- importPrivateKey (c1 , "src/main/resources/examples/private_key_rsa.pem" );
199
+ importPrivateKeyPemFormat (c1 , "src/main/resources/examples/private_key_rsa.pem" );
200
200
importCertificateChain ("src/main/resources/examples/example.org_chain.pem" );
201
201
setCertificateDetails (c1 );
202
202
}
@@ -311,29 +311,34 @@ public List<BurpCertificate> importCertificateChain(String filename) {
311
311
}
312
312
313
313
/**
314
- * Import a private RSA key in PEM format from a file and add it to the
314
+ * Import a private key in PEM format from a file and add it to the
315
315
* selected certificate.
316
316
*
317
317
* @param certificate which the private key is for.
318
- * @param filename of the private RSA key in PEM format
318
+ * @param filename of the private key in PEM format
319
319
*/
320
- public void importPrivateKey (BurpCertificate certificate , String filename ) {
320
+ public void importPrivateKeyPemFormat (BurpCertificate certificate , String filename ) {
321
321
setStatus ("Importing private key..." );
322
- BufferedReader br ;
323
- try {
324
- br = new BufferedReader (new FileReader (filename ));
325
- PEMParser pp = new PEMParser (br );
326
- PEMKeyPair pemKeyPair = (PEMKeyPair ) pp .readObject ();
327
- KeyPair kp = new JcaPEMKeyConverter ().getKeyPair (pemKeyPair );
328
- pp .close ();
322
+ try (var pemParser = new PEMParser (new FileReader (filename ))) {
323
+ PrivateKeyInfo privateKeyInfo = null ;
324
+ var object = pemParser .readObject ();
325
+ if (object instanceof PEMKeyPair pemKeyPair ) {
326
+ privateKeyInfo = pemKeyPair .getPrivateKeyInfo ();
327
+ } else if (object instanceof PrivateKeyInfo ) {
328
+ privateKeyInfo = (PrivateKeyInfo ) object ;
329
+ }
330
+ if (privateKeyInfo == null ) {
331
+ setStatus ("Error importing private key." );
332
+ return ;
333
+ }
334
+ var converter = new JcaPEMKeyConverter ();
335
+ var privateKey = converter .getPrivateKey (privateKeyInfo );
336
+ certificate .setPrivateKey (privateKey );
329
337
setCertificateTree ();
330
338
setStatus ("Private Key imported." );
331
- certificate .setPrivateKey (kp .getPrivate ());
332
- } catch (IOException e ) {
333
- setStatus ("Error importing private key. (" + e .getMessage () + ")" );
334
- BurpExtender .api .logging ().logToError (e );
335
339
} catch (Exception e ) {
336
- setStatus ("Error (" + e .getMessage () + ")" );
340
+ setStatus ("Error importing private Key. (" + e .getMessage () + ")" );
341
+ BurpExtender .api .logging ().logToError (e );
337
342
}
338
343
}
339
344
@@ -345,7 +350,7 @@ public void importPrivateKey(BurpCertificate certificate, String filename) {
345
350
* <code>openssl pkcs8 -topk8 -inform PEM -outform DER -in privatekey.pem -out private_key_pkcs8.pem -nocrypt</code>
346
351
* @param filename of the PKCS8 key
347
352
*/
348
- public void importPKCS8 (BurpCertificate certificate , String filename ) {
353
+ public void importPrivateKeyPkcs8DerFormat (BurpCertificate certificate , String filename ) {
349
354
setStatus ("Importing private key..." );
350
355
FileInputStream fis ;
351
356
File file = new File (filename );
@@ -363,11 +368,9 @@ public void importPKCS8(BurpCertificate certificate, String filename) {
363
368
certificate .setPrivateKey (privateKey );
364
369
setCertificateTree ();
365
370
setStatus ("Private Key imported." );
366
- } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e ) {
371
+ } catch (Exception e ) {
367
372
setStatus ("Error importing private Key. (" + e .getMessage () + ")" );
368
373
BurpExtender .api .logging ().logToError (e );
369
- } catch (Exception e ) {
370
- setStatus ("Error (" + e .getMessage () + ")" );
371
374
}
372
375
}
373
376
@@ -394,12 +397,12 @@ public void exportCertificate(BurpCertificate certificate, String filename) {
394
397
}
395
398
396
399
/**
397
- * Export Private RSA Key in PEM format.
400
+ * Export private key in PEM format.
398
401
*
399
402
* @param certificate to export
400
- * @param filename for the exported private RSA key
403
+ * @param filename for the exported private key
401
404
*/
402
- public void exportPrivateKey (BurpCertificate certificate , String filename ) {
405
+ public void exportPrivateKeyPemFormat (BurpCertificate certificate , String filename ) {
403
406
setStatus ("Exporting private key..." );
404
407
try {
405
408
fileHelper .exportPEMObject (certificate .getPrivateKey (), filename );
0 commit comments