< prev index next >

src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/Config.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2015, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.pkcs11;
  27 
  28 import java.io.*;
  29 import static java.io.StreamTokenizer.*;
  30 import java.math.BigInteger;
  31 import java.util.*;
  32 
  33 import java.security.*;
  34 
  35 import sun.security.action.GetPropertyAction;
  36 import sun.security.util.PropertyExpander;
  37 
  38 import sun.security.pkcs11.wrapper.*;
  39 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
  40 import static sun.security.pkcs11.wrapper.CK_ATTRIBUTE.*;
  41 
  42 import static sun.security.pkcs11.TemplateManager.*;
  43 
  44 /**
  45  * Configuration container and file parsing.
  46  *
  47  * @author  Andreas Sterbenz
  48  * @since   1.5
  49  */
  50 final class Config {
  51 
  52     static final int ERR_HALT       = 1;
  53     static final int ERR_IGNORE_ALL = 2;
  54     static final int ERR_IGNORE_LIB = 3;
  55 
  56     // same as allowSingleThreadedModules but controlled via a system property
  57     // and applied to all providers. if set to false, no SunPKCS11 instances
  58     // will accept single threaded modules regardless of the setting in their
  59     // config files.
  60     private static final boolean staticAllowSingleThreadedModules;


  61 
  62     static {
  63         String p = "sun.security.pkcs11.allowSingleThreadedModules";
  64         String s = AccessController.doPrivileged(new GetPropertyAction(p));
  65         if ("false".equalsIgnoreCase(s)) {











  66             staticAllowSingleThreadedModules = false;
  67         } else {
  68             staticAllowSingleThreadedModules = true;
  69         }


  70     }
  71 
  72     private final static boolean DEBUG = false;
  73 
  74     private static void debug(Object o) {
  75         if (DEBUG) {
  76             System.out.println(o);
  77         }
  78     }
  79 
  80     // file name containing this configuration
  81     private String filename;
  82 
  83     // Reader and StringTokenizer used during parsing
  84     private Reader reader;
  85 
  86     private StreamTokenizer st;
  87 
  88     private Set<String> parsedKeywords;
  89 


 633     private void checkDup(String keyword) throws IOException {
 634         if (parsedKeywords.contains(keyword)) {
 635             throw excLine(keyword + " must only be specified once");
 636         }
 637     }
 638 
 639     //
 640     // individual entry parsing methods
 641     //
 642 
 643     private String parseLibrary(String keyword) throws IOException {
 644         checkDup(keyword);
 645         parseEquals();
 646         String lib = parseLine();
 647         lib = expand(lib);
 648         int i = lib.indexOf("/$ISA/");
 649         if (i != -1) {
 650             // replace "/$ISA/" with "/sparcv9/" on 64-bit Solaris SPARC
 651             // and with "/amd64/" on Solaris AMD64.
 652             // On all other platforms, just turn it into a "/"
 653             String osName = System.getProperty("os.name", "");
 654             String osArch = System.getProperty("os.arch", "");
 655             String prefix = lib.substring(0, i);
 656             String suffix = lib.substring(i + 5);
 657             if (osName.equals("SunOS") && osArch.equals("sparcv9")) {
 658                 lib = prefix + "/sparcv9" + suffix;
 659             } else if (osName.equals("SunOS") && osArch.equals("amd64")) {
 660                 lib = prefix + "/amd64" + suffix;
 661             } else {
 662                 lib = prefix + suffix;
 663             }
 664         }
 665         debug(keyword + ": " + lib);
 666 
 667         // Check to see if full path is specified to prevent the DLL
 668         // preloading attack
 669         if (!(new File(lib)).isAbsolute()) {
 670             throw new ConfigurationException(
 671                 "Absolute path required for library value: " + lib);
 672         }
 673         return lib;
 674     }


   1 /*
   2  * Copyright (c) 2003, 2016, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.pkcs11;
  27 
  28 import java.io.*;
  29 import static java.io.StreamTokenizer.*;
  30 import java.math.BigInteger;
  31 import java.util.*;
  32 
  33 import java.security.*;
  34 

  35 import sun.security.util.PropertyExpander;
  36 
  37 import sun.security.pkcs11.wrapper.*;
  38 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
  39 import static sun.security.pkcs11.wrapper.CK_ATTRIBUTE.*;
  40 
  41 import static sun.security.pkcs11.TemplateManager.*;
  42 
  43 /**
  44  * Configuration container and file parsing.
  45  *
  46  * @author  Andreas Sterbenz
  47  * @since   1.5
  48  */
  49 final class Config {
  50 
  51     static final int ERR_HALT       = 1;
  52     static final int ERR_IGNORE_ALL = 2;
  53     static final int ERR_IGNORE_LIB = 3;
  54 
  55     // same as allowSingleThreadedModules but controlled via a system property
  56     // and applied to all providers. if set to false, no SunPKCS11 instances
  57     // will accept single threaded modules regardless of the setting in their
  58     // config files.
  59     private static final boolean staticAllowSingleThreadedModules;
  60     private static final String osName;
  61     private static final String osArch;
  62 
  63     static {
  64         List<String> props = AccessController.doPrivileged(
  65             new PrivilegedAction<>() {
  66                 @Override
  67                 public List<String> run() {
  68                     return List.of(
  69                         System.getProperty(
  70                             "sun.security.pkcs11.allowSingleThreadedModules",
  71                             "true"),
  72                         System.getProperty("os.name"),
  73                         System.getProperty("os.arch"));
  74                 }
  75             }
  76         );
  77         if ("false".equalsIgnoreCase(props.get(0))) {
  78             staticAllowSingleThreadedModules = false;
  79         } else {
  80             staticAllowSingleThreadedModules = true;
  81         }
  82         osName = props.get(1);
  83         osArch = props.get(2);
  84     }
  85 
  86     private final static boolean DEBUG = false;
  87 
  88     private static void debug(Object o) {
  89         if (DEBUG) {
  90             System.out.println(o);
  91         }
  92     }
  93 
  94     // file name containing this configuration
  95     private String filename;
  96 
  97     // Reader and StringTokenizer used during parsing
  98     private Reader reader;
  99 
 100     private StreamTokenizer st;
 101 
 102     private Set<String> parsedKeywords;
 103 


 647     private void checkDup(String keyword) throws IOException {
 648         if (parsedKeywords.contains(keyword)) {
 649             throw excLine(keyword + " must only be specified once");
 650         }
 651     }
 652 
 653     //
 654     // individual entry parsing methods
 655     //
 656 
 657     private String parseLibrary(String keyword) throws IOException {
 658         checkDup(keyword);
 659         parseEquals();
 660         String lib = parseLine();
 661         lib = expand(lib);
 662         int i = lib.indexOf("/$ISA/");
 663         if (i != -1) {
 664             // replace "/$ISA/" with "/sparcv9/" on 64-bit Solaris SPARC
 665             // and with "/amd64/" on Solaris AMD64.
 666             // On all other platforms, just turn it into a "/"


 667             String prefix = lib.substring(0, i);
 668             String suffix = lib.substring(i + 5);
 669             if (osName.equals("SunOS") && osArch.equals("sparcv9")) {
 670                 lib = prefix + "/sparcv9" + suffix;
 671             } else if (osName.equals("SunOS") && osArch.equals("amd64")) {
 672                 lib = prefix + "/amd64" + suffix;
 673             } else {
 674                 lib = prefix + suffix;
 675             }
 676         }
 677         debug(keyword + ": " + lib);
 678 
 679         // Check to see if full path is specified to prevent the DLL
 680         // preloading attack
 681         if (!(new File(lib)).isAbsolute()) {
 682             throw new ConfigurationException(
 683                 "Absolute path required for library value: " + lib);
 684         }
 685         return lib;
 686     }


< prev index next >