jdk/src/share/classes/javax/crypto/CryptoPolicyParser.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 14014 da3648e13e67
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
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) 1999, 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 javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import static java.util.Locale.ENGLISH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * JCE has two pairs of jurisdiction policy files: one represents U.S. export
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * laws, and the other represents the local laws of the country where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * JCE will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The jurisdiction policy file has the same syntax as JDK policy files except
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * that JCE has new permission classes called javax.crypto.CryptoPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * and javax.crypto.CryptoAllPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The format of a permission entry in the jurisdiction policy file is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *   permission <crypto permission class name>[, <algorithm name>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *              [[, <exemption mechanism name>][, <maxKeySize>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *              [, <AlgrithomParameterSpec class name>, <parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *              for constructing an AlgrithomParameterSpec object>]]]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Sharon Liu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see java.security.spec.AlgrithomParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see javax.crypto.CryptoPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see javax.crypto.CryptoAllPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see javax.crypto.CryptoPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
final class CryptoPolicyParser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    66
    private Vector<GrantEntry> grantEntries;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // Convenience variables for parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private StreamTokenizer st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private int lookahead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Creates a CryptoPolicyParser object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    CryptoPolicyParser() {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    76
        grantEntries = new Vector<GrantEntry>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Reads a policy configuration using a Reader object. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param policy the policy Reader object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @exception ParsingException if the policy configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * contains a syntax error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @exception IOException if an error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * the policy configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    void read(Reader policy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (!(policy instanceof BufferedReader)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            policy = new BufferedReader(policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         * Configure the stream tokenizer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         *      Recognize strings between "..."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         *      Don't convert words to lowercase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         *      Recognize both C-style and C++-style comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         *      Treat end-of-line as white space, not as a token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        st = new StreamTokenizer(policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        st.resetSyntax();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        st.wordChars('a', 'z');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        st.wordChars('A', 'Z');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        st.wordChars('.', '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        st.wordChars('0', '9');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        st.wordChars('_', '_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        st.wordChars('$', '$');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        st.wordChars(128 + 32, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        st.whitespaceChars(0, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        st.commentChar('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        st.quoteChar('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        st.quoteChar('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        st.lowerCaseMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        st.ordinaryChar('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        st.slashSlashComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        st.slashStarComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        st.parseNumbers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         * The crypto jurisdiction policy must be consistent. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         * following hashtable is used for checking consistency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   129
        Hashtable<String, Vector<String>> processedPermissions = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * The main parsing loop.  The loop is executed once for each entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * in the policy file. The entries are delimited by semicolons. Once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         * we've read in the information for an entry, go ahead and try to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * add it to the grantEntries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        while (lookahead != StreamTokenizer.TT_EOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (peek("grant")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                GrantEntry ge = parseGrantEntry(processedPermissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (ge != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    grantEntries.addElement(ge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                throw new ParsingException(st.lineno(), "expected grant " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                           "statement");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            match(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * parse a Grant entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   154
    private GrantEntry parseGrantEntry(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   155
            Hashtable<String, Vector<String>> processedPermissions)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        GrantEntry e = new GrantEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        match("grant");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        match("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        while(!peek("}")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (peek("Permission")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                CryptoPermissionEntry pe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    parsePermissionEntry(processedPermissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                e.add(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                match(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    ParsingException(st.lineno(), "expected permission entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        match("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * parse a CryptoPermission entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private CryptoPermissionEntry parsePermissionEntry(
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   183
            Hashtable<String, Vector<String>> processedPermissions)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        CryptoPermissionEntry e = new CryptoPermissionEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        match("Permission");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        e.cryptoPermission = match("permission type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (e.cryptoPermission.equals("javax.crypto.CryptoAllPermission")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            // Done with the CryptoAllPermission entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            e.alg = CryptoAllPermission.ALG_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            e.maxKeySize = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // Should see the algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (peek("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // Algorithm name - always convert to upper case after parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            e.alg = match("quoted string").toUpperCase(ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            // The algorithm name can be a wildcard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (peek("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                match("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                e.alg = CryptoPermission.ALG_NAME_WILDCARD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                throw new ParsingException(st.lineno(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                           "Missing the algorithm name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        peekAndMatch(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // May see the exemption mechanism name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (peek("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            // Exemption mechanism name - convert to upper case too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            e.exemptionMechanism = match("quoted string").toUpperCase(ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        peekAndMatch(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // Check whether this entry is consistent with other permission entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // that have been read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (!isConsistent(e.alg, e.exemptionMechanism, processedPermissions)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throw new ParsingException(st.lineno(), "Inconsistent policy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // Should see the maxKeySize if not at the end of this entry yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (peek("number")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            e.maxKeySize = match();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (peek("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                match("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                e.maxKeySize = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                if (!peek(";")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    throw new ParsingException(st.lineno(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                               "Missing the maximum " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                               "allowable key size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    // At the end of this permission entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    e.maxKeySize = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        peekAndMatch(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        // May see an AlgorithmParameterSpec class name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (peek("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // AlgorithmParameterSpec class name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            String algParamSpecClassName = match("quoted string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   255
            Vector<Integer> paramsV = new Vector<>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            while (peek(",")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                match(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                if (peek("number")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    paramsV.addElement(new Integer(match()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    if (peek("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        match("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        paramsV.addElement(new Integer(Integer.MAX_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        throw new ParsingException(st.lineno(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                                   "Expecting an integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            Integer[] params = new Integer[paramsV.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            paramsV.copyInto(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            e.checkParam = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            e.algParamSpec = getInstance(algParamSpecClassName, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private static final AlgorithmParameterSpec getInstance(String type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                                            Integer[] params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        throws ParsingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        AlgorithmParameterSpec ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   288
            Class<?> apsClass = Class.forName(type);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   289
            Class<?>[] paramClasses = new Class<?>[params.length];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            for (int i = 0; i < params.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                paramClasses[i] = int.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   295
            Constructor<?> c = apsClass.getConstructor(paramClasses);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            ret = (AlgorithmParameterSpec) c.newInstance((Object[]) params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw new ParsingException("Cannot call the constructor of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                       type + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private boolean peekAndMatch(String expect)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (peek(expect)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            match(expect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private boolean peek(String expect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        switch (lookahead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        case StreamTokenizer.TT_WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (expect.equalsIgnoreCase(st.sval))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        case StreamTokenizer.TT_NUMBER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            if (expect.equalsIgnoreCase("number")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        case ',':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (expect.equals(","))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (expect.equals("{"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        case '}':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (expect.equals("}"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        case '"':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if (expect.equals("\""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (expect.equals("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        case ';':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (expect.equals(";"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return found;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Excepts to match a non-negative number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private int match()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        int value = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        int lineno = st.lineno();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        String sValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        switch (lookahead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        case StreamTokenizer.TT_NUMBER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            value = (int)st.nval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (value < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                sValue = String.valueOf(st.nval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            sValue = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (value <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            throw new ParsingException(lineno, "a non-negative number",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                       sValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    private String match(String expect)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        String value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        switch (lookahead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        case StreamTokenizer.TT_NUMBER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            throw new ParsingException(st.lineno(), expect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                       "number "+String.valueOf(st.nval));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        case StreamTokenizer.TT_EOF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
           throw new ParsingException("expected "+expect+", read end of file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        case StreamTokenizer.TT_WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            if (expect.equalsIgnoreCase(st.sval)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            else if (expect.equalsIgnoreCase("permission type")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                throw new ParsingException(st.lineno(), expect, st.sval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        case '"':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            if (expect.equalsIgnoreCase("quoted string")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            } else if (expect.equalsIgnoreCase("permission type")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                throw new ParsingException(st.lineno(), expect, st.sval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        case ',':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (expect.equals(","))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                throw new ParsingException(st.lineno(), expect, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            if (expect.equals("{"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                throw new ParsingException(st.lineno(), expect, "{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        case '}':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (expect.equals("}"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                throw new ParsingException(st.lineno(), expect, "}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        case ';':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            if (expect.equals(";"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                throw new ParsingException(st.lineno(), expect, ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            if (expect.equals("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                throw new ParsingException(st.lineno(), expect, "*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            throw new ParsingException(st.lineno(), expect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                               new String(new char[] {(char)lookahead}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    CryptoPermission[] getPermissions() {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   459
        Vector<CryptoPermission> result = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   461
        Enumeration<GrantEntry> grantEnum = grantEntries.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        while (grantEnum.hasMoreElements()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   463
            GrantEntry ge = grantEnum.nextElement();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   464
            Enumeration<CryptoPermissionEntry> permEnum =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   465
                    ge.permissionElements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            while (permEnum.hasMoreElements()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   467
                CryptoPermissionEntry pe = permEnum.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                if (pe.cryptoPermission.equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                                        "javax.crypto.CryptoAllPermission")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    result.addElement(CryptoAllPermission.INSTANCE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    if (pe.checkParam) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        result.addElement(new CryptoPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                                pe.alg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                                                pe.maxKeySize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                                                pe.algParamSpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                                pe.exemptionMechanism));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                        result.addElement(new CryptoPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                                pe.alg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                                                pe.maxKeySize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                                                pe.exemptionMechanism));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        CryptoPermission[] ret = new CryptoPermission[result.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        result.copyInto(ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   494
    private boolean isConsistent(String alg, String exemptionMechanism,
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   495
            Hashtable<String, Vector<String>> processedPermissions) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        String thisExemptionMechanism =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            exemptionMechanism == null ? "none" : exemptionMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (processedPermissions == null) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   500
            processedPermissions = new Hashtable<String, Vector<String>>();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   501
            Vector<String> exemptionMechanisms = new Vector<>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            exemptionMechanisms.addElement(thisExemptionMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            processedPermissions.put(alg, exemptionMechanisms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (processedPermissions.containsKey(CryptoAllPermission.ALG_NAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   511
        Vector<String> exemptionMechanisms;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (processedPermissions.containsKey(alg)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   514
            exemptionMechanisms = processedPermissions.get(alg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            if (exemptionMechanisms.contains(thisExemptionMechanism)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   519
            exemptionMechanisms = new Vector<String>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        exemptionMechanisms.addElement(thisExemptionMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        processedPermissions.put(alg, exemptionMechanisms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Each grant entry in the policy configuration file is  represented by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * GrantEntry object.  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * For example, the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *      grant {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *       permission javax.crypto.CryptoPermission "DES", 56;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *      };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * is represented internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * pe = new CryptoPermissionEntry("javax.crypto.CryptoPermission",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *                           "DES", 56);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * ge = new GrantEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * ge.add(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @see javax.crypto.CryptoPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @see javax.crypto.CryptoPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    private static class GrantEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   558
        private Vector<CryptoPermissionEntry> permissionEntries;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        GrantEntry() {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   561
            permissionEntries = new Vector<CryptoPermissionEntry>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        void add(CryptoPermissionEntry pe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            permissionEntries.addElement(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        boolean remove(CryptoPermissionEntry pe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            return permissionEntries.removeElement(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        boolean contains(CryptoPermissionEntry pe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            return permissionEntries.contains(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         * Enumerate all the permission entries in this GrantEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
         */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   582
        Enumeration<CryptoPermissionEntry> permissionElements(){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            return permissionEntries.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * Each crypto permission entry in the policy configuration file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * represented by a CryptoPermissionEntry object.  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * For example, the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *     permission javax.crypto.CryptoPermission "DES", 56;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * is represented internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * pe = new CryptoPermissionEntry("javax.crypto.cryptoPermission",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *                           "DES", 56);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @see javax.crypto.CryptoPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @see javax.crypto.CryptoAllPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    private static class CryptoPermissionEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        String cryptoPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        String alg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        String exemptionMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        int maxKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        boolean checkParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        AlgorithmParameterSpec algParamSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        CryptoPermissionEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            // Set default values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            maxKeySize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            alg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            exemptionMechanism = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            checkParam = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            algParamSpec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * Calculates a hash code value for the object.  Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         * which are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            int retval = cryptoPermission.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            if (alg != null) retval ^= alg.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            if (exemptionMechanism != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                retval ^= exemptionMechanism.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            retval ^= maxKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            if (checkParam) retval ^= 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            if (algParamSpec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                retval ^= algParamSpec.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (!(obj instanceof CryptoPermissionEntry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            CryptoPermissionEntry that = (CryptoPermissionEntry) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            if (this.cryptoPermission == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                if (that.cryptoPermission != null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                if (!this.cryptoPermission.equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                                 that.cryptoPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            if (this.alg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                if (that.alg != null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                if (!this.alg.equalsIgnoreCase(that.alg))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            if (!(this.maxKeySize == that.maxKeySize)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            if (this.checkParam != that.checkParam) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            if (this.algParamSpec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                if (that.algParamSpec != null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                if (!this.algParamSpec.equals(that.algParamSpec))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            // everything matched -- the 2 objects are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    static final class ParsingException extends GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        private static final long serialVersionUID = 7147241245566588374L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
         * Constructs a ParsingException with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
         * detail message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         * @param msg the detail message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        ParsingException(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            super(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        ParsingException(int line, String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            super("line " + line + ": " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        ParsingException(int line, String expect, String actual) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            super("line "+line+": expected '"+expect+"', found '"+actual+"'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
}