summaryrefslogtreecommitdiffstats
path: root/aaf/src/src/main/java/com/att/cadi/cm/Factory.java
blob: 20b77d3af4e31e2e494f5c617c13d220d3c60219 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package com.att.cadi.cm;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Collection;
import java.util.List;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

import com.att.cadi.Symm;
import com.att.inno.env.Env;
import com.att.inno.env.TimeTaken;
import com.att.inno.env.Trans;

public class Factory {
	public static final String KEY_ALGO = "RSA";
	private static final String PRIVATE_KEY_HEADER = KEY_ALGO + " PRIVATE KEY";
	public static final String SIG_ALGO = "SHA256withRSA";

	public  static final int KEY_LENGTH = 2048;
	private static final KeyPairGenerator keygen;
	private static final KeyFactory keyFactory;
	private static final CertificateFactory certificateFactory;
	private static final SecureRandom random;
	
	
	private static final Symm base64 = Symm.base64.copy(64);

	static {
			random = new SecureRandom();
			KeyPairGenerator tempKeygen;
			try {
				tempKeygen = KeyPairGenerator.getInstance(KEY_ALGO);//,"BC");
				tempKeygen.initialize(KEY_LENGTH, random);
			} catch (NoSuchAlgorithmException e) {
				tempKeygen = null;
				e.printStackTrace(System.err);
			}
			keygen = tempKeygen;

			KeyFactory tempKeyFactory;
			try {
				tempKeyFactory=KeyFactory.getInstance(KEY_ALGO);//,"BC"
			} catch (NoSuchAlgorithmException e) {
				tempKeyFactory = null;
				e.printStackTrace(System.err);
			};
			keyFactory = tempKeyFactory;
			 
			CertificateFactory tempCertificateFactory;
			try {
				tempCertificateFactory = CertificateFactory.getInstance("X.509");
			} catch (CertificateException e) {
				tempCertificateFactory = null;
				e.printStackTrace(System.err);
			}
			certificateFactory = tempCertificateFactory;

		 
	}


	public static KeyPair generateKeyPair(Trans trans) {
		TimeTaken tt;
		if(trans!=null) {
			tt = trans.start("Generate KeyPair", Env.SUB);
		} else {
			tt = null;
		}
		try {
			return keygen.generateKeyPair();
		} finally {
			if(tt!=null) {
				tt.done();
			}
		}
	}  

	private static final String LINE_END = "-----\n";

	protected static String textBuilder(String kind, byte[] bytes) throws IOException {
		StringBuilder sb = new StringBuilder();
		sb.append("-----BEGIN ");
		sb.append(kind);
		sb.append(LINE_END);

		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		base64.encode(bais, baos);
		sb.append(new String(baos.toByteArray()));
		
		if(sb.charAt(sb.length()-1)!='\n') {
			sb.append('\n');
		}
		sb.append("-----END ");
		sb.append(kind);
		sb.append(LINE_END);
		return sb.toString();
	}
	
	public static PrivateKey toPrivateKey(Trans trans, String pk) throws IOException, CertException {
		byte[] bytes = decode(new StringReader(pk));
		return toPrivateKey(trans, bytes);
	}
	
