1 | /** |
---|
2 | * Fedid |
---|
3 | * |
---|
4 | * Wrapper around a federated is |
---|
5 | */ |
---|
6 | |
---|
7 | package net.deterlab.isi; |
---|
8 | |
---|
9 | import java.security.MessageDigest; |
---|
10 | import java.security.PublicKey; |
---|
11 | import java.security.NoSuchAlgorithmException; |
---|
12 | import java.security.cert.X509Certificate; |
---|
13 | |
---|
14 | public class Fedid { |
---|
15 | |
---|
16 | /** The bytes in the fedid */ |
---|
17 | protected byte[] buf; |
---|
18 | |
---|
19 | |
---|
20 | /** |
---|
21 | * Empty constructor |
---|
22 | */ |
---|
23 | public Fedid() { buf = null; } |
---|
24 | |
---|
25 | /** |
---|
26 | * Copy Constructor |
---|
27 | */ |
---|
28 | public Fedid(Fedid f) { assign( f != null ? f.getBytes() : null); } |
---|
29 | /** |
---|
30 | * Create from a byte array |
---|
31 | */ |
---|
32 | public Fedid(byte[] b) { assign(b); } |
---|
33 | |
---|
34 | /** |
---|
35 | * Create from X.509 certificate. |
---|
36 | */ |
---|
37 | public Fedid(X509Certificate c) throws NoSuchAlgorithmException { |
---|
38 | assign(c); |
---|
39 | } |
---|
40 | |
---|
41 | public void assign(byte[] b) { |
---|
42 | if ( b == null ) |
---|
43 | buf = null; |
---|
44 | else{ |
---|
45 | buf = new byte[b.length]; |
---|
46 | System.arraycopy(b, 0, buf, 0, b.length); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | public void assign(X509Certificate c) throws NoSuchAlgorithmException { |
---|
51 | if ( c != null ) { |
---|
52 | MessageDigest md = MessageDigest.getInstance("SHA1"); |
---|
53 | PublicKey pk = c.getPublicKey(); |
---|
54 | |
---|
55 | if (pk.getFormat() == "X.509" && pk.getAlgorithm() == "RSA") { |
---|
56 | // This 22 is a hack, but I don't want to parse the ASN.1 |
---|
57 | byte[] asn1 = pk.getEncoded(); |
---|
58 | byte[] bits = new byte[asn1.length -22]; |
---|
59 | |
---|
60 | System.arraycopy(asn1, 22, bits, 0, bits.length); |
---|
61 | |
---|
62 | buf = md.digest(bits); |
---|
63 | } |
---|
64 | else { |
---|
65 | throw new IllegalArgumentException("Unknown Key type"); |
---|
66 | } |
---|
67 | } |
---|
68 | else buf = null; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Printable version of the Fedid |
---|
73 | * |
---|
74 | * throws Null exception if the Fedid is uninitialized |
---|
75 | */ |
---|
76 | public String toString() { |
---|
77 | String rv ="fedid:"; |
---|
78 | |
---|
79 | for ( byte b : buf) { |
---|
80 | rv += String.format("%02x", b); |
---|
81 | } |
---|
82 | return rv; |
---|
83 | } |
---|
84 | |
---|
85 | public byte[] getBytes() { return buf; } |
---|
86 | |
---|
87 | public boolean equals(Fedid f) { |
---|
88 | byte[] b = f.getBytes(); |
---|
89 | |
---|
90 | if (buf.length != b.length) return false; |
---|
91 | else { |
---|
92 | for ( int i =0; i < b.length; i++) |
---|
93 | if ( buf[i] != b[i]) return false; |
---|
94 | return true; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|