1 19 20 package com.sslexplorer.security.pki; 21 22 import java.io.BufferedReader ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStreamReader ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 import com.maverick.crypto.encoders.Base64; 33 import com.maverick.util.ByteArrayWriter; 34 35 36 public abstract class Base64EncodedFileFormat implements SshKeyFormatConversion { 37 38 protected String begin; 39 40 41 protected String end; 42 private Map headers = new HashMap (); 43 private int MAX_LINE_LENGTH = 70; 44 45 51 protected Base64EncodedFileFormat(String begin, String end) { 52 this.begin = begin; 53 this.end = end; 54 } 55 56 61 public String getFormatType() { 62 return "Base64Encoded"; 63 } 64 65 72 public boolean isFormatted(byte[] formattedKey) { 73 String test = new String (formattedKey); 74 75 if ((test.indexOf(begin) >= 0) && (test.indexOf(end) > 0)) { 76 return true; 77 } else { 78 return false; 79 } 80 } 81 82 88 public void setHeaderValue(String headerTag, String headerValue) { 89 headers.put(headerTag, headerValue); 90 } 91 92 99 public String getHeaderValue(String headerTag) { 100 return (String ) headers.get(headerTag); 101 } 102 103 112 public byte[] getKeyBlob(byte[] formattedKey) throws InvalidKeyException { 113 BufferedReader reader = new BufferedReader (new InputStreamReader ( 114 new ByteArrayInputStream (formattedKey))); 115 String line; 116 String headerTag; 117 String headerValue; 118 String blob = ""; 119 int index; 120 121 try { 122 while (true) { 124 line = reader.readLine(); 125 126 if (line == null) { 127 throw new InvalidKeyException("Incorrect file format!"); 128 } 129 130 if (line.endsWith(begin)) { 131 break; 132 } 133 } 134 135 while (true) { 137 line = reader.readLine(); 138 139 if (line == null) { 140 throw new InvalidKeyException("Incorrect file format!"); 141 } 142 143 index = line.indexOf(": "); 144 145 if (index > 0) { 146 while (line.endsWith("\\")) { 147 line = line.substring(0, line.length() - 1); 148 149 String tmp = reader.readLine(); 150 151 if (tmp == null) { 152 throw new InvalidKeyException( 153 "Incorrect file format!"); 154 } 155 156 line += tmp; 157 } 158 159 headerTag = line.substring(0, index); 161 headerValue = line.substring(index + 2); 162 headers.put(headerTag, headerValue); 163 } else { 164 break; 165 } 166 } 167 168 ByteArrayWriter baw = new ByteArrayWriter(); 170 171 while (true) { 172 blob += line; 173 line = reader.readLine(); 174 175 if (line == null) { 176 throw new InvalidKeyException("Invalid file format!"); 177 } 178 179 if (line.endsWith(end)) { 180 break; 181 } 182 } 183 184 return Base64.decode(blob); 186 } catch (IOException ioe) { 187 throw new InvalidKeyException(); 188 } 189 } 190 191 198 public byte[] formatKey(byte[] keyblob) { 199 try { 200 ByteArrayOutputStream out = new ByteArrayOutputStream (); 201 String headerTag; 202 String headerValue; 203 String line; 204 out.write(begin.getBytes()); 205 out.write('\n'); 206 207 int pos; 208 Set tags = headers.keySet(); 209 Iterator it = tags.iterator(); 210 211 while (it.hasNext()) { 212 headerTag = (String ) it.next(); 213 headerValue = (String ) headers.get(headerTag); 214 215 String header = headerTag + ": " + headerValue; 216 pos = 0; 217 218 while (pos < header.length()) { 219 line = header.substring(pos, 220 (((pos + MAX_LINE_LENGTH) < header.length()) 221 ? (pos + MAX_LINE_LENGTH) : header.length())) + 222 (((pos + MAX_LINE_LENGTH) < header.length()) ? "\\" : ""); 223 out.write(line.getBytes()); 224 out.write('\n'); 225 pos += MAX_LINE_LENGTH; 226 } 227 } 228 229 String encoded = new String (Base64.encode(keyblob)); 230 out.write(encoded.getBytes()); 231 out.write('\n'); 232 out.write(end.getBytes()); 233 out.write('\n'); 234 235 return out.toByteArray(); 236 } catch (IOException ioe) { 237 return null; 238 } 239 } 240 } 241 | Popular Tags |