[9725efb] | 1 | package net.deterlab.abac; |
---|
| 2 | |
---|
| 3 | import java.io.*; |
---|
| 4 | |
---|
| 5 | import java.util.*; |
---|
| 6 | import java.security.*; |
---|
| 7 | import java.security.cert.*; |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | import org.bouncycastle.asn1.*; |
---|
| 11 | import org.bouncycastle.asn1.util.*; |
---|
| 12 | import org.bouncycastle.asn1.x509.*; |
---|
| 13 | import org.bouncycastle.x509.*; |
---|
| 14 | import org.bouncycastle.jce.provider.X509AttrCertParser; |
---|
| 15 | import org.bouncycastle.jce.provider.X509CertificateObject; |
---|
| 16 | import org.bouncycastle.openssl.PEMReader; |
---|
| 17 | |
---|
| 18 | public class Identity { |
---|
| 19 | private X509CertificateObject m_cert; |
---|
| 20 | private String m_keyid; |
---|
| 21 | private String m_cn; |
---|
| 22 | |
---|
| 23 | public void init(String filename) throws |
---|
| 24 | CertificateException, NoSuchAlgorithmException,InvalidKeyException, |
---|
| 25 | NoSuchProviderException, SignatureException, FileNotFoundException, |
---|
| 26 | IOException { |
---|
| 27 | PEMReader pr = new PEMReader(new FileReader(filename)); |
---|
| 28 | Object c = pr.readObject(); |
---|
| 29 | |
---|
| 30 | if (c instanceof X509CertificateObject) { |
---|
| 31 | m_cert = (X509CertificateObject) c; |
---|
| 32 | m_cert.verify(m_cert.getPublicKey()); |
---|
| 33 | |
---|
| 34 | // Cert is valid, fill in the CN and keyid |
---|
| 35 | // |
---|
| 36 | // This little rigamarole is to get to the SHA1 hash of the |
---|
| 37 | // key. |
---|
| 38 | PublicKey k = m_cert.getPublicKey(); |
---|
| 39 | ASN1Sequence seq = (ASN1Sequence) new ASN1InputStream( |
---|
| 40 | k.getEncoded()).readObject(); |
---|
| 41 | SubjectPublicKeyInfo ki = new SubjectPublicKeyInfo(seq); |
---|
| 42 | SubjectKeyIdentifier id = |
---|
| 43 | SubjectKeyIdentifier.createSHA1KeyIdentifier(ki); |
---|
| 44 | |
---|
| 45 | // Now format it into a string for keeps |
---|
| 46 | Formatter fmt = new Formatter(new StringWriter()); |
---|
| 47 | for (byte b: id.getKeyIdentifier()) |
---|
| 48 | fmt.format("%02x", b); |
---|
| 49 | m_keyid = fmt.out().toString(); |
---|
| 50 | |
---|
| 51 | m_cn = m_cert.getSubjectDN().getName(); |
---|
| 52 | /// XXX: better parse |
---|
| 53 | if (m_cn.startsWith("CN=")) m_cn = m_cn.substring(3); |
---|
| 54 | } |
---|
| 55 | else throw new CertificateException("Not an identity certificate"); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | public Identity(String filename) throws |
---|
| 59 | CertificateException, NoSuchAlgorithmException,InvalidKeyException, |
---|
| 60 | NoSuchProviderException, SignatureException, FileNotFoundException, |
---|
| 61 | IOException { init(filename); } |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | public Identity(File file) throws |
---|
| 65 | CertificateException, NoSuchAlgorithmException,InvalidKeyException, |
---|
| 66 | NoSuchProviderException, SignatureException, FileNotFoundException, |
---|
| 67 | IOException { init(file.getPath()); } |
---|
| 68 | |
---|
| 69 | public String getKeyID() { return m_keyid; } |
---|
| 70 | public String getName() { return m_cn; } |
---|
| 71 | public String toString() { return m_keyid + " (" + m_cn + ")"; } |
---|
| 72 | public X509CertificateObject getCertificate() { return m_cert; } |
---|
| 73 | }; |
---|