src/java.security.sasl/share/classes/com/sun/security/sasl/ntlm/FactoryImpl.java
author prr
Mon, 13 Nov 2017 12:10:49 -0800
changeset 47832 4182b3b158e0
parent 47216 71c04702a3d5
permissions -rw-r--r--
8181139: Memory leak in awt_Font.cpp / AwtFont::Create Reviewed-by: serb, ssadetsky
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10348
diff changeset
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     4
 *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    10
 *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    15
 * accompanied this code).
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    16
 *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    20
 *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    23
 * questions.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    24
 */
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    25
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    26
package com.sun.security.sasl.ntlm;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    27
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    28
import java.util.Map;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    29
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    30
import javax.security.sasl.*;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    31
import javax.security.auth.callback.CallbackHandler;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    32
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    33
import com.sun.security.sasl.util.PolicyUtils;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    34
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    35
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    36
/**
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    37
  * Client and server factory for NTLM SASL client/server mechanisms.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    38
  * See NTLMClient and NTLMServer for input requirements.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    39
  *
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    40
  * @since 1.7
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    41
  */
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    42
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    43
public final class FactoryImpl implements SaslClientFactory,
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    44
SaslServerFactory{
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    45
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
    46
    private static final String[] myMechs = { "NTLM" };
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
    47
    private static final int[] mechPolicies = {
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    48
            PolicyUtils.NOPLAINTEXT|PolicyUtils.NOANONYMOUS
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    49
    };
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    50
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    51
    /**
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    52
      * Empty constructor.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    53
      */
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    54
    public FactoryImpl() {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    55
    }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    56
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    57
    /**
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    58
     * Returns a new instance of the NTLM SASL client mechanism.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    59
     * Argument checks are performed in SaslClient's constructor.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
    60
     * @return a new SaslClient; otherwise null if unsuccessful.
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    61
     * @throws SaslException If there is an error creating the NTLM
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    62
     * SASL client.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    63
     */
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    64
    public SaslClient createSaslClient(String[] mechs,
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    65
         String authorizationId, String protocol, String serverName,
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    66
         Map<String,?> props, CallbackHandler cbh)
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    67
         throws SaslException {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    68
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    69
         for (int i=0; i<mechs.length; i++) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    70
            if (mechs[i].equals("NTLM") &&
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    71
                    PolicyUtils.checkPolicy(mechPolicies[0], props)) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    72
10348
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 6517
diff changeset
    73
                if (cbh == null) {
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 6517
diff changeset
    74
                    throw new SaslException(
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 6517
diff changeset
    75
                        "Callback handler with support for " +
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 6517
diff changeset
    76
                        "RealmCallback, NameCallback, and PasswordCallback " +
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 6517
diff changeset
    77
                        "required");
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 6517
diff changeset
    78
                }
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    79
                return new NTLMClient(mechs[i], authorizationId,
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    80
                    protocol, serverName, props, cbh);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    81
            }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    82
        }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    83
        return null;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    84
    }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    85
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    86
    /**
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    87
     * Returns a new instance of the NTLM SASL server mechanism.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    88
     * Argument checks are performed in SaslServer's constructor.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
    89
     * @return a new SaslServer; otherwise null if unsuccessful.
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    90
     * @throws SaslException If there is an error creating the NTLM
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    91
     * SASL server.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    92
     */
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    93
    public SaslServer createSaslServer(String mech,
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    94
         String protocol, String serverName, Map<String,?> props, CallbackHandler cbh)
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    95
         throws SaslException {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    96
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    97
         if (mech.equals("NTLM") &&
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    98
                 PolicyUtils.checkPolicy(mechPolicies[0], props)) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
    99
             if (props != null) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   100
                 String qop = (String)props.get(Sasl.QOP);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   101
                 if (qop != null && !qop.equals("auth")) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   102
                     throw new SaslException("NTLM only support auth");
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   103
                 }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   104
             }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   105
             if (cbh == null) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   106
                 throw new SaslException(
10348
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 6517
diff changeset
   107
                     "Callback handler with support for " +
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 6517
diff changeset
   108
                     "RealmCallback, NameCallback, and PasswordCallback " +
7d1a82029332 7043847: NTML impl of SaslServer throws UnsupportedOperationException from (un)wrap method
weijun
parents: 6517
diff changeset
   109
                     "required");
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   110
             }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   111
             return new NTLMServer(mech, protocol, serverName, props, cbh);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   112
         }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   113
         return null;
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   114
    }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   115
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   116
    /**
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   117
      * Returns the authentication mechanisms that this factory can produce.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   118
      *
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
   119
      * @return String[] {"NTLM"} if policies in env match those of this
6517
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   120
      * factory.
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   121
      */
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   122
    public String[] getMechanismNames(Map<String,?> env) {
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   123
        return PolicyUtils.filterMechs(myMechs, mechPolicies, env);
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   124
    }
151856936fd8 6911951: NTLM should be a supported Java SASL mechanism
weijun
parents:
diff changeset
   125
}