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 
26 package sun.security.jgss.wrapper;
27 
28 import java.util.Hashtable;
29 import org.ietf.jgss.Oid;
30 import org.ietf.jgss.GSSName;
31 import org.ietf.jgss.ChannelBinding;
32 import org.ietf.jgss.MessageProp;
33 import org.ietf.jgss.GSSException;
34 import 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 
43 class 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      */
init(String lib, boolean debug)55     static native boolean init(String lib, boolean debug);
getMechPtr(byte[] oidDerEncoding)56     private static native long getMechPtr(byte[] oidDerEncoding);
57 
58     // Miscellaneous routines
indicateMechs()59     static native Oid[] indicateMechs();
inquireNamesForMech()60     native Oid[] inquireNamesForMech() throws GSSException;
61 
62     // Name related routines
releaseName(long pName)63     native void releaseName(long pName);
importName(byte[] name, Oid type)64     native long importName(byte[] name, Oid type);
compareName(long pName1, long pName2)65     native boolean compareName(long pName1, long pName2);
canonicalizeName(long pName)66     native long canonicalizeName(long pName);
exportName(long pName)67     native byte[] exportName(long pName) throws GSSException;
displayName(long pName)68     native Object[] displayName(long pName) throws GSSException;
69 
70     // Credential related routines
acquireCred(long pName, int lifetime, int usage)71     native long acquireCred(long pName, int lifetime, int usage)
72                                         throws GSSException;
releaseCred(long pCred)73     native long releaseCred(long pCred);
getCredName(long pCred)74     native long getCredName(long pCred);
getCredTime(long pCred)75     native int getCredTime(long pCred);
getCredUsage(long pCred)76     native int getCredUsage(long pCred);
77 
78     // Context related routines
importContext(byte[] interProcToken)79     native NativeGSSContext importContext(byte[] interProcToken);
initContext(long pCred, long targetName, ChannelBinding cb, byte[] inToken, NativeGSSContext context)80     native byte[] initContext(long pCred, long targetName, ChannelBinding cb,
81                               byte[] inToken, NativeGSSContext context);
acceptContext(long pCred, ChannelBinding cb, byte[] inToken, NativeGSSContext context)82     native byte[] acceptContext(long pCred, ChannelBinding cb,
83                                 byte[] inToken, NativeGSSContext context);
inquireContext(long pContext)84     native long[] inquireContext(long pContext);
getContextMech(long pContext)85     native Oid getContextMech(long pContext);
getContextName(long pContext, boolean isSrc)86     native long getContextName(long pContext, boolean isSrc);
getContextTime(long pContext)87     native int getContextTime(long pContext);
deleteContext(long pContext)88     native long deleteContext(long pContext);
wrapSizeLimit(long pContext, int flags, int qop, int outSize)89     native int wrapSizeLimit(long pContext, int flags, int qop, int outSize);
exportContext(long pContext)90     native byte[] exportContext(long pContext);
getMic(long pContext, int qop, byte[] msg)91     native byte[] getMic(long pContext, int qop, byte[] msg);
verifyMic(long pContext, byte[] token, byte[] msg, MessageProp prop)92     native void verifyMic(long pContext, byte[] token, byte[] msg,
93                           MessageProp prop) ;
wrap(long pContext, byte[] msg, MessageProp prop)94     native byte[] wrap(long pContext, byte[] msg, MessageProp prop);
unwrap(long pContext, byte[] msgToken, 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 
getInstance(Oid mech)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     }
GSSLibStub(Oid mech)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     }
equals(Object obj)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     }
hashCode()120     public int hashCode() {
121         return mech.hashCode();
122     }
getMech()123     Oid getMech() {
124         return mech;
125     }
126 }
127