Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/pkcs11/P11Util.java
38919 views
/*1* Copyright (c) 2003, 2013, 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}8182private static Provider getProvider(Provider p, String providerName,83String className) {84if (p != null) {85return p;86}87p = Security.getProvider(providerName);88if (p == null) {89try {90Class<?> clazz = Class.forName(className);91p = (Provider)clazz.newInstance();92} catch (Exception e) {93throw new ProviderException94("Could not find provider " + providerName, e);95}96}97return p;98}99100static byte[] convert(byte[] input, int offset, int len) {101if ((offset == 0) && (len == input.length)) {102return input;103} else {104byte[] t = new byte[len];105System.arraycopy(input, offset, t, 0, len);106return t;107}108}109110static byte[] subarray(byte[] b, int ofs, int len) {111byte[] out = new byte[len];112System.arraycopy(b, ofs, out, 0, len);113return out;114}115116static byte[] concat(byte[] b1, byte[] b2) {117byte[] b = new byte[b1.length + b2.length];118System.arraycopy(b1, 0, b, 0, b1.length);119System.arraycopy(b2, 0, b, b1.length, b2.length);120return b;121}122123static long[] concat(long[] b1, long[] b2) {124if (b1.length == 0) {125return b2;126}127long[] b = new long[b1.length + b2.length];128System.arraycopy(b1, 0, b, 0, b1.length);129System.arraycopy(b2, 0, b, b1.length, b2.length);130return b;131}132133public static byte[] getMagnitude(BigInteger bi) {134byte[] b = bi.toByteArray();135if ((b.length > 1) && (b[0] == 0)) {136int n = b.length - 1;137byte[] newarray = new byte[n];138System.arraycopy(b, 1, newarray, 0, n);139b = newarray;140}141return b;142}143144static byte[] getBytesUTF8(String s) {145try {146return s.getBytes("UTF8");147} catch (java.io.UnsupportedEncodingException e) {148throw new RuntimeException(e);149}150}151152static byte[] sha1(byte[] data) {153try {154MessageDigest md = MessageDigest.getInstance("SHA-1");155md.update(data);156return md.digest();157} catch (GeneralSecurityException e) {158throw new ProviderException(e);159}160}161162private final static char[] hexDigits = "0123456789abcdef".toCharArray();163164static String toString(byte[] b) {165if (b == null) {166return "(null)";167}168StringBuffer sb = new StringBuffer(b.length * 3);169for (int i = 0; i < b.length; i++) {170int k = b[i] & 0xff;171if (i != 0) {172sb.append(':');173}174sb.append(hexDigits[k >>> 4]);175sb.append(hexDigits[k & 0xf]);176}177return sb.toString();178}179180}181182183