1 /*
2 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
122 if (p.getName().equals("SunPKCS11")) {
123 found = true;
124 break;
125 }
126 } catch (Exception | ServiceConfigurationError e) {
127 // ignore and move on to the next one
128 }
129 }
130 // Nothing found through ServiceLoader; fall back to reflection
131 if (!found) {
132 try {
133 Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11");
134 p = (Provider) clazz.newInstance();
135 } catch (Exception ex) {
136 ex.printStackTrace();
137 }
138 }
139 pkcs11 = p;
140 }
141
142 /*
143 * Use Solaris SPARC 11.2 or later to avoid an intermittent failure
144 * when running SunPKCS11-Solaris (8044554)
145 */
146 static boolean isBadSolarisSparc(Provider p) {
147 if ("SunPKCS11-Solaris".equals(p.getName()) && badSolarisSparc) {
148 System.out.println("SunPKCS11-Solaris provider requires " +
149 "Solaris SPARC 11.2 or later, skipping");
150 return true;
151 }
152 return false;
153 }
154
155 // Return a SunPKCS11 provider configured with the specified config file
156 static Provider getSunPKCS11(String config) throws Exception {
157 if (pkcs11 == null) {
158 throw new NoSuchProviderException("No PKCS11 provider available");
159 }
160 return pkcs11.configure(config);
161 }
162
163 public abstract void main(Provider p) throws Exception;
164
165 protected boolean skipTest(Provider p) {
166 return false;
167 }
168
169 private void premain(Provider p) throws Exception {
170 if (skipTest(p)) {
171 return;
172 }
173
174 // set a security manager and policy before a test case runs,
667 // Check support for a curve with a provided Vector of EC support
668 boolean checkSupport(List<ECParameterSpec> supportedEC,
669 ECParameterSpec curve) {
670 for (ECParameterSpec ec: supportedEC) {
671 if (ec.equals(curve)) {
672 return true;
673 }
674 }
675 return false;
676 }
677
678 private static Map<String,String[]> osMap;
679
680 // Location of the NSS libraries on each supported platform
681 private static Map<String, String[]> getOsMap() {
682 if (osMap != null) {
683 return osMap;
684 }
685
686 osMap = new HashMap<>();
687 osMap.put("SunOS-sparc-32", new String[] { "/usr/lib/mps/" });
688 osMap.put("SunOS-sparcv9-64", new String[] { "/usr/lib/mps/64/" });
689 osMap.put("SunOS-x86-32", new String[] { "/usr/lib/mps/" });
690 osMap.put("SunOS-amd64-64", new String[] { "/usr/lib/mps/64/" });
691 osMap.put("Linux-i386-32", new String[] {
692 "/usr/lib/i386-linux-gnu/",
693 "/usr/lib32/",
694 "/usr/lib/" });
695 osMap.put("Linux-amd64-64", new String[] {
696 "/usr/lib/x86_64-linux-gnu/",
697 "/usr/lib/x86_64-linux-gnu/nss/",
698 "/usr/lib64/" });
699 osMap.put("Linux-ppc64-64", new String[] { "/usr/lib64/" });
700 osMap.put("Linux-ppc64le-64", new String[] { "/usr/lib64/" });
701 osMap.put("Linux-s390x-64", new String[] { "/usr/lib64/" });
702 osMap.put("Windows-x86-32", new String[] {});
703 osMap.put("Windows-amd64-64", new String[] {});
704 osMap.put("MacOSX-x86_64-64", new String[] {});
705 osMap.put("Linux-arm-32", new String[] {
706 "/usr/lib/arm-linux-gnueabi/nss/",
707 "/usr/lib/arm-linux-gnueabihf/nss/" });
708 osMap.put("Linux-aarch64-64", new String[] {
709 "/usr/lib/aarch64-linux-gnu/",
710 "/usr/lib/aarch64-linux-gnu/nss/" });
730 String path = fetchNssLib(osId);
731 if (path != null) {
732 nssLibPaths.add(path);
733 }
734 } else {
735 String[] paths = customNssLibPaths.split(",");
736 for (String path : paths) {
737 if (!path.endsWith(File.separator)) {
738 nssLibPaths.add(path + File.separator);
739 } else {
740 nssLibPaths.add(path);
741 }
742 }
743 }
744
745 return nssLibPaths.toArray(new String[nssLibPaths.size()]);
746 }
747
748 private final static char[] hexDigits = "0123456789abcdef".toCharArray();
749
750 private static final String distro = distro();
751
752 static final boolean badSolarisSparc =
753 System.getProperty("os.name").equals("SunOS") &&
754 System.getProperty("os.arch").equals("sparcv9") &&
755 System.getProperty("os.version").compareTo("5.11") <= 0 &&
756 getDistro().compareTo("11.2") < 0;
757
758 public static String toString(byte[] b) {
759 if (b == null) {
760 return "(null)";
761 }
762 StringBuilder sb = new StringBuilder(b.length * 3);
763 for (int i = 0; i < b.length; i++) {
764 int k = b[i] & 0xff;
765 if (i != 0) {
766 sb.append(':');
767 }
768 sb.append(hexDigits[k >>> 4]);
769 sb.append(hexDigits[k & 0xf]);
770 }
771 return sb.toString();
772 }
773
774 public static byte[] parse(String s) {
775 if (s.equals("(null)")) {
776 return null;
777 }
819 T[] r = Arrays.copyOf(a, a.length + b.length);
820 System.arraycopy(b, 0, r, a.length, b.length);
821 return r;
822 }
823
824 /**
825 * Returns supported algorithms of specified type.
826 */
827 static List<String> getSupportedAlgorithms(String type, String alg,
828 Provider p) {
829 // prepare a list of supported algorithms
830 List<String> algorithms = new ArrayList<>();
831 Set<Provider.Service> services = p.getServices();
832 for (Provider.Service service : services) {
833 if (service.getType().equals(type)
834 && service.getAlgorithm().startsWith(alg)) {
835 algorithms.add(service.getAlgorithm());
836 }
837 }
838 return algorithms;
839 }
840
841 /**
842 * Get the identifier for the operating system distribution
843 */
844 static String getDistro() {
845 return distro;
846 }
847
848 private static String distro() {
849 if (props.getProperty("os.name").equals("SunOS")) {
850 try (BufferedReader in =
851 new BufferedReader(new InputStreamReader(
852 Runtime.getRuntime().exec("uname -v").getInputStream()))) {
853
854 return in.readLine();
855 } catch (Exception e) {
856 throw new RuntimeException("Failed to determine distro.", e);
857 }
858 } else {
859 // Not used outside Solaris
860 return null;
861 }
862 }
863
864 static byte[] generateData(int length) {
865 byte data[] = new byte[length];
866 for (int i=0; i<data.length; i++) {
867 data[i] = (byte) (i % 256);
868 }
869 return data;
870 }
871
872 private static String fetchNssLib(String osId) {
873 switch (osId) {
874 case "Windows-x86-32":
875 return fetchNssLib(WINDOWS_X86.class);
876
877 case "Windows-amd64-64":
878 return fetchNssLib(WINDOWS_X64.class);
879
880 case "MacOSX-x86_64-64":
881 return fetchNssLib(MACOSX_X64.class);
|
1 /*
2 * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
122 if (p.getName().equals("SunPKCS11")) {
123 found = true;
124 break;
125 }
126 } catch (Exception | ServiceConfigurationError e) {
127 // ignore and move on to the next one
128 }
129 }
130 // Nothing found through ServiceLoader; fall back to reflection
131 if (!found) {
132 try {
133 Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11");
134 p = (Provider) clazz.newInstance();
135 } catch (Exception ex) {
136 ex.printStackTrace();
137 }
138 }
139 pkcs11 = p;
140 }
141
142 // Return a SunPKCS11 provider configured with the specified config file
143 static Provider getSunPKCS11(String config) throws Exception {
144 if (pkcs11 == null) {
145 throw new NoSuchProviderException("No PKCS11 provider available");
146 }
147 return pkcs11.configure(config);
148 }
149
150 public abstract void main(Provider p) throws Exception;
151
152 protected boolean skipTest(Provider p) {
153 return false;
154 }
155
156 private void premain(Provider p) throws Exception {
157 if (skipTest(p)) {
158 return;
159 }
160
161 // set a security manager and policy before a test case runs,
654 // Check support for a curve with a provided Vector of EC support
655 boolean checkSupport(List<ECParameterSpec> supportedEC,
656 ECParameterSpec curve) {
657 for (ECParameterSpec ec: supportedEC) {
658 if (ec.equals(curve)) {
659 return true;
660 }
661 }
662 return false;
663 }
664
665 private static Map<String,String[]> osMap;
666
667 // Location of the NSS libraries on each supported platform
668 private static Map<String, String[]> getOsMap() {
669 if (osMap != null) {
670 return osMap;
671 }
672
673 osMap = new HashMap<>();
674 osMap.put("Linux-i386-32", new String[] {
675 "/usr/lib/i386-linux-gnu/",
676 "/usr/lib32/",
677 "/usr/lib/" });
678 osMap.put("Linux-amd64-64", new String[] {
679 "/usr/lib/x86_64-linux-gnu/",
680 "/usr/lib/x86_64-linux-gnu/nss/",
681 "/usr/lib64/" });
682 osMap.put("Linux-ppc64-64", new String[] { "/usr/lib64/" });
683 osMap.put("Linux-ppc64le-64", new String[] { "/usr/lib64/" });
684 osMap.put("Linux-s390x-64", new String[] { "/usr/lib64/" });
685 osMap.put("Windows-x86-32", new String[] {});
686 osMap.put("Windows-amd64-64", new String[] {});
687 osMap.put("MacOSX-x86_64-64", new String[] {});
688 osMap.put("Linux-arm-32", new String[] {
689 "/usr/lib/arm-linux-gnueabi/nss/",
690 "/usr/lib/arm-linux-gnueabihf/nss/" });
691 osMap.put("Linux-aarch64-64", new String[] {
692 "/usr/lib/aarch64-linux-gnu/",
693 "/usr/lib/aarch64-linux-gnu/nss/" });
713 String path = fetchNssLib(osId);
714 if (path != null) {
715 nssLibPaths.add(path);
716 }
717 } else {
718 String[] paths = customNssLibPaths.split(",");
719 for (String path : paths) {
720 if (!path.endsWith(File.separator)) {
721 nssLibPaths.add(path + File.separator);
722 } else {
723 nssLibPaths.add(path);
724 }
725 }
726 }
727
728 return nssLibPaths.toArray(new String[nssLibPaths.size()]);
729 }
730
731 private final static char[] hexDigits = "0123456789abcdef".toCharArray();
732
733 public static String toString(byte[] b) {
734 if (b == null) {
735 return "(null)";
736 }
737 StringBuilder sb = new StringBuilder(b.length * 3);
738 for (int i = 0; i < b.length; i++) {
739 int k = b[i] & 0xff;
740 if (i != 0) {
741 sb.append(':');
742 }
743 sb.append(hexDigits[k >>> 4]);
744 sb.append(hexDigits[k & 0xf]);
745 }
746 return sb.toString();
747 }
748
749 public static byte[] parse(String s) {
750 if (s.equals("(null)")) {
751 return null;
752 }
794 T[] r = Arrays.copyOf(a, a.length + b.length);
795 System.arraycopy(b, 0, r, a.length, b.length);
796 return r;
797 }
798
799 /**
800 * Returns supported algorithms of specified type.
801 */
802 static List<String> getSupportedAlgorithms(String type, String alg,
803 Provider p) {
804 // prepare a list of supported algorithms
805 List<String> algorithms = new ArrayList<>();
806 Set<Provider.Service> services = p.getServices();
807 for (Provider.Service service : services) {
808 if (service.getType().equals(type)
809 && service.getAlgorithm().startsWith(alg)) {
810 algorithms.add(service.getAlgorithm());
811 }
812 }
813 return algorithms;
814 }
815
816 static byte[] generateData(int length) {
817 byte data[] = new byte[length];
818 for (int i=0; i<data.length; i++) {
819 data[i] = (byte) (i % 256);
820 }
821 return data;
822 }
823
824 private static String fetchNssLib(String osId) {
825 switch (osId) {
826 case "Windows-x86-32":
827 return fetchNssLib(WINDOWS_X86.class);
828
829 case "Windows-amd64-64":
830 return fetchNssLib(WINDOWS_X64.class);
831
832 case "MacOSX-x86_64-64":
833 return fetchNssLib(MACOSX_X64.class);
|