jdk/src/share/classes/com/sun/security/sasl/util/AbstractSaslImpl.java
author prr
Tue, 15 Jul 2014 11:22:14 -0700
changeset 25522 10d789df41bb
parent 25187 08aff438def8
permissions -rw-r--r--
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes Reviewed-by: psandoz, prr Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18793
diff changeset
     2
 * Copyright (c) 2000, 2013, 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: 3472
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: 3472
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: 3472
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3472
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3472
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The base class used by client and server implementations of SASL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * mechanisms to process properties passed in the props argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * and strings with the same format (e.g., used in digest-md5).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Also contains utilities for doing int to network-byte-order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * transformations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public abstract class AbstractSaslImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    protected boolean completed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    protected boolean privacy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    protected boolean integrity = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    protected byte[] qop;           // ordered list of qops
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected byte allQop;          // a mask indicating which QOPs are requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    protected byte[] strength;      // ordered list of cipher strengths
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // These are relevant only when privacy or integray have been negotiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected int sendMaxBufSize = 0;     // specified by peer but can override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected int recvMaxBufSize = 65536; // optionally specified by self
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected int rawSendSize;            // derived from sendMaxBufSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected String myClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    64
    protected AbstractSaslImpl(Map<String, ?> props, String className)
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    65
            throws SaslException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        myClassName = className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // Parse properties  to set desired context options
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (props != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            String prop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            // "auth", "auth-int", "auth-conf"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            qop = parseQop(prop=(String)props.get(Sasl.QOP));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            logger.logp(Level.FINE, myClassName, "constructor",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                "SASLIMPL01:Preferred qop property: {0}", prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            allQop = combineMasks(qop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (logger.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                logger.logp(Level.FINE, myClassName, "constructor",
25187
08aff438def8 8048874: Replace uses of 'new Byte', 'new Short' and 'new Character' with appropriate alternative across core classes
prappo
parents: 24969
diff changeset
    80
                    "SASLIMPL02:Preferred qop mask: {0}", allQop);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                if (qop.length > 0) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
    83
                    StringBuilder str = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    for (int i = 0; i < qop.length; i++) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
    85
                        str.append(Byte.toString(qop[i]));
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
    86
                        str.append(' ');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    logger.logp(Level.FINE, myClassName, "constructor",
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
    89
                            "SASLIMPL03:Preferred qops : {0}", str.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // "low", "medium", "high"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            strength = parseStrength(prop=(String)props.get(Sasl.STRENGTH));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            logger.logp(Level.FINE, myClassName, "constructor",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                "SASLIMPL04:Preferred strength property: {0}", prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (logger.isLoggable(Level.FINE) && strength.length > 0) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
    98
                StringBuilder str = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                for (int i = 0; i < strength.length; i++) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   100
                    str.append(Byte.toString(strength[i]));
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   101
                    str.append(' ');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                logger.logp(Level.FINE, myClassName, "constructor",
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   104
                        "SASLIMPL05:Cipher strengths: {0}", str.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            // Max receive buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            prop = (String)props.get(Sasl.MAX_BUFFER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (prop != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    logger.logp(Level.FINE, myClassName, "constructor",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        "SASLIMPL06:Max receive buffer size: {0}", prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    recvMaxBufSize = Integer.parseInt(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                "Property must be string representation of integer: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        Sasl.MAX_BUFFER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // Max send buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            prop = (String)props.get(MAX_SEND_BUF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (prop != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    logger.logp(Level.FINE, myClassName, "constructor",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                        "SASLIMPL07:Max send buffer size: {0}", prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    sendMaxBufSize = Integer.parseInt(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                "Property must be string representation of integer: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        MAX_SEND_BUF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            qop = DEFAULT_QOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            allQop = NO_PROTECTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            strength = STRENGTH_MASKS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Determines whether this mechanism has completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return true if has completed; false otherwise;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public boolean isComplete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return completed;
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
     * Retrieves the negotiated property.
14340
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   152
     * @exception IllegalStateException if this authentication exchange has
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
   153
     * not completed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public Object getNegotiatedProperty(String propName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (!completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new IllegalStateException("SASL authentication not completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   159
        switch (propName) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   160
            case Sasl.QOP:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   161
                if (privacy) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   162
                    return "auth-conf";
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   163
                } else if (integrity) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   164
                    return "auth-int";
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   165
                } else {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   166
                    return "auth";
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   167
                }
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   168
            case Sasl.MAX_BUFFER:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   169
                return Integer.toString(recvMaxBufSize);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   170
            case Sasl.RAW_SEND_SIZE:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   171
                return Integer.toString(rawSendSize);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   172
            case MAX_SEND_BUF:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   173
                return Integer.toString(sendMaxBufSize);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   174
            default:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   175
                return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    protected static final byte combineMasks(byte[] in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        byte answer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        for (int i = 0; i < in.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            answer |= in[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    protected static final byte findPreferredMask(byte pref, byte[] in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        for (int i = 0; i < in.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if ((in[i]&pref) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                return in[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return (byte)0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private static final byte[] parseQop(String qop) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return parseQop(qop, null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    protected static final byte[] parseQop(String qop, String[] saveTokens,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        boolean ignore) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (qop == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return DEFAULT_QOP;   // default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return parseProp(Sasl.QOP, qop, QOP_TOKENS, QOP_MASKS, saveTokens, ignore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private static final byte[] parseStrength(String strength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (strength == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return DEFAULT_STRENGTH;   // default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return parseProp(Sasl.STRENGTH, strength, STRENGTH_TOKENS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            STRENGTH_MASKS, null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private static final byte[] parseProp(String propName, String propVal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        String[] vals, byte[] masks, String[] tokens, boolean ignore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        StringTokenizer parser = new StringTokenizer(propVal, ", \t\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        String token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        byte[] answer = new byte[vals.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        boolean found;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        while (parser.hasMoreTokens() && i < answer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            token = parser.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            for (int j = 0; !found && j < vals.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                if (token.equalsIgnoreCase(vals[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    answer[i++] = masks[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    if (tokens != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        tokens[j] = token;    // save what was parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (!found && !ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    "Invalid token in " + propName + ": " + propVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // Initialize rest of array with 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        for (int j = i; j < answer.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            answer[j] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
18793
4d9455e24050 8019267: NPE in AbstractSaslImpl when trace level >= FINER in KRB5
weijun
parents: 14342
diff changeset
   255
     * Outputs a byte array. Can be null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    protected static final void traceOutput(String srcClass, String srcMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        String traceTag, byte[] output) {
18793
4d9455e24050 8019267: NPE in AbstractSaslImpl when trace level >= FINER in KRB5
weijun
parents: 14342
diff changeset
   259
        traceOutput(srcClass, srcMethod, traceTag, output, 0,
4d9455e24050 8019267: NPE in AbstractSaslImpl when trace level >= FINER in KRB5
weijun
parents: 14342
diff changeset
   260
                output == null ? 0 : output.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    protected static final void traceOutput(String srcClass, String srcMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        String traceTag, byte[] output, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            int origlen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            Level lev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (!logger.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                len = Math.min(16, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                lev = Level.FINER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                lev = Level.FINEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
18793
4d9455e24050 8019267: NPE in AbstractSaslImpl when trace level >= FINER in KRB5
weijun
parents: 14342
diff changeset
   276
            String content;
4d9455e24050 8019267: NPE in AbstractSaslImpl when trace level >= FINER in KRB5
weijun
parents: 14342
diff changeset
   277
4d9455e24050 8019267: NPE in AbstractSaslImpl when trace level >= FINER in KRB5
weijun
parents: 14342
diff changeset
   278
            if (output != null) {
4d9455e24050 8019267: NPE in AbstractSaslImpl when trace level >= FINER in KRB5
weijun
parents: 14342
diff changeset
   279
                ByteArrayOutputStream out = new ByteArrayOutputStream(len);
4d9455e24050 8019267: NPE in AbstractSaslImpl when trace level >= FINER in KRB5
weijun
parents: 14342
diff changeset
   280
                new HexDumpEncoder().encodeBuffer(
4d9455e24050 8019267: NPE in AbstractSaslImpl when trace level >= FINER in KRB5
weijun
parents: 14342
diff changeset
   281
                    new ByteArrayInputStream(output, offset, len), out);
4d9455e24050 8019267: NPE in AbstractSaslImpl when trace level >= FINER in KRB5
weijun
parents: 14342
diff changeset
   282
                content = out.toString();
4d9455e24050 8019267: NPE in AbstractSaslImpl when trace level >= FINER in KRB5
weijun
parents: 14342
diff changeset
   283
            } else {
4d9455e24050 8019267: NPE in AbstractSaslImpl when trace level >= FINER in KRB5
weijun
parents: 14342
diff changeset
   284
                content = "NULL";
4d9455e24050 8019267: NPE in AbstractSaslImpl when trace level >= FINER in KRB5
weijun
parents: 14342
diff changeset
   285
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // Message id supplied by caller as part of traceTag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            logger.logp(lev, srcClass, srcMethod, "{0} ( {1} ): {2}",
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25187
diff changeset
   289
                new Object[] {traceTag, origlen, content});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            logger.logp(Level.WARNING, srcClass, srcMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                "SASLIMPL09:Error generating trace output: {0}", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Returns the integer represented by  4 bytes in network byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    protected static final int networkByteOrderToInt(byte[] buf, int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (count > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            throw new IllegalArgumentException("Cannot handle more than 4 bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        int answer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            answer <<= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            answer |= ((int)buf[start+i] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Encodes an integer into 4 bytes in network byte order in the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    protected static final void intToNetworkByteOrder(int num, byte[] buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        int start, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (count > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            throw new IllegalArgumentException("Cannot handle more than 4 bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        for (int i = count-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            buf[start+i] = (byte)(num & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            num >>>= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    // ---------------- Constants  -----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private static final String SASL_LOGGER_NAME = "javax.security.sasl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    protected static final String MAX_SEND_BUF = "javax.security.sasl.sendmaxbuffer";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
3472
ff6f0e6f7c83 6657695: AbstractSaslImpl.logger is a static mutable (findbugs)
vinnie
parents: 2
diff changeset
   335
    /**
ff6f0e6f7c83 6657695: AbstractSaslImpl.logger is a static mutable (findbugs)
vinnie
parents: 2
diff changeset
   336
     * Logger for debug messages
ff6f0e6f7c83 6657695: AbstractSaslImpl.logger is a static mutable (findbugs)
vinnie
parents: 2
diff changeset
   337
     */
ff6f0e6f7c83 6657695: AbstractSaslImpl.logger is a static mutable (findbugs)
vinnie
parents: 2
diff changeset
   338
    protected static final Logger logger = Logger.getLogger(SASL_LOGGER_NAME);
ff6f0e6f7c83 6657695: AbstractSaslImpl.logger is a static mutable (findbugs)
vinnie
parents: 2
diff changeset
   339
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    // default 0 (no protection); 1 (integrity only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    protected static final byte NO_PROTECTION = (byte)1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    protected static final byte INTEGRITY_ONLY_PROTECTION = (byte)2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    protected static final byte PRIVACY_PROTECTION = (byte)4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    protected static final byte LOW_STRENGTH = (byte)1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    protected static final byte MEDIUM_STRENGTH = (byte)2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    protected static final byte HIGH_STRENGTH = (byte)4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private static final byte[] DEFAULT_QOP = new byte[]{NO_PROTECTION};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private static final String[] QOP_TOKENS = {"auth-conf",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                       "auth-int",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                       "auth"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private static final byte[] QOP_MASKS = {PRIVACY_PROTECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                     INTEGRITY_ONLY_PROTECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                     NO_PROTECTION};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private static final byte[] DEFAULT_STRENGTH = new byte[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        HIGH_STRENGTH, MEDIUM_STRENGTH, LOW_STRENGTH};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    private static final String[] STRENGTH_TOKENS = {"low",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                                     "medium",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                                     "high"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private static final byte[] STRENGTH_MASKS = {LOW_STRENGTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                                  MEDIUM_STRENGTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                                  HIGH_STRENGTH};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
}