1 /*
   2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.util;
  27 
  28 import java.io.*;
  29 import java.nio.*;
  30 import java.nio.charset.*;
  31 import java.util.Arrays;
  32 
  33 /**
  34  * A utility class for reading passwords
  35  *
  36  */
  37 public class Password {
  38     /** Reads user password from given input stream. */
  39     @SuppressWarnings("fallthrough")
  40     public static char[] readPassword(InputStream in) throws IOException {
  41 
  42         char[] consoleEntered = null;
  43         byte[] consoleBytes = null;
  44 
  45         try {
  46             // Use the new java.io.Console class
  47             Console con = null;
  48             if (in == System.in && ((con = System.console()) != null)) {
  49                 consoleEntered = con.readPassword();
  50                 // readPassword returns "" if you just print ENTER,
  51                 // to be compatible with old Password class, change to null
  52                 if (consoleEntered != null && consoleEntered.length == 0) {
  53                     return null;
  54                 }
  55                 consoleBytes = convertToBytes(consoleEntered);
  56                 in = new ByteArrayInputStream(consoleBytes);
  57             }
  58 
  59             // Rest of the lines still necessary for KeyStoreLoginModule
  60             // and when there is no console.
  61 
  62             char[] lineBuffer;
  63             char[] buf;
  64             int i;
  65 
  66             buf = lineBuffer = new char[128];
  67 
  68             int room = buf.length;
  69             int offset = 0;
  70             int c;
  71 
  72             boolean done = false;
  73             while (!done) {
  74                 switch (c = in.read()) {
  75                   case -1:
  76                   case '\n':
  77                       done = true;
  78                       break;
  79 
  80                   case '\r':
  81                     int c2 = in.read();
  82                     if ((c2 != '\n') && (c2 != -1)) {
  83                         if (!(in instanceof PushbackInputStream)) {
  84                             in = new PushbackInputStream(in);
  85                         }
  86                         ((PushbackInputStream)in).unread(c2);
  87                     } else {
  88                         done = true;
  89                         break;
  90                     }
  91                     /* fall through */
  92                   default:
  93                     if (--room < 0) {
  94                         buf = new char[offset + 128];
  95                         room = buf.length - offset - 1;
  96                         System.arraycopy(lineBuffer, 0, buf, 0, offset);
  97                         Arrays.fill(lineBuffer, ' ');
  98                         lineBuffer = buf;
  99                     }
 100                     buf[offset++] = (char) c;
 101                     break;
 102                 }
 103             }
 104 
 105             if (offset == 0) {
 106                 return null;
 107             }
 108 
 109             char[] ret = new char[offset];
 110             System.arraycopy(buf, 0, ret, 0, offset);
 111             Arrays.fill(buf, ' ');
 112 
 113             return ret;
 114         } finally {
 115             if (consoleEntered != null) {
 116                 Arrays.fill(consoleEntered, ' ');
 117             }
 118             if (consoleBytes != null) {
 119                 Arrays.fill(consoleBytes, (byte)0);
 120             }
 121         }
 122     }
 123 
 124     /**
 125      * Change a password read from Console.readPassword() into
 126      * its original bytes.
 127      *
 128      * @param pass a char[]
 129      * @return its byte[] format, similar to new String(pass).getBytes()
 130      */
 131     private static byte[] convertToBytes(char[] pass) {
 132         if (enc == null) {
 133             synchronized (Password.class) {
 134                 enc = sun.misc.SharedSecrets.getJavaIOAccess()
 135                         .charset()
 136                         .newEncoder()
 137                         .onMalformedInput(CodingErrorAction.REPLACE)
 138                         .onUnmappableCharacter(CodingErrorAction.REPLACE);
 139             }
 140         }
 141         byte[] ba = new byte[(int)(enc.maxBytesPerChar() * pass.length)];
 142         ByteBuffer bb = ByteBuffer.wrap(ba);
 143         synchronized (enc) {
 144             enc.reset().encode(CharBuffer.wrap(pass), bb, true);
 145         }
 146         if (bb.position() < ba.length) {
 147             ba[bb.position()] = '\n';
 148         }
 149         return ba;
 150     }
 151     private static volatile CharsetEncoder enc;
 152 }