source: java/net/deterlab/abac/Identity.java @ 9725efb

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since 9725efb was 9725efb, checked in by Ted Faber <faber@…>, 13 years ago

Parse Identity certs fully. Put 'em in a class.

  • Property mode set to 100644
File size: 2.4 KB
Line 
1package net.deterlab.abac;
2
3import java.io.*;
4
5import java.util.*;
6import java.security.*;
7import java.security.cert.*;
8
9
10import org.bouncycastle.asn1.*;
11import org.bouncycastle.asn1.util.*;
12import org.bouncycastle.asn1.x509.*;
13import org.bouncycastle.x509.*;
14import org.bouncycastle.jce.provider.X509AttrCertParser;
15import org.bouncycastle.jce.provider.X509CertificateObject;
16import org.bouncycastle.openssl.PEMReader;
17
18public 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};
Note: See TracBrowser for help on using the repository browser.