View Javadoc
1 package jsdsi.ldap; 2 3 import java.io.*; 4 import java.math.BigInteger; 5 import java.security.cert.*; 6 import java.util.*; 7 import java.net.*; 8 import jsdsi.*; 9 import jsdsi.util.DigestAlgoEnum; 10 import jsdsi.util.SignatureAlgoEnum; 11 12 /*** 13 * @see jsdsi.util.Loader 14 * 15 * @author Lu�s Pedro 16 * @version $Revision: 1.7 $ $Date: 2004/11/08 12:08:08 $ 17 * 18 **/ 19 20 public class LDAPLoader { 21 22 /*** 23 * id to key 24 */ 25 private Map keys = new HashMap(); 26 27 /*** 28 * key to id 29 */ 30 private HashMap key2id = new HashMap(); 31 32 /*** 33 * names 34 */ 35 private Set nameSet = new HashSet(); 36 37 /*** 38 * Creates a new LDAPLoader from a given filename and LDAPParameters 39 * 40 * @param filename filename to read the certificates from 41 * @param params ldap parameters 42 * @throws IOException if an error occurs reading the file filename 43 */ 44 public LDAPLoader(String filename, 45 LDAPParameters params) throws IOException { 46 String certCn = "sdsi."; 47 LineNumberReader in = new LineNumberReader(new FileReader(filename)); 48 in.setLineNumber(1); 49 for (String line = in.readLine(); line != null; line = in.readLine()) { 50 StringTokenizer t = new StringTokenizer(line); 51 if (t.countTokens() == 0) { 52 continue; // skip empty lines 53 } 54 if (t.countTokens() < 4) { 55 throw new IOException("bad input on line " 56 + in.getLineNumber() + ": " + line); 57 } 58 jsdsi.Principal issuer = getPrincipal(t.nextToken()); 59 String name = t.nextToken(); 60 String arrow = t.nextToken(); 61 if (!arrow.equals("->")) { 62 throw new IOException("bad arrow on line " 63 + in.getLineNumber() + ": " + line); 64 } 65 jsdsi.Principal sub = getPrincipal(t.nextToken()); 66 String[] names = new String[t.countTokens()]; 67 for (int i = 0; i < names.length; i++) { 68 names[i] = t.nextToken(); 69 } 70 Subject subject = (names.length > 0) 71 ? (Subject)new Name(sub, names) 72 : (Subject)sub; 73 74 // create a fake certificate with a fake signature and fake 75 // validity 76 Cert c; 77 Calendar date = Calendar.getInstance(); 78 date.set(1, 79 date.get(1) + 1); // adds an year to the current year 80 81 if (name.startsWith("!") || name.startsWith("+")) { 82 // this is an auth cert 83 Tag tag = new StringTag(name.substring(1)); 84 c = new AuthCert(issuer, 85 subject, 86 new Validity(null, 87 date.getTime()), 88 null, null, tag, name.startsWith("+")); 89 // propagate? 90 } else { 91 // this is a name cert 92 nameSet.add(new Name(issuer, name)); 93 c = new NameCert(issuer, 94 subject, 95 new Validity(null, date.getTime()), 96 null, 97 null, 98 name); 99 } 100 jsdsi.Signature s = new jsdsi.Signature( 101 issuer, 102 new Hash("md5", "HASH-VALUE".getBytes(), null), 103 "rsa-pkcs1-md5", 104 "SIGNATURE-VALUE".getBytes()); 105 try { 106 LDAPOperations util = new LDAPOperations(params); 107 util.storeCertificate(certCn + (in.getLineNumber() - 1), 108 new jsdsi.Certificate(c, s)); 109 } catch(CertificateException e) { 110 throw new Error(e); 111 } 112 } 113 } 114 115 /*** 116 * Writes to file a collection of spki based certificates 117 * 118 * @param filename filename to write the certificates 119 * @param certs collection of certificates 120 */ 121 public void loaderOut(String filename, Collection certs) { 122 try { 123 FileOutputStream fos = new FileOutputStream(filename); 124 PrintStream ps = new PrintStream(fos); 125 Iterator allCerts = certs.iterator(); 126 while (allCerts.hasNext()) { 127 Cert cert = ((jsdsi.Certificate)allCerts.next()).getCert(); 128 String issuer; 129 if (cert instanceof NameCert) { 130 Name fullName = ((NameCert)cert).getFullName(); 131 issuer = fullName.getIssuer().toString(); 132 String issuerName = fullName.getNames()[0]; 133 ps.print(getKey2Id(issuer) + " " + issuerName + " -> "); 134 } 135 if (cert instanceof AuthCert) { 136 String tag = 137 ((StringTag)((AuthCert)cert).getTag()).getValue(); 138 boolean delegate = ((AuthCert)cert).getPropagate(); 139 issuer = ((AuthCert)cert).getIssuer().toString(); 140 if (delegate) 141 ps.print(getKey2Id(issuer) + " +" + tag + " -> "); 142 else 143 ps.print(getKey2Id(issuer) + " !" + tag + " -> "); 144 } 145 if (cert.getSubject() instanceof Name) { 146 Name subject = (Name)cert.getSubject(); 147 String[] names = subject.getNames(); 148 String sub = subject.getIssuer().toString(); 149 ps.print(getKey2Id(sub.toString()) + " "); 150 for(int i = 0; i < names.length; i++) 151 ps.print(names[i] + " "); 152 ps.print("\n"); 153 } else { 154 PublicKey subject = (PublicKey)cert.getSubject(); 155 ps.println(getKey2Id(subject.toString())); 156 } 157 158 } 159 ps.flush(); 160 fos.flush(); 161 ps.close(); 162 fos.close(); 163 } catch (IOException e) { 164 throw new Error(e); 165 } 166 } 167 168 /*** 169 * @see jsdsi.util.Loader#getKeys() 170 * 171 */ 172 public Collection getKeys() { 173 return keys.values(); 174 } 175 176 /*** 177 * @see jsdsi.util.Loader#getNames() 178 * 179 */ 180 public Collection getNames() { 181 return nameSet; 182 } 183 184 /*** 185 * RSAPublicKey id 186 * 187 * @param RSAPublicKey string representation 188 * @return id of the RSAPublicKey 189 */ 190 private String getKey2Id(String RSAPublicKey) { 191 return (String)key2id.get(RSAPublicKey); 192 } 193 194 private jsdsi.Principal getPrincipal(String id) { 195 RSAPublicKey k = (RSAPublicKey)keys.get(id); 196 if (k == null) { 197 k = new RSAPublicKey(new BigInteger(id.getBytes()), 198 new BigInteger(new byte[] { 0x03 }), 199 "MD5/RSA/PKCS#1", (URI[])null); 200 keys.put(id, k); 201 key2id.put(k.toString(), id); 202 } 203 return k; 204 } 205 }

This page was automatically generated by Maven