1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.cadi.cm;
\r
25 import java.io.BufferedReader;
\r
26 import java.io.ByteArrayInputStream;
\r
27 import java.io.ByteArrayOutputStream;
\r
28 import java.io.DataInputStream;
\r
29 import java.io.File;
\r
30 import java.io.FileInputStream;
\r
31 import java.io.FileNotFoundException;
\r
32 import java.io.FileReader;
\r
33 import java.io.IOException;
\r
34 import java.io.InputStream;
\r
35 import java.io.InputStreamReader;
\r
36 import java.io.Reader;
\r
37 import java.io.StringReader;
\r
38 import java.security.InvalidKeyException;
\r
39 import java.security.Key;
\r
40 import java.security.KeyFactory;
\r
41 import java.security.KeyPair;
\r
42 import java.security.KeyPairGenerator;
\r
43 import java.security.NoSuchAlgorithmException;
\r
44 import java.security.PrivateKey;
\r
45 import java.security.PublicKey;
\r
46 import java.security.SecureRandom;
\r
47 import java.security.Signature;
\r
48 import java.security.SignatureException;
\r
49 import java.security.cert.Certificate;
\r
50 import java.security.cert.CertificateEncodingException;
\r
51 import java.security.cert.CertificateException;
\r
52 import java.security.cert.CertificateFactory;
\r
53 import java.security.cert.X509Certificate;
\r
54 import java.security.spec.InvalidKeySpecException;
\r
55 import java.security.spec.PKCS8EncodedKeySpec;
\r
56 import java.security.spec.X509EncodedKeySpec;
\r
57 import java.util.Collection;
\r
58 import java.util.List;
\r
60 import javax.crypto.Cipher;
\r
61 import javax.crypto.NoSuchPaddingException;
\r
63 import com.att.cadi.Symm;
\r
64 import com.att.inno.env.Env;
\r
65 import com.att.inno.env.TimeTaken;
\r
66 import com.att.inno.env.Trans;
\r
68 public class Factory {
\r
69 private static final String PRIVATE_KEY_HEADER = "PRIVATE KEY";
\r
70 public static final String KEY_ALGO = "RSA";
\r
71 public static final String SIG_ALGO = "SHA256withRSA";
\r
73 public static final int KEY_LENGTH = 2048;
\r
74 private static final KeyPairGenerator keygen;
\r
75 private static final KeyFactory keyFactory;
\r
76 private static final CertificateFactory certificateFactory;
\r
77 private static final SecureRandom random;
\r
80 private static final Symm base64 = Symm.base64.copy(64);
\r
83 random = new SecureRandom();
\r
84 KeyPairGenerator tempKeygen;
\r
86 tempKeygen = KeyPairGenerator.getInstance(KEY_ALGO);//,"BC");
\r
87 tempKeygen.initialize(KEY_LENGTH, random);
\r
88 } catch (NoSuchAlgorithmException e) {
\r
90 e.printStackTrace(System.err);
\r
92 keygen = tempKeygen;
\r
94 KeyFactory tempKeyFactory;
\r
96 tempKeyFactory=KeyFactory.getInstance(KEY_ALGO);//,"BC"
\r
97 } catch (NoSuchAlgorithmException e) {
\r
98 tempKeyFactory = null;
\r
99 e.printStackTrace(System.err);
\r
101 keyFactory = tempKeyFactory;
\r
103 CertificateFactory tempCertificateFactory;
\r
105 tempCertificateFactory = CertificateFactory.getInstance("X.509");
\r
106 } catch (CertificateException e) {
\r
107 tempCertificateFactory = null;
\r
108 e.printStackTrace(System.err);
\r
110 certificateFactory = tempCertificateFactory;
\r
116 public static KeyPair generateKeyPair(Trans trans) {
\r
119 tt = trans.start("Generate KeyPair", Env.SUB);
\r
124 return keygen.generateKeyPair();
\r
132 private static final String LINE_END = "-----\n";
\r
134 protected static String textBuilder(String kind, byte[] bytes) throws IOException {
\r
135 StringBuilder sb = new StringBuilder();
\r
136 sb.append("-----BEGIN ");
\r
138 sb.append(LINE_END);
\r
140 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
\r
141 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
142 base64.encode(bais, baos);
\r
143 sb.append(new String(baos.toByteArray()));
\r
145 if(sb.charAt(sb.length()-1)!='\n') {
\r
148 sb.append("-----END ");
\r
150 sb.append(LINE_END);
\r
151 return sb.toString();
\r
154 public static PrivateKey toPrivateKey(Trans trans, String pk) throws IOException, CertException {
\r
155 byte[] bytes = decode(new StringReader(pk));
\r
156 return toPrivateKey(trans, bytes);
\r
159 public static PrivateKey toPrivateKey(Trans trans, byte[] bytes) throws IOException, CertException {
\r
160 TimeTaken tt=trans.start("Reconstitute Private Key", Env.SUB);
\r
162 return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(bytes));
\r
163 } catch (InvalidKeySpecException e) {
\r
164 throw new CertException("Translating Private Key from PKCS8 KeySpec",e);
\r
170 public static PrivateKey toPrivateKey(Trans trans, File file) throws IOException, CertException {
\r
171 TimeTaken tt = trans.start("Decode Private Key File", Env.SUB);
\r
173 return toPrivateKey(trans,decode(file));
\r
179 public static String toString(Trans trans, PrivateKey pk) throws IOException {
\r
180 // PKCS8EncodedKeySpec pemContents = new PKCS8EncodedKeySpec(pk.getEncoded());
\r
181 trans.debug().log("Private Key to String");
\r
182 return textBuilder(PRIVATE_KEY_HEADER,pk.getEncoded());
\r
185 public static PublicKey toPublicKey(Trans trans, String pk) throws IOException {
\r
186 TimeTaken tt = trans.start("Reconstitute Public Key", Env.SUB);
\r
188 ByteArrayInputStream bais = new ByteArrayInputStream(pk.getBytes());
\r
189 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
190 Symm.base64noSplit.decode(bais, baos);
\r
192 return keyFactory.generatePublic(new X509EncodedKeySpec(baos.toByteArray()));
\r
193 } catch (InvalidKeySpecException e) {
\r
194 trans.error().log(e,"Translating Public Key from X509 KeySpec");
\r
201 public static String toString(Trans trans, PublicKey pk) throws IOException {
\r
202 trans.debug().log("Public Key to String");
\r
203 return textBuilder("PUBLIC KEY",pk.getEncoded());
\r
206 public static Collection<? extends Certificate> toX509Certificate(String x509) throws CertificateException {
\r
207 return toX509Certificate(x509.getBytes());
\r
210 public static Collection<? extends Certificate> toX509Certificate(List<String> x509s) throws CertificateException {
\r
211 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
213 for(String x509 : x509s) {
\r
214 baos.write(x509.getBytes());
\r
216 } catch (IOException e) {
\r
217 throw new CertificateException(e);
\r
219 return toX509Certificate(new ByteArrayInputStream(baos.toByteArray()));
\r
222 public static Collection<? extends Certificate> toX509Certificate(byte[] x509) throws CertificateException {
\r
223 return certificateFactory.generateCertificates(new ByteArrayInputStream(x509));
\r
226 public static Collection<? extends Certificate> toX509Certificate(Trans trans, File file) throws CertificateException, FileNotFoundException {
\r
227 FileInputStream fis = new FileInputStream(file);
\r
229 return toX509Certificate(fis);
\r
233 } catch (IOException e) {
\r
234 throw new CertificateException(e);
\r
239 public static Collection<? extends Certificate> toX509Certificate(InputStream is) throws CertificateException {
\r
240 return certificateFactory.generateCertificates(is);
\r
243 public static String toString(Trans trans, Certificate cert) throws IOException, CertException {
\r
244 if(trans.debug().isLoggable()) {
\r
245 StringBuilder sb = new StringBuilder("Certificate to String");
\r
246 if(cert instanceof X509Certificate) {
\r
248 sb.append(((X509Certificate)cert).getSubjectDN());
\r
250 trans.debug().log(sb);
\r
254 throw new CertException("Certificate not built");
\r
256 return textBuilder("CERTIFICATE",cert.getEncoded());
\r
257 } catch (CertificateEncodingException e) {
\r
258 throw new CertException(e);
\r
262 public static Cipher pkCipher() throws NoSuchAlgorithmException, NoSuchPaddingException {
\r
263 return Cipher.getInstance(KEY_ALGO);
\r
266 public static Cipher pkCipher(Key key, boolean encrypt) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
\r
267 Cipher cipher = Cipher.getInstance(KEY_ALGO);
\r
268 cipher.init(encrypt?Cipher.ENCRYPT_MODE:Cipher.DECRYPT_MODE,key);
\r
272 public static byte[] strip(Reader rdr) throws IOException {
\r
273 BufferedReader br = new BufferedReader(rdr);
\r
274 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
276 while((line=br.readLine())!=null) {
\r
277 if(line.length()>0 &&
\r
278 !line.startsWith("-----") &&
\r
279 line.indexOf(':')<0) { // Header elements
\r
280 baos.write(line.getBytes());
\r
283 return baos.toByteArray();
\r
286 public static class StripperInputStream extends InputStream {
\r
287 private Reader created;
\r
288 private BufferedReader br;
\r
290 private String line;
\r
292 public StripperInputStream(Reader rdr) {
\r
293 if(rdr instanceof BufferedReader) {
\r
294 br = (BufferedReader)rdr;
\r
296 br = new BufferedReader(rdr);
\r
301 public StripperInputStream(File file) throws FileNotFoundException {
\r
302 this(new FileReader(file));
\r
306 public StripperInputStream(InputStream is) throws FileNotFoundException {
\r
307 this(new InputStreamReader(is));
\r
312 public int read() throws IOException {
\r
313 if(line==null || idx>=line.length()) {
\r
314 while((line=br.readLine())!=null) {
\r
315 if(line.length()>0 &&
\r
316 !line.startsWith("-----") &&
\r
317 line.indexOf(':')<0) { // Header elements
\r
327 return line.charAt(idx++);
\r
331 * @see java.io.InputStream#close()
\r
334 public void close() throws IOException {
\r
335 if(created!=null) {
\r
341 public static class Base64InputStream extends InputStream {
\r
342 private InputStream created;
\r
343 private InputStream is;
\r
344 private byte trio[];
\r
345 private byte duo[];
\r
349 public Base64InputStream(File file) throws FileNotFoundException {
\r
350 this(new FileInputStream(file));
\r
354 public Base64InputStream(InputStream is) throws FileNotFoundException {
\r
356 trio = new byte[3];
\r
361 public int read() throws IOException {
\r
362 if(duo==null || idx>=duo.length) {
\r
363 int read = is.read(trio);
\r
367 duo = Symm.base64.decode(trio);
\r
368 if(duo==null || duo.length==0) {
\r
378 * @see java.io.InputStream#close()
\r
381 public void close() throws IOException {
\r
382 if(created!=null) {
\r
388 public static byte[] decode(byte[] bytes) throws IOException {
\r
389 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
\r
390 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
391 Symm.base64.decode(bais, baos);
\r
392 return baos.toByteArray();
\r
395 public static byte[] decode(File f) throws IOException {
\r
396 FileReader fr = new FileReader(f);
\r
398 return Factory.decode(fr);
\r
404 public static byte[] decode(Reader rdr) throws IOException {
\r
405 return decode(strip(rdr));
\r
409 public static byte[] binary(File file) throws IOException {
\r
410 DataInputStream dis = new DataInputStream(new FileInputStream(file));
\r
412 byte[] bytes = new byte[(int)file.length()];
\r
413 dis.readFully(bytes);
\r
421 public static byte[] sign(Trans trans, byte[] bytes, PrivateKey pk) throws IOException, InvalidKeyException, SignatureException, NoSuchAlgorithmException {
\r
422 TimeTaken tt = trans.start("Sign Data", Env.SUB);
\r
424 Signature sig = Signature.getInstance(SIG_ALGO);
\r
425 sig.initSign(pk, random);
\r
433 public static String toSignatureString(byte[] signed) throws IOException {
\r
434 return textBuilder("SIGNATURE", signed);
\r
437 public static boolean verify(Trans trans, byte[] bytes, byte[] signature, PublicKey pk) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
\r
438 TimeTaken tt = trans.start("Verify Data", Env.SUB);
\r
440 Signature sig = Signature.getInstance(SIG_ALGO);
\r
441 sig.initVerify(pk);
\r
443 return sig.verify(signature);
\r