	public static PrivateKey toPrivateKey(Trans trans, byte[] bytes) throws IOException, CertException {
		TimeTaken tt=trans.start("Reconstitute Private Key", Env.SUB);
		try {
			return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(bytes));
		} catch (InvalidKeySpecException e) {
			throw new CertException("Translating Private Key from PKCS8 KeySpec",e);
		} finally {
			tt.done();
		}
	}
	
	public static PrivateKey toPrivateKey(Trans trans, File file) throws IOException, CertException {
		TimeTaken tt = trans.start("Decode Private Key File", Env.SUB);
		try {
			return toPrivateKey(trans,decode(file));
		}finally {
			tt.done();
		}
	}


	public static String toString(Trans trans, PrivateKey pk) throws IOException {
		trans.debug().log("Private Key to String");
		return textBuilder(PRIVATE_KEY_HEADER,pk.getEncoded());
	}

	public static PublicKey toPublicKey(Trans trans, String pk) throws IOException {
		TimeTaken tt = trans.start("Reconstitute Public Key", Env.SUB);
		try {
			ByteArrayInputStream bais = new ByteArrayInputStream(pk.getBytes());
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			Symm.base64noSplit.decode(bais, baos);

			return keyFactory.generatePublic(new X509EncodedKeySpec(baos.toByteArray()));
		} catch (InvalidKeySpecException e) {
			trans.error().log(e,"Translating Public Key from X509 KeySpec");
			return null;
		} finally {
			tt.done();
		}
	}
	
	public static String toString(Trans trans, PublicKey pk) throws IOException {
		trans.debug().log("Public Key to String");
		return textBuilder("PUBLIC KEY",pk.getEncoded());
	}

	public static Collection<? extends Certificate> toX509Certificate(Trans trans, String x509) throws CertificateException {
		return toX509Certificate(trans, x509.getBytes());
	}
	
	public static Collection<? extends Certificate> toX509Certificate(Trans trans, List<String> x509s) throws CertificateException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			for(String x509 : x509s) {
				baos.write(x509.getBytes());
			}
		} catch (IOException e) {
			throw new CertificateException(e);
		}
		return toX509Certificate(trans, new ByteArrayInputStream(baos.toByteArray()));
	}

	public static Collection<? extends Certificate> toX509Certificate(Trans trans, byte[] x509) throws CertificateException {
		return certificateFactory.generateCertificates(new ByteArrayInputStream(x509));
	}

	public static Collection<? extends Certificate> toX509Certificate(Trans trans, File file) throws CertificateException, FileNotFoundException {
		FileInputStream fis = new FileInputStream(file);
		try {
			return toX509Certificate(trans,fis);
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
				throw new CertificateException(e);
			}
		}
	}

	public static Collection<? extends Certificate> toX509Certificate(Trans trans, InputStream is) throws CertificateException {
		TimeTaken tt=trans.start("Reconstitute Certificates", Env.SUB);
		try {
			return certificateFactory.generateCertificates(is);
		} finally {
			tt.done();
		}
	}

	

	public static String toString(Trans trans, Certificate cert) throws IOException, CertException {
		if(trans.debug().isLoggable()) {
			StringBuilder sb = new StringBuilder("Certificate to String");
			if(cert instanceof X509Certificate) {
				sb.append(" - ");
				sb.append(((X509Certificate)cert).getSubjectDN());
			}
			trans.debug().log(sb);
		}
		try {
			if(cert==null) {
				throw new CertException("Certificate not built");
			}
			return textBuilder("CERTIFICATE",cert.getEncoded());
		} catch (CertificateEncodingException e) {
			throw new CertException(e);
		}
	}

	public static Cipher pkCipher() throws NoSuchAlgorithmException, NoSuchPaddingException {
		return Cipher.getInstance(KEY_ALGO); 
	}

	public static Cipher pkCipher(Key key, boolean encrypt) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
		Cipher cipher = Cipher.getInstance(KEY_ALGO);
		cipher.init(encrypt?Cipher.ENCRYPT_MODE:Cipher.DECRYPT_MODE,key);
		return cipher;
	}

	public static byte[] strip(Reader rdr) throws IOException {
		BufferedReader br = new BufferedReader(rdr);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		String line;
		while((line=br.readLine())!=null) {
			if(line.length()>0 &&
			   !line.startsWith("-----") &&
			   line.indexOf(':')<0) {  // Header elements
				baos.write(line.getBytes());
			}
		}
		return baos.toByteArray();
	}
	
	public static class StripperInputStream extends InputStream {
		private Reader created;
		private BufferedReader br;
		private int idx;
		private String line;

		public StripperInputStream(Reader rdr) {
			if(rdr instanceof BufferedReader) {
				br = (BufferedReader)rdr;
			} else {
				br = new BufferedReader(rdr);
			}
			created = null;
		}
		
		public StripperInputStream(File file) throws FileNotFoundException {
			this(new FileReader(file));
			created = br;
		}

		public StripperInputStream(InputStream is) throws FileNotFoundException {
			this(new InputStreamReader(is));
			created = br;
		}

		@Override
		public int read() throws IOException {
			if(line==null || idx>=line.length()) {
				while((line=br.readLine())!=null) {
					if(line.length()>0 &&
					   !line.startsWith("-----") &&
					   line.indexOf(':')<0) {  // Header elements
						break;
					}
				}

				if(line==null) {
					return -1;
				}
				idx = 0;
			}
			return line.charAt(idx++);
		}

		/* (non-Javadoc)
		 * @see java.io.InputStream#close()
		 */
		@Override
		public void close() throws IOException {
			if(created!=null) {
				created.close();
			}
		}
	}

	public static class Base64InputStream extends InputStream {
		private InputStream created;
		private InputStream is;
		private byte trio[];
		private byte duo[];
		private int idx;

		
		public Base64InputStream(File file) throws FileNotFoundException {
			this(new FileInputStream(file));
			created = is;
		}

		public Base64InputStream(InputStream is) throws FileNotFoundException {
			this.is = is;
			trio = new byte[3];
			idx = 4;
		}

		@Override
		public int read() throws IOException {
			if(duo==null || idx>=duo.length) {
				int read = is.read(trio);
				if(read==-1) {
					return -1;
				}
				duo = Symm.base64.decode(trio);
				if(duo==null || duo.length==0) {
					return -1;
				}
				idx=0;
			}
			
			return duo[idx++];
		}

		/* (non-Javadoc)
		 * @see java.io.InputStream#close()
		 */
		@Override
		public void close() throws IOException {
			if(created!=null) {
				created.close();
			}
		}
	}

	public static byte[] decode(byte[] bytes) throws IOException {
		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		Symm.base64.decode(bais, baos);
		return baos.toByteArray();
	}
	
	public static byte[] decode(File f) throws IOException {
		FileReader fr = new FileReader(f);
		try {
			return Factory.decode(fr);
		} finally {
			fr.close();
		}

	}
	public static byte[] decode(Reader rdr) throws IOException {
		return decode(strip(rdr));
	}


	public static byte[] binary(File file) throws IOException {
		DataInputStream dis = new DataInputStream(new FileInputStream(file));
		try {
			byte[] bytes = new byte[(int)file.length()];
			dis.readFully(bytes);
			return bytes;
		} finally {
			dis.close();
		}
	}


	public static byte[] sign(Trans trans, byte[] bytes, PrivateKey pk) throws IOException, InvalidKeyException, SignatureException, NoSuchAlgorithmException {
		TimeTaken tt = trans.start("Sign Data", Env.SUB);
		try {
			Signature sig = Signature.getInstance(SIG_ALGO);
			sig.initSign(pk, random);
			sig.update(bytes);
			return sig.sign();
		} finally {
			tt.done();
		}
	}

	// TODO IMPLEMENT!
	public static void getSignature(byte[] signed) {
		// TODO Auto-generated method stub
		
	}

}