1 /*
   2  * Copyright (c) 2003, 2015, 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 package com.sun.security.sasl;
  26 
  27 import java.security.AccessController;
  28 import java.security.PrivilegedAction;
  29 import java.security.NoSuchAlgorithmException;
  30 import java.security.InvalidParameterException;
  31 import java.security.ProviderException;
  32 
  33 /**
  34  * The SASL provider.
  35  * Provides client support for
  36  * - EXTERNAL
  37  * - PLAIN
  38  * - CRAM-MD5
  39  * - DIGEST-MD5
  40  * - NTLM
  41  * And server support for
  42  * - CRAM-MD5
  43  * - DIGEST-MD5
  44  * - NTLM
  45  */
  46 
  47 public final class Provider extends java.security.Provider {
  48 
  49     private static final long serialVersionUID = 8622598936488630849L;
  50 
  51     private static final String info = "Sun SASL provider" +
  52         "(implements client mechanisms for: " +
  53         "DIGEST-MD5, EXTERNAL, PLAIN, CRAM-MD5, NTLM;" +
  54         " server mechanisms for: DIGEST-MD5, CRAM-MD5, NTLM)";
  55 
  56     private static final class ProviderService
  57         extends java.security.Provider.Service {
  58         ProviderService(java.security.Provider p, String type, String algo,
  59             String cn) {
  60             super(p, type, algo, cn, null, null);
  61         }
  62 
  63         @Override
  64         public Object newInstance(Object ctrParamObj)
  65             throws NoSuchAlgorithmException {
  66             String type = getType();
  67             if (ctrParamObj != null) {
  68                 throw new InvalidParameterException
  69                     ("constructorParameter not used with " + type + " engines");
  70             }
  71 
  72             String algo = getAlgorithm();
  73             try {
  74                 // DIGEST-MD5, NTLM uses same impl class for client and server
  75                 if (algo.equals("DIGEST-MD5")) {
  76                     return new com.sun.security.sasl.digest.FactoryImpl();
  77                 }
  78                 if (algo.equals("NTLM")) {
  79                     return new com.sun.security.sasl.ntlm.FactoryImpl();
  80                 }
  81                 if (type.equals("SaslClientFactory")) {
  82                     if (algo.equals("EXTERNAL") || algo.equals("PLAIN") ||
  83                         algo.equals("CRAM-MD5")) {
  84                         return new com.sun.security.sasl.ClientFactoryImpl();
  85                     }
  86                 } else if (type.equals("SaslServerFactory")) {
  87                     if (algo.equals("CRAM-MD5")) {
  88                         return new com.sun.security.sasl.ServerFactoryImpl();
  89                     }
  90                 }
  91             } catch (Exception ex) {
  92                 throw new NoSuchAlgorithmException("Error constructing " +
  93                     type + " for " + algo + " using SunSASL", ex);
  94             }
  95             throw new ProviderException("No impl for " + algo +
  96                 " " + type);
  97         }
  98     }
  99 
 100     public Provider() {
 101         super("SunSASL", 1.9d, info);
 102 
 103         final Provider p = this;
 104         AccessController.doPrivileged(new PrivilegedAction<Void>() {
 105             public Void run() {
 106                 // Client mechanisms
 107                 putService(new ProviderService(p, "SaslClientFactory",
 108                            "DIGEST-MD5", "com.sun.security.sasl.digest.FactoryImpl"));
 109                 putService(new ProviderService(p, "SaslClientFactory",
 110                            "NTLM", "com.sun.security.sasl.ntlm.FactoryImpl"));
 111                 putService(new ProviderService(p, "SaslClientFactory",
 112                            "EXTERNAL", "com.sun.security.sasl.ClientFactoryImpl"));
 113                 putService(new ProviderService(p, "SaslClientFactory",
 114                            "PLAIN", "com.sun.security.sasl.ClientFactoryImpl"));
 115                 putService(new ProviderService(p, "SaslClientFactory",
 116                            "CRAM-MD5", "com.sun.security.sasl.ClientFactoryImpl"));
 117 
 118                 // Server mechanisms
 119                 putService(new ProviderService(p, "SaslServerFactory",
 120                            "CRAM-MD5", "com.sun.security.sasl.ServerFactoryImpl"));
 121                 putService(new ProviderService(p, "SaslServerFactory",
 122                            "DIGEST-MD5", "com.sun.security.sasl.digest.FactoryImpl"));
 123                 putService(new ProviderService(p, "SaslServerFactory",
 124                            "NTLM", "com.sun.security.sasl.ntlm.FactoryImpl"));
 125                 return null;
 126             }
 127         });
 128     }
 129 }