jdk/src/java.security.sasl/share/classes/com/sun/security/sasl/util/PolicyUtils.java
author roland
Thu, 08 Jan 2015 11:00:38 +0100
changeset 28486 b0df113b962e
parent 25859 3317bb8137f4
permissions -rw-r--r--
8027626: assert(Opcode() != Op_If || outcnt() == 2) failed: bad if #1 Summary: IGVN encounters IfNode with single projection when optimizing dying subgraph Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.security.sasl.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.security.sasl.Sasl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Static class that contains utilities for dealing with Java SASL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * security policy-related properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
final public class PolicyUtils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    // Can't create one of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private PolicyUtils() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public final static int NOPLAINTEXT = 0x0001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public final static int NOACTIVE = 0x0002;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public final static int NODICTIONARY = 0x0004;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public final static int FORWARD_SECRECY = 0x0008;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public final static int NOANONYMOUS = 0x0010;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public final static int PASS_CREDENTIALS = 0x0200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Determines whether a mechanism's characteristics, as defined in flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * fits the security policy properties found in props.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @param flags The mechanism's security characteristics
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @param props The security policy properties to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * @return true if passes; false if fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    56
    public static boolean checkPolicy(int flags, Map<String, ?> props) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (props == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if ("true".equalsIgnoreCase((String)props.get(Sasl.POLICY_NOPLAINTEXT))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            && (flags&NOPLAINTEXT) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if ("true".equalsIgnoreCase((String)props.get(Sasl.POLICY_NOACTIVE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            && (flags&NOACTIVE) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if ("true".equalsIgnoreCase((String)props.get(Sasl.POLICY_NODICTIONARY))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            && (flags&NODICTIONARY) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if ("true".equalsIgnoreCase((String)props.get(Sasl.POLICY_NOANONYMOUS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            && (flags&NOANONYMOUS) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if ("true".equalsIgnoreCase((String)props.get(Sasl.POLICY_FORWARD_SECRECY))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            && (flags&FORWARD_SECRECY) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if ("true".equalsIgnoreCase((String)props.get(Sasl.POLICY_PASS_CREDENTIALS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            && (flags&PASS_CREDENTIALS) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Given a list of mechanisms and their characteristics, select the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * subset that conforms to the policies defined in props.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Useful for SaslXXXFactory.getMechanismNames(props) implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static String[] filterMechs(String[] mechs, int[] policies,
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    96
        Map<String, ?> props) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (props == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return mechs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        boolean[] passed = new boolean[mechs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        for (int i = 0; i< mechs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (passed[i] = checkPolicy(policies[i], props)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                ++count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        String[] answer = new String[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        for (int i = 0, j=0; i< mechs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (passed[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                answer[j++] = mechs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}