1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * Copyright © 2017 Amdocs
\r
7 * * ===========================================================================
\r
8 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * * you may not use this file except in compliance with the License.
\r
10 * * You may obtain a copy of the License at
\r
12 * * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * * Unless required by applicable law or agreed to in writing, software
\r
15 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * * See the License for the specific language governing permissions and
\r
18 * * limitations under the License.
\r
19 * * ============LICENSE_END====================================================
\r
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.cadi.cm;
\r
26 import java.io.BufferedReader;
\r
27 import java.io.ByteArrayInputStream;
\r
28 import java.io.ByteArrayOutputStream;
\r
29 import java.io.DataInputStream;
\r
30 import java.io.File;
\r
31 import java.io.FileInputStream;
\r
32 import java.io.FileNotFoundException;
\r
33 import java.io.FileReader;
\r
34 import java.io.IOException;
\r
35 import java.io.InputStream;
\r
36 import java.io.InputStreamReader;
\r
37 import java.io.Reader;
\r
38 import java.io.StringReader;
\r
39 import java.security.InvalidKeyException;
\r
40 import java.security.Key;
\r
41 import java.security.KeyFactory;
\r
42 import java.security.KeyPair;
\r
43 import java.security.KeyPairGenerator;
\r
44 import java.security.NoSuchAlgorithmException;
\r
45 import java.security.PrivateKey;
\r
46 import java.security.PublicKey;
\r
47 import java.security.SecureRandom;
\r
48 import java.security.Signature;
\r
49 import java.security.SignatureException;
\r
50 import java.security.cert.Certificate;
\r
51 import java.security.cert.CertificateEncodingException;
\r
52 import java.security.cert.CertificateException;
\r
53 import java.security.cert.CertificateFactory;
\r
54 import java.security.cert.X509Certificate;
\r
55 import java.security.spec.InvalidKeySpecException;
\r
56 import java.security.spec.PKCS8EncodedKeySpec;
\r
57 import java.security.spec.X509EncodedKeySpec;
\r
58 import java.util.Collection;
\r
59 import java.util.List;
\r
61 import javax.crypto.Cipher;
\r
62 import javax.crypto.NoSuchPaddingException;
\r
64 import com.att.cadi.Symm;
\r
65 import com.att.inno.env.Env;
\r
66 import com.att.inno.env.TimeTaken;
\r
67 import com.att.inno.env.Trans;
\r
69 public class Factory {
\r
70 public static final String KEY_ALGO = "RSA";
\r
71 private static final String PRIVATE_KEY_HEADER = KEY_ALGO + " PRIVATE KEY";
\r
72 public static final String SIG_ALGO = "SHA256withRSA";
\r
74 public static final int KEY_LENGTH = 2048;
\r
75 private static final KeyPairGenerator keygen;
\r
76 private static final KeyFactory keyFactory;
\r
77 private static final CertificateFactory certificateFactory;
\r
78 private static final SecureRandom random;
\r
81 private static final Symm base64 = Symm.base64.copy(64);
\r
84 random = new SecureRandom();
\r
85 KeyPairGenerator tempKeygen;
\r
87 tempKeygen = KeyPairGenerator.getInstance(KEY_ALGO);//,"BC");
\r
88 tempKeygen.initialize(KEY_LENGTH, random);
\r
89 } catch (NoSuchAlgorithmException e) {
\r
91 e.printStackTrace(System.err);
\r
93 keygen = tempKeygen;
\r
95 KeyFactory tempKeyFactory;
\r
97 tempKeyFactory=KeyFactory.getInstance(KEY_ALGO);//,"BC"
\r
98 } catch (NoSuchAlgorithmException e) {
\r
99 tempKeyFactory = null;
\r
100 e.printStackTrace(System.err);
\r
102 keyFactory = tempKeyFactory;
\r
104 CertificateFactory tempCertificateFactory;
\r
106 tempCertificateFactory = CertificateFactory.getInstance("X.509");
\r
107 } catch (CertificateException e) {
\r
108 tempCertificateFactory = null;
\r
109 e.printStackTrace(System.err);
\r
111 certificateFactory = tempCertificateFactory;
\r
117 public static KeyPair generateKeyPair(Trans trans) {
\r
120 tt = trans.start("Generate KeyPair", Env.SUB);
\r
125 return keygen.generateKeyPair();
\r
133 private static final String LINE_END = "-----\n";
\r
135 protected static String textBuilder(String kind, byte[] bytes) throws IOException {
\r
136 StringBuilder sb = new StringBuilder();
\r
137 sb.append("-----BEGIN ");
\r
139 sb.append(LINE_END);
\r
141 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
\r
142 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
143 base64.encode(bais, baos);
\r
144 sb.append(new String(baos.toByteArray()));
\r
146 if(sb.charAt(sb.length()-1)!='\n') {
\r
149 sb.append("-----END ");
\r
151 sb.append(LINE_END);
\r
152 return sb.toString();
\r
155 public static PrivateKey toPrivateKey(Trans trans, String pk) throws IOException, CertException {
\r
156 byte[] bytes = decode(new StringReader(pk));
\r
157 return toPrivateKey(trans, bytes);
\r
160 public static PrivateKey toPrivateKey(Trans trans, byte[] bytes) throws IOException, CertException {
\r
161 TimeTaken tt=trans.start("Reconstitute Private Key", Env.SUB);
\r
163 return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(bytes));
\r
164 } catch (InvalidKeySpecException e) {
\r
165 throw new CertException("Translating Private Key from PKCS8 KeySpec",e);
\r
171 public static PrivateKey toPrivateKey(Trans trans, File file) throws IOException, CertException {
\r
172 TimeTaken tt = trans.start("Decode Private Key File", Env.SUB);
\r
174 return toPrivateKey(trans,decode(file));
\r
181 public static String toString(Trans trans, PrivateKey pk) throws IOException {
\r
182 trans.debug().log("Private Key to String");
\r
183 return textBuilder(PRIVATE_KEY_HEADER,pk.getEncoded());
\r
186 public static PublicKey toPublicKey(Trans trans, String pk) throws IOException {
\r
187 TimeTaken tt = trans.start("Reconstitute Public Key", Env.SUB);
\r
189 ByteArrayInputStream bais = new ByteArrayInputStream(pk.getBytes());
\r
190 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
191 Symm.base64noSplit.decode(bais, baos);
\r
193 return keyFactory.generatePublic(new X509EncodedKeySpec(baos.toByteArray()));
\r
194 } catch (InvalidKeySpecException e) {
\r
195 trans.error().log(e,"Translating Public Key from X509 KeySpec");
\r
202 public static String toString(Trans trans, PublicKey pk) throws IOException {
\r
203 trans.debug().log("Public Key to String");
\r
204 return textBuilder("PUBLIC KEY",pk.getEncoded());
\r
207 public static Collection<? extends Certificate> toX509Certificate(Trans trans, String x509) throws CertificateException {
\r
208 return toX509Certificate(trans, x509.getBytes());
\r
211 public static Collection<? extends Certificate> toX509Certificate(Trans trans, List<String> x509s) throws CertificateException {
\r
212 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
214 for(String x509 : x509s) {
\r
215 baos.write(x509.getBytes());
\r
217 } catch (IOException e) {
\r
218 throw new CertificateException(e);
\r
220 return toX509Certificate(trans, new ByteArrayInputStream(baos.toByteArray()));
\r
223 public static Collection<? extends Certificate> toX509Certificate(Trans trans, byte[] x509) throws CertificateException {
\r
224 return certificateFactory.generateCertificates(new ByteArrayInputStream(x509));
\r
227 public static Collection<? extends Certificate> toX509Certificate(Trans trans, File file) throws CertificateException, FileNotFoundException {
\r
228 FileInputStream fis = new FileInputStream(file);
\r
230 return toX509Certificate(trans,fis);
\r
234 } catch (IOException e) {
\r
235 throw new CertificateException(e);
\r
240 public static Collection<? extends Certificate> toX509Certificate(Trans trans, InputStream is) throws CertificateException {
\r
241 TimeTaken tt=trans.start("Reconstitute Certificates", Env.SUB);
\r
243 return certificateFactory.generateCertificates(is);
\r
251 public static String toString(Trans trans, Certificate cert) throws IOException, CertException {
\r
252 if(trans.debug().isLoggable()) {
\r
253 StringBuilder sb = new StringBuilder("Certificate to String");
\r
254 if(cert instanceof X509Certificate) {
\r
256 sb.append(((X509Certificate)cert).getSubjectDN());
\r
258 trans.debug().log(sb);
\r
262 throw new CertException("Certificate not built");
\r
264 return textBuilder("CERTIFICATE",cert.getEncoded());
\r
265 } catch (CertificateEncodingException e) {
\r
266 throw new CertException(e);
\r
270 public static Cipher pkCipher() throws NoSuchAlgorithmException, NoSuchPaddingException {
\r
271 return Cipher.getInstance(KEY_ALGO);
\r
274 public static Cipher pkCipher(Key key, boolean encrypt) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
\r
275 Cipher cipher = Cipher.getInstance(KEY_ALGO);
\r
276 cipher.init(encrypt?Cipher.ENCRYPT_MODE:Cipher.DECRYPT_MODE,key);
\r
280 public static byte[] strip(Reader rdr) throws IOException {
\r
281 BufferedReader br = new BufferedReader(rdr);
\r
282 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
284 while((line=br.readLine())!=null) {
\r
285 if(line.length()>0 &&
\r
286 !line.startsWith("-----") &&
\r
287 line.indexOf(':')<0) { // Header elements
\r
288 baos.write(line.getBytes());
\r
291 return baos.toByteArray();
\r
294 public static class StripperInputStream extends InputStream {
\r
295 private Reader created;
\r
296 private BufferedReader br;
\r
298 private String line;
\r
300 public StripperInputStream(Reader rdr) {
\r
301 if(rdr instanceof BufferedReader) {
\r
302 br = (BufferedReader)rdr;
\r
304 br = new BufferedReader(rdr);
\r
309 public StripperInputStream(File file) throws FileNotFoundException {
\r
310 this(new FileReader(file));
\r
314 public StripperInputStream(InputStream is) throws FileNotFoundException {
\r
315 this(new InputStreamReader(is));
\r
320 public int read() throws IOException {
\r
321 if(line==null || idx>=line.length()) {
\r
322 while((line=br.readLine())!=null) {
\r
323 if(line.length()>0 &&
\r
324 !line.startsWith("-----") &&
\r
325 line.indexOf(':')<0) { // Header elements
\r
335 return line.charAt(idx++);
\r
339 * @see java.io.InputStream#close()
\r
342 public void close() throws IOException {
\r
343 if(created!=null) {
\r
349 public static class Base64InputStream extends InputStream {
\r
350 private InputStream created;
\r
351 private InputStream is;
\r
352 private byte trio[];
\r
353 private byte duo[];
\r
357 public Base64InputStream(File file) throws FileNotFoundException {
\r
358 this(new FileInputStream(file));
\r
362 public Base64InputStream(InputStream is) throws FileNotFoundException {
\r
364 trio = new byte[3];
\r
369 public int read() throws IOException {
\r
370 if(duo==null || idx>=duo.length) {
\r
371 int read = is.read(trio);
\r
375 duo = Symm.base64.decode(trio);
\r
376 if(duo==null || duo.length==0) {
\r
386 * @see java.io.InputStream#close()
\r
389 public void close() throws IOException {
\r
390 if(created!=null) {
\r
396 public static byte[] decode(byte[] bytes) throws IOException {
\r
397 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
\r
398 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
399 Symm.base64.decode(bais, baos);
\r
400 return baos.toByteArray();
\r
403 public static byte[] decode(File f) throws IOException {
\r
404 FileReader fr = new FileReader(f);
\r
406 return Factory.decode(fr);
\r
412 public static byte[] decode(Reader rdr) throws IOException {
\r
413 return decode(strip(rdr));
\r
417 public static byte[] binary(File file) throws IOException {
\r
418 DataInputStream dis = new DataInputStream(new FileInputStream(file));
\r
420 byte[] bytes = new byte[(int)file.length()];
\r
421 dis.readFully(bytes);
\r
429 public static byte[] sign(Trans trans, byte[] bytes, PrivateKey pk) throws IOException, InvalidKeyException, SignatureException, NoSuchAlgorithmException {
\r
430 TimeTaken tt = trans.start("Sign Data", Env.SUB);
\r
432 Signature sig = Signature.getInstance(SIG_ALGO);
\r
433 sig.initSign(pk, random);
\r
442 public static void getSignature(byte[] signed) {
\r
443 // TODO Auto-generated method stub
\r