0N/A/*
2362N/A * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.pkcs11;
0N/A
0N/Aimport java.math.BigInteger;
0N/Aimport java.security.*;
0N/A
0N/A/**
0N/A * Collection of static utility methods.
0N/A *
0N/A * @author Andreas Sterbenz
0N/A * @since 1.5
0N/A */
0N/Apublic final class P11Util {
0N/A
0N/A private static Object LOCK = new Object();
0N/A
0N/A private static volatile Provider sun, sunRsaSign, sunJce;
0N/A
0N/A private P11Util() {
0N/A // empty
0N/A }
0N/A
0N/A static Provider getSunProvider() {
0N/A Provider p = sun;
0N/A if (p == null) {
0N/A synchronized (LOCK) {
0N/A p = getProvider
0N/A (sun, "SUN", "sun.security.provider.Sun");
0N/A sun = p;
0N/A }
0N/A }
0N/A return p;
0N/A }
0N/A
0N/A static Provider getSunRsaSignProvider() {
0N/A Provider p = sunRsaSign;
0N/A if (p == null) {
0N/A synchronized (LOCK) {
0N/A p = getProvider
0N/A (sunRsaSign, "SunRsaSign", "sun.security.rsa.SunRsaSign");
0N/A sunRsaSign = p;
0N/A }
0N/A }
0N/A return p;
0N/A }
0N/A
0N/A static Provider getSunJceProvider() {
0N/A Provider p = sunJce;
0N/A if (p == null) {
0N/A synchronized (LOCK) {
0N/A p = getProvider
0N/A (sunJce, "SunJCE", "com.sun.crypto.provider.SunJCE");
0N/A sunJce = p;
0N/A }
0N/A }
0N/A return p;
0N/A }
0N/A
0N/A private static Provider getProvider(Provider p, String providerName,
0N/A String className) {
0N/A if (p != null) {
0N/A return p;
0N/A }
0N/A p = Security.getProvider(providerName);
0N/A if (p == null) {
0N/A try {
0N/A Class clazz = Class.forName(className);
0N/A p = (Provider)clazz.newInstance();
0N/A } catch (Exception e) {
0N/A throw new ProviderException
0N/A ("Could not find provider " + providerName, e);
0N/A }
0N/A }
0N/A return p;
0N/A }
0N/A
0N/A static byte[] convert(byte[] input, int offset, int len) {
0N/A if ((offset == 0) && (len == input.length)) {
0N/A return input;
0N/A } else {
0N/A byte[] t = new byte[len];
0N/A System.arraycopy(input, offset, t, 0, len);
0N/A return t;
0N/A }
0N/A }
0N/A
0N/A static byte[] subarray(byte[] b, int ofs, int len) {
0N/A byte[] out = new byte[len];
0N/A System.arraycopy(b, ofs, out, 0, len);
0N/A return out;
0N/A }
0N/A
0N/A static byte[] concat(byte[] b1, byte[] b2) {
0N/A byte[] b = new byte[b1.length + b2.length];
0N/A System.arraycopy(b1, 0, b, 0, b1.length);
0N/A System.arraycopy(b2, 0, b, b1.length, b2.length);
0N/A return b;
0N/A }
0N/A
0N/A static long[] concat(long[] b1, long[] b2) {
0N/A if (b1.length == 0) {
0N/A return b2;
0N/A }
0N/A long[] b = new long[b1.length + b2.length];
0N/A System.arraycopy(b1, 0, b, 0, b1.length);
0N/A System.arraycopy(b2, 0, b, b1.length, b2.length);
0N/A return b;
0N/A }
0N/A
0N/A // trim leading (most significant) zeroes from the result
0N/A static byte[] trimZeroes(byte[] b) {
0N/A int i = 0;
0N/A while ((i < b.length - 1) && (b[i] == 0)) {
0N/A i++;
0N/A }
0N/A if (i == 0) {
0N/A return b;
0N/A }
0N/A byte[] t = new byte[b.length - i];
0N/A System.arraycopy(b, i, t, 0, t.length);
0N/A return t;
0N/A }
0N/A
0N/A public static byte[] getMagnitude(BigInteger bi) {
0N/A byte[] b = bi.toByteArray();
0N/A if ((b.length > 1) && (b[0] == 0)) {
0N/A int n = b.length - 1;
0N/A byte[] newarray = new byte[n];
0N/A System.arraycopy(b, 1, newarray, 0, n);
0N/A b = newarray;
0N/A }
0N/A return b;
0N/A }
0N/A
0N/A static byte[] getBytesUTF8(String s) {
0N/A try {
0N/A return s.getBytes("UTF8");
0N/A } catch (java.io.UnsupportedEncodingException e) {
0N/A throw new RuntimeException(e);
0N/A }
0N/A }
0N/A
0N/A static byte[] sha1(byte[] data) {
0N/A try {
0N/A MessageDigest md = MessageDigest.getInstance("SHA-1");
0N/A md.update(data);
0N/A return md.digest();
0N/A } catch (GeneralSecurityException e) {
0N/A throw new ProviderException(e);
0N/A }
0N/A }
0N/A
0N/A private final static char[] hexDigits = "0123456789abcdef".toCharArray();
0N/A
0N/A static String toString(byte[] b) {
0N/A if (b == null) {
0N/A return "(null)";
0N/A }
0N/A StringBuffer sb = new StringBuffer(b.length * 3);
0N/A for (int i = 0; i < b.length; i++) {
0N/A int k = b[i] & 0xff;
0N/A if (i != 0) {
0N/A sb.append(':');
0N/A }
0N/A sb.append(hexDigits[k >>> 4]);
0N/A sb.append(hexDigits[k & 0xf]);
0N/A }
0N/A return sb.toString();
0N/A }
0N/A
0N/A}