/* * Parse a keystore domain configuration file and associated collection * of keystore passwords to create a collection of KeyStore.Builder. */ private List<KeyStoreBuilderComponents> getBuilders( URI configuration, Map<String, KeyStore.ProtectionParameter> passwords) throws IOException { PolicyParser parser = new PolicyParser(true); // expand properties Collection<PolicyParser.DomainEntry> domains = null; List<KeyStoreBuilderComponents> builders = new ArrayList<>(); String uriDomain = configuration.getFragment(); try (InputStreamReader configurationReader = new InputStreamReader(PolicyUtil.getInputStream(configuration.toURL()), "UTF-8")) { parser.read(configurationReader); domains = parser.getDomainEntries(); } catch (MalformedURLException mue) { throw new IOException(mue); } catch (PolicyParser.ParsingException pe) { throw new IOException(pe); } for (PolicyParser.DomainEntry domain : domains) { Map<String, String> domainProperties = domain.getProperties(); if (uriDomain != null && (!uriDomain.equalsIgnoreCase(domain.getName()))) { continue; // skip this domain } if (domainProperties.containsKey(ENTRY_NAME_SEPARATOR)) { this.entryNameSeparator = domainProperties.get(ENTRY_NAME_SEPARATOR); // escape any regex meta characters char ch = 0; StringBuilder s = new StringBuilder(); for (int i = 0; i < this.entryNameSeparator.length(); i++) { ch = this.entryNameSeparator.charAt(i); if (REGEX_META.indexOf(ch) != -1) { s.append('\\'); } s.append(ch); } this.entryNameSeparatorRegEx = s.toString(); } Collection<PolicyParser.KeyStoreEntry> keystores = domain.getEntries(); for (PolicyParser.KeyStoreEntry keystore : keystores) { String keystoreName = keystore.getName(); Map<String, String> properties = new HashMap<>(domainProperties); properties.putAll(keystore.getProperties()); String keystoreType = DEFAULT_KEYSTORE_TYPE; if (properties.containsKey(KEYSTORE_TYPE)) { keystoreType = properties.get(KEYSTORE_TYPE); } Provider keystoreProvider = null; if (properties.containsKey(KEYSTORE_PROVIDER_NAME)) { String keystoreProviderName = properties.get(KEYSTORE_PROVIDER_NAME); keystoreProvider = Security.getProvider(keystoreProviderName); if (keystoreProvider == null) { throw new IOException("Error locating JCE provider: " + keystoreProviderName); } } File keystoreFile = null; if (properties.containsKey(KEYSTORE_URI)) { String uri = properties.get(KEYSTORE_URI); try { if (uri.startsWith("file://")) { keystoreFile = new File(new URI(uri)); } else { keystoreFile = new File(uri); } } catch (URISyntaxException | IllegalArgumentException e) { throw new IOException( "Error processing keystore property: " + "keystoreURI=\"" + uri + "\"", e); } } KeyStore.ProtectionParameter keystoreProtection = null; if (passwords.containsKey(keystoreName)) { keystoreProtection = passwords.get(keystoreName); } else if (properties.containsKey(KEYSTORE_PASSWORD_ENV)) { String env = properties.get(KEYSTORE_PASSWORD_ENV); String pwd = System.getenv(env); if (pwd != null) { keystoreProtection = new KeyStore.PasswordProtection(pwd.toCharArray()); } else { throw new IOException( "Error processing keystore property: " + "keystorePasswordEnv=\"" + env + "\""); } } else { keystoreProtection = new KeyStore.PasswordProtection(null); } builders.add( new KeyStoreBuilderComponents( keystoreName, keystoreType, keystoreProvider, keystoreFile, keystoreProtection)); } break; // skip other domains } if (builders.isEmpty()) { throw new IOException("Error locating domain configuration data " + "for: " + configuration); } return builders; }
private static List<String> splitStringAtNonEnclosedWhiteSpace(String value) throws IllegalArgumentException { List<String> al = new ArrayList<String>(); char[] arr; int startPosition = 0; int endPosition = 0; final char SPACE = ' '; final char DOUBLEQ = '"'; final char SINGLEQ = '\''; /* * An "open" or "active" enclosing state is where * the first valid start quote qualifier is found, * and there is a search in progress for the * relevant end matching quote * * enclosingTargetChar set to SPACE * is used to signal a non open enclosing state */ char enclosingTargetChar = SPACE; if (value == null) { throw new IllegalArgumentException(MessageOutput.format("value string is null")); } // split parameter string into individual chars arr = value.toCharArray(); for (int i = 0; i < arr.length; i++) { switch (arr[i]) { case SPACE: { // do nothing for spaces // unless last in array if (isLastChar(arr, i)) { endPosition = i; // break for substring creation break; } continue; } case DOUBLEQ: case SINGLEQ: { if (enclosingTargetChar == arr[i]) { // potential match to close open enclosing if (isNextCharWhitespace(arr, i)) { // if peek next is whitespace // then enclosing is a valid substring endPosition = i; // reset enclosing target char enclosingTargetChar = SPACE; // break for substring creation break; } } if (enclosingTargetChar == SPACE) { // no open enclosing state // handle as normal char if (isPreviousCharWhitespace(arr, i)) { startPosition = i; // peek forward for end candidates if (value.indexOf(arr[i], i + 1) >= 0) { // set open enclosing state by // setting up the target char enclosingTargetChar = arr[i]; } else { // no more target chars left to match // end enclosing, handle as normal char if (isNextCharWhitespace(arr, i)) { endPosition = i; // break for substring creation break; } } } } continue; } default: { // normal non-space, non-" and non-' chars if (enclosingTargetChar == SPACE) { // no open enclosing state if (isPreviousCharWhitespace(arr, i)) { // start of space delim substring startPosition = i; } if (isNextCharWhitespace(arr, i)) { // end of space delim substring endPosition = i; // break for substring creation break; } } continue; } } // break's end up here if (startPosition > endPosition) { throw new IllegalArgumentException(MessageOutput.format("Illegal option values")); } // extract substring and add to List<String> al.add(value.substring(startPosition, ++endPosition)); // set new start position i = startPosition = endPosition; } // for loop return al; }