jdk/src/jdk.crypto.ucrypto/solaris/classes/com/oracle/security/ucrypto/UcryptoProvider.java
author sundar
Mon, 31 Aug 2015 17:51:02 +0530
changeset 32434 769b3d81ae69
parent 31270 e6470b24700d
child 33991 619bfc4d582d
permissions -rw-r--r--
8134731: Function.prototype.apply interacts incorrectly with arguments Reviewed-by: attila, hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     1
/*
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     4
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    10
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    15
 * accompanied this code).
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    16
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    20
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    23
 * questions.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    24
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    25
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    26
package com.oracle.security.ucrypto;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    27
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    28
import java.io.IOException;
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    29
import java.io.File;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    30
import java.util.*;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    31
import java.security.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    32
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    33
/**
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    34
 * OracleUcrypto provider main class.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    35
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    36
 * @since 1.9
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    37
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    38
public final class UcryptoProvider extends Provider {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    39
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    40
    private static final long serialVersionUID = 351251234302833L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    41
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    42
    private static boolean DEBUG = false;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    43
    private static HashMap<String, ServiceDesc> provProp = null;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    44
    private static String defConfigName = "";
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    45
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    46
    static {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    47
        try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    48
            // cannot use LoadLibraryAction because that would make the native
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    49
            // library available to the bootclassloader, but we run in the
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    50
            // extension classloader.
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    51
            String osname = System.getProperty("os.name");
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    52
            if (osname.startsWith("SunOS")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    53
                provProp = AccessController.doPrivileged
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    54
                    (new PrivilegedAction<HashMap<String, ServiceDesc>>() {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    55
                        public HashMap<String, ServiceDesc> run() {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    56
                            try {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    57
                                DEBUG = Boolean.parseBoolean(System.getProperty("com.oracle.security.ucrypto.debug"));
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    58
                                String javaHome = System.getProperty("java.home");
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    59
                                String sep = System.getProperty("file.separator");
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    60
                                defConfigName = javaHome + sep + "conf" + sep + "security" + sep +
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    61
                                    "ucrypto-solaris.cfg";
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    62
                                System.loadLibrary("j2ucrypto");
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    63
                                return new HashMap<>();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    64
                            } catch (Error err) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    65
                                if (DEBUG) err.printStackTrace();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    66
                                return null;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    67
                            } catch (SecurityException se) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    68
                                if (DEBUG) se.printStackTrace();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    69
                                return null;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    70
                            }
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    71
                        }
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    72
                    });
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    73
            }
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    74
            if (provProp != null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    75
                boolean[] result = loadLibraries();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    76
                if (result.length == 2) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    77
                    if (result[0]) { // successfully loaded libmd
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    78
                        provProp.put("MessageDigest.MD5",
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    79
                            sd("MessageDigest", "MD5",
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    80
                               "com.oracle.security.ucrypto.NativeDigest$MD5"));
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    81
                        provProp.put("MessageDigest.SHA",
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    82
                            sd("MessageDigest", "SHA",
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    83
                               "com.oracle.security.ucrypto.NativeDigest$SHA1",
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    84
                               "SHA-1", "SHA1"));
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    85
                        provProp.put("MessageDigest.SHA-256",
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    86
                            sd("MessageDigest", "SHA-256",
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    87
                               "com.oracle.security.ucrypto.NativeDigest$SHA256",
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    88
                               "2.16.840.1.101.3.4.2.1", "OID.2.16.840.1.101.3.4.2.1"));
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    89
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    90
                        provProp.put("MessageDigest.SHA-384",
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    91
                            sd("MessageDigest", "SHA-384",
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    92
                               "com.oracle.security.ucrypto.NativeDigest$SHA384",
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    93
                               "2.16.840.1.101.3.4.2.2", "OID.2.16.840.1.101.3.4.2.2"));
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    94
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    95
                        provProp.put("MessageDigest.SHA-512",
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    96
                            sd("MessageDigest", "SHA-512",
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    97
                               "com.oracle.security.ucrypto.NativeDigest$SHA512",
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    98
                               "2.16.840.1.101.3.4.2.3", "OID.2.16.840.1.101.3.4.2.3"));
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
    99
                    };
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   100
                    if (result[1]) { // successfully loaded libsoftcrypto
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   101
                        String supportedMechs = getMechList();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   102
                        debug("Prov: supported mechs = " + supportedMechs);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   103
                        for (UcryptoMech m : UcryptoMech.values()) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   104
                            if (supportedMechs.indexOf(m.name() + ",") != -1) {
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   105
                                ServiceDesc[] services = m.getServiceDescriptions();
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   106
                                // skip unsupported UcryptoMech
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   107
                                if (services == null || services.length == 0) continue;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   108
                                for (int p = 0; p < services.length; p++) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   109
                                    ServiceDesc entry = services[p];
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   110
                                    provProp.put(entry.getType() + "." + entry.getAlgorithm(),
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   111
                                                 entry);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   112
                                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   113
                            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   114
                        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   115
                        // NOTE: GCM support is only available since jdk 7
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   116
                        provProp.put("AlgorithmParameters.GCM",
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   117
                                     sd("AlgorithmParameters", "GCM", "com.oracle.security.ucrypto.GCMParameters"));
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   118
                    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   119
                } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   120
                    debug("Prov: unexpected ucrypto library loading error, got " + result.length);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   121
                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   122
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   123
        } catch (AccessControlException ace) {
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   124
            if (DEBUG) ace.printStackTrace();
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   125
            // disable Ucrypto provider
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   126
            provProp = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   127
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   128
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   129
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   130
    private static ServiceDesc sd(String type, String algo, String cn,
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   131
        String... aliases) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   132
        return new ServiceDesc(type, algo, cn, aliases);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   133
    }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   134
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   135
    private static final class ProviderService extends Provider.Service {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   136
        ProviderService(Provider p, ServiceDesc sd) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   137
            super(p, sd.getType(), sd.getAlgorithm(), sd.getClassName(),
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   138
                  sd.getAliases(), null);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   139
        }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   140
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   141
        @Override
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   142
        public Object newInstance(Object ctrParamObj)
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   143
            throws NoSuchAlgorithmException {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   144
            String type = getType();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   145
            if (ctrParamObj != null) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   146
                throw new InvalidParameterException
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   147
                    ("constructorParameter not used with " + type + " engines");
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   148
            }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   149
            String algo = getAlgorithm();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   150
            try {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   151
                if (type.equals("Cipher")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   152
                    int keySize = -1;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   153
                    if (algo.charAt(3) == '_') {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   154
                        keySize = Integer.parseInt(algo.substring(4, 7))/8;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   155
                        algo = algo.substring(0, 3) + algo.substring(7);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   156
                    }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   157
                    if (algo.equals("AES/ECB/NoPadding")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   158
                        return new NativeCipher.AesEcbNoPadding(keySize);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   159
                    } else if (algo.equals("AES/ECB/PKCS5Padding")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   160
                        return new NativeCipherWithJavaPadding.AesEcbPKCS5();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   161
                    } else if (algo.equals("AES/CBC/NoPadding")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   162
                        return new NativeCipher.AesCbcNoPadding(keySize);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   163
                    } else if (algo.equals("AES/CBC/PKCS5Padding")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   164
                        return new NativeCipherWithJavaPadding.AesCbcPKCS5();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   165
                    } else if (algo.equals("AES/CTR/NoPadding")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   166
                        return new NativeCipher.AesCtrNoPadding();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   167
                    } else if (algo.equals("AES/GCM/NoPadding")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   168
                        return new NativeGCMCipher.AesGcmNoPadding(keySize);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   169
                    } else if (algo.equals("AES/CFB128/NoPadding")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   170
                        return new NativeCipher.AesCfb128NoPadding();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   171
                    } else if (algo.equals("AES/CFB128/PKCS5Padding")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   172
                        return new NativeCipherWithJavaPadding.AesCfb128PKCS5();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   173
                    } else if (algo.equals("RSA/ECB/NoPadding")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   174
                        return new NativeRSACipher.NoPadding();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   175
                    } else if (algo.equals("RSA/ECB/PKCS1Padding")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   176
                        return new NativeRSACipher.PKCS1Padding();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   177
                    }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   178
                } else if (type.equals("Signature")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   179
                    if (algo.equals("SHA1withRSA")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   180
                        return new NativeRSASignature.SHA1();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   181
                    } else if (algo.equals("SHA256withRSA")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   182
                        return new NativeRSASignature.SHA256();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   183
                    } else if (algo.equals("SHA384withRSA")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   184
                        return new NativeRSASignature.SHA384();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   185
                    } else if (algo.equals("SHA512withRSA")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   186
                        return new NativeRSASignature.SHA512();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   187
                    } else if (algo.equals("MD5withRSA")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   188
                        return new NativeRSASignature.MD5();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   189
                    }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   190
                } else if (type.equals("MessageDigest")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   191
                    if (algo.equals("SHA")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   192
                        return new NativeDigest.SHA1();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   193
                    } else if (algo.equals("SHA-256")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   194
                        return new NativeDigest.SHA256();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   195
                    } else if (algo.equals("SHA-384")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   196
                        return new NativeDigest.SHA384();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   197
                    } else if (algo.equals("SHA-512")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   198
                        return new NativeDigest.SHA512();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   199
                    } else if (algo.equals("MD5")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   200
                        return new NativeDigest.MD5();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   201
                    }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   202
                } else if (type.equals("AlgorithmParameters")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   203
                    if (algo.equals("GCM")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   204
                        return new GCMParameters();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   205
                    }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   206
                }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   207
            } catch (Exception ex) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   208
                throw new NoSuchAlgorithmException("Error constructing " +
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   209
                    type + " for " + algo + " using OracleUcrypto", ex);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   210
            }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   211
            throw new ProviderException("No impl for " + algo +
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   212
                " " + type);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   213
        }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   214
    }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   215
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   216
    static Provider provider = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   217
    private static native boolean[] loadLibraries();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   218
    private static native String getMechList();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   219
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   220
    static void debug(String msg) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   221
        if (DEBUG) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   222
            System.out.println("UCrypto/" + msg);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   223
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   224
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   225
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   226
    public UcryptoProvider() {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   227
        super("OracleUcrypto", 1.9d, "Provider using Oracle Ucrypto API");
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   228
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   229
        AccessController.doPrivileged(new PrivilegedAction<>() {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   230
            public Void run() {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   231
                init(defConfigName);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   232
                return null;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   233
            }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   234
        });
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   235
        if (provider == null) provider = this;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   236
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   237
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   238
    private void init(final String configName) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   239
        if (provProp != null) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   240
            debug("Prov: configuration file " + configName);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   241
            Config c;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   242
            try {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   243
                c = new Config(configName);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   244
            } catch (Exception ex) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   245
                throw new UcryptoException("Error parsing Config", ex);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   246
            }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   247
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   248
            String[] disabledServices = c.getDisabledServices();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   249
            for (String ds : disabledServices) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   250
                if (provProp.remove(ds) != null) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   251
                    debug("Prov: remove config-disabled service " + ds);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   252
                } else {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   253
                    debug("Prov: ignore unsupported service " + ds);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   254
                }
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   255
            }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   256
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   257
            for (ServiceDesc s: provProp.values()) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   258
                debug("Prov: add service for " + s);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   259
                putService(new ProviderService(this, s));
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   260
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   261
        }
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   262
    }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   263
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   264
    @Override
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   265
    public Provider configure(String configArg) throws InvalidParameterException {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   266
        // default policy entry only grants read access to default config
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   267
        if (!defConfigName.equals(configArg)) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   268
            throw new InvalidParameterException("Ucrypto provider can only be " +
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   269
                "configured with default configuration file");
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   270
        }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   271
        // re-read the config
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   272
        init(defConfigName);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 27182
diff changeset
   273
        return this;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   274
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   275
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   276
    public boolean equals(Object obj) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   277
        return this == obj;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   278
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   279
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   280
    public int hashCode() {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   281
        return System.identityHashCode(this);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   282
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   283
}