Add Cert Cred for aafcli
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / Symm.java
index 095f6e9..ea3891f 100644 (file)
@@ -71,6 +71,7 @@ import org.onap.aaf.cadi.config.Config;
 public class Symm {
        private static final byte[] DOUBLE_EQ = new byte[] {'=','='}; 
        public static final String ENC = "enc:";
+       private static final Object LOCK = new Object();
        private static final SecureRandom random = new SecureRandom();
        
        public final char[] codeset;
@@ -116,7 +117,8 @@ public class Symm {
        private static char passChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+!@#$%^&*(){}[]?:;,.".toCharArray();
                        
 
-
+       private static Symm internalOnly = null;
+       
        /**
         * Use this to create special case Case Sets and/or Line breaks
         * 
@@ -207,7 +209,7 @@ public class Symm {
        }
 
        public <T> T exec(SyncExec<T> exec) throws Exception {
-               synchronized(ENC) {
+               synchronized(LOCK) {
                        if(keyBytes == null) {
                                keyBytes = new byte[AES.AES_KEY_SIZE/8];
                                int offset = (Math.abs(codeset[0])+47)%(codeset.length-keyBytes.length);
@@ -388,7 +390,9 @@ public class Symm {
     }
 
     public void decode(InputStream is, OutputStream os, int skip) throws IOException {
-       is.skip(skip);
+       if(is.skip(skip)!=skip) {
+               throw new IOException("Error skipping on IOStream in Symm");
+       }
        decode(is,os);
     }
 
@@ -449,7 +453,8 @@ public class Symm {
                   switch(read) {
                           case -1: 
                           case '=':
-                          case '\n': 
+                          case '\n':
+                          case '\r':
                                   return -1;
                   }
                   for(int i=0;i<range.length;++i) {
@@ -533,10 +538,10 @@ public class Symm {
  * @throws CadiException 
     */
    public static Symm obtain(Access access) throws CadiException {
-               Symm symm = Symm.baseCrypt();
-
                String keyfile = access.getProperty(Config.CADI_KEYFILE,null);
                if(keyfile!=null) {
+                       Symm symm = Symm.baseCrypt();
+
                        File file = new File(keyfile);
                        try {
                                access.log(Level.INIT, Config.CADI_KEYFILE,"points to",file.getCanonicalPath());
@@ -566,8 +571,14 @@ public class Symm {
                                }
                                throw new CadiException("ERROR: " + filename + " does not exist!");
                        }
+                       return symm;
+               } else {
+                       try {
+                               return internalOnly();
+                       } catch (IOException e) {
+                               throw new CadiException(e);
+                       }
                }
-               return symm;
    }
   /**
    *  Create a new random key 
@@ -851,4 +862,22 @@ public class Symm {
 
          return newSymm;
   }
+  
+  /** 
+   * This Symm is generated for internal JVM use.  It has no external keyfile, but can be used
+   * for securing Memory, as it remains the same ONLY of the current JVM
+   * @return
+ * @throws IOException 
+   */
+  public static synchronized Symm internalOnly() throws IOException {
+         if(internalOnly==null) {
+                 ByteArrayInputStream baos = new ByteArrayInputStream(keygen());
+                 try {
+                         internalOnly = Symm.obtain(baos);
+                 } finally {
+                         baos.close();
+                 }
+         }
+         return internalOnly;
+  }
 }