1/* 2 * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.security.sasl.digest; 27 28import java.util.Map; 29 30import javax.security.sasl.*; 31import javax.security.auth.callback.CallbackHandler; 32 33import com.sun.security.sasl.util.PolicyUtils; 34 35 36/** 37 * Client and server factory for DIGEST-MD5 SASL client/server mechanisms. 38 * See DigestMD5Client and DigestMD5Server for input requirements. 39 * 40 * @author Jonathan Bruce 41 * @author Rosanna Lee 42 */ 43 44public final class FactoryImpl implements SaslClientFactory, 45SaslServerFactory{ 46 47 private static final String[] myMechs = { "DIGEST-MD5" }; 48 private static final int DIGEST_MD5 = 0; 49 private static final int[] mechPolicies = { 50 PolicyUtils.NOPLAINTEXT|PolicyUtils.NOANONYMOUS}; 51 52 /** 53 * Empty constructor. 54 */ 55 public FactoryImpl() { 56 } 57 58 /** 59 * Returns a new instance of the DIGEST-MD5 SASL client mechanism. 60 * 61 * @throws SaslException If there is an error creating the DigestMD5 62 * SASL client. 63 * @return a new SaslClient; otherwise null if unsuccessful. 64 */ 65 public SaslClient createSaslClient(String[] mechs, 66 String authorizationId, String protocol, String serverName, 67 Map<String,?> props, CallbackHandler cbh) 68 throws SaslException { 69 70 for (int i=0; i<mechs.length; i++) { 71 if (mechs[i].equals(myMechs[DIGEST_MD5]) && 72 PolicyUtils.checkPolicy(mechPolicies[DIGEST_MD5], props)) { 73 74 if (cbh == null) { 75 throw new SaslException( 76 "Callback handler with support for RealmChoiceCallback, " + 77 "RealmCallback, NameCallback, and PasswordCallback " + 78 "required"); 79 } 80 81 return new DigestMD5Client(authorizationId, 82 protocol, serverName, props, cbh); 83 } 84 } 85 return null; 86 } 87 88 /** 89 * Returns a new instance of the DIGEST-MD5 SASL server mechanism. 90 * 91 * @throws SaslException If there is an error creating the DigestMD5 92 * SASL server. 93 * @return a new SaslServer; otherwise null if unsuccessful. 94 */ 95 public SaslServer createSaslServer(String mech, 96 String protocol, String serverName, Map<String,?> props, CallbackHandler cbh) 97 throws SaslException { 98 99 if (mech.equals(myMechs[DIGEST_MD5]) && 100 PolicyUtils.checkPolicy(mechPolicies[DIGEST_MD5], props)) { 101 102 if (cbh == null) { 103 throw new SaslException( 104 "Callback handler with support for AuthorizeCallback, "+ 105 "RealmCallback, NameCallback, and PasswordCallback " + 106 "required"); 107 } 108 109 return new DigestMD5Server(protocol, serverName, props, cbh); 110 } 111 return null; 112 } 113 114 /** 115 * Returns the authentication mechanisms that this factory can produce. 116 * 117 * @return String[] {"DigestMD5"} if policies in env match those of this 118 * factory. 119 */ 120 public String[] getMechanismNames(Map<String,?> env) { 121 return PolicyUtils.filterMechs(myMechs, mechPolicies, env); 122 } 123} 124