KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > pki > Base64EncodedFileFormat


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security.pki;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
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 JavaDoc begin;
39
40     /** */
41     protected String JavaDoc end;
42     private Map JavaDoc headers = new HashMap JavaDoc();
43     private int MAX_LINE_LENGTH = 70;
44
45     /**
46      * Creates a new Base64EncodedFileFormat object.
47      *
48      * @param begin
49      * @param end
50      */

51     protected Base64EncodedFileFormat(String JavaDoc begin, String JavaDoc end) {
52         this.begin = begin;
53         this.end = end;
54     }
55
56     /**
57      *
58      *
59      * @return
60      */

61     public String JavaDoc getFormatType() {
62         return "Base64Encoded";
63     }
64
65     /**
66      *
67      *
68      * @param formattedKey
69      *
70      * @return
71      */

72     public boolean isFormatted(byte[] formattedKey) {
73         String JavaDoc test = new String JavaDoc(formattedKey);
74
75         if ((test.indexOf(begin) >= 0) && (test.indexOf(end) > 0)) {
76             return true;
77         } else {
78             return false;
79         }
80     }
81
82     /**
83      *
84      *
85      * @param headerTag
86      * @param headerValue
87      */

88     public void setHeaderValue(String JavaDoc headerTag, String JavaDoc headerValue) {
89         headers.put(headerTag, headerValue);
90     }
91
92     /**
93      *
94      *
95      * @param headerTag
96      *
97      * @return
98      */

99     public String JavaDoc getHeaderValue(String JavaDoc headerTag) {
100         return (String JavaDoc) headers.get(headerTag);
101     }
102
103     /**
104      *
105      *
106      * @param formattedKey
107      *
108      * @return byte[]
109      *
110      * @throws InvalidKeyException
111      */

112     public byte[] getKeyBlob(byte[] formattedKey) throws InvalidKeyException {
113         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
114                     new ByteArrayInputStream JavaDoc(formattedKey)));
115         String JavaDoc line;
116         String JavaDoc headerTag;
117         String JavaDoc headerValue;
118         String JavaDoc blob = "";
119         int index;
120
121         try {
122             // Read in the lines looking for the start
123
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             // Read the headers
136
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 JavaDoc tmp = reader.readLine();
150
151                         if (tmp == null) {
152                             throw new InvalidKeyException(
153                                 "Incorrect file format!");
154                         }
155
156                         line += tmp;
157                     }
158
159                     // Record the header
160
headerTag = line.substring(0, index);
161                     headerValue = line.substring(index + 2);
162                     headers.put(headerTag, headerValue);
163                 } else {
164                     break;
165                 }
166             }
167
168             // This is now the public key blob Base64 encoded
169
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             // Convert the blob to some useful data
185
return Base64.decode(blob);
186         } catch (IOException JavaDoc ioe) {
187             throw new InvalidKeyException();
188         }
189     }
190
191     /**
192      *
193      *
194      * @param keyblob
195      *
196      * @return
197      */

198     public byte[] formatKey(byte[] keyblob) {
199         try {
200             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
201             String JavaDoc headerTag;
202             String JavaDoc headerValue;
203             String JavaDoc line;
204             out.write(begin.getBytes());
205             out.write('\n');
206
207             int pos;
208             Set JavaDoc tags = headers.keySet();
209             Iterator JavaDoc it = tags.iterator();
210
211             while (it.hasNext()) {
212                 headerTag = (String JavaDoc) it.next();
213                 headerValue = (String JavaDoc) headers.get(headerTag);
214
215                 String JavaDoc 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 JavaDoc encoded = new String JavaDoc(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 JavaDoc ioe) {
237             return null;
238         }
239     }
240 }
241
Popular Tags