[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.*; |
---|
[3a52bed] | 8 | import javax.security.auth.x500.*; |
---|
[9725efb] | 9 | |
---|
[3a52bed] | 10 | import java.math.BigInteger; |
---|
[9725efb] | 11 | |
---|
| 12 | import org.bouncycastle.asn1.*; |
---|
| 13 | import org.bouncycastle.asn1.util.*; |
---|
| 14 | import org.bouncycastle.asn1.x509.*; |
---|
| 15 | import org.bouncycastle.x509.*; |
---|
| 16 | import org.bouncycastle.jce.provider.X509AttrCertParser; |
---|
| 17 | import org.bouncycastle.jce.provider.X509CertificateObject; |
---|
| 18 | import org.bouncycastle.openssl.PEMReader; |
---|
[1a7e6d3] | 19 | import org.bouncycastle.openssl.PEMWriter; |
---|
[9725efb] | 20 | |
---|
[5cf72cc] | 21 | public class Identity implements Comparable { |
---|
[9725efb] | 22 | private X509CertificateObject m_cert; |
---|
| 23 | private String m_keyid; |
---|
| 24 | private String m_cn; |
---|
[42ca4b8] | 25 | private KeyPair kp; |
---|
[9725efb] | 26 | |
---|
[1a7e6d3] | 27 | /** |
---|
| 28 | * Initialize internals from PEM cert in a reader. Use a PEMReader to get |
---|
[8a14e37] | 29 | * the certificate, and call init(cert) on it. |
---|
[1a7e6d3] | 30 | */ |
---|
[8a14e37] | 31 | protected void init(Reader r) throws |
---|
[42ca4b8] | 32 | CertificateException, NoSuchAlgorithmException,InvalidKeyException, |
---|
| 33 | NoSuchProviderException, SignatureException, IOException { |
---|
| 34 | PEMReader pr = new PEMReader(r); |
---|
| 35 | Object c = null; |
---|
| 36 | |
---|
| 37 | while ( ( c= pr.readObject()) != null ){ |
---|
| 38 | |
---|
| 39 | if (c instanceof X509CertificateObject) { |
---|
| 40 | if ( m_cn == null ) |
---|
| 41 | init((X509CertificateObject)c); |
---|
| 42 | else |
---|
| 43 | throw new CertificateException("Two certs in one file"); |
---|
| 44 | } |
---|
| 45 | else if (c instanceof KeyPair) setKeyPair((KeyPair)c); |
---|
[8a14e37] | 46 | else |
---|
[42ca4b8] | 47 | throw new CertificateException( |
---|
| 48 | "Not an identity certificate"); |
---|
| 49 | } |
---|
[8a14e37] | 50 | } |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Initialize internals from cert. Confirm it is self signed, and then |
---|
| 54 | * the keyid and common name. There's some work to get this stuff, but |
---|
| 55 | * it's all an incantation of getting the right classes to get the right |
---|
| 56 | * data. Looks more complex than it is. |
---|
| 57 | */ |
---|
| 58 | protected void init(X509CertificateObject c) throws |
---|
| 59 | CertificateException, NoSuchAlgorithmException,InvalidKeyException, |
---|
| 60 | NoSuchProviderException, SignatureException, IOException { |
---|
| 61 | m_cert = (X509CertificateObject) c; |
---|
| 62 | m_cert.verify(m_cert.getPublicKey()); |
---|
| 63 | // Cert is valid, fill in the CN and keyid |
---|
| 64 | m_keyid = extractKeyID(m_cert.getPublicKey()); |
---|
| 65 | m_cn = m_cert.getSubjectDN().getName(); |
---|
| 66 | /// XXX: better parse |
---|
| 67 | if (m_cn.startsWith("CN=")) m_cn = m_cn.substring(3); |
---|
[9725efb] | 68 | } |
---|
| 69 | |
---|
[1a7e6d3] | 70 | /** |
---|
[3a52bed] | 71 | * Construct from a string, used as a CN |
---|
[1a7e6d3] | 72 | */ |
---|
[3a52bed] | 73 | public Identity(String cn) throws |
---|
| 74 | CertificateException, NoSuchAlgorithmException,InvalidKeyException, |
---|
| 75 | NoSuchProviderException, SignatureException, IOException { |
---|
| 76 | X509V1CertificateGenerator gen = new X509V1CertificateGenerator(); |
---|
[42ca4b8] | 77 | kp = KeyPairGenerator.getInstance("RSA").genKeyPair(); |
---|
[3a52bed] | 78 | |
---|
| 79 | gen.setIssuerDN(new X500Principal("CN=" + cn)); |
---|
| 80 | gen.setSubjectDN(new X500Principal("CN=" + cn)); |
---|
| 81 | gen.setNotAfter(new Date(System.currentTimeMillis() |
---|
| 82 | + 3600 * 1000 * 24 * 365)); |
---|
| 83 | gen.setNotBefore(new Date(System.currentTimeMillis())); |
---|
| 84 | gen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis())); |
---|
| 85 | gen.setPublicKey(kp.getPublic()); |
---|
| 86 | gen.setSignatureAlgorithm("SHA256WithRSAEncryption"); |
---|
[42ca4b8] | 87 | X509CertificateObject a = |
---|
| 88 | (X509CertificateObject) gen.generate(kp.getPrivate()); |
---|
[3a52bed] | 89 | init(a); |
---|
| 90 | } |
---|
| 91 | |
---|
[9725efb] | 92 | |
---|
| 93 | |
---|
[1a7e6d3] | 94 | /** |
---|
| 95 | * Construct from a file, containing a self-signed PEM certificate. |
---|
| 96 | */ |
---|
[9725efb] | 97 | public Identity(File file) throws |
---|
| 98 | CertificateException, NoSuchAlgorithmException,InvalidKeyException, |
---|
| 99 | NoSuchProviderException, SignatureException, FileNotFoundException, |
---|
[42ca4b8] | 100 | IOException { |
---|
| 101 | kp = null; |
---|
| 102 | init(new FileReader(file)); |
---|
| 103 | } |
---|
[1a7e6d3] | 104 | |
---|
| 105 | /** |
---|
| 106 | * Construct from a reader, containing a self-signed PEM certificate. |
---|
| 107 | */ |
---|
| 108 | public Identity(Reader r) throws |
---|
| 109 | CertificateException, NoSuchAlgorithmException,InvalidKeyException, |
---|
[42ca4b8] | 110 | NoSuchProviderException, SignatureException, IOException { |
---|
| 111 | kp = null; |
---|
| 112 | init(r); |
---|
| 113 | } |
---|
[1a7e6d3] | 114 | |
---|
| 115 | /** |
---|
| 116 | * Construct from an InputStream, containing a self-signed PEM certificate. |
---|
| 117 | */ |
---|
| 118 | public Identity(InputStream s) throws |
---|
| 119 | CertificateException, NoSuchAlgorithmException,InvalidKeyException, |
---|
| 120 | NoSuchProviderException, SignatureException, IOException { |
---|
[42ca4b8] | 121 | kp = null; |
---|
[1a7e6d3] | 122 | init(new InputStreamReader(s)); |
---|
| 123 | } |
---|
| 124 | |
---|
[8a14e37] | 125 | /** |
---|
| 126 | * Construct from an X509CertificateObject, if you parsed one somewhere |
---|
| 127 | * else. |
---|
| 128 | */ |
---|
| 129 | public Identity(X509CertificateObject cert) throws |
---|
| 130 | CertificateException, NoSuchAlgorithmException,InvalidKeyException, |
---|
| 131 | NoSuchProviderException, SignatureException, FileNotFoundException, |
---|
[42ca4b8] | 132 | IOException { |
---|
| 133 | kp = null; |
---|
| 134 | init(cert); |
---|
| 135 | } |
---|
[8a14e37] | 136 | |
---|
[8a93b41] | 137 | /** |
---|
| 138 | * Write the PEM key to the given writer. |
---|
| 139 | */ |
---|
| 140 | public boolean writePrivateKey(Writer w) throws IOException { |
---|
| 141 | if (kp != null ) { |
---|
| 142 | PEMWriter pw = new PEMWriter(w); |
---|
| 143 | |
---|
| 144 | pw.writeObject(kp.getPrivate()); |
---|
| 145 | pw.flush(); |
---|
| 146 | return true; |
---|
| 147 | } |
---|
| 148 | else return false; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * Write the PEM key to a file with the given name. |
---|
| 153 | */ |
---|
| 154 | public boolean writePrivateKey(String fn) |
---|
| 155 | throws IOException, FileNotFoundException { |
---|
| 156 | return writePrivateKey(new FileWriter(fn)); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | * Write the PEM key to the given file. |
---|
| 161 | */ |
---|
| 162 | public boolean writePrivateKey(File fn) |
---|
| 163 | throws IOException, FileNotFoundException { |
---|
| 164 | return writePrivateKey(new FileWriter(fn)); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * Write the PEM key to the given OutputStream. |
---|
| 169 | */ |
---|
| 170 | public boolean writePrivateKey(OutputStream s) |
---|
| 171 | throws IOException, FileNotFoundException { |
---|
| 172 | return writePrivateKey(new OutputStreamWriter(s)); |
---|
| 173 | } |
---|
| 174 | |
---|
[1a7e6d3] | 175 | |
---|
| 176 | /** |
---|
| 177 | * Write the PEM cert to the given writer. |
---|
| 178 | */ |
---|
| 179 | public void write(Writer w) throws IOException { |
---|
| 180 | PEMWriter pw = new PEMWriter(w); |
---|
| 181 | |
---|
| 182 | pw.writeObject(m_cert); |
---|
[5cf72cc] | 183 | pw.flush(); |
---|
[1a7e6d3] | 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * Write the PEM cert to a file with the given name. |
---|
| 188 | */ |
---|
| 189 | public void write(String fn) throws IOException, FileNotFoundException { |
---|
| 190 | write(new FileWriter(fn)); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | /** |
---|
| 194 | * Write the PEM cert to the given file. |
---|
| 195 | */ |
---|
| 196 | public void write(File fn) throws IOException, FileNotFoundException { |
---|
| 197 | write(new FileWriter(fn)); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /** |
---|
| 201 | * Write the PEM cert to the given OutputStream. |
---|
| 202 | */ |
---|
| 203 | public void write(OutputStream s) |
---|
| 204 | throws IOException, FileNotFoundException { |
---|
| 205 | write(new OutputStreamWriter(s)); |
---|
| 206 | } |
---|
[9725efb] | 207 | |
---|
[e9360e2] | 208 | |
---|
[8a14e37] | 209 | /** |
---|
| 210 | * Get to the SHA1 hash of the key. |
---|
| 211 | */ |
---|
| 212 | public static String extractKeyID(PublicKey k) { |
---|
[e9360e2] | 213 | SubjectPublicKeyInfo ki = extractSubjectPublicKeyInfo(k); |
---|
| 214 | SubjectKeyIdentifier id = |
---|
| 215 | SubjectKeyIdentifier.createSHA1KeyIdentifier(ki); |
---|
| 216 | |
---|
| 217 | // Now format it into a string for keeps |
---|
| 218 | Formatter fmt = new Formatter(new StringWriter()); |
---|
| 219 | for (byte b: id.getKeyIdentifier()) |
---|
| 220 | fmt.format("%02x", b); |
---|
| 221 | return fmt.out().toString(); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | /** |
---|
| 225 | * Extratct the SubjectPublicKeyInfo. Useful for some other encryptions, |
---|
| 226 | * notably Certificate.make_cert(). |
---|
| 227 | */ |
---|
| 228 | public static SubjectPublicKeyInfo extractSubjectPublicKeyInfo(PublicKey k) { |
---|
[8a14e37] | 229 | ASN1Sequence seq = null; |
---|
| 230 | try { |
---|
| 231 | seq = (ASN1Sequence) new ASN1InputStream( |
---|
| 232 | k.getEncoded()).readObject(); |
---|
| 233 | } |
---|
| 234 | catch (IOException ie) { |
---|
| 235 | // Badly formatted key?? |
---|
| 236 | return null; |
---|
| 237 | } |
---|
[e9360e2] | 238 | return new SubjectPublicKeyInfo(seq); |
---|
[8a14e37] | 239 | } |
---|
| 240 | |
---|
[e9360e2] | 241 | |
---|
[1a7e6d3] | 242 | // Accessors |
---|
[9725efb] | 243 | public String getKeyID() { return m_keyid; } |
---|
| 244 | public String getName() { return m_cn; } |
---|
[42ca4b8] | 245 | public String toString() { |
---|
| 246 | String s = m_keyid + " (" + m_cn ; |
---|
| 247 | |
---|
| 248 | if (m_keyid != null ) s += " [keyed]"; |
---|
| 249 | s += ")"; |
---|
| 250 | return s; |
---|
| 251 | } |
---|
| 252 | /** |
---|
| 253 | * Associate a keypair with this Identity. If the ID has a certificate, |
---|
| 254 | * make sure that the keypair matches it. If not throw an |
---|
| 255 | * IllegalArgumentException. |
---|
| 256 | */ |
---|
| 257 | public void setKeyPair(KeyPair k) { |
---|
| 258 | if (m_keyid != null) { |
---|
| 259 | String kid = extractKeyID(k.getPublic()); |
---|
| 260 | |
---|
| 261 | if ( kid != null && kid.equals(m_keyid)) kp = k; |
---|
| 262 | else |
---|
| 263 | throw new IllegalArgumentException( |
---|
| 264 | "Keypair does not match certificate"); |
---|
| 265 | } |
---|
| 266 | else kp = k; |
---|
| 267 | } |
---|
| 268 | public KeyPair getKeyPair() { return kp; } |
---|
[5cf72cc] | 269 | public boolean equals(Object o) { |
---|
| 270 | if ( o == null ) return false; |
---|
| 271 | else if ( ! (o instanceof Identity) ) return false; |
---|
| 272 | else return getKeyID().equals(((Identity)o).getKeyID()); |
---|
| 273 | } |
---|
| 274 | public int compareTo(Object o) { |
---|
| 275 | if ( ! (o instanceof Identity) ) return 1; |
---|
| 276 | else return getKeyID().compareTo(((Identity)o).getKeyID()); |
---|
| 277 | } |
---|
[9725efb] | 278 | public X509CertificateObject getCertificate() { return m_cert; } |
---|
[1a7e6d3] | 279 | |
---|
[9725efb] | 280 | }; |
---|