1 /*
   2  * Copyright (c) 2005, 2007, 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 /**
  25  * @see IsSunMSCAPIAvailable.sh
  26  */
  27 
  28 import java.security.Provider;
  29 import java.security.*;
  30 import javax.crypto.Cipher;
  31 
  32 public class IsSunMSCAPIAvailable {
  33 
  34     public static void main(String[] args) throws Exception {
  35 
  36         // Check if the provider is available
  37         try {
  38             Class.forName("sun.security.mscapi.SunMSCAPI");
  39 
  40         } catch (Exception e) {
  41             System.out.println(
  42                 "The SunMSCAPI provider is not available on this platform");
  43             return;
  44         }
  45 
  46         // Dynamically register the SunMSCAPI provider
  47         Security.addProvider(new sun.security.mscapi.SunMSCAPI());
  48 
  49 
  50         Provider p = Security.getProvider("SunMSCAPI");
  51 
  52         System.out.println("SunMSCAPI provider classname is " +
  53             p.getClass().getName());
  54         System.out.println("SunMSCAPI provider name is " + p.getName());
  55         System.out.println("SunMSCAPI provider version # is " + p.getVersion());
  56         System.out.println("SunMSCAPI provider info is " + p.getInfo());
  57 
  58         /*
  59          * Secure Random
  60          */
  61 
  62         SecureRandom random = SecureRandom.getInstance("Windows-PRNG", p);
  63         System.out.println("    Windows-PRNG is implemented by: " +
  64             random.getClass().getName());
  65 
  66         /*
  67          * Key Store
  68          */
  69         KeyStore keystore = KeyStore.getInstance("Windows-MY", p);
  70         System.out.println("    Windows-MY is implemented by: " +
  71             keystore.getClass().getName());
  72 
  73         keystore = KeyStore.getInstance("Windows-ROOT", p);
  74         System.out.println("    Windows-ROOT is implemented by: " +
  75             keystore.getClass().getName());
  76 
  77         /*
  78          * Signature
  79          */
  80         Signature signature = Signature.getInstance("SHA1withRSA", p);
  81         System.out.println("    SHA1withRSA is implemented by: " +
  82             signature.getClass().getName());
  83 
  84         signature = Signature.getInstance("MD5withRSA", p);
  85         System.out.println("    MD5withRSA is implemented by: " +
  86             signature.getClass().getName());
  87 
  88         signature = Signature.getInstance("MD2withRSA", p);
  89         System.out.println("    MD2withRSA is implemented by: " +
  90             signature.getClass().getName());
  91 
  92         /*
  93          * Key Pair Generator
  94          */
  95         KeyPairGenerator keypairGenerator =
  96             KeyPairGenerator.getInstance("RSA", p);
  97         System.out.println("    RSA is implemented by: " +
  98             keypairGenerator.getClass().getName());
  99 
 100         /*
 101          * Cipher
 102          */
 103         Cipher cipher = null;
 104 
 105         try {
 106             cipher = Cipher.getInstance("RSA", p);
 107             System.out.println("    RSA is implemented by: " +
 108                 cipher.getClass().getName());
 109 
 110             cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", p);
 111             System.out.println("    RSA/ECB/PKCS1Padding is implemented by: " +
 112                 cipher.getClass().getName());
 113 
 114         } catch (GeneralSecurityException e) {
 115             System.out.println("Cipher not supported by provider, skipping...");
 116         }
 117     }
 118 }