1/*
2 * Copyright (c) 2005, 2014, 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 sun.security.jgss.wrapper;
27
28import java.util.Hashtable;
29import org.ietf.jgss.Oid;
30import org.ietf.jgss.GSSName;
31import org.ietf.jgss.ChannelBinding;
32import org.ietf.jgss.MessageProp;
33import org.ietf.jgss.GSSException;
34import sun.security.jgss.GSSUtil;
35
36/**
37 * This class is essentially a JNI calling stub for all wrapper classes.
38 *
39 * @author Valerie Peng
40 * @since 1.6
41 */
42
43class GSSLibStub {
44
45    private Oid mech;
46    private long pMech;
47
48    /**
49     * Initialization routine to dynamically load function pointers.
50     *
51     * @param lib library name to dlopen
52     * @param debug set to true for reporting native debugging info
53     * @return true if succeeded, false otherwise.
54     */
55    static native boolean init(String lib, boolean debug);
56    private static native long getMechPtr(byte[] oidDerEncoding);
57
58    // Miscellaneous routines
59    static native Oid[] indicateMechs();
60    native Oid[] inquireNamesForMech() throws GSSException;
61
62    // Name related routines
63    native void releaseName(long pName);
64    native long importName(byte[] name, Oid type);
65    native boolean compareName(long pName1, long pName2);
66    native long canonicalizeName(long pName);
67    native byte[] exportName(long pName) throws GSSException;
68    native Object[] displayName(long pName) throws GSSException;
69
70    // Credential related routines
71    native long acquireCred(long pName, int lifetime, int usage)
72                                        throws GSSException;
73    native long releaseCred(long pCred);
74    native long getCredName(long pCred);
75    native int getCredTime(long pCred);
76    native int getCredUsage(long pCred);
77
78    // Context related routines
79    native NativeGSSContext importContext(byte[] interProcToken);
80    native byte[] initContext(long pCred, long targetName, ChannelBinding cb,
81                              byte[] inToken, NativeGSSContext context);
82    native byte[] acceptContext(long pCred, ChannelBinding cb,
83                                byte[] inToken, NativeGSSContext context);
84    native long[] inquireContext(long pContext);
85    native Oid getContextMech(long pContext);
86    native long getContextName(long pContext, boolean isSrc);
87    native int getContextTime(long pContext);
88    native long deleteContext(long pContext);
89    native int wrapSizeLimit(long pContext, int flags, int qop, int outSize);
90    native byte[] exportContext(long pContext);
91    native byte[] getMic(long pContext, int qop, byte[] msg);
92    native void verifyMic(long pContext, byte[] token, byte[] msg,
93                          MessageProp prop) ;
94    native byte[] wrap(long pContext, byte[] msg, MessageProp prop);
95    native byte[] unwrap(long pContext, byte[] msgToken, MessageProp prop);
96
97    private static Hashtable<Oid, GSSLibStub>
98        table = new Hashtable<Oid, GSSLibStub>(5);
99
100    static GSSLibStub getInstance(Oid mech) throws GSSException {
101        GSSLibStub s = table.get(mech);
102        if (s == null) {
103            s = new GSSLibStub(mech);
104            table.put(mech, s);
105        }
106        return s;
107    }
108    private GSSLibStub(Oid mech) throws GSSException {
109        SunNativeProvider.debug("Created GSSLibStub for mech " + mech);
110        this.mech = mech;
111        this.pMech = getMechPtr(mech.getDER());
112    }
113    public boolean equals(Object obj) {
114        if (obj == this) return true;
115        if (!(obj instanceof GSSLibStub)) {
116            return false;
117        }
118        return (mech.equals(((GSSLibStub) obj).getMech()));
119    }
120    public int hashCode() {
121        return mech.hashCode();
122    }
123    Oid getMech() {
124        return mech;
125    }
126}
127