2 * Copyright (c) 2001-2002 International Business Machines Corp.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
9 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
12 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
13 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
14 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
15 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
16 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
17 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
18 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 #include "org_openafs_jafs_Key.h"
25 #include <afs_bosAdmin.h>
26 #include <afs_AdminCommonErrors.h>
29 //// definitions in Internal.c //////////////////
31 extern jfieldID key_versionField
;
32 extern jfieldID key_encryptionKeyField
;
33 extern jfieldID key_lastModDateField
;
34 extern jfieldID key_lastModMsField
;
35 extern jfieldID key_checkSumField
;
37 //////////////////////////////////////////////////////////////////
40 * Extract the information from the given key entry and populate the
43 * env the Java environment
44 * key the Key object to populate with the info
45 * keyEntry the container of the key's information
47 void fillKeyInfo( JNIEnv
*env
, jobject key
, bos_KeyInfo_t keyEntry
)
49 jstring jencryptionKey
;
53 // get the class fields if need be
55 internal_getKeyClass( env
, key
);
59 (*env
)->SetIntField( env
, key
, key_versionField
, keyEntry
.keyVersionNumber
);
61 convertedKey
= malloc( sizeof(char *) * (sizeof(keyEntry
.key
.key
)*4+1) );
63 throwAFSException( env
, JAFSADMNOMEM
);
66 for( i
= 0; i
< sizeof(keyEntry
.key
.key
); i
++ ) {
67 sprintf( &(convertedKey
[i
*4]), "\\%0.3o", keyEntry
.key
.key
[i
] );
69 jencryptionKey
= (*env
)->NewStringUTF(env
, convertedKey
);
70 (*env
)->SetObjectField( env
, key
, key_encryptionKeyField
, jencryptionKey
);
72 (*env
)->SetIntField( env
, key
, key_lastModDateField
,
73 keyEntry
.keyStatus
.lastModificationDate
);
74 (*env
)->SetIntField( env
, key
, key_lastModMsField
,
75 keyEntry
.keyStatus
.lastModificationMicroSeconds
);
76 (*env
)->SetLongField( env
, key
, key_checkSumField
,
77 (unsigned int) keyEntry
.keyStatus
.checkSum
);
83 * Fills in the information fields of the provided Key.
85 * env the Java environment
86 * cls the current Java class
87 * serverHandle the bos handle of the server to which the key
89 * version the version of the key for which to get the information
90 * key the Key object in which to fill in the
93 JNIEXPORT
void JNICALL
94 Java_org_openafs_jafs_Key_getKeyInfo
95 (JNIEnv
*env
, jclass cls
, jlong serverHandle
, jint version
, jobject key
)
98 bos_KeyInfo_t keyEntry
;
102 if( !bos_KeyGetBegin( (void *) serverHandle
, &iterationId
, &ast
) ) {
103 throwAFSException( env
, ast
);
109 // there's no KeyGet function, so we must iterate and find the
110 // one with the matching version
113 if( !bos_KeyGetNext( iterationId
, &keyEntry
, &ast
) ) {
115 if( ast
== ADMITERATORDONE
) {
117 if( !bos_KeyGetDone( iterationId
, &astnew
) ) {
118 throwAFSException( env
, astnew
);
121 throwAFSException( env
, KAUNKNOWNKEY
);
125 throwAFSException( env
, ast
);
130 if( keyEntry
.keyVersionNumber
== version
) {
136 fillKeyInfo( env
, key
, keyEntry
);
138 if( !bos_KeyGetDone( iterationId
, &ast
) ) {
139 throwAFSException( env
, ast
);
146 * Create a server key.
148 * env the Java environment
149 * cls the current Java class
150 * cellHandle the handle of the cell to which the server belongs
151 * serverHandle the bos handle of the server to which the key will
153 * versionNumber the version number of the key to create (0 to 255)
154 * jkeyString the String version of the key that will
157 JNIEXPORT
void JNICALL
158 Java_org_openafs_jafs_Key_create
159 (JNIEnv
*env
, jclass cls
, jlong cellHandle
, jlong serverHandle
, jint version
,
163 const char *keyString
;
165 kas_encryptionKey_p key
=
166 (kas_encryptionKey_p
) malloc( sizeof(kas_encryptionKey_t
) );
169 throwAFSException( env
, JAFSADMNOMEM
);
173 if( jkeyString
!= NULL
) {
174 keyString
= (*env
)->GetStringUTFChars(env
, jkeyString
, 0);
176 throwAFSException( env
, JAFSADMNOMEM
);
183 if( !afsclient_CellNameGet( (void *) cellHandle
, &cellName
, &ast
) ) {
185 if( keyString
!= NULL
) {
186 (*env
)->ReleaseStringUTFChars(env
, jkeyString
, keyString
);
188 throwAFSException( env
, ast
);
192 if( !kas_StringToKey( cellName
, keyString
, key
, &ast
) ) {
194 if( keyString
!= NULL
) {
195 (*env
)->ReleaseStringUTFChars(env
, jkeyString
, keyString
);
197 throwAFSException( env
, ast
);
201 if( !bos_KeyCreate( (void *) serverHandle
, version
, key
, &ast
) ) {
203 if( keyString
!= NULL
) {
204 (*env
)->ReleaseStringUTFChars(env
, jkeyString
, keyString
);
206 throwAFSException( env
, ast
);
211 if( keyString
!= NULL
) {
212 (*env
)->ReleaseStringUTFChars(env
, jkeyString
, keyString
);
217 * Delete a server key.
219 * env the Java environment
220 * cls the current Java class
221 * serverHandle the bos handle of the server to which the key belongs
222 * versionNumber the version number of the key to remove (0 to 255)
224 JNIEXPORT
void JNICALL
225 Java_org_openafs_jafs_Key_delete
226 (JNIEnv
*env
, jclass cls
, jlong serverHandle
, jint version
)
230 if( !bos_KeyDelete( (void *) serverHandle
, version
, &ast
) ) {
231 throwAFSException( env
, ast
);
236 // reclaim global memory being used by this portion
237 JNIEXPORT
void JNICALL
238 Java_org_openafs_jafs_Key_reclaimKeyMemory (JNIEnv
*env
, jclass cls
)
241 (*env
)->DeleteGlobalRef(env
, keyCls
);