1 /*
   2  * Copyright (c) 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.gsskerb;
  26 
  27 import java.security.AccessController;
  28 import java.security.PrivilegedAction;
  29 import java.security.Provider;
  30 import java.security.NoSuchAlgorithmException;
  31 import java.security.InvalidParameterException;
  32 import java.security.ProviderException;
  33 
  34 /**
  35  * The JdkSASL provider class -
  36  * provides client and server support for GSSAPI/Kerberos v5
  37  */
  38 
  39 public final class JdkSASL extends Provider {
  40 
  41     private static final long serialVersionUID = 8622590901641830849L;
  42 
  43     private static final String info = "JDK SASL provider" +
  44         "(implements client and server mechanisms for GSSAPI)";
  45 
  46     private static final class ProviderService extends Provider.Service {
  47 
  48         ProviderService(Provider p, String type, String algo, String cn) {
  49             super(p, type, algo, cn, null, null);
  50         }
  51 
  52         @Override
  53         public Object newInstance(Object ctrParamObj)
  54             throws NoSuchAlgorithmException {
  55             String type = getType();
  56             if (ctrParamObj != null) {
  57                 throw new InvalidParameterException
  58                     ("constructorParameter not used with " + type + " engines");
  59             }
  60             String algo = getAlgorithm();
  61             // GSSAPI uses same impl class for client and server
  62             try {
  63                 if (algo.equals("GSSAPI")) {
  64                     return new com.sun.security.sasl.gsskerb.FactoryImpl();
  65                 }
  66             } catch (Exception ex) {
  67                 throw new NoSuchAlgorithmException("Error constructing " +
  68                      type + " for " + algo + " using JdkSASL", ex);
  69             }
  70             throw new ProviderException("No impl for " + algo +
  71                 " " + type);
  72         }
  73     }
  74 
  75     public JdkSASL() {
  76         super("JdkSASL", 1.9d, info);
  77 
  78         final Provider p = this;
  79         AccessController.doPrivileged(new PrivilegedAction<Void>() {
  80             public Void run() {
  81                 putService(new ProviderService(p, "SaslClientFactory",
  82                            "GSSAPI", "com.sun.security.sasl.gsskerb.FactoryImpl"));
  83                 putService(new ProviderService(p, "SaslServerFactory",
  84                            "GSSAPI", "com.sun.security.sasl.gsskerb.FactoryImpl"));
  85                 return null;
  86             }
  87         });
  88     }
  89 }