1 /*
   2  * Copyright (c) 2014, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package java.util;
  25 
  26 /**
  27  * Implementation of SystemRandom for Windows Crypto API.
  28  */
  29 final class SystemRandomImpl extends SystemRandom {
  30 
  31     static {
  32         staticInit0();
  33     }
  34 
  35     /**
  36      * Native code keeps a HCRYPTPROV cryptographic provider handle here.
  37      * (see windows/native/libjava/SystemRandomImpl_md.c)
  38      */
  39     @SuppressWarnings("UnusedDeclaration")
  40     private long cryptProvider;
  41 
  42     private boolean closed;
  43     private final Object lock = new Object();
  44 
  45     SystemRandomImpl() {
  46         synchronized (lock) {
  47             if (!init0()) {
  48                 closed = true;
  49                 throw new UnsupportedOperationException("Cryptographic Provider not available.");
  50             }
  51         }
  52     }
  53 
  54     @Override
  55     public void getBytes(byte[] bytes) {
  56         if (bytes == null) throw new NullPointerException();
  57         synchronized (lock) {
  58             if (closed) {
  59                 throw new IllegalStateException("Already closed.");
  60             }
  61             getBytes0(bytes);
  62         }
  63     }
  64 
  65     @Override
  66     public void close() {
  67         synchronized (lock) {
  68             if (!closed) {
  69                 close0();
  70                 closed = true;
  71             }
  72         }
  73     }
  74 
  75     /**
  76      * In case user fails to call close() explicitly,
  77      * it is called when object is finalized.
  78      */
  79     @Override
  80     protected void finalize() {
  81         close();
  82     }
  83 
  84     // native methods (see windows/native/libjava/SystemRandomImpl_md.c)
  85 
  86     private static native void staticInit0();
  87 
  88     private native boolean init0();
  89 
  90     private native void getBytes0(byte[] bytes);
  91 
  92     private native void close0();
  93 }