1 16 17 package de.schlichtherle.key.passwd.swing; 18 19 import de.schlichtherle.key.*; 20 21 import java.util.*; 22 import java.util.logging.*; 23 24 36 public class KeyMgmtLifeCycle implements Runnable { 37 38 private static final Logger logger 39 = Logger.getLogger(KeyMgmtLifeCycle.class.getName()); 40 41 42 public final String id; 43 44 45 private Object refKey; 46 47 51 public Throwable throwable; 52 53 56 public KeyMgmtLifeCycle(final String id) { 57 this.id = id; 58 } 59 60 public void run() { 61 try { 62 runIt(); 63 } catch (Throwable t) { 64 throwable = t; 65 } 66 } 67 68 private void runIt() throws UnknownKeyException { 69 createResource(); 71 72 KeyManager.resetKeyProvider(id); 74 75 openResource(); 77 78 createResource(); 82 83 openResource(); 85 } 86 87 private void createResource() throws UnknownKeyException { 88 final KeyManager manager = KeyManager.getInstance(); 89 final KeyProvider provider = getKeyProvider(manager, id); 90 91 refKey = provider.getCreateKey(); 94 String msg = id + ": getCreateKey() returned " + toString(refKey) + "."; 95 logger.fine(msg); 96 97 createResourceHook(provider); 98 } 99 100 protected KeyProvider getKeyProvider(KeyManager manager, String id) { 101 return manager.getKeyProvider(id); 102 } 103 104 private void openResource() throws UnknownKeyException { 105 final KeyManager manager = KeyManager.getInstance(); 106 final KeyProvider provider = getKeyProvider(manager, id); 107 108 boolean correct = false; 109 while (!correct) { 110 correct = authenticateKey(provider); 111 } 112 113 openResourceHook(provider); 114 } 115 116 private boolean authenticateKey(final KeyProvider provider) throws UnknownKeyException { 117 final Object providedKey = provider.getOpenKey(); 118 final boolean correct = equals(refKey, providedKey); 119 String msg = id + ": getOpenKey() returned " + toString(providedKey) + ". "; 120 nullify(providedKey); 123 if (correct) { 124 msg += "That's correct."; 125 logger.fine(msg); 126 } else { 127 provider.invalidOpenKey(); 128 msg += "That's wrong!"; 129 logger.fine(msg); 130 try { 131 Thread.sleep(3000); 132 } catch (InterruptedException ex) { 133 ex.printStackTrace(); 134 } 135 } 136 137 return correct; 138 } 139 140 private static boolean equals(final Object o1, final Object o2) { 141 if (o1.getClass() != o2.getClass()) 142 return false; 143 144 if (o1 instanceof char[]) 145 return Arrays.equals((char[]) o1, (char[]) o2); 146 else if (o1 instanceof byte[]) 147 return Arrays.equals((byte[]) o1, (byte[]) o2); 148 else 149 return o1.equals(o2); 150 } 151 152 private static String toString(final Object o) { 153 if (o instanceof char[]) 154 return "\"" + new String ((char[]) o) + "\".toCharArray()"; 155 else if (o instanceof byte[]) 156 return "\"" + new String ((byte[]) o) + "\".getBytes(\"UTF16BE\")"; 157 else 158 return "\"" + o.toString() + "\".toString()"; 159 } 160 161 private static void nullify(final Object o) { 162 if (o instanceof char[]) 163 Arrays.fill((char[]) o, (char) 0); 164 else if (o instanceof byte[]) 165 Arrays.fill((byte[]) o, (byte) 0); 166 } 167 168 protected void createResourceHook(KeyProvider provider) { 169 } 170 171 protected void openResourceHook(KeyProvider provider) { 172 } 173 } | Popular Tags |