Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Programmatically read informations from a Key/Certificate

Tags:

java

security

I'm tryin to build a certificate/key management tool, but i don't understand how to obtain md5 fingerprint of a certificate/key.

For example if i use keytool command on a keystore i obtain the

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: myname
Creation date: 21-Aug-2011
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=bla bla, L=bla, ST=bla
Issuer: CN=bla bla, L=bla, ST=bla
Serial number: 123w3qa
Valid from: Sun Aug 21 00:12:31 CEST 2011 until: Mon Jul 28 00:12:31 CEST 2110
Certificate fingerprints:
         MD5:  1A:DE:60:21:DE:B1:BF:C3:D1:AD:11:F1:21:22:D7:9E
         SHA1: 72:3A:D9:2E:1A:DE:60:21:DE:B1:BF:C3:D1:AD:11:F1:21:22:D7:9E
         Signature algorithm name: SHA256withRSA
         Version: 3

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: AA EA FA FE 34 DA 6E C6   FC 8B 6C DE S9 21 S9 S4  ......^...l.I!.D
0010: S3 33 29 SD                                        .S..
]
]

*******************************************
*******************************************

Now i want to obtain via java the following informations: 1. MD5 fingerprint 2. KeyIdentifier

I obtained some informations using X500Certificate object and X500Principal(for example date from and to, owner, issuer, alias name), but i didn't found where i can obtain other informations. Can someone help me?

like image 784
Ivan Avatar asked Nov 21 '25 01:11

Ivan


1 Answers

If you check the source code for keytool you can see the following:

 2830       getCertFingerPrint("MD5", cert),

which calls:

 3167       /**
 3168        * Gets the requested finger print of the certificate.
 3169        */
 3170       private String getCertFingerPrint(String mdAlg, Certificate cert)
 3171           throws Exception
 3172       {
 3173           byte[] encCertInfo = cert.getEncoded();
 3174           MessageDigest md = MessageDigest.getInstance(mdAlg);
 3175           byte[] digest = md.digest(encCertInfo);
 3176           return toHexString(digest);
 3177       }
like image 197
DNA Avatar answered Nov 23 '25 15:11

DNA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!