Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Util.java
41154 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.pkcs11;2627import java.math.BigInteger;28import java.security.*;2930/**31* Collection of static utility methods.32*33* @author Andreas Sterbenz34* @since 1.535*/36public final class P11Util {3738private static Object LOCK = new Object();3940private static volatile Provider sun, sunRsaSign, sunJce;4142private P11Util() {43// empty44}4546static Provider getSunProvider() {47Provider p = sun;48if (p == null) {49synchronized (LOCK) {50p = getProvider51(sun, "SUN", "sun.security.provider.Sun");52sun = p;53}54}55return p;56}5758static Provider getSunRsaSignProvider() {59Provider p = sunRsaSign;60if (p == null) {61synchronized (LOCK) {62p = getProvider63(sunRsaSign, "SunRsaSign", "sun.security.rsa.SunRsaSign");64sunRsaSign = p;65}66}67return p;68}6970static Provider getSunJceProvider() {71Provider p = sunJce;72if (p == null) {73synchronized (LOCK) {74p = getProvider75(sunJce, "SunJCE", "com.sun.crypto.provider.SunJCE");76sunJce = p;77}78}79return p;80}8182@SuppressWarnings("removal")83private static Provider getProvider(Provider p, String providerName,84String className) {85if (p != null) {86return p;87}88p = Security.getProvider(providerName);89if (p == null) {90try {91final Class<?> c = Class.forName(className);92p = AccessController.doPrivileged(93new PrivilegedAction<Provider>() {94public Provider run() {95try {96@SuppressWarnings("deprecation")97Object o = c.newInstance();98return (Provider) o;99} catch (Exception e) {100throw new ProviderException(101"Could not find provider " +102providerName, e);103}104}105}, null, new RuntimePermission(106"accessClassInPackage." + c.getPackageName()));107} catch (ClassNotFoundException e) {108// Unexpected, as className is not a user but a109// P11Util-internal value.110throw new ProviderException("Could not find provider " +111providerName, e);112}113}114return p;115}116117static byte[] convert(byte[] input, int offset, int len) {118if ((offset == 0) && (len == input.length)) {119return input;120} else {121byte[] t = new byte[len];122System.arraycopy(input, offset, t, 0, len);123return t;124}125}126127static byte[] subarray(byte[] b, int ofs, int len) {128byte[] out = new byte[len];129System.arraycopy(b, ofs, out, 0, len);130return out;131}132133static byte[] concat(byte[] b1, byte[] b2) {134byte[] b = new byte[b1.length + b2.length];135System.arraycopy(b1, 0, b, 0, b1.length);136System.arraycopy(b2, 0, b, b1.length, b2.length);137return b;138}139140static long[] concat(long[] b1, long[] b2) {141if (b1.length == 0) {142return b2;143}144long[] b = new long[b1.length + b2.length];145System.arraycopy(b1, 0, b, 0, b1.length);146System.arraycopy(b2, 0, b, b1.length, b2.length);147return b;148}149150public static byte[] getMagnitude(BigInteger bi) {151byte[] b = bi.toByteArray();152if ((b.length > 1) && (b[0] == 0)) {153int n = b.length - 1;154byte[] newarray = new byte[n];155System.arraycopy(b, 1, newarray, 0, n);156b = newarray;157}158return b;159}160161static byte[] sha1(byte[] data) {162try {163MessageDigest md = MessageDigest.getInstance("SHA-1");164md.update(data);165return md.digest();166} catch (GeneralSecurityException e) {167throw new ProviderException(e);168}169}170171private static final char[] hexDigits = "0123456789abcdef".toCharArray();172173static String toString(byte[] b) {174if (b == null) {175return "(null)";176}177StringBuilder sb = new StringBuilder(b.length * 3);178for (int i = 0; i < b.length; i++) {179int k = b[i] & 0xff;180if (i != 0) {181sb.append(':');182}183sb.append(hexDigits[k >>> 4]);184sb.append(hexDigits[k & 0xf]);185}186return sb.toString();187}188189}190191192