jdk/src/share/classes/sun/security/pkcs11/P11Util.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 17916 e02ddef88f77
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2011, 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
    // trim leading (most significant) zeroes from the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    static byte[] trimZeroes(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        while ((i < b.length - 1) && (b[i] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        byte[] t = new byte[b.length - i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        System.arraycopy(b, i, t, 0, t.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static byte[] getMagnitude(BigInteger bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        byte[] b = bi.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if ((b.length > 1) && (b[0] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            int n = b.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            byte[] newarray = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            System.arraycopy(b, 1, newarray, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            b = newarray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    static byte[] getBytesUTF8(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return s.getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } catch (java.io.UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    static byte[] sha1(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            MessageDigest md = MessageDigest.getInstance("SHA-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            md.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private final static char[] hexDigits = "0123456789abcdef".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    static String toString(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return "(null)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        StringBuffer sb = new StringBuffer(b.length * 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        for (int i = 0; i < b.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            int k = b[i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            sb.append(hexDigits[k >>> 4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            sb.append(hexDigits[k & 0xf]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}