[31b67d5] | 1 | package net.deterlab.abac; |
---|
| 2 | |
---|
[7ef13e3] | 3 | import java.io.*; |
---|
| 4 | |
---|
| 5 | import java.util.*; |
---|
| 6 | import java.security.*; |
---|
| 7 | import java.security.cert.*; |
---|
| 8 | |
---|
[9725efb] | 9 | import net.deterlab.abac.Identity; |
---|
[90f939f] | 10 | |
---|
| 11 | import org.bouncycastle.asn1.*; |
---|
| 12 | import org.bouncycastle.x509.*; |
---|
| 13 | import org.bouncycastle.jce.provider.X509AttrCertParser; |
---|
[7ef13e3] | 14 | import org.bouncycastle.jce.provider.X509CertificateObject; |
---|
| 15 | import org.bouncycastle.openssl.PEMReader; |
---|
[90f939f] | 16 | |
---|
[31b67d5] | 17 | public class Credential { |
---|
[9725efb] | 18 | protected static Vector<Identity> s_ids = new Vector<Identity>(); |
---|
[31b67d5] | 19 | /** |
---|
| 20 | * Create a credential from a head and tail role. This is only for testing. |
---|
| 21 | * In a real implementation the Credential must be loaded from an X.509 |
---|
| 22 | * attribute cert. |
---|
| 23 | */ |
---|
| 24 | public Credential(Role head, Role tail) { |
---|
| 25 | m_head = head; |
---|
| 26 | m_tail = tail; |
---|
| 27 | } |
---|
| 28 | |
---|
[90f939f] | 29 | /** |
---|
[7ef13e3] | 30 | * Do the credential initialization from a filename. |
---|
[90f939f] | 31 | */ |
---|
[7ef13e3] | 32 | protected void init(String filename) throws Exception { |
---|
[90f939f] | 33 | FileInputStream fis = new FileInputStream(filename); |
---|
| 34 | X509AttrCertParser parser = new X509AttrCertParser(); |
---|
| 35 | parser.engineInit(fis); |
---|
| 36 | m_ac = (X509V2AttributeCertificate)parser.engineRead(); |
---|
[7ef13e3] | 37 | m_id = null; |
---|
| 38 | |
---|
[9725efb] | 39 | for (Identity id: s_ids) { |
---|
[7ef13e3] | 40 | try { |
---|
[9725efb] | 41 | m_ac.verify(id.getCertificate().getPublicKey(), "BC"); |
---|
[7ef13e3] | 42 | m_id = id; |
---|
| 43 | break; |
---|
| 44 | } |
---|
[9725efb] | 45 | catch (InvalidKeyException e) { } |
---|
[7ef13e3] | 46 | } |
---|
| 47 | if (m_id == null) throw new InvalidKeyException("Unknown identity"); |
---|
[90f939f] | 48 | |
---|
| 49 | load_roles(); |
---|
| 50 | } |
---|
| 51 | |
---|
[7ef13e3] | 52 | /** |
---|
| 53 | * Create a credential from an attribute cert. Throws an exception if the |
---|
| 54 | * cert file can't be opened or if there's a format problem with the cert. |
---|
| 55 | */ |
---|
| 56 | public Credential(String filename) throws Exception { |
---|
| 57 | init(filename); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Create a credential from an attribute cert. Throws an exception if the |
---|
| 62 | * cert file can't be opened or if there's a format problem with the cert. |
---|
| 63 | */ |
---|
| 64 | public Credential(File file) throws Exception { |
---|
| 65 | init(file.getPath()); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | |
---|
[90f939f] | 69 | /** |
---|
| 70 | * Load the roles off the attribute cert. Throws a RuntimeException if |
---|
| 71 | * there's something wrong with the cert. |
---|
| 72 | */ |
---|
| 73 | private void load_roles() throws RuntimeException { |
---|
| 74 | String roles = null; |
---|
| 75 | try { |
---|
| 76 | X509Attribute attr = m_ac.getAttributes()[0]; |
---|
| 77 | |
---|
| 78 | DERSequence java = (DERSequence)attr.getValues()[0]; |
---|
| 79 | DERSequence fucking = (DERSequence)java.getObjectAt(0); |
---|
| 80 | DERUTF8String sucks = (DERUTF8String)fucking.getObjectAt(0); |
---|
| 81 | |
---|
| 82 | roles = sucks.getString(); |
---|
| 83 | } |
---|
| 84 | catch (Exception e) { |
---|
| 85 | throw new RuntimeException("Your attribute certificate is funky and I'm not gonna debug it", e); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | String[] parts = roles.split("\\s*<--?\\s*"); |
---|
| 89 | if (parts.length != 2) |
---|
| 90 | throw new RuntimeException("Invalid attribute: " + roles); |
---|
| 91 | |
---|
| 92 | m_head = new Role(parts[0]); |
---|
| 93 | m_tail = new Role(parts[1]); |
---|
| 94 | } |
---|
| 95 | |
---|
[31b67d5] | 96 | /** |
---|
| 97 | * Get the head role from the credential. |
---|
| 98 | */ |
---|
| 99 | public Role head() { |
---|
| 100 | return m_head; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Get the tail role from the credential |
---|
| 105 | */ |
---|
| 106 | public Role tail() { |
---|
| 107 | return m_tail; |
---|
| 108 | } |
---|
| 109 | |
---|
[90f939f] | 110 | /** |
---|
| 111 | * Gets the cert associated with this credential (if any). |
---|
| 112 | */ |
---|
| 113 | public X509V2AttributeCertificate cert() { |
---|
| 114 | return m_ac; |
---|
| 115 | } |
---|
| 116 | |
---|
[31b67d5] | 117 | /** |
---|
| 118 | * Turn the credential into string form. The format is head <- tail. For |
---|
| 119 | * example: A.r1 <- B.r2.r3. |
---|
| 120 | */ |
---|
| 121 | public String toString() { |
---|
| 122 | return m_head + " <- " + m_tail; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | private Role m_head, m_tail; |
---|
[90f939f] | 126 | |
---|
| 127 | private X509V2AttributeCertificate m_ac; |
---|
[9725efb] | 128 | private Identity m_id; |
---|
[7ef13e3] | 129 | |
---|
[9725efb] | 130 | public static void addIdentity(Identity id) { s_ids.add(id); } |
---|
| 131 | public static Collection<Identity> identities() { return s_ids; } |
---|
[31b67d5] | 132 | } |
---|