jdk/src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/P11Util.java
author stefank
Mon, 25 Aug 2014 09:10:13 +0200
changeset 26314 f8bc1966fb30
parent 25859 3317bb8137f4
child 37782 ad8fe7507ecc
permissions -rw-r--r--
8055416: Several vm/gc/heap/summary "After GC" events emitted for the same GC ID Reviewed-by: brutisso, ehelin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24578
bbf15045dfbb 8043342: Replace uses of StringBuffer with StringBuilder within crypto code
wetmore
parents: 17916
diff changeset
     2
 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.pkcs11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Collection of static utility methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public final class P11Util {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static Object LOCK = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static volatile Provider sun, sunRsaSign, sunJce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private P11Util() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static Provider getSunProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        Provider p = sun;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            synchronized (LOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                p = getProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                    (sun, "SUN", "sun.security.provider.Sun");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                sun = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static Provider getSunRsaSignProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Provider p = sunRsaSign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            synchronized (LOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                p = getProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    (sunRsaSign, "SunRsaSign", "sun.security.rsa.SunRsaSign");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                sunRsaSign = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static Provider getSunJceProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        Provider p = sunJce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            synchronized (LOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                p = getProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    (sunJce, "SunJCE", "com.sun.crypto.provider.SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                sunJce = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static Provider getProvider(Provider p, String providerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        p = Security.getProvider(providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    91
                Class<?> clazz = Class.forName(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                p = (Provider)clazz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                throw new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        ("Could not find provider " + providerName, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static byte[] convert(byte[] input, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if ((offset == 0) && (len == input.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            byte[] t = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.arraycopy(input, offset, t, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static byte[] subarray(byte[] b, int ofs, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        byte[] out = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        System.arraycopy(b, ofs, out, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static byte[] concat(byte[] b1, byte[] b2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        byte[] b = new byte[b1.length + b2.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        System.arraycopy(b1, 0, b, 0, b1.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        System.arraycopy(b2, 0, b, b1.length, b2.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    static long[] concat(long[] b1, long[] b2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (b1.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return b2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        long[] b = new long[b1.length + b2.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        System.arraycopy(b1, 0, b, 0, b1.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        System.arraycopy(b2, 0, b, b1.length, b2.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public static byte[] getMagnitude(BigInteger bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        byte[] b = bi.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if ((b.length > 1) && (b[0] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            int n = b.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            byte[] newarray = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            System.arraycopy(b, 1, newarray, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            b = newarray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    static byte[] getBytesUTF8(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return s.getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } catch (java.io.UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    static byte[] sha1(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            MessageDigest md = MessageDigest.getInstance("SHA-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            md.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private final static char[] hexDigits = "0123456789abcdef".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static String toString(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return "(null)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
24578
bbf15045dfbb 8043342: Replace uses of StringBuffer with StringBuilder within crypto code
wetmore
parents: 17916
diff changeset
   169
        StringBuilder sb = new StringBuilder(b.length * 3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        for (int i = 0; i < b.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            int k = b[i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            sb.append(hexDigits[k >>> 4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            sb.append(hexDigits[k & 0xf]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}