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