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