2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.cadi;
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.DataInputStream;
27 import java.io.DataOutputStream;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.security.SecureRandom;
34 import java.util.ArrayList;
35 import java.util.Random;
37 import javax.crypto.CipherInputStream;
38 import javax.crypto.CipherOutputStream;
40 import org.onap.aaf.cadi.Access.Level;
41 import org.onap.aaf.cadi.config.Config;
44 * Key Conversion, primarily "Base64"
46 * Base64 is required for "Basic Authorization", which is an important part of the overall CADI Package.
48 * Note: This author found that there is not a "standard" library for Base64 conversion within Java.
49 * The source code implementations available elsewhere were surprisingly inefficient, requiring, for
50 * instance, multiple string creation, on a transaction pass. Integrating other packages that might be
51 * efficient enough would put undue Jar File Dependencies given this Framework should have none-but-Java
54 * The essential algorithm is good for a symmetrical key system, as Base64 is really just
55 * a symmetrical key that everyone knows the values.
57 * This code is quite fast, taking about .016 ms for encrypting, decrypting and even .08 for key
58 * generation. The speed quality, especially of key generation makes this a candidate for a short term token
61 * It may be used to easily avoid placing Clear-Text passwords in configurations, etc. and contains
62 * supporting functions such as 2048 keyfile generation (see keygen). This keyfile should, of course,
63 * be set to "400" (Unix) and protected as any other mechanism requires.
65 * However, this algorithm has not been tested against hackers. Until such a time, utilize more tested
66 * packages to protect Data, especially sensitive data at rest (long term).
72 private static final byte[] DOUBLE_EQ = new byte[] {'=','='};
73 public static final String ENC = "enc:";
74 private static final SecureRandom random = new SecureRandom();
76 public final char[] codeset;
77 private final int splitLinesAt;
78 private final String encoding;
79 private final Convert convert;
80 private final boolean endEquals;
81 private byte[] keyBytes = null;
82 //Note: AES Encryption is not Thread Safe. It is Synchronized
83 //private AES aes = null; // only initialized from File, and only if needed for Passwords
86 * This is the standard base64 Key Set.
89 public static final Symm base64 = new Symm(
90 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray()
91 ,76, Config.UTF_8,true);
93 public static final Symm base64noSplit = new Symm(
94 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray()
95 ,Integer.MAX_VALUE, Config.UTF_8,true);
98 * This is the standard base64 set suitable for URLs and Filenames
101 public static final Symm base64url = new Symm(
102 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".toCharArray()
103 ,76, Config.UTF_8,true);
106 * A Password set, using US-ASCII
109 public static final Symm encrypt = new Symm(base64url.codeset,1024, "US-ASCII", false);
110 private static final byte[] EMPTY = new byte[0];
113 * A typical set of Password Chars
114 * Note, this is too large to fit into the algorithm. Only use with PassGen
116 private static char passChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+!@#$%^&*(){}[]?:;,.".toCharArray();
121 * Use this to create special case Case Sets and/or Line breaks
123 * If you don't know why you need this, use the Singleton Method
128 public Symm(char[] codeset, int split, String charset, boolean useEndEquals) {
129 this.codeset = codeset;
130 splitLinesAt = split;
132 endEquals = useEndEquals;
133 char prev = 0, curr=0, first = 0;
134 int offset=Integer.SIZE; // something that's out of range for integer array
136 // There can be time efficiencies gained when the underlying keyset consists mainly of ordered
137 // data (i.e. abcde...). Therefore, we'll quickly analyze the keyset. If it proves to have
138 // too much entropy, the "Unordered" algorithm, which is faster in such cases is used.
139 ArrayList<int[]> la = new ArrayList<int[]>();
140 for(int i=0;i<codeset.length;++i) {
142 if(prev+1==curr) { // is next character in set
145 if(offset!=Integer.SIZE) { // add previous range
146 la.add(new int[]{first,prev,offset});
152 la.add(new int[]{first,curr,offset});
153 if(la.size()>codeset.length/3) {
154 convert = new Unordered(codeset);
155 } else { // too random to get speed enhancement from range algorithm
156 int[][] range = new int[la.size()][];
158 convert = new Ordered(range);
162 public Symm copy(int lines) {
163 return new Symm(codeset,lines,encoding,endEquals);
166 // Only used by keygen, which is intentionally randomized. Therefore, always use unordered
167 private Symm(char[] codeset, Symm parent) {
168 this.codeset = codeset;
169 splitLinesAt = parent.splitLinesAt;
170 endEquals = parent.endEquals;
171 encoding = parent.encoding;
172 convert = new Unordered(codeset);
176 * Obtain the base64() behavior of this class, for use in standard BASIC AUTH mechanism, etc.
180 public static final Symm base64() {
185 * Obtain the base64() behavior of this class, for use in standard BASIC AUTH mechanism, etc.
190 public static final Symm base64noSplit() {
191 return base64noSplit;
195 * Obtain the base64 "URL" behavior of this class, for use in File Names, etc. (no "/")
198 public static final Symm base64url() {
203 * Obtain a special ASCII version for Scripting, with base set of base64url use in File Names, etc. (no "/")
205 public static final Symm baseCrypt() {
209 public <T> T exec(SyncExec<T> exec) throws Exception {
211 if(keyBytes == null) {
212 keyBytes = new byte[AES.AES_KEY_SIZE/8];
213 int offset = (Math.abs(codeset[0])+47)%(codeset.length-keyBytes.length);
214 for(int i=0;i<keyBytes.length;++i) {
215 keyBytes[i] = (byte)codeset[i+offset];
219 return exec.exec(new AES(keyBytes,0,keyBytes.length));
222 public interface Encryption {
223 public CipherOutputStream outputStream(OutputStream os, boolean encrypt);
224 public CipherInputStream inputStream(InputStream is, boolean encrypt);
227 public static interface SyncExec<T> {
228 public T exec(Encryption enc) throws IOException, Exception;
231 public byte[] encode(byte[] toEncrypt) throws IOException {
232 if(toEncrypt==null) {
235 ByteArrayOutputStream baos = new ByteArrayOutputStream((int)(toEncrypt.length*1.25));
236 encode(new ByteArrayInputStream(toEncrypt),baos);
237 return baos.toByteArray();
241 public byte[] decode(byte[] encrypted) throws IOException {
242 ByteArrayOutputStream baos = new ByteArrayOutputStream((int)(encrypted.length*1.25));
243 decode(new ByteArrayInputStream(encrypted),baos);
244 return baos.toByteArray();
248 * Helper function for String API of "Encode"
249 * use "getBytes" with appropriate char encoding, etc.
253 * @throws IOException
255 public String encode(String str) throws IOException {
257 boolean useDefaultEncoding = false;
259 array = str.getBytes(encoding);
260 } catch (IOException e) {
261 array = str.getBytes(); // take default
262 useDefaultEncoding = true;
264 // Calculate expected size to avoid any buffer expansion copies within the ByteArrayOutput code
265 ByteArrayOutputStream baos = new ByteArrayOutputStream((int)(array.length*1.363)); // account for 4 bytes for 3 and a byte or two more
267 encode(new ByteArrayInputStream(array),baos);
268 if (useDefaultEncoding) {
269 return baos.toString();
271 return baos.toString(encoding);
275 * Helper function for the String API of "Decode"
276 * use "getBytes" with appropriate char encoding, etc.
279 * @throws IOException
281 public String decode(String str) throws IOException {
283 boolean useDefaultEncoding = false;
285 array = str.getBytes(encoding);
286 } catch (IOException e) {
287 array = str.getBytes(); // take default
288 useDefaultEncoding = true;
290 // Calculate expected size to avoid any buffer expansion copies within the ByteArrayOutput code
291 ByteArrayOutputStream baos = new ByteArrayOutputStream((int)(array.length*.76)); // Decoding is 3 bytes for 4. Allocate slightly more than 3/4s
292 decode(new ByteArrayInputStream(array), baos);
293 if (useDefaultEncoding) {
294 return baos.toString();
296 return baos.toString(encoding);
300 * Convenience Function
302 * encode String into InputStream and call encode(InputStream, OutputStream)
306 * @throws IOException
308 public void encode(String string, OutputStream out) throws IOException {
309 encode(new ByteArrayInputStream(string.getBytes()),out);
313 * Convenience Function
315 * encode String into InputStream and call decode(InputStream, OutputStream)
319 * @throws IOException
321 public void decode(String string, OutputStream out) throws IOException {
322 decode(new ByteArrayInputStream(string.getBytes()),out);
325 public void encode(InputStream is, OutputStream os, byte[] prefix) throws IOException {
331 * encode InputStream onto Output Stream
336 * @throws IOException
338 public void encode(InputStream is, OutputStream os) throws IOException {
339 // StringBuilder sb = new StringBuilder((int)(estimate*1.255)); // try to get the right size of StringBuilder from start.. slightly more than 1.25 times
341 int read, idx=0, line=0;
346 if(line>=splitLinesAt) {
350 switch(++idx) { // 1 based reading, slightly faster ++
351 case 1: // ptr is the first 6 bits of read
352 os.write(codeset[read>>2]);
355 case 2: // ptr is the last 2 bits of prev followed by the first 4 bits of read
356 os.write(codeset[((prev & 0x03)<<4) | (read>>4)]);
360 // Char 1 is last 4 bits of prev plus the first 2 bits of read
361 // Char 2 is the last 6 bits of read
362 os.write(codeset[(((prev & 0xF)<<2) | (read>>6))]);
363 if(line==splitLinesAt) { // deal with line splitting for two characters
367 os.write(codeset[(read & 0x3F)]);
373 } else { // deal with any remaining bits from Prev, then pad
375 case 1: // just the last 2 bits of prev
376 os.write(codeset[(prev & 0x03)<<4]);
377 if(endEquals)os.write(DOUBLE_EQ);
379 case 2: // just the last 4 bits of prev
380 os.write(codeset[(prev & 0xF)<<2]);
381 if(endEquals)os.write('=');
390 public void decode(InputStream is, OutputStream os, int skip) throws IOException {
396 * Decode InputStream onto OutputStream
399 * @throws IOException
401 public void decode(InputStream is, OutputStream os) throws IOException {
404 while((read = is.read())>=0) {
405 index = convert.convert(read);
407 switch(++idx) { // 1 based cases, slightly faster ++
408 case 1: // index goes into first 6 bits of prev
411 case 2: // write second 2 bits of into prev, write byte, last 4 bits go into prev
412 os.write((byte)(prev|(index>>4)));
415 case 3: // first 4 bits of index goes into prev, write byte, last 2 bits go into prev
416 os.write((byte)(prev|(index>>2)));
419 default: // (3+) | prev and last six of index
420 os.write((byte)(prev|(index&0x3F)));
429 * Interface to allow this class to choose which algorithm to find index of character in Key
433 private interface Convert {
434 public int convert(int read) throws IOException;
438 * Ordered uses a range of orders to compare against, rather than requiring the investigation
439 * of every character needed.
443 private static final class Ordered implements Convert {
444 private int[][] range;
445 public Ordered(int[][] range) {
448 public int convert(int read) throws IOException {
455 for(int i=0;i<range.length;++i) {
456 if(read >= range[i][0] && read<=range[i][1]) {
457 return read-range[i][2];
460 throw new IOException("Unacceptable Character in Stream");
465 * Unordered, i.e. the key is purposely randomized, simply has to investigate each character
466 * until we find a match.
470 private static final class Unordered implements Convert {
471 private char[] codec;
472 public Unordered(char[] codec) {
475 public int convert(int read) throws IOException {
482 for(int i=0;i<codec.length;++i) {
483 if(codec[i]==read)return i;
485 // don't give clue in Encryption mode
486 throw new IOException("Unacceptable Character in Stream");
491 * Generate a 2048 based Key from which we extract our code base
494 * @throws IOException
496 public static byte[] keygen() throws IOException {
497 byte inkey[] = new byte[0x600];
498 new SecureRandom().nextBytes(inkey);
499 ByteArrayOutputStream baos = new ByteArrayOutputStream(0x800);
500 base64url.encode(new ByteArrayInputStream(inkey), baos);
501 return baos.toByteArray();
504 // A class allowing us to be less predictable about significant digits (i.e. not picking them up from the
505 // beginning, and not picking them up in an ordered row. Gives a nice 2048 with no visible patterns.
506 private class Obtain {
512 private Obtain(Symm b64, byte[] key) {
513 skip = Math.abs(key[key.length-13]%key.length);
514 if((key.length&0x1) == (skip&0x1)) { // if both are odd or both are even
517 length = b64.codeset.length;
518 last = 17+length%59; // never start at beginning
523 return Math.abs(key[(++last*skip)%key.length])%length;
528 * Obtain a Symm from "keyfile" (Config.KEYFILE) property
532 * @throws IOException
533 * @throws CadiException
535 public static Symm obtain(Access access) throws CadiException {
536 Symm symm = Symm.baseCrypt();
538 String keyfile = access.getProperty(Config.CADI_KEYFILE,null);
540 File file = new File(keyfile);
542 access.log(Level.INIT, Config.CADI_KEYFILE,"points to",file.getCanonicalPath());
543 } catch (IOException e1) {
544 access.log(Level.INIT, Config.CADI_KEYFILE,"points to",file.getAbsolutePath());
548 FileInputStream fis = new FileInputStream(file);
550 symm = Symm.obtain(fis);
554 } catch (IOException e) {
557 } catch (IOException e) {
558 access.log(e, "Cannot load keyfile");
563 filename = file.getCanonicalPath();
564 } catch (IOException e) {
565 filename = file.getAbsolutePath();
567 throw new CadiException("ERROR: " + filename + " does not exist!");
573 * Create a new random key
575 public Symm obtain() throws IOException {
576 byte inkey[] = new byte[0x800];
577 new SecureRandom().nextBytes(inkey);
578 return obtain(inkey);
582 * Obtain a Symm from 2048 key from a String
586 * @throws IOException
588 public static Symm obtain(String key) throws IOException {
589 return obtain(new ByteArrayInputStream(key.getBytes()));
593 * Obtain a Symm from 2048 key from a Stream
597 * @throws IOException
599 public static Symm obtain(InputStream is) throws IOException {
600 ByteArrayOutputStream baos = new ByteArrayOutputStream();
602 base64url.decode(is, baos);
603 } catch (IOException e) {
605 throw new IOException("Invalid Key");
607 byte[] bkey = baos.toByteArray();
608 if(bkey.length<0x88) { // 2048 bit key
609 throw new IOException("Invalid key");
611 return baseCrypt().obtain(bkey);
615 * Convenience for picking up Keyfile
619 * @throws IOException
621 public static Symm obtain(File f) throws IOException {
622 FileInputStream fis = new FileInputStream(f);
630 * Decrypt into a String
636 * @throws IOException
638 public String enpass(String password) throws IOException {
639 ByteArrayOutputStream baos = new ByteArrayOutputStream();
640 enpass(password,baos);
641 return new String(baos.toByteArray());
645 * Create an encrypted password, making sure that even short passwords have a minimum length.
649 * @throws IOException
651 public void enpass(final String password, final OutputStream os) throws IOException {
652 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
653 DataOutputStream dos = new DataOutputStream(baos);
654 byte[] bytes = password.getBytes();
655 if(this.getClass().getSimpleName().startsWith("base64")) { // don't expose randomization
659 Random r = new SecureRandom();
662 for(int i=0;i<3;++i) {
663 dos.writeByte(b=(byte)r.nextInt());
667 for(int i=0;i<start;++i) {
668 dos.writeByte(r.nextInt());
670 dos.writeInt((int)System.currentTimeMillis());
671 int minlength = Math.min(0x9,bytes.length);
672 dos.writeByte(minlength); // expect truncation
673 if(bytes.length<0x9) {
674 for(int i=0;i<bytes.length;++i) {
675 dos.writeByte(r.nextInt());
676 dos.writeByte(bytes[i]);
678 // make sure it's long enough
679 for(int i=bytes.length;i<0x9;++i) {
680 dos.writeByte(r.nextInt());
687 // 7/21/2016 Jonathan add AES Encryption to the mix
689 exec(new SyncExec<Void>() {
691 public Void exec(Encryption enc) throws Exception {
692 CipherInputStream cis = enc.inputStream(new ByteArrayInputStream(baos.toByteArray()), true);
702 } catch (IOException e) {
704 } catch (Exception e) {
705 throw new IOException(e);
710 * Decrypt a password into a String
716 * @throws IOException
718 public String depass(String password) throws IOException {
719 if(password==null)return null;
720 ByteArrayOutputStream baos = new ByteArrayOutputStream();
721 depass(password,baos);
722 return new String(baos.toByteArray());
733 * @throws IOException
735 public long depass(final String password, final OutputStream os) throws IOException {
736 int offset = password.startsWith(ENC)?4:0;
737 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
738 final ByteArrayInputStream bais = new ByteArrayInputStream(password.getBytes(),offset,password.length()-offset);
740 exec(new SyncExec<Void>() {
742 public Void exec(Encryption enc) throws IOException {
743 CipherOutputStream cos = enc.outputStream(baos, false);
745 cos.close(); // flush
749 } catch (IOException e) {
751 } catch (Exception e) {
752 throw new IOException(e);
755 byte[] bytes = baos.toByteArray();
756 DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
758 if(this.getClass().getSimpleName().startsWith("base64")) { // don't expose randomization
763 for(int i=0;i<3;++i) {
764 start+=Math.abs(dis.readByte());
767 for(int i=0;i<start;++i) {
770 time = (dis.readInt() & 0xFFFF)|(System.currentTimeMillis()&0xFFFF0000);
771 int minlength = dis.readByte();
773 DataOutputStream dos = new DataOutputStream(os);
774 for(int i=0;i<minlength;++i) {
776 dos.writeByte(dis.readByte());
779 int pre =((Byte.SIZE*3+Integer.SIZE+Byte.SIZE)/Byte.SIZE)+start;
780 os.write(bytes, pre, bytes.length-pre);
786 public static String randomGen(int numBytes) {
787 return randomGen(passChars,numBytes);
790 public static String randomGen(char[] chars ,int numBytes) {
792 StringBuilder sb = new StringBuilder(numBytes);
793 for(int i=0;i<numBytes;++i) {
794 rint = random.nextInt(chars.length);
795 sb.append(chars[rint]);
797 return sb.toString();
799 // Internal mechanism for helping to randomize placement of characters within a Symm codeset
800 // Based on an incoming data stream (originally created randomly, but can be recreated within
801 // 2048 key), go after a particular place in the new codeset. If that codeset spot is used, then move
802 // right or left (depending on iteration) to find the next available slot. In this way, key generation
803 // is speeded up by only enacting N iterations, but adds a spreading effect of the random number stream, so that keyset is also
804 // shuffled for a good spread. It is, however, repeatable, given the same number set, allowing for
805 // quick recreation when the official stream is actually obtained.
806 public Symm obtain(byte[] key) throws IOException {
807 int filled = codeset.length;
808 char[] seq = new char[filled];
811 boolean right = true;
813 Obtain o = new Obtain(this,key);
817 if(index<0 || index>=codeset.length) {
818 System.out.println("uh, oh");
820 if(right) { // alternate going left or right to find the next open slot (keeps it from taking too long to hit something)
821 for(int j=index;j<end;++j) {
823 seq[j]=codeset[filled];
830 for(int j=index;j>=0;--j) {
832 seq[j]=codeset[filled];
840 Symm newSymm = new Symm(seq,this);
843 newSymm.keyBytes = new byte[AES.AES_KEY_SIZE/8];
844 int offset = (Math.abs(key[(47%key.length)])+137)%(key.length-newSymm.keyBytes.length);
845 for(int i=0;i<newSymm.keyBytes.length;++i) {
846 newSymm.keyBytes[i] = key[i+offset];
848 } catch (Exception e) {
849 throw new IOException(e);