Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / JAVA / libjafs / Key.c
CommitLineData
805e021f
CE
1/*
2 * Copyright (c) 2001-2002 International Business Machines Corp.
3 * All rights reserved.
4 *
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
8 *
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.
20 */
21
22#include "Internal.h"
23#include "org_openafs_jafs_Key.h"
24
25#include <afs_bosAdmin.h>
26#include <afs_AdminCommonErrors.h>
27#include <kautils.h>
28
29//// definitions in Internal.c //////////////////
30extern jclass keyCls;
31extern jfieldID key_versionField;
32extern jfieldID key_encryptionKeyField;
33extern jfieldID key_lastModDateField;
34extern jfieldID key_lastModMsField;
35extern jfieldID key_checkSumField;
36
37//////////////////////////////////////////////////////////////////
38
39/**
40 * Extract the information from the given key entry and populate the
41 * given object
42 *
43 * env the Java environment
44 * key the Key object to populate with the info
45 * keyEntry the container of the key's information
46 */
47void fillKeyInfo( JNIEnv *env, jobject key, bos_KeyInfo_t keyEntry )
48{
49 jstring jencryptionKey;
50 char *convertedKey;
51 int i;
52
53 // get the class fields if need be
54 if( keyCls == 0 ) {
55 internal_getKeyClass( env, key );
56 }
57
58 // set all the fields
59 (*env)->SetIntField( env, key, key_versionField, keyEntry.keyVersionNumber );
60
61 convertedKey = malloc( sizeof(char *) * (sizeof(keyEntry.key.key)*4+1) );
62 if( !convertedKey ) {
63 throwAFSException( env, JAFSADMNOMEM );
64 return;
65 }
66 for( i = 0; i < sizeof(keyEntry.key.key); i++ ) {
67 sprintf( &(convertedKey[i*4]), "\\%0.3o", keyEntry.key.key[i] );
68 }
69 jencryptionKey = (*env)->NewStringUTF(env, convertedKey);
70 (*env)->SetObjectField( env, key, key_encryptionKeyField, jencryptionKey );
71
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 );
78
79 free( convertedKey );
80}
81
82/**
83 * Fills in the information fields of the provided Key.
84 *
85 * env the Java environment
86 * cls the current Java class
87 * serverHandle the bos handle of the server to which the key
88 * belongs
89 * version the version of the key for which to get the information
90 * key the Key object in which to fill in the
91 * information
92 */
93JNIEXPORT void JNICALL
94Java_org_openafs_jafs_Key_getKeyInfo
95 (JNIEnv *env, jclass cls, jlong serverHandle, jint version, jobject key)
96{
97 afs_status_t ast;
98 bos_KeyInfo_t keyEntry;
99 void *iterationId;
100 int done;
101
102 if( !bos_KeyGetBegin( (void *) serverHandle, &iterationId, &ast ) ) {
103 throwAFSException( env, ast );
104 return;
105 }
106
107 done = FALSE;
108
109 // there's no KeyGet function, so we must iterate and find the
110 // one with the matching version
111 while( !done ) {
112
113 if( !bos_KeyGetNext( iterationId, &keyEntry, &ast ) ) {
114 // no matching key
115 if( ast == ADMITERATORDONE ) {
116 afs_status_t astnew;
117 if( !bos_KeyGetDone( iterationId, &astnew ) ) {
118 throwAFSException( env, astnew );
119 return;
120 }
121 throwAFSException( env, KAUNKNOWNKEY );
122 return;
123 // other
124 } else {
125 throwAFSException( env, ast );
126 return;
127 }
128 }
129
130 if( keyEntry.keyVersionNumber == version ) {
131 done = TRUE;
132 }
133
134 }
135
136 fillKeyInfo( env, key, keyEntry );
137
138 if( !bos_KeyGetDone( iterationId, &ast ) ) {
139 throwAFSException( env, ast );
140 return;
141 }
142
143}
144
145/**
146 * Create a server key.
147 *
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
152 * belong
153 * versionNumber the version number of the key to create (0 to 255)
154 * jkeyString the String version of the key that will
155 * be encrypted
156 */
157JNIEXPORT void JNICALL
158Java_org_openafs_jafs_Key_create
159 (JNIEnv *env, jclass cls, jlong cellHandle, jlong serverHandle, jint version,
160 jstring jkeyString)
161{
162 afs_status_t ast;
163 const char *keyString;
164 char *cellName;
165 kas_encryptionKey_p key =
166 (kas_encryptionKey_p) malloc( sizeof(kas_encryptionKey_t) );
167
168 if( !key ) {
169 throwAFSException( env, JAFSADMNOMEM );
170 return;
171 }
172
173 if( jkeyString != NULL ) {
174 keyString = (*env)->GetStringUTFChars(env, jkeyString, 0);
175 if( !keyString ) {
176 throwAFSException( env, JAFSADMNOMEM );
177 return;
178 }
179 } else {
180 keyString = NULL;
181 }
182
183 if( !afsclient_CellNameGet( (void *) cellHandle, &cellName, &ast ) ) {
184 free( key );
185 if( keyString != NULL ) {
186 (*env)->ReleaseStringUTFChars(env, jkeyString, keyString);
187 }
188 throwAFSException( env, ast );
189 return;
190 }
191
192 if( !kas_StringToKey( cellName, keyString, key, &ast ) ) {
193 free( key );
194 if( keyString != NULL ) {
195 (*env)->ReleaseStringUTFChars(env, jkeyString, keyString);
196 }
197 throwAFSException( env, ast );
198 return;
199 }
200
201 if( !bos_KeyCreate( (void *) serverHandle, version, key, &ast ) ) {
202 free( key );
203 if( keyString != NULL ) {
204 (*env)->ReleaseStringUTFChars(env, jkeyString, keyString);
205 }
206 throwAFSException( env, ast );
207 return;
208 }
209
210 free( key );
211 if( keyString != NULL ) {
212 (*env)->ReleaseStringUTFChars(env, jkeyString, keyString);
213 }
214}
215
216/**
217 * Delete a server key.
218 *
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)
223 */
224JNIEXPORT void JNICALL
225Java_org_openafs_jafs_Key_delete
226 (JNIEnv *env, jclass cls, jlong serverHandle, jint version )
227{
228 afs_status_t ast;
229
230 if( !bos_KeyDelete( (void *) serverHandle, version, &ast ) ) {
231 throwAFSException( env, ast );
232 return;
233 }
234}
235
236// reclaim global memory being used by this portion
237JNIEXPORT void JNICALL
238Java_org_openafs_jafs_Key_reclaimKeyMemory (JNIEnv *env, jclass cls)
239{
240 if( keyCls ) {
241 (*env)->DeleteGlobalRef(env, keyCls);
242 keyCls = 0;
243 }
244}
245
246
247
248