提交 c60a1e96 编写于 作者: V valeriep

6812738: SSL stress test with GF leads to 32 bit max process size in less than...

6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
Summary: Removed finalize() and add more error handling to native code
Reviewed-by: vinnie
上级 8d22cdf3
/* /*
* Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved. * Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
package sun.security.pkcs11; package sun.security.pkcs11;
import java.io.*; import java.io.*;
import java.lang.ref.*;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.*; import java.util.*;
...@@ -67,9 +68,6 @@ abstract class P11Key implements Key { ...@@ -67,9 +68,6 @@ abstract class P11Key implements Key {
// type of key, one of (PUBLIC, PRIVATE, SECRET) // type of key, one of (PUBLIC, PRIVATE, SECRET)
final String type; final String type;
// session in which the key was created, relevant for session objects
final Session session;
// token instance // token instance
final Token token; final Token token;
...@@ -85,10 +83,12 @@ abstract class P11Key implements Key { ...@@ -85,10 +83,12 @@ abstract class P11Key implements Key {
// flags indicating whether the key is a token object, sensitive, extractable // flags indicating whether the key is a token object, sensitive, extractable
final boolean tokenObject, sensitive, extractable; final boolean tokenObject, sensitive, extractable;
// weak reference notification clean up for session keys
private final SessionKeyRef sessionKeyRef;
P11Key(String type, Session session, long keyID, String algorithm, P11Key(String type, Session session, long keyID, String algorithm,
int keyLength, CK_ATTRIBUTE[] attributes) { int keyLength, CK_ATTRIBUTE[] attributes) {
this.type = type; this.type = type;
this.session = session;
this.token = session.token; this.token = session.token;
this.keyID = keyID; this.keyID = keyID;
this.algorithm = algorithm; this.algorithm = algorithm;
...@@ -111,7 +111,9 @@ abstract class P11Key implements Key { ...@@ -111,7 +111,9 @@ abstract class P11Key implements Key {
this.sensitive = sensitive; this.sensitive = sensitive;
this.extractable = extractable; this.extractable = extractable;
if (tokenObject == false) { if (tokenObject == false) {
session.addObject(); sessionKeyRef = new SessionKeyRef(this, keyID, session);
} else {
sessionKeyRef = null;
} }
} }
...@@ -236,24 +238,6 @@ abstract class P11Key implements Key { ...@@ -236,24 +238,6 @@ abstract class P11Key implements Key {
} }
} }
protected void finalize() throws Throwable {
if (tokenObject || (token.isValid() == false)) {
super.finalize();
return;
}
Session newSession = null;
try {
newSession = token.getOpSession();
token.p11.C_DestroyObject(newSession.id(), keyID);
} catch (PKCS11Exception e) {
// ignore
} finally {
token.releaseSession(newSession);
session.removeObject();
super.finalize();
}
}
private final static CK_ATTRIBUTE[] A0 = new CK_ATTRIBUTE[0]; private final static CK_ATTRIBUTE[] A0 = new CK_ATTRIBUTE[0];
private static CK_ATTRIBUTE[] getAttributes(Session session, long keyID, private static CK_ATTRIBUTE[] getAttributes(Session session, long keyID,
...@@ -1055,5 +1039,65 @@ abstract class P11Key implements Key { ...@@ -1055,5 +1039,65 @@ abstract class P11Key implements Key {
+ "\n parameters: " + params; + "\n parameters: " + params;
} }
} }
}
final class SessionKeyRef extends WeakReference<P11Key>
implements Comparable<SessionKeyRef> {
private static ReferenceQueue<P11Key> refQueue =
new ReferenceQueue<P11Key>();
private static Set<SessionKeyRef> refList =
Collections.synchronizedSortedSet(new TreeSet<SessionKeyRef>());
static ReferenceQueue<P11Key> referenceQueue() {
return refQueue;
}
static final private int MAX_ITERATIONS = 2;
private static void drainRefQueueBounded() {
int iterations = 0;
while (iterations < MAX_ITERATIONS) {
SessionKeyRef next = (SessionKeyRef) refQueue.poll();
if (next != null) next.dispose();
++iterations;
}
}
// handle to the native key
private long keyID;
private Session session;
SessionKeyRef(P11Key key , long keyID, Session session) {
super(key, refQueue);
this.keyID = keyID;
this.session = session;
this.session.addObject();
refList.add(this);
// TBD: run at some interval and not every time?
drainRefQueueBounded();
}
void dispose() {
refList.remove(this);
if (session.token.isValid()) {
Session newSession = null;
try {
newSession = session.token.getOpSession();
session.token.p11.C_DestroyObject(newSession.id(), keyID);
} catch (PKCS11Exception e) {
// ignore
} finally {
session.token.releaseSession(newSession);
session.removeObject();
}
}
}
public int compareTo(SessionKeyRef other) {
if (this.keyID == other.keyID) {
return 0;
} else {
return (this.keyID < other.keyID) ? -1 : 1;
}
}
} }
/* /*
* Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved. * Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -191,7 +191,9 @@ final class P11RSACipher extends CipherSpi { ...@@ -191,7 +191,9 @@ final class P11RSACipher extends CipherSpi {
throw new InvalidKeyException throw new InvalidKeyException
("Unwrap has to be used with private keys"); ("Unwrap has to be used with private keys");
} }
encrypt = false; // No further setup needed for C_Unwrap(). We'll initialize later
// if we can't use C_Unwrap().
return;
} else { } else {
throw new InvalidKeyException("Unsupported mode: " + opmode); throw new InvalidKeyException("Unsupported mode: " + opmode);
} }
...@@ -452,7 +454,7 @@ final class P11RSACipher extends CipherSpi { ...@@ -452,7 +454,7 @@ final class P11RSACipher extends CipherSpi {
long keyID = token.p11.C_UnwrapKey(s.id(), long keyID = token.p11.C_UnwrapKey(s.id(),
new CK_MECHANISM(mechanism), p11Key.keyID, wrappedKey, new CK_MECHANISM(mechanism), p11Key.keyID, wrappedKey,
attributes); attributes);
return P11Key.secretKey(session, keyID, algorithm, 48 << 3, return P11Key.secretKey(s, keyID, algorithm, 48 << 3,
attributes); attributes);
} catch (PKCS11Exception e) { } catch (PKCS11Exception e) {
throw new InvalidKeyException("unwrap() failed", e); throw new InvalidKeyException("unwrap() failed", e);
...@@ -461,6 +463,7 @@ final class P11RSACipher extends CipherSpi { ...@@ -461,6 +463,7 @@ final class P11RSACipher extends CipherSpi {
} }
} }
// XXX implement unwrap using C_Unwrap() for all keys // XXX implement unwrap using C_Unwrap() for all keys
implInit(Cipher.DECRYPT_MODE, p11Key);
if (wrappedKey.length > maxInputSize) { if (wrappedKey.length > maxInputSize) {
throw new InvalidKeyException("Key is too long for unwrapping"); throw new InvalidKeyException("Key is too long for unwrapping");
} }
......
...@@ -151,7 +151,7 @@ final class P11SecretKeyFactory extends SecretKeyFactorySpi { ...@@ -151,7 +151,7 @@ final class P11SecretKeyFactory extends SecretKeyFactorySpi {
session = token.getObjSession(); session = token.getObjSession();
long newKeyID = token.p11.C_CopyObject(session.id(), long newKeyID = token.p11.C_CopyObject(session.id(),
p11Key.keyID, extraAttrs); p11Key.keyID, extraAttrs);
p11Key = (P11Key) (P11Key.secretKey(p11Key.session, p11Key = (P11Key) (P11Key.secretKey(session,
newKeyID, p11Key.algorithm, p11Key.keyLength, newKeyID, p11Key.algorithm, p11Key.keyLength,
extraAttrs)); extraAttrs));
} catch (PKCS11Exception p11e) { } catch (PKCS11Exception p11e) {
......
/* /*
* Portions Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. * Portions Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
*/ */
/* Copyright (c) 2002 Graz University of Technology. All rights reserved. /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
...@@ -81,6 +81,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1EncryptInit ...@@ -81,6 +81,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1EncryptInit
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
ckKeyHandle = jLongToCKULong(jKeyHandle); ckKeyHandle = jLongToCKULong(jKeyHandle);
jMechanismToCKMechanism(env, jMechanism, &ckMechanism); jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
if ((*env)->ExceptionCheck(env)) { return; }
rv = (*ckpFunctions->C_EncryptInit)(ckSessionHandle, &ckMechanism, rv = (*ckpFunctions->C_EncryptInit)(ckSessionHandle, &ckMechanism,
ckKeyHandle); ckKeyHandle);
...@@ -126,14 +127,29 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1Encrypt ...@@ -126,14 +127,29 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1Encrypt
if (jInLen > MAX_STACK_BUFFER_LEN) { if (jInLen > MAX_STACK_BUFFER_LEN) {
inBufP = (CK_BYTE_PTR)malloc((size_t)jInLen); inBufP = (CK_BYTE_PTR)malloc((size_t)jInLen);
if (inBufP == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
} else { } else {
inBufP = IBUF; inBufP = IBUF;
} }
(*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP); (*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP);
if ((*env)->ExceptionCheck(env)) {
if (inBufP != IBUF) { free(inBufP); }
return 0;
}
ckEncryptedPartLen = jOutLen; ckEncryptedPartLen = jOutLen;
if (jOutLen > MAX_STACK_BUFFER_LEN) { if (jOutLen > MAX_STACK_BUFFER_LEN) {
outBufP = (CK_BYTE_PTR)malloc((size_t)jOutLen); outBufP = (CK_BYTE_PTR)malloc((size_t)jOutLen);
if (outBufP == NULL) {
if (inBufP != IBUF) {
free(inBufP);
}
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
} else { } else {
outBufP = OBUF; outBufP = OBUF;
} }
...@@ -193,10 +209,18 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1EncryptUpdate ...@@ -193,10 +209,18 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1EncryptUpdate
} else { } else {
if (jInLen > MAX_STACK_BUFFER_LEN) { if (jInLen > MAX_STACK_BUFFER_LEN) {
inBufP = (CK_BYTE_PTR)malloc((size_t)jInLen); inBufP = (CK_BYTE_PTR)malloc((size_t)jInLen);
if (inBufP == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
} else { } else {
inBufP = IBUF; inBufP = IBUF;
} }
(*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP); (*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP);
if ((*env)->ExceptionCheck(env)) {
if (directIn == 0 && inBufP != IBUF) { free(inBufP); }
return 0;
}
} }
ckEncryptedPartLen = jOutLen; ckEncryptedPartLen = jOutLen;
...@@ -205,6 +229,13 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1EncryptUpdate ...@@ -205,6 +229,13 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1EncryptUpdate
} else { } else {
if (jOutLen > MAX_STACK_BUFFER_LEN) { if (jOutLen > MAX_STACK_BUFFER_LEN) {
outBufP = (CK_BYTE_PTR)malloc((size_t)jOutLen); outBufP = (CK_BYTE_PTR)malloc((size_t)jOutLen);
if (outBufP == NULL) {
if (directIn == 0 && inBufP != IBUF) {
free(inBufP);
}
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
} else { } else {
outBufP = OBUF; outBufP = OBUF;
} }
...@@ -317,6 +348,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptInit ...@@ -317,6 +348,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptInit
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
ckKeyHandle = jLongToCKULong(jKeyHandle); ckKeyHandle = jLongToCKULong(jKeyHandle);
jMechanismToCKMechanism(env, jMechanism, &ckMechanism); jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
if ((*env)->ExceptionCheck(env)) { return; }
rv = (*ckpFunctions->C_DecryptInit)(ckSessionHandle, &ckMechanism, rv = (*ckpFunctions->C_DecryptInit)(ckSessionHandle, &ckMechanism,
ckKeyHandle); ckKeyHandle);
...@@ -362,14 +394,29 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1Decrypt ...@@ -362,14 +394,29 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1Decrypt
if (jInLen > MAX_STACK_BUFFER_LEN) { if (jInLen > MAX_STACK_BUFFER_LEN) {
inBufP = (CK_BYTE_PTR)malloc((size_t)jInLen); inBufP = (CK_BYTE_PTR)malloc((size_t)jInLen);
if (inBufP == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
} else { } else {
inBufP = IBUF; inBufP = IBUF;
} }
(*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP); (*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP);
if ((*env)->ExceptionCheck(env)) {
if (inBufP != IBUF) { free(inBufP); }
return 0;
}
ckPartLen = jOutLen; ckPartLen = jOutLen;
if (jOutLen > MAX_STACK_BUFFER_LEN) { if (jOutLen > MAX_STACK_BUFFER_LEN) {
outBufP = (CK_BYTE_PTR)malloc((size_t)jOutLen); outBufP = (CK_BYTE_PTR)malloc((size_t)jOutLen);
if (outBufP == NULL) {
if (inBufP != IBUF) {
free(inBufP);
}
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
} else { } else {
outBufP = OBUF; outBufP = OBUF;
} }
...@@ -429,10 +476,18 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptUpdate ...@@ -429,10 +476,18 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptUpdate
} else { } else {
if (jInLen > MAX_STACK_BUFFER_LEN) { if (jInLen > MAX_STACK_BUFFER_LEN) {
inBufP = (CK_BYTE_PTR)malloc((size_t)jInLen); inBufP = (CK_BYTE_PTR)malloc((size_t)jInLen);
if (inBufP == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
} else { } else {
inBufP = IBUF; inBufP = IBUF;
} }
(*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP); (*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP);
if ((*env)->ExceptionCheck(env)) {
if (directIn == 0 && inBufP != IBUF) { free(inBufP); }
return 0;
}
} }
ckDecryptedPartLen = jOutLen; ckDecryptedPartLen = jOutLen;
...@@ -441,6 +496,13 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptUpdate ...@@ -441,6 +496,13 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptUpdate
} else { } else {
if (jOutLen > MAX_STACK_BUFFER_LEN) { if (jOutLen > MAX_STACK_BUFFER_LEN) {
outBufP = (CK_BYTE_PTR)malloc((size_t)jOutLen); outBufP = (CK_BYTE_PTR)malloc((size_t)jOutLen);
if (outBufP == NULL) {
if (directIn == 0 && inBufP != IBUF) {
free(inBufP);
}
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
} else { } else {
outBufP = OBUF; outBufP = OBUF;
} }
......
/* /*
* Portions Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. * Portions Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
*/ */
/* Copyright (c) 2002 Graz University of Technology. All rights reserved. /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
...@@ -75,6 +75,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestInit ...@@ -75,6 +75,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestInit
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jMechanismToCKMechanism(env, jMechanism, &ckMechanism); jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
if ((*env)->ExceptionCheck(env)) { return; }
rv = (*ckpFunctions->C_DigestInit)(ckSessionHandle, &ckMechanism); rv = (*ckpFunctions->C_DigestInit)(ckSessionHandle, &ckMechanism);
...@@ -82,7 +83,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestInit ...@@ -82,7 +83,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestInit
free(ckMechanism.pParameter); free(ckMechanism.pParameter);
} }
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
...@@ -114,6 +115,7 @@ JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestSingle ...@@ -114,6 +115,7 @@ JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestSingle
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jMechanismToCKMechanism(env, jMechanism, &ckMechanism); jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
if ((*env)->ExceptionCheck(env)) { return 0; }
rv = (*ckpFunctions->C_DigestInit)(ckSessionHandle, &ckMechanism); rv = (*ckpFunctions->C_DigestInit)(ckSessionHandle, &ckMechanism);
...@@ -121,29 +123,32 @@ JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestSingle ...@@ -121,29 +123,32 @@ JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestSingle
free(ckMechanism.pParameter); free(ckMechanism.pParameter);
} }
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0; }
if (jInLen <= MAX_STACK_BUFFER_LEN) { if (jInLen <= MAX_STACK_BUFFER_LEN) {
bufP = BUF; bufP = BUF;
} else { } else {
/* always use single part op, even for large data */ /* always use single part op, even for large data */
bufP = (CK_BYTE_PTR)malloc((size_t)jInLen); bufP = (CK_BYTE_PTR) malloc((size_t)jInLen);
if (bufP == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
} }
(*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)bufP); (*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)bufP);
rv = (*ckpFunctions->C_Digest)(ckSessionHandle, bufP, jInLen, DIGESTBUF, &ckDigestLength); if ((*env)->ExceptionCheck(env)) {
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { if (bufP != BUF) { free(bufP); }
if (bufP != BUF) {
free(bufP);
}
return 0; return 0;
} }
(*env)->SetByteArrayRegion(env, jDigest, jDigestOfs, ckDigestLength, (jbyte *)DIGESTBUF); rv = (*ckpFunctions->C_Digest)(ckSessionHandle, bufP, jInLen, DIGESTBUF, &ckDigestLength);
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
if (bufP != BUF) { (*env)->SetByteArrayRegion(env, jDigest, jDigestOfs, ckDigestLength, (jbyte *)DIGESTBUF);
free(bufP);
} }
if (bufP != BUF) { free(bufP); }
return ckDigestLength; return ckDigestLength;
} }
#endif #endif
...@@ -183,17 +188,23 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestUpdate ...@@ -183,17 +188,23 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestUpdate
bufP = BUF; bufP = BUF;
} else { } else {
bufLen = min(MAX_HEAP_BUFFER_LEN, jInLen); bufLen = min(MAX_HEAP_BUFFER_LEN, jInLen);
bufP = (CK_BYTE_PTR)malloc((size_t)bufLen); bufP = (CK_BYTE_PTR) malloc((size_t)bufLen);
if (bufP == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return;
}
} }
while (jInLen > 0) { while (jInLen > 0) {
jsize chunkLen = min(bufLen, jInLen); jsize chunkLen = min(bufLen, jInLen);
(*env)->GetByteArrayRegion(env, jIn, jInOfs, chunkLen, (jbyte *)bufP); (*env)->GetByteArrayRegion(env, jIn, jInOfs, chunkLen, (jbyte *)bufP);
if ((*env)->ExceptionCheck(env)) {
if (bufP != BUF) { free(bufP); }
return;
}
rv = (*ckpFunctions->C_DigestUpdate)(ckSessionHandle, bufP, chunkLen); rv = (*ckpFunctions->C_DigestUpdate)(ckSessionHandle, bufP, chunkLen);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
if (bufP != BUF) { if (bufP != BUF) { free(bufP); }
free(bufP);
}
return; return;
} }
jInOfs += chunkLen; jInOfs += chunkLen;
...@@ -229,7 +240,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestKey ...@@ -229,7 +240,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestKey
ckKeyHandle = jLongToCKULong(jKeyHandle); ckKeyHandle = jLongToCKULong(jKeyHandle);
rv = (*ckpFunctions->C_DigestKey)(ckSessionHandle, ckKeyHandle); rv = (*ckpFunctions->C_DigestKey)(ckSessionHandle, ckKeyHandle);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
...@@ -257,10 +268,9 @@ JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestFinal ...@@ -257,10 +268,9 @@ JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestFinal
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
rv = (*ckpFunctions->C_DigestFinal)(ckSessionHandle, BUF, &ckDigestLength); rv = (*ckpFunctions->C_DigestFinal)(ckSessionHandle, BUF, &ckDigestLength);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0 ; } if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
(*env)->SetByteArrayRegion(env, jDigest, jDigestOfs, ckDigestLength, (jbyte *)BUF);
(*env)->SetByteArrayRegion(env, jDigest, jDigestOfs, ckDigestLength, (jbyte *)BUF); }
return ckDigestLength; return ckDigestLength;
} }
#endif #endif
...@@ -288,12 +298,13 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SeedRandom ...@@ -288,12 +298,13 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SeedRandom
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jByteArrayToCKByteArray(env, jSeed, &ckpSeed, &ckSeedLength); jByteArrayToCKByteArray(env, jSeed, &ckpSeed, &ckSeedLength);
if ((*env)->ExceptionCheck(env)) { return; }
rv = (*ckpFunctions->C_SeedRandom)(ckSessionHandle, ckpSeed, ckSeedLength); rv = (*ckpFunctions->C_SeedRandom)(ckSessionHandle, ckpSeed, ckSeedLength);
free(ckpSeed); free(ckpSeed);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
...@@ -322,6 +333,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateRandom ...@@ -322,6 +333,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateRandom
jRandomBufferLength = (*env)->GetArrayLength(env, jRandomData); jRandomBufferLength = (*env)->GetArrayLength(env, jRandomData);
jRandomBuffer = (*env)->GetByteArrayElements(env, jRandomData, NULL); jRandomBuffer = (*env)->GetByteArrayElements(env, jRandomData, NULL);
if (jRandomBuffer == NULL) { return; }
rv = (*ckpFunctions->C_GenerateRandom)(ckSessionHandle, rv = (*ckpFunctions->C_GenerateRandom)(ckSessionHandle,
(CK_BYTE_PTR) jRandomBuffer, (CK_BYTE_PTR) jRandomBuffer,
...@@ -330,6 +342,6 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateRandom ...@@ -330,6 +342,6 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateRandom
/* copy back generated bytes */ /* copy back generated bytes */
(*env)->ReleaseByteArrayElements(env, jRandomData, jRandomBuffer, 0); (*env)->ReleaseByteArrayElements(env, jRandomData, jRandomBuffer, 0);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
/* /*
* Portions Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. * Portions Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
*/ */
/* Copyright (c) 2002 Graz University of Technology. All rights reserved. /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
...@@ -73,7 +73,7 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestEn ...@@ -73,7 +73,7 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestEn
CK_SESSION_HANDLE ckSessionHandle; CK_SESSION_HANDLE ckSessionHandle;
CK_BYTE_PTR ckpPart = NULL_PTR, ckpEncryptedPart; CK_BYTE_PTR ckpPart = NULL_PTR, ckpEncryptedPart;
CK_ULONG ckPartLength, ckEncryptedPartLength = 0; CK_ULONG ckPartLength, ckEncryptedPartLength = 0;
jbyteArray jEncryptedPart; jbyteArray jEncryptedPart = NULL;
CK_RV rv; CK_RV rv;
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
...@@ -81,20 +81,28 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestEn ...@@ -81,20 +81,28 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestEn
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jByteArrayToCKByteArray(env, jPart, &ckpPart, &ckPartLength); jByteArrayToCKByteArray(env, jPart, &ckpPart, &ckPartLength);
if ((*env)->ExceptionCheck(env)) { return NULL; }
rv = (*ckpFunctions->C_DigestEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, NULL_PTR, &ckEncryptedPartLength); rv = (*ckpFunctions->C_DigestEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, NULL_PTR, &ckEncryptedPartLength);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
free(ckpPart);
return NULL;
}
ckpEncryptedPart = (CK_BYTE_PTR) malloc(ckEncryptedPartLength * sizeof(CK_BYTE)); ckpEncryptedPart = (CK_BYTE_PTR) malloc(ckEncryptedPartLength * sizeof(CK_BYTE));
if (ckpEncryptedPart == NULL) {
free(ckpPart);
JNU_ThrowOutOfMemoryError(env, 0);
return NULL;
}
rv = (*ckpFunctions->C_DigestEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, ckpEncryptedPart, &ckEncryptedPartLength); rv = (*ckpFunctions->C_DigestEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, ckpEncryptedPart, &ckEncryptedPartLength);
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jEncryptedPart = ckByteArrayToJByteArray(env, ckpEncryptedPart, ckEncryptedPartLength); jEncryptedPart = ckByteArrayToJByteArray(env, ckpEncryptedPart, ckEncryptedPartLength);
}
free(ckpPart); free(ckpPart);
free(ckpEncryptedPart); free(ckpEncryptedPart);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; }
return jEncryptedPart ; return jEncryptedPart ;
} }
#endif #endif
...@@ -117,7 +125,7 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptD ...@@ -117,7 +125,7 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptD
CK_SESSION_HANDLE ckSessionHandle; CK_SESSION_HANDLE ckSessionHandle;
CK_BYTE_PTR ckpPart, ckpEncryptedPart = NULL_PTR; CK_BYTE_PTR ckpPart, ckpEncryptedPart = NULL_PTR;
CK_ULONG ckPartLength = 0, ckEncryptedPartLength; CK_ULONG ckPartLength = 0, ckEncryptedPartLength;
jbyteArray jPart; jbyteArray jPart = NULL;
CK_RV rv; CK_RV rv;
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
...@@ -125,19 +133,27 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptD ...@@ -125,19 +133,27 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptD
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jByteArrayToCKByteArray(env, jEncryptedPart, &ckpEncryptedPart, &ckEncryptedPartLength); jByteArrayToCKByteArray(env, jEncryptedPart, &ckpEncryptedPart, &ckEncryptedPartLength);
if ((*env)->ExceptionCheck(env)) { return NULL; }
rv = (*ckpFunctions->C_DecryptDigestUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, NULL_PTR, &ckPartLength); rv = (*ckpFunctions->C_DecryptDigestUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, NULL_PTR, &ckPartLength);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
free(ckpEncryptedPart);
return NULL;
}
ckpPart = (CK_BYTE_PTR) malloc(ckPartLength * sizeof(CK_BYTE)); ckpPart = (CK_BYTE_PTR) malloc(ckPartLength * sizeof(CK_BYTE));
if (ckpPart == NULL) {
free(ckpEncryptedPart);
JNU_ThrowOutOfMemoryError(env, 0);
return NULL;
}
rv = (*ckpFunctions->C_DecryptDigestUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, ckpPart, &ckPartLength); rv = (*ckpFunctions->C_DecryptDigestUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, ckpPart, &ckPartLength);
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jPart = ckByteArrayToJByteArray(env, ckpPart, ckPartLength); jPart = ckByteArrayToJByteArray(env, ckpPart, ckPartLength);
free(ckpPart); }
free(ckpEncryptedPart); free(ckpEncryptedPart);
free(ckpPart);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; }
return jPart ; return jPart ;
} }
...@@ -161,7 +177,7 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignEncr ...@@ -161,7 +177,7 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignEncr
CK_SESSION_HANDLE ckSessionHandle; CK_SESSION_HANDLE ckSessionHandle;
CK_BYTE_PTR ckpPart = NULL_PTR, ckpEncryptedPart; CK_BYTE_PTR ckpPart = NULL_PTR, ckpEncryptedPart;
CK_ULONG ckPartLength, ckEncryptedPartLength = 0; CK_ULONG ckPartLength, ckEncryptedPartLength = 0;
jbyteArray jEncryptedPart; jbyteArray jEncryptedPart = NULL;
CK_RV rv; CK_RV rv;
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
...@@ -169,20 +185,28 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignEncr ...@@ -169,20 +185,28 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignEncr
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jByteArrayToCKByteArray(env, jPart, &ckpPart, &ckPartLength); jByteArrayToCKByteArray(env, jPart, &ckpPart, &ckPartLength);
if ((*env)->ExceptionCheck(env)) { return NULL; }
rv = (*ckpFunctions->C_SignEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, NULL_PTR, &ckEncryptedPartLength); rv = (*ckpFunctions->C_SignEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, NULL_PTR, &ckEncryptedPartLength);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
free(ckpPart);
return NULL;
}
ckpEncryptedPart = (CK_BYTE_PTR) malloc(ckEncryptedPartLength * sizeof(CK_BYTE)); ckpEncryptedPart = (CK_BYTE_PTR) malloc(ckEncryptedPartLength * sizeof(CK_BYTE));
if (ckpEncryptedPart == NULL) {
free(ckpPart);
JNU_ThrowOutOfMemoryError(env, 0);
return NULL;
}
rv = (*ckpFunctions->C_SignEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, ckpEncryptedPart, &ckEncryptedPartLength); rv = (*ckpFunctions->C_SignEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, ckpEncryptedPart, &ckEncryptedPartLength);
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jEncryptedPart = ckByteArrayToJByteArray(env, ckpEncryptedPart, ckEncryptedPartLength); jEncryptedPart = ckByteArrayToJByteArray(env, ckpEncryptedPart, ckEncryptedPartLength);
}
free(ckpPart); free(ckpPart);
free(ckpEncryptedPart); free(ckpEncryptedPart);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; }
return jEncryptedPart ; return jEncryptedPart ;
} }
#endif #endif
...@@ -205,7 +229,7 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptV ...@@ -205,7 +229,7 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptV
CK_SESSION_HANDLE ckSessionHandle; CK_SESSION_HANDLE ckSessionHandle;
CK_BYTE_PTR ckpPart, ckpEncryptedPart = NULL_PTR; CK_BYTE_PTR ckpPart, ckpEncryptedPart = NULL_PTR;
CK_ULONG ckPartLength = 0, ckEncryptedPartLength; CK_ULONG ckPartLength = 0, ckEncryptedPartLength;
jbyteArray jPart; jbyteArray jPart = NULL;
CK_RV rv; CK_RV rv;
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
...@@ -213,19 +237,28 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptV ...@@ -213,19 +237,28 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptV
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jByteArrayToCKByteArray(env, jEncryptedPart, &ckpEncryptedPart, &ckEncryptedPartLength); jByteArrayToCKByteArray(env, jEncryptedPart, &ckpEncryptedPart, &ckEncryptedPartLength);
if ((*env)->ExceptionCheck(env)) { return NULL; }
rv = (*ckpFunctions->C_DecryptVerifyUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, NULL_PTR, &ckPartLength); rv = (*ckpFunctions->C_DecryptVerifyUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, NULL_PTR, &ckPartLength);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
free(ckpEncryptedPart);
return NULL;
}
ckpPart = (CK_BYTE_PTR) malloc(ckPartLength * sizeof(CK_BYTE)); ckpPart = (CK_BYTE_PTR) malloc(ckPartLength * sizeof(CK_BYTE));
if (ckpPart == NULL) {
free(ckpEncryptedPart);
JNU_ThrowOutOfMemoryError(env, 0);
return NULL;
}
rv = (*ckpFunctions->C_DecryptVerifyUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, ckpPart, &ckPartLength); rv = (*ckpFunctions->C_DecryptVerifyUpdate)(ckSessionHandle, ckpEncryptedPart, ckEncryptedPartLength, ckpPart, &ckPartLength);
jPart = ckByteArrayToJByteArray(env, ckpPart, ckPartLength); if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
free(ckpPart); jPart = ckByteArrayToJByteArray(env, ckpPart, ckPartLength);
}
free(ckpEncryptedPart); free(ckpEncryptedPart);
free(ckpPart);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; }
return jPart ; return jPart ;
} }
...@@ -252,7 +285,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetFunctionSta ...@@ -252,7 +285,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetFunctionSta
/* C_GetFunctionStatus should always return CKR_FUNCTION_NOT_PARALLEL */ /* C_GetFunctionStatus should always return CKR_FUNCTION_NOT_PARALLEL */
rv = (*ckpFunctions->C_GetFunctionStatus)(ckSessionHandle); rv = (*ckpFunctions->C_GetFunctionStatus)(ckSessionHandle);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
...@@ -277,6 +310,6 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CancelFunction ...@@ -277,6 +310,6 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CancelFunction
/* C_GetFunctionStatus should always return CKR_FUNCTION_NOT_PARALLEL */ /* C_GetFunctionStatus should always return CKR_FUNCTION_NOT_PARALLEL */
rv = (*ckpFunctions->C_CancelFunction)(ckSessionHandle); rv = (*ckpFunctions->C_CancelFunction)(ckSessionHandle);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
/* /*
* Portions Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved. * Portions Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
*/ */
/* Copyright (c) 2002 Graz University of Technology. All rights reserved. /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
...@@ -102,6 +102,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_initializeLibrary ...@@ -102,6 +102,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_initializeLibrary
jclass fetchClass(JNIEnv *env, const char *name) { jclass fetchClass(JNIEnv *env, const char *name) {
jclass tmpClass = (*env)->FindClass(env, name); jclass tmpClass = (*env)->FindClass(env, name);
if (tmpClass == NULL) { return NULL; }
return (*env)->NewGlobalRef(env, tmpClass); return (*env)->NewGlobalRef(env, tmpClass);
} }
...@@ -110,14 +111,18 @@ void prefetchFields(JNIEnv *env, jclass thisClass) { ...@@ -110,14 +111,18 @@ void prefetchFields(JNIEnv *env, jclass thisClass) {
/* PKCS11 */ /* PKCS11 */
pNativeDataID = (*env)->GetFieldID(env, thisClass, "pNativeData", "J"); pNativeDataID = (*env)->GetFieldID(env, thisClass, "pNativeData", "J");
if (pNativeDataID == NULL) { return; }
/* CK_MECHANISM */ /* CK_MECHANISM */
tmpClass = (*env)->FindClass(env, CLASS_MECHANISM); tmpClass = (*env)->FindClass(env, CLASS_MECHANISM);
if (tmpClass == NULL) { return; }
mech_mechanismID = (*env)->GetFieldID(env, tmpClass, "mechanism", "J"); mech_mechanismID = (*env)->GetFieldID(env, tmpClass, "mechanism", "J");
if (mech_mechanismID == NULL) { return; }
mech_pParameterID = (*env)->GetFieldID(env, tmpClass, "pParameter", mech_pParameterID = (*env)->GetFieldID(env, tmpClass, "pParameter",
"Ljava/lang/Object;"); "Ljava/lang/Object;");
if (mech_pParameterID == NULL) { return; }
jByteArrayClass = fetchClass(env, "[B"); jByteArrayClass = fetchClass(env, "[B");
if (jByteArrayClass == NULL) { return; }
jLongClass = fetchClass(env, "java/lang/Long"); jLongClass = fetchClass(env, "java/lang/Long");
} }
...@@ -252,10 +257,9 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetInfo ...@@ -252,10 +257,9 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetInfo
if (ckpFunctions == NULL) { return NULL; } if (ckpFunctions == NULL) { return NULL; }
rv = (*ckpFunctions->C_GetInfo)(&ckLibInfo); rv = (*ckpFunctions->C_GetInfo)(&ckLibInfo);
if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jInfoObject = ckInfoPtrToJInfo(env, &ckLibInfo);
jInfoObject = ckInfoPtrToJInfo(env, &ckLibInfo); }
return jInfoObject ; return jInfoObject ;
} }
...@@ -279,28 +283,31 @@ jobject ckInfoPtrToJInfo(JNIEnv *env, const CK_INFO_PTR ckpInfo) ...@@ -279,28 +283,31 @@ jobject ckInfoPtrToJInfo(JNIEnv *env, const CK_INFO_PTR ckpInfo)
/* load CK_INFO class */ /* load CK_INFO class */
jInfoClass = (*env)->FindClass(env, CLASS_INFO); jInfoClass = (*env)->FindClass(env, CLASS_INFO);
assert(jInfoClass != 0); if (jInfoClass == NULL) { return NULL; };
/* load CK_INFO constructor */ /* load CK_INFO constructor */
jCtrId = (*env)->GetMethodID jCtrId = (*env)->GetMethodID
(env, jInfoClass, "<init>", (env, jInfoClass, "<init>",
"(Lsun/security/pkcs11/wrapper/CK_VERSION;[CJ[CLsun/security/pkcs11/wrapper/CK_VERSION;)V"); "(Lsun/security/pkcs11/wrapper/CK_VERSION;[CJ[CLsun/security/pkcs11/wrapper/CK_VERSION;)V");
if (jCtrId == NULL) { return NULL; }
assert(jCtrId != 0);
/* prep all fields */ /* prep all fields */
jCryptokiVer = ckVersionPtrToJVersion(env, &(ckpInfo->cryptokiVersion)); jCryptokiVer = ckVersionPtrToJVersion(env, &(ckpInfo->cryptokiVersion));
if (jCryptokiVer == NULL) { return NULL; }
jVendor = jVendor =
ckUTF8CharArrayToJCharArray(env, &(ckpInfo->manufacturerID[0]), 32); ckUTF8CharArrayToJCharArray(env, &(ckpInfo->manufacturerID[0]), 32);
if (jVendor == NULL) { return NULL; }
jFlags = ckULongToJLong(ckpInfo->flags); jFlags = ckULongToJLong(ckpInfo->flags);
jLibraryDesc = jLibraryDesc =
ckUTF8CharArrayToJCharArray(env, &(ckpInfo->libraryDescription[0]), 32); ckUTF8CharArrayToJCharArray(env, &(ckpInfo->libraryDescription[0]), 32);
if (jLibraryDesc == NULL) { return NULL; }
jLibraryVer = ckVersionPtrToJVersion(env, &(ckpInfo->libraryVersion)); jLibraryVer = ckVersionPtrToJVersion(env, &(ckpInfo->libraryVersion));
if (jLibraryVer == NULL) { return NULL; }
/* create new CK_INFO object */ /* create new CK_INFO object */
jInfoObject = (*env)->NewObject(env, jInfoClass, jCtrId, jCryptokiVer, jInfoObject = (*env)->NewObject(env, jInfoClass, jCtrId, jCryptokiVer,
jVendor, jFlags, jLibraryDesc, jLibraryVer); jVendor, jFlags, jLibraryDesc, jLibraryVer);
assert(jInfoObject != 0); if (jInfoObject == NULL) { return NULL; }
/* free local references */ /* free local references */
(*env)->DeleteLocalRef(env, jInfoClass); (*env)->DeleteLocalRef(env, jInfoClass);
...@@ -343,15 +350,18 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetSlotList ...@@ -343,15 +350,18 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetSlotList
if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; }
ckpSlotList = (CK_SLOT_ID_PTR) malloc(ckTokenNumber * sizeof(CK_SLOT_ID)); ckpSlotList = (CK_SLOT_ID_PTR) malloc(ckTokenNumber * sizeof(CK_SLOT_ID));
if (ckpSlotList == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return NULL;
}
rv = (*ckpFunctions->C_GetSlotList)(ckTokenPresent, ckpSlotList, rv = (*ckpFunctions->C_GetSlotList)(ckTokenPresent, ckpSlotList,
&ckTokenNumber); &ckTokenNumber);
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jSlotList = ckULongArrayToJLongArray(env, ckpSlotList, ckTokenNumber); jSlotList = ckULongArrayToJLongArray(env, ckpSlotList, ckTokenNumber);
}
free(ckpSlotList); free(ckpSlotList);
if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; }
return jSlotList ; return jSlotList ;
} }
#endif #endif
...@@ -380,10 +390,9 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetSlotInfo ...@@ -380,10 +390,9 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetSlotInfo
ckSlotID = jLongToCKULong(jSlotID); ckSlotID = jLongToCKULong(jSlotID);
rv = (*ckpFunctions->C_GetSlotInfo)(ckSlotID, &ckSlotInfo); rv = (*ckpFunctions->C_GetSlotInfo)(ckSlotID, &ckSlotInfo);
if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jSlotInfoObject = ckSlotInfoPtrToJSlotInfo(env, &ckSlotInfo);
jSlotInfoObject = ckSlotInfoPtrToJSlotInfo(env, &ckSlotInfo); }
return jSlotInfoObject ; return jSlotInfoObject ;
} }
...@@ -410,28 +419,32 @@ ckSlotInfoPtrToJSlotInfo ...@@ -410,28 +419,32 @@ ckSlotInfoPtrToJSlotInfo
/* load CK_SLOT_INFO class */ /* load CK_SLOT_INFO class */
jSlotInfoClass = (*env)->FindClass(env, CLASS_SLOT_INFO); jSlotInfoClass = (*env)->FindClass(env, CLASS_SLOT_INFO);
assert(jSlotInfoClass != 0); if (jSlotInfoClass == NULL) { return NULL; };
/* load CK_SLOT_INFO constructor */ /* load CK_SLOT_INFO constructor */
jCtrId = (*env)->GetMethodID jCtrId = (*env)->GetMethodID
(env, jSlotInfoClass, "<init>", (env, jSlotInfoClass, "<init>",
"([C[CJLsun/security/pkcs11/wrapper/CK_VERSION;Lsun/security/pkcs11/wrapper/CK_VERSION;)V"); "([C[CJLsun/security/pkcs11/wrapper/CK_VERSION;Lsun/security/pkcs11/wrapper/CK_VERSION;)V");
assert(jCtrId != 0); if (jCtrId == NULL) { return NULL; }
/* prep all fields */ /* prep all fields */
jSlotDesc = jSlotDesc =
ckUTF8CharArrayToJCharArray(env, &(ckpSlotInfo->slotDescription[0]), 64); ckUTF8CharArrayToJCharArray(env, &(ckpSlotInfo->slotDescription[0]), 64);
if (jSlotDesc == NULL) { return NULL; }
jVendor = jVendor =
ckUTF8CharArrayToJCharArray(env, &(ckpSlotInfo->manufacturerID[0]), 32); ckUTF8CharArrayToJCharArray(env, &(ckpSlotInfo->manufacturerID[0]), 32);
if (jVendor == NULL) { return NULL; }
jFlags = ckULongToJLong(ckpSlotInfo->flags); jFlags = ckULongToJLong(ckpSlotInfo->flags);
jHardwareVer = ckVersionPtrToJVersion(env, &(ckpSlotInfo->hardwareVersion)); jHardwareVer = ckVersionPtrToJVersion(env, &(ckpSlotInfo->hardwareVersion));
if (jHardwareVer == NULL) { return NULL; }
jFirmwareVer = ckVersionPtrToJVersion(env, &(ckpSlotInfo->firmwareVersion)); jFirmwareVer = ckVersionPtrToJVersion(env, &(ckpSlotInfo->firmwareVersion));
if (jFirmwareVer == NULL) { return NULL; }
/* create new CK_SLOT_INFO object */ /* create new CK_SLOT_INFO object */
jSlotInfoObject = (*env)->NewObject jSlotInfoObject = (*env)->NewObject
(env, jSlotInfoClass, jCtrId, jSlotDesc, jVendor, jFlags, (env, jSlotInfoClass, jCtrId, jSlotDesc, jVendor, jFlags,
jHardwareVer, jFirmwareVer); jHardwareVer, jFirmwareVer);
assert(jSlotInfoObject != 0); if (jSlotInfoObject == NULL) { return NULL; }
/* free local references */ /* free local references */
(*env)->DeleteLocalRef(env, jSlotInfoClass); (*env)->DeleteLocalRef(env, jSlotInfoClass);
...@@ -460,7 +473,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetTokenInfo ...@@ -460,7 +473,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetTokenInfo
{ {
CK_SLOT_ID ckSlotID; CK_SLOT_ID ckSlotID;
CK_TOKEN_INFO ckTokenInfo; CK_TOKEN_INFO ckTokenInfo;
jobject jInfoTokenObject; jobject jInfoTokenObject = NULL;
CK_RV rv; CK_RV rv;
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
...@@ -469,10 +482,9 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetTokenInfo ...@@ -469,10 +482,9 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetTokenInfo
ckSlotID = jLongToCKULong(jSlotID); ckSlotID = jLongToCKULong(jSlotID);
rv = (*ckpFunctions->C_GetTokenInfo)(ckSlotID, &ckTokenInfo); rv = (*ckpFunctions->C_GetTokenInfo)(ckSlotID, &ckTokenInfo);
if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jInfoTokenObject = ckTokenInfoPtrToJTokenInfo(env, &ckTokenInfo);
jInfoTokenObject = ckTokenInfoPtrToJTokenInfo(env, &ckTokenInfo); }
return jInfoTokenObject ; return jInfoTokenObject ;
} }
...@@ -512,21 +524,25 @@ ckTokenInfoPtrToJTokenInfo ...@@ -512,21 +524,25 @@ ckTokenInfoPtrToJTokenInfo
/* load CK_TOKEN_INFO class */ /* load CK_TOKEN_INFO class */
jTokenInfoClass = (*env)->FindClass(env, CLASS_TOKEN_INFO); jTokenInfoClass = (*env)->FindClass(env, CLASS_TOKEN_INFO);
assert(jTokenInfoClass != 0); if (jTokenInfoClass == NULL) { return NULL; };
/* load CK_TOKEN_INFO constructor */ /* load CK_TOKEN_INFO constructor */
jCtrId = (*env)->GetMethodID jCtrId = (*env)->GetMethodID
(env, jTokenInfoClass, "<init>", (env, jTokenInfoClass, "<init>",
"([C[C[C[CJJJJJJJJJJJLsun/security/pkcs11/wrapper/CK_VERSION;Lsun/security/pkcs11/wrapper/CK_VERSION;[C)V"); "([C[C[C[CJJJJJJJJJJJLsun/security/pkcs11/wrapper/CK_VERSION;Lsun/security/pkcs11/wrapper/CK_VERSION;[C)V");
assert(jCtrId != 0); if (jCtrId == NULL) { return NULL; };
/* prep all fields */ /* prep all fields */
jLabel = ckUTF8CharArrayToJCharArray(env, &(ckpTokenInfo->label[0]), 32); jLabel = ckUTF8CharArrayToJCharArray(env, &(ckpTokenInfo->label[0]), 32);
if (jLabel == NULL) { return NULL; };
jVendor = jVendor =
ckUTF8CharArrayToJCharArray(env, &(ckpTokenInfo->manufacturerID[0]), 32); ckUTF8CharArrayToJCharArray(env, &(ckpTokenInfo->manufacturerID[0]), 32);
if (jVendor == NULL) { return NULL; };
jModel = ckUTF8CharArrayToJCharArray(env, &(ckpTokenInfo->model[0]), 16); jModel = ckUTF8CharArrayToJCharArray(env, &(ckpTokenInfo->model[0]), 16);
if (jModel == NULL) { return NULL; };
jSerialNo = jSerialNo =
ckUTF8CharArrayToJCharArray(env, &(ckpTokenInfo->serialNumber[0]), 16); ckUTF8CharArrayToJCharArray(env, &(ckpTokenInfo->serialNumber[0]), 16);
if (jSerialNo == NULL) { return NULL; };
jFlags = ckULongToJLong(ckpTokenInfo->flags); jFlags = ckULongToJLong(ckpTokenInfo->flags);
jMaxSnCnt = ckULongSpecialToJLong(ckpTokenInfo->ulMaxSessionCount); jMaxSnCnt = ckULongSpecialToJLong(ckpTokenInfo->ulMaxSessionCount);
jSnCnt = ckULongSpecialToJLong(ckpTokenInfo->ulSessionCount); jSnCnt = ckULongSpecialToJLong(ckpTokenInfo->ulSessionCount);
...@@ -540,10 +556,13 @@ ckTokenInfoPtrToJTokenInfo ...@@ -540,10 +556,13 @@ ckTokenInfoPtrToJTokenInfo
jFreePrivMem = ckULongSpecialToJLong(ckpTokenInfo->ulFreePrivateMemory); jFreePrivMem = ckULongSpecialToJLong(ckpTokenInfo->ulFreePrivateMemory);
jHardwareVer = jHardwareVer =
ckVersionPtrToJVersion(env, &(ckpTokenInfo->hardwareVersion)); ckVersionPtrToJVersion(env, &(ckpTokenInfo->hardwareVersion));
if (jHardwareVer == NULL) { return NULL; }
jFirmwareVer = jFirmwareVer =
ckVersionPtrToJVersion(env, &(ckpTokenInfo->firmwareVersion)); ckVersionPtrToJVersion(env, &(ckpTokenInfo->firmwareVersion));
if (jFirmwareVer == NULL) { return NULL; }
jUtcTime = jUtcTime =
ckUTF8CharArrayToJCharArray(env, &(ckpTokenInfo->utcTime[0]), 16); ckUTF8CharArrayToJCharArray(env, &(ckpTokenInfo->utcTime[0]), 16);
if (jUtcTime == NULL) { return NULL; }
/* create new CK_TOKEN_INFO object */ /* create new CK_TOKEN_INFO object */
jTokenInfoObject = jTokenInfoObject =
...@@ -553,7 +572,7 @@ ckTokenInfoPtrToJTokenInfo ...@@ -553,7 +572,7 @@ ckTokenInfoPtrToJTokenInfo
jMaxPinLen, jMinPinLen, jMaxPinLen, jMinPinLen,
jTotalPubMem, jFreePubMem, jTotalPrivMem, jFreePrivMem, jTotalPubMem, jFreePubMem, jTotalPrivMem, jFreePrivMem,
jHardwareVer, jFirmwareVer, jUtcTime); jHardwareVer, jFirmwareVer, jUtcTime);
assert(jTokenInfoObject != 0); if (jTokenInfoObject == NULL) { return NULL; }
/* free local references */ /* free local references */
(*env)->DeleteLocalRef(env, jTokenInfoClass); (*env)->DeleteLocalRef(env, jTokenInfoClass);
...@@ -584,7 +603,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1WaitForSlotEvent ...@@ -584,7 +603,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1WaitForSlotEvent
{ {
CK_FLAGS ckFlags; CK_FLAGS ckFlags;
CK_SLOT_ID ckSlotID; CK_SLOT_ID ckSlotID;
jlong jSlotID; jlong jSlotID = 0L;
CK_RV rv; CK_RV rv;
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
...@@ -593,9 +612,9 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1WaitForSlotEvent ...@@ -593,9 +612,9 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1WaitForSlotEvent
ckFlags = jLongToCKULong(jFlags); ckFlags = jLongToCKULong(jFlags);
rv = (*ckpFunctions->C_WaitForSlotEvent)(ckFlags, &ckSlotID, NULL_PTR); rv = (*ckpFunctions->C_WaitForSlotEvent)(ckFlags, &ckSlotID, NULL_PTR);
if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L; } if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jSlotID = ckULongToJLong(ckSlotID);
jSlotID = ckULongToJLong(ckSlotID); }
return jSlotID ; return jSlotID ;
} }
...@@ -632,16 +651,19 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetMechanismList ...@@ -632,16 +651,19 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetMechanismList
ckpMechanismList = (CK_MECHANISM_TYPE_PTR) ckpMechanismList = (CK_MECHANISM_TYPE_PTR)
malloc(ckMechanismNumber * sizeof(CK_MECHANISM_TYPE)); malloc(ckMechanismNumber * sizeof(CK_MECHANISM_TYPE));
if (ckpMechanismList == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return NULL;
}
rv = (*ckpFunctions->C_GetMechanismList)(ckSlotID, ckpMechanismList, rv = (*ckpFunctions->C_GetMechanismList)(ckSlotID, ckpMechanismList,
&ckMechanismNumber); &ckMechanismNumber);
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jMechanismList = ckULongArrayToJLongArray(env, ckpMechanismList, jMechanismList = ckULongArrayToJLongArray(env, ckpMechanismList,
ckMechanismNumber); ckMechanismNumber);
}
free(ckpMechanismList); free(ckpMechanismList);
if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; }
return jMechanismList ; return jMechanismList ;
} }
#endif #endif
...@@ -663,7 +685,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetMechanismInfo ...@@ -663,7 +685,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetMechanismInfo
CK_SLOT_ID ckSlotID; CK_SLOT_ID ckSlotID;
CK_MECHANISM_TYPE ckMechanismType; CK_MECHANISM_TYPE ckMechanismType;
CK_MECHANISM_INFO ckMechanismInfo; CK_MECHANISM_INFO ckMechanismInfo;
jobject jMechanismInfo; jobject jMechanismInfo = NULL;
CK_RV rv; CK_RV rv;
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
...@@ -674,10 +696,9 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetMechanismInfo ...@@ -674,10 +696,9 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetMechanismInfo
rv = (*ckpFunctions->C_GetMechanismInfo)(ckSlotID, ckMechanismType, rv = (*ckpFunctions->C_GetMechanismInfo)(ckSlotID, ckMechanismType,
&ckMechanismInfo); &ckMechanismInfo);
if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jMechanismInfo = ckMechanismInfoPtrToJMechanismInfo(env, &ckMechanismInfo);
jMechanismInfo = ckMechanismInfoPtrToJMechanismInfo(env, &ckMechanismInfo); }
return jMechanismInfo ; return jMechanismInfo ;
} }
...@@ -703,11 +724,11 @@ ckMechanismInfoPtrToJMechanismInfo ...@@ -703,11 +724,11 @@ ckMechanismInfoPtrToJMechanismInfo
/* load CK_MECHANISM_INFO class */ /* load CK_MECHANISM_INFO class */
jMechanismInfoClass = (*env)->FindClass(env, CLASS_MECHANISM_INFO); jMechanismInfoClass = (*env)->FindClass(env, CLASS_MECHANISM_INFO);
assert(jMechanismInfoClass != 0); if (jMechanismInfoClass == NULL) { return NULL; };
/* load CK_MECHANISM_INFO constructor */ /* load CK_MECHANISM_INFO constructor */
jCtrId = (*env)->GetMethodID(env, jMechanismInfoClass, "<init>", "(JJJ)V"); jCtrId = (*env)->GetMethodID(env, jMechanismInfoClass, "<init>", "(JJJ)V");
assert(jCtrId != 0); if (jCtrId == NULL) { return NULL; };
/* prep all fields */ /* prep all fields */
jMinKeySize = ckULongToJLong(ckpMechanismInfo->ulMinKeySize); jMinKeySize = ckULongToJLong(ckpMechanismInfo->ulMinKeySize);
...@@ -717,7 +738,7 @@ ckMechanismInfoPtrToJMechanismInfo ...@@ -717,7 +738,7 @@ ckMechanismInfoPtrToJMechanismInfo
/* create new CK_MECHANISM_INFO object */ /* create new CK_MECHANISM_INFO object */
jMechanismInfoObject = (*env)->NewObject(env, jMechanismInfoClass, jCtrId, jMechanismInfoObject = (*env)->NewObject(env, jMechanismInfoClass, jCtrId,
jMinKeySize, jMaxKeySize, jFlags); jMinKeySize, jMaxKeySize, jFlags);
assert(jMechanismInfoObject != 0); if (jMechanismInfoObject == NULL) { return NULL; };
/* free local references */ /* free local references */
(*env)->DeleteLocalRef(env, jMechanismInfoClass); (*env)->DeleteLocalRef(env, jMechanismInfoClass);
...@@ -753,8 +774,13 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1InitToken ...@@ -753,8 +774,13 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1InitToken
ckSlotID = jLongToCKULong(jSlotID); ckSlotID = jLongToCKULong(jSlotID);
jCharArrayToCKCharArray(env, jPin, &ckpPin, &ckPinLength); jCharArrayToCKCharArray(env, jPin, &ckpPin, &ckPinLength);
jCharArrayToCKUTF8CharArray(env, jLabel, &ckpLabel, &ckLabelLength); if ((*env)->ExceptionCheck(env)) { return; }
/* ckLabelLength <= 32 !!! */ /* ckLabelLength <= 32 !!! */
jCharArrayToCKUTF8CharArray(env, jLabel, &ckpLabel, &ckLabelLength);
if ((*env)->ExceptionCheck(env)) {
free(ckpPin);
return;
}
rv = (*ckpFunctions->C_InitToken)(ckSlotID, ckpPin, ckPinLength, ckpLabel); rv = (*ckpFunctions->C_InitToken)(ckSlotID, ckpPin, ckPinLength, ckpLabel);
TRACE1("InitToken return code: %d", rv); TRACE1("InitToken return code: %d", rv);
...@@ -790,6 +816,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1InitPIN ...@@ -790,6 +816,7 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1InitPIN
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jCharArrayToCKCharArray(env, jPin, &ckpPin, &ckPinLength); jCharArrayToCKCharArray(env, jPin, &ckpPin, &ckPinLength);
if ((*env)->ExceptionCheck(env)) { return; }
rv = (*ckpFunctions->C_InitPIN)(ckSessionHandle, ckpPin, ckPinLength); rv = (*ckpFunctions->C_InitPIN)(ckSessionHandle, ckpPin, ckPinLength);
...@@ -828,7 +855,12 @@ jcharArray jNewPin) ...@@ -828,7 +855,12 @@ jcharArray jNewPin)
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jCharArrayToCKCharArray(env, jOldPin, &ckpOldPin, &ckOldPinLength); jCharArrayToCKCharArray(env, jOldPin, &ckpOldPin, &ckOldPinLength);
if ((*env)->ExceptionCheck(env)) { return; }
jCharArrayToCKCharArray(env, jNewPin, &ckpNewPin, &ckNewPinLength); jCharArrayToCKCharArray(env, jNewPin, &ckpNewPin, &ckNewPinLength);
if ((*env)->ExceptionCheck(env)) {
free(ckpOldPin);
return;
}
rv = (*ckpFunctions->C_SetPIN)(ckSessionHandle, ckpOldPin, ckOldPinLength, rv = (*ckpFunctions->C_SetPIN)(ckSessionHandle, ckpOldPin, ckOldPinLength,
ckpNewPin, ckNewPinLength); ckpNewPin, ckNewPinLength);
......
/* /*
* Portions Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. * Portions Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
*/ */
/* Copyright (c) 2002 Graz University of Technology. All rights reserved. /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
...@@ -76,7 +76,7 @@ CK_C_INITIALIZE_ARGS_PTR ckpGlobalInitArgs; ...@@ -76,7 +76,7 @@ CK_C_INITIALIZE_ARGS_PTR ckpGlobalInitArgs;
CK_C_INITIALIZE_ARGS_PTR makeCKInitArgsAdapter(JNIEnv *env, jobject jInitArgs) CK_C_INITIALIZE_ARGS_PTR makeCKInitArgsAdapter(JNIEnv *env, jobject jInitArgs)
{ {
CK_C_INITIALIZE_ARGS_PTR ckpInitArgs; CK_C_INITIALIZE_ARGS_PTR ckpInitArgs;
jclass jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS); jclass jInitArgsClass;
jfieldID fieldID; jfieldID fieldID;
jlong jFlags; jlong jFlags;
jobject jReserved; jobject jReserved;
...@@ -91,10 +91,20 @@ CK_C_INITIALIZE_ARGS_PTR makeCKInitArgsAdapter(JNIEnv *env, jobject jInitArgs) ...@@ -91,10 +91,20 @@ CK_C_INITIALIZE_ARGS_PTR makeCKInitArgsAdapter(JNIEnv *env, jobject jInitArgs)
/* convert the Java InitArgs object to a pointer to a CK_C_INITIALIZE_ARGS structure */ /* convert the Java InitArgs object to a pointer to a CK_C_INITIALIZE_ARGS structure */
ckpInitArgs = (CK_C_INITIALIZE_ARGS_PTR) malloc(sizeof(CK_C_INITIALIZE_ARGS)); ckpInitArgs = (CK_C_INITIALIZE_ARGS_PTR) malloc(sizeof(CK_C_INITIALIZE_ARGS));
if (ckpInitArgs == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return NULL_PTR;
}
/* Set the mutex functions that will call the Java mutex functions, but /* Set the mutex functions that will call the Java mutex functions, but
* only set it, if the field is not null. * only set it, if the field is not null.
*/ */
jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
if (jInitArgsClass == NULL) {
free(ckpInitArgs);
return NULL;
}
#ifdef NO_CALLBACKS #ifdef NO_CALLBACKS
ckpInitArgs->CreateMutex = NULL_PTR; ckpInitArgs->CreateMutex = NULL_PTR;
ckpInitArgs->DestroyMutex = NULL_PTR; ckpInitArgs->DestroyMutex = NULL_PTR;
...@@ -102,22 +112,22 @@ CK_C_INITIALIZE_ARGS_PTR makeCKInitArgsAdapter(JNIEnv *env, jobject jInitArgs) ...@@ -102,22 +112,22 @@ CK_C_INITIALIZE_ARGS_PTR makeCKInitArgsAdapter(JNIEnv *env, jobject jInitArgs)
ckpInitArgs->UnlockMutex = NULL_PTR; ckpInitArgs->UnlockMutex = NULL_PTR;
#else #else
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "CreateMutex", "Lsun/security/pkcs11/wrapper/CK_CREATEMUTEX;"); fieldID = (*env)->GetFieldID(env, jInitArgsClass, "CreateMutex", "Lsun/security/pkcs11/wrapper/CK_CREATEMUTEX;");
assert(fieldID != 0); if (fieldID == NULL) { return NULL; }
jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID); jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID);
ckpInitArgs->CreateMutex = (jMutexHandler != NULL) ? &callJCreateMutex : NULL_PTR; ckpInitArgs->CreateMutex = (jMutexHandler != NULL) ? &callJCreateMutex : NULL_PTR;
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "DestroyMutex", "Lsun/security/pkcs11/wrapper/CK_DESTROYMUTEX;"); fieldID = (*env)->GetFieldID(env, jInitArgsClass, "DestroyMutex", "Lsun/security/pkcs11/wrapper/CK_DESTROYMUTEX;");
assert(fieldID != 0); if (fieldID == NULL) { return NULL; }
jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID); jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID);
ckpInitArgs->DestroyMutex = (jMutexHandler != NULL) ? &callJDestroyMutex : NULL_PTR; ckpInitArgs->DestroyMutex = (jMutexHandler != NULL) ? &callJDestroyMutex : NULL_PTR;
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "LockMutex", "Lsun/security/pkcs11/wrapper/CK_LOCKMUTEX;"); fieldID = (*env)->GetFieldID(env, jInitArgsClass, "LockMutex", "Lsun/security/pkcs11/wrapper/CK_LOCKMUTEX;");
assert(fieldID != 0); if (fieldID == NULL) { return NULL; }
jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID); jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID);
ckpInitArgs->LockMutex = (jMutexHandler != NULL) ? &callJLockMutex : NULL_PTR; ckpInitArgs->LockMutex = (jMutexHandler != NULL) ? &callJLockMutex : NULL_PTR;
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "UnlockMutex", "Lsun/security/pkcs11/wrapper/CK_UNLOCKMUTEX;"); fieldID = (*env)->GetFieldID(env, jInitArgsClass, "UnlockMutex", "Lsun/security/pkcs11/wrapper/CK_UNLOCKMUTEX;");
assert(fieldID != 0); if (fieldID == NULL) { return NULL; }
jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID); jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID);
ckpInitArgs->UnlockMutex = (jMutexHandler != NULL) ? &callJUnlockMutex : NULL_PTR; ckpInitArgs->UnlockMutex = (jMutexHandler != NULL) ? &callJUnlockMutex : NULL_PTR;
...@@ -129,19 +139,25 @@ CK_C_INITIALIZE_ARGS_PTR makeCKInitArgsAdapter(JNIEnv *env, jobject jInitArgs) ...@@ -129,19 +139,25 @@ CK_C_INITIALIZE_ARGS_PTR makeCKInitArgsAdapter(JNIEnv *env, jobject jInitArgs)
/* set the global object jInitArgs so that the right Java mutex functions will be called */ /* set the global object jInitArgs so that the right Java mutex functions will be called */
jInitArgsObject = (*env)->NewGlobalRef(env, jInitArgs); jInitArgsObject = (*env)->NewGlobalRef(env, jInitArgs);
ckpGlobalInitArgs = (CK_C_INITIALIZE_ARGS_PTR) malloc(sizeof(CK_C_INITIALIZE_ARGS)); ckpGlobalInitArgs = (CK_C_INITIALIZE_ARGS_PTR) malloc(sizeof(CK_C_INITIALIZE_ARGS));
if (ckpGlobalInitArgs == NULL) {
free(ckpInitArgs);
JNU_ThrowOutOfMemoryError(env, 0);
return NULL_PTR;
}
memcpy(ckpGlobalInitArgs, ckpInitArgs, sizeof(CK_C_INITIALIZE_ARGS)); memcpy(ckpGlobalInitArgs, ckpInitArgs, sizeof(CK_C_INITIALIZE_ARGS));
} }
#endif /* NO_CALLBACKS */ #endif /* NO_CALLBACKS */
/* convert and set the flags field */ /* convert and set the flags field */
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "flags", "J"); fieldID = (*env)->GetFieldID(env, jInitArgsClass, "flags", "J");
assert(fieldID != 0); if (fieldID == NULL) { return NULL; }
jFlags = (*env)->GetLongField(env, jInitArgs, fieldID); jFlags = (*env)->GetLongField(env, jInitArgs, fieldID);
ckpInitArgs->flags = jLongToCKULong(jFlags); ckpInitArgs->flags = jLongToCKULong(jFlags);
/* pReserved should be NULL_PTR in this version */ /* pReserved should be NULL_PTR in this version */
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "pReserved", "Ljava/lang/Object;"); fieldID = (*env)->GetFieldID(env, jInitArgsClass, "pReserved", "Ljava/lang/Object;");
assert(fieldID != 0); if (fieldID == NULL) { return NULL; }
jReserved = (*env)->GetObjectField(env, jInitArgs, fieldID); jReserved = (*env)->GetObjectField(env, jInitArgs, fieldID);
/* we try to convert the reserved parameter also */ /* we try to convert the reserved parameter also */
...@@ -201,20 +217,21 @@ CK_RV callJCreateMutex(CK_VOID_PTR_PTR ppMutex) ...@@ -201,20 +217,21 @@ CK_RV callJCreateMutex(CK_VOID_PTR_PTR ppMutex)
wasAttached = 1; wasAttached = 1;
} }
jCreateMutexClass = (*env)->FindClass(env, CLASS_CREATEMUTEX); jCreateMutexClass = (*env)->FindClass(env, CLASS_CREATEMUTEX);
if (jCreateMutexClass == NULL) { return rv; }
jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS); jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
if (jInitArgsClass == NULL) { return rv; }
/* get the CreateMutex object out of the jInitArgs object */ /* get the CreateMutex object out of the jInitArgs object */
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "CreateMutex", "Lsun/security/pkcs11/wrapper/CK_CREATEMUTEX;"); fieldID = (*env)->GetFieldID(env, jInitArgsClass, "CreateMutex", "Lsun/security/pkcs11/wrapper/CK_CREATEMUTEX;");
assert(fieldID != 0); if (fieldID == NULL) { return rv; }
jCreateMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID); jCreateMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID);
assert(jCreateMutex != 0); assert(jCreateMutex != 0);
/* call the CK_CREATEMUTEX function of the CreateMutex object */ /* call the CK_CREATEMUTEX function of the CreateMutex object */
/* and get the new Java mutex object */ /* and get the new Java mutex object */
methodID = (*env)->GetMethodID(env, jCreateMutexClass, "CK_CREATEMUTEX", "()Ljava/lang/Object;"); methodID = (*env)->GetMethodID(env, jCreateMutexClass, "CK_CREATEMUTEX", "()Ljava/lang/Object;");
assert(methodID != 0); if (methodID == NULL) { return rv; }
jMutex = (*env)->CallObjectMethod(env, jCreateMutex, methodID); jMutex = (*env)->CallObjectMethod(env, jCreateMutex, methodID);
/* set a global reference on the Java mutex */ /* set a global reference on the Java mutex */
...@@ -227,10 +244,13 @@ CK_RV callJCreateMutex(CK_VOID_PTR_PTR ppMutex) ...@@ -227,10 +244,13 @@ CK_RV callJCreateMutex(CK_VOID_PTR_PTR ppMutex)
pkcs11Exception = (*env)->ExceptionOccurred(env); pkcs11Exception = (*env)->ExceptionOccurred(env);
if (pkcs11Exception != NULL) { if (pkcs11Exception != NULL) {
/* TBD: clear the pending exception with ExceptionClear? */
/* The was an exception thrown, now we get the error-code from it */ /* The was an exception thrown, now we get the error-code from it */
pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION); pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
if (pkcs11ExceptionClass == NULL) { return rv; }
methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J"); methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
assert(methodID != 0); if (methodID == NULL) { return rv; }
errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID); errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID);
rv = jLongToCKULong(errorCode); rv = jLongToCKULong(errorCode);
} }
...@@ -292,22 +312,23 @@ CK_RV callJDestroyMutex(CK_VOID_PTR pMutex) ...@@ -292,22 +312,23 @@ CK_RV callJDestroyMutex(CK_VOID_PTR pMutex)
wasAttached = 1; wasAttached = 1;
} }
jDestroyMutexClass = (*env)->FindClass(env, CLASS_DESTROYMUTEX); jDestroyMutexClass = (*env)->FindClass(env, CLASS_DESTROYMUTEX);
if (jDestroyMutexClass == NULL) { return rv; }
jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS); jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
if (jInitArgsClass == NULL) { return rv; }
/* convert the CK mutex to a Java mutex */ /* convert the CK mutex to a Java mutex */
jMutex = ckVoidPtrToJObject(pMutex); jMutex = ckVoidPtrToJObject(pMutex);
/* get the DestroyMutex object out of the jInitArgs object */ /* get the DestroyMutex object out of the jInitArgs object */
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "DestroyMutex", "Lsun/security/pkcs11/wrapper/CK_DESTROYMUTEX;"); fieldID = (*env)->GetFieldID(env, jInitArgsClass, "DestroyMutex", "Lsun/security/pkcs11/wrapper/CK_DESTROYMUTEX;");
assert(fieldID != 0); if (fieldID == NULL) { return rv; }
jDestroyMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID); jDestroyMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID);
assert(jDestroyMutex != 0); assert(jDestroyMutex != 0);
/* call the CK_DESTROYMUTEX method of the DestroyMutex object */ /* call the CK_DESTROYMUTEX method of the DestroyMutex object */
methodID = (*env)->GetMethodID(env, jDestroyMutexClass, "CK_DESTROYMUTEX", "(Ljava/lang/Object;)V"); methodID = (*env)->GetMethodID(env, jDestroyMutexClass, "CK_DESTROYMUTEX", "(Ljava/lang/Object;)V");
assert(methodID != 0); if (methodID == NULL) { return rv; }
(*env)->CallVoidMethod(env, jDestroyMutex, methodID, jMutex); (*env)->CallVoidMethod(env, jDestroyMutex, methodID, jMutex);
/* delete the global reference on the Java mutex */ /* delete the global reference on the Java mutex */
...@@ -318,10 +339,12 @@ CK_RV callJDestroyMutex(CK_VOID_PTR pMutex) ...@@ -318,10 +339,12 @@ CK_RV callJDestroyMutex(CK_VOID_PTR pMutex)
pkcs11Exception = (*env)->ExceptionOccurred(env); pkcs11Exception = (*env)->ExceptionOccurred(env);
if (pkcs11Exception != NULL) { if (pkcs11Exception != NULL) {
/* TBD: clear the pending exception with ExceptionClear? */
/* The was an exception thrown, now we get the error-code from it */ /* The was an exception thrown, now we get the error-code from it */
pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION); pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
if (pkcs11ExceptionClass == NULL) { return rv; }
methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J"); methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
assert(methodID != 0); if (methodID == NULL) { return rv; }
errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID); errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID);
rv = jLongToCKULong(errorCode); rv = jLongToCKULong(errorCode);
} }
...@@ -383,33 +406,35 @@ CK_RV callJLockMutex(CK_VOID_PTR pMutex) ...@@ -383,33 +406,35 @@ CK_RV callJLockMutex(CK_VOID_PTR pMutex)
wasAttached = 1; wasAttached = 1;
} }
jLockMutexClass = (*env)->FindClass(env, CLASS_LOCKMUTEX); jLockMutexClass = (*env)->FindClass(env, CLASS_LOCKMUTEX);
if (jLockMutexClass == NULL) { return rv; }
jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS); jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
if (jInitArgsClass == NULL) { return rv; }
/* convert the CK mutex to a Java mutex */ /* convert the CK mutex to a Java mutex */
jMutex = ckVoidPtrToJObject(pMutex); jMutex = ckVoidPtrToJObject(pMutex);
/* get the LockMutex object out of the jInitArgs object */ /* get the LockMutex object out of the jInitArgs object */
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "LockMutex", "Lsun/security/pkcs11/wrapper/CK_LOCKMUTEX;"); fieldID = (*env)->GetFieldID(env, jInitArgsClass, "LockMutex", "Lsun/security/pkcs11/wrapper/CK_LOCKMUTEX;");
assert(fieldID != 0); if (fieldID == NULL) { return rv; }
jLockMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID); jLockMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID);
assert(jLockMutex != 0); assert(jLockMutex != 0);
/* call the CK_LOCKMUTEX method of the LockMutex object */ /* call the CK_LOCKMUTEX method of the LockMutex object */
methodID = (*env)->GetMethodID(env, jLockMutexClass, "CK_LOCKMUTEX", "(Ljava/lang/Object;)V"); methodID = (*env)->GetMethodID(env, jLockMutexClass, "CK_LOCKMUTEX", "(Ljava/lang/Object;)V");
assert(methodID != 0); if (methodID == NULL) { return rv; }
(*env)->CallVoidMethod(env, jLockMutex, methodID, jMutex); (*env)->CallVoidMethod(env, jLockMutex, methodID, jMutex);
/* check, if callback threw an exception */ /* check, if callback threw an exception */
pkcs11Exception = (*env)->ExceptionOccurred(env); pkcs11Exception = (*env)->ExceptionOccurred(env);
if (pkcs11Exception != NULL) { if (pkcs11Exception != NULL) {
/* TBD: clear the pending exception with ExceptionClear? */
/* The was an exception thrown, now we get the error-code from it */ /* The was an exception thrown, now we get the error-code from it */
pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION); pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
if (pkcs11ExceptionClass == NULL) { return rv; }
methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J"); methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
assert(methodID != 0); if (methodID == NULL) { return rv; }
errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID); errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID);
rv = jLongToCKULong(errorCode); rv = jLongToCKULong(errorCode);
} }
...@@ -471,33 +496,35 @@ CK_RV callJUnlockMutex(CK_VOID_PTR pMutex) ...@@ -471,33 +496,35 @@ CK_RV callJUnlockMutex(CK_VOID_PTR pMutex)
wasAttached = 1; wasAttached = 1;
} }
jUnlockMutexClass = (*env)->FindClass(env, CLASS_UNLOCKMUTEX); jUnlockMutexClass = (*env)->FindClass(env, CLASS_UNLOCKMUTEX);
if (jUnlockMutexClass == NULL) { return rv; }
jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS); jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
if (jInitArgsClass == NULL) { return rv; }
/* convert the CK-type mutex to a Java mutex */ /* convert the CK-type mutex to a Java mutex */
jMutex = ckVoidPtrToJObject(pMutex); jMutex = ckVoidPtrToJObject(pMutex);
/* get the UnlockMutex object out of the jInitArgs object */ /* get the UnlockMutex object out of the jInitArgs object */
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "UnlockMutex", "Lsun/security/pkcs11/wrapper/CK_UNLOCKMUTEX;"); fieldID = (*env)->GetFieldID(env, jInitArgsClass, "UnlockMutex", "Lsun/security/pkcs11/wrapper/CK_UNLOCKMUTEX;");
assert(fieldID != 0); if (fieldID == NULL) { return rv; }
jUnlockMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID); jUnlockMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID);
assert(jUnlockMutex != 0); assert(jUnlockMutex != 0);
/* call the CK_UNLOCKMUTEX method of the UnLockMutex object */ /* call the CK_UNLOCKMUTEX method of the UnLockMutex object */
methodID = (*env)->GetMethodID(env, jUnlockMutexClass, "CK_UNLOCKMUTEX", "(Ljava/lang/Object;)V"); methodID = (*env)->GetMethodID(env, jUnlockMutexClass, "CK_UNLOCKMUTEX", "(Ljava/lang/Object;)V");
assert(methodID != 0); if (methodID == NULL) { return rv; }
(*env)->CallVoidMethod(env, jUnlockMutex, methodID, jMutex); (*env)->CallVoidMethod(env, jUnlockMutex, methodID, jMutex);
/* check, if callback threw an exception */ /* check, if callback threw an exception */
pkcs11Exception = (*env)->ExceptionOccurred(env); pkcs11Exception = (*env)->ExceptionOccurred(env);
if (pkcs11Exception != NULL) { if (pkcs11Exception != NULL) {
/* TBD: clear the pending exception with ExceptionClear? */
/* The was an exception thrown, now we get the error-code from it */ /* The was an exception thrown, now we get the error-code from it */
pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION); pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
if (pkcs11ExceptionClass == NULL) { return rv; }
methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J"); methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
assert(methodID != 0); if (methodID == NULL) { return rv; }
errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID); errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID);
rv = jLongToCKULong(errorCode); rv = jLongToCKULong(errorCode);
} }
......
/* /*
* Portions Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. * Portions Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
*/ */
/* Copyright (c) 2002 Graz University of Technology. All rights reserved. /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
...@@ -81,16 +81,14 @@ JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CreateObject ...@@ -81,16 +81,14 @@ JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CreateObject
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength); jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
if ((*env)->ExceptionCheck(env)) { return 0L; }
rv = (*ckpFunctions->C_CreateObject)(ckSessionHandle, ckpAttributes, ckAttributesLength, &ckObjectHandle); rv = (*ckpFunctions->C_CreateObject)(ckSessionHandle, ckpAttributes, ckAttributesLength, &ckObjectHandle);
jObjectHandle = ckULongToJLong(ckObjectHandle); jObjectHandle = ckULongToJLong(ckObjectHandle);
for(i=0; i<ckAttributesLength; i++) freeCKAttributeArray(ckpAttributes, ckAttributesLength);
if(ckpAttributes[i].pValue != NULL_PTR)
free(ckpAttributes[i].pValue);
free(ckpAttributes);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
return jObjectHandle ; return jObjectHandle ;
} }
...@@ -126,14 +124,12 @@ JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CopyObject ...@@ -126,14 +124,12 @@ JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CopyObject
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
ckObjectHandle = jLongToCKULong(jObjectHandle); ckObjectHandle = jLongToCKULong(jObjectHandle);
jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength); jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
if ((*env)->ExceptionCheck(env)) { return 0L; }
rv = (*ckpFunctions->C_CopyObject)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength, &ckNewObjectHandle); rv = (*ckpFunctions->C_CopyObject)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength, &ckNewObjectHandle);
jNewObjectHandle = ckULongToJLong(ckNewObjectHandle); jNewObjectHandle = ckULongToJLong(ckNewObjectHandle);
for(i=0; i<ckAttributesLength; i++) freeCKAttributeArray(ckpAttributes, ckAttributesLength);
if(ckpAttributes[i].pValue != NULL_PTR)
free(ckpAttributes[i].pValue);
free(ckpAttributes);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; } if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
...@@ -164,7 +160,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DestroyObject ...@@ -164,7 +160,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DestroyObject
ckObjectHandle = jLongToCKULong(jObjectHandle); ckObjectHandle = jLongToCKULong(jObjectHandle);
rv = (*ckpFunctions->C_DestroyObject)(ckSessionHandle, ckObjectHandle); rv = (*ckpFunctions->C_DestroyObject)(ckSessionHandle, ckObjectHandle);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
...@@ -194,7 +190,7 @@ JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetObjectSize ...@@ -194,7 +190,7 @@ JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetObjectSize
ckObjectHandle = jLongToCKULong(jObjectHandle); ckObjectHandle = jLongToCKULong(jObjectHandle);
rv = (*ckpFunctions->C_GetObjectSize)(ckSessionHandle, ckObjectHandle, &ckObjectSize); rv = (*ckpFunctions->C_GetObjectSize)(ckSessionHandle, ckObjectHandle, &ckObjectSize);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
jObjectSize = ckULongToJLong(ckObjectSize); jObjectSize = ckULongToJLong(ckObjectSize);
...@@ -221,7 +217,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetAttributeVa ...@@ -221,7 +217,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetAttributeVa
CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR; CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
CK_ULONG ckAttributesLength; CK_ULONG ckAttributesLength;
CK_ULONG ckBufferLength; CK_ULONG ckBufferLength;
CK_ULONG i; CK_ULONG i, j;
jobject jAttribute; jobject jAttribute;
CK_RV rv; CK_RV rv;
...@@ -238,19 +234,20 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetAttributeVa ...@@ -238,19 +234,20 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetAttributeVa
ckObjectHandle = jLongToCKULong(jObjectHandle); ckObjectHandle = jLongToCKULong(jObjectHandle);
TRACE1("jAttributeArrayToCKAttributeArray now with jTemplate = %d", jTemplate); TRACE1("jAttributeArrayToCKAttributeArray now with jTemplate = %d", jTemplate);
jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength); jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
if ((*env)->ExceptionCheck(env)) { return; }
TRACE2("DEBUG: jAttributeArrayToCKAttributeArray finished with ckpAttribute = %d, Length = %d\n", ckpAttributes, ckAttributesLength); TRACE2("DEBUG: jAttributeArrayToCKAttributeArray finished with ckpAttribute = %d, Length = %d\n", ckpAttributes, ckAttributesLength);
/* first set all pValue to NULL, to get the needed buffer length */ /* first set all pValue to NULL, to get the needed buffer length */
for(i = 0; i < ckAttributesLength; i++) { for(i = 0; i < ckAttributesLength; i++) {
if(ckpAttributes[i].pValue != NULL_PTR) { if (ckpAttributes[i].pValue != NULL_PTR) {
free(ckpAttributes[i].pValue); free(ckpAttributes[i].pValue);
ckpAttributes[i].pValue = NULL_PTR;
} }
} }
for (i = 0; i < ckAttributesLength; i++) {
ckpAttributes[i].pValue = NULL_PTR;
}
rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength); rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
free(ckpAttributes); free(ckpAttributes);
return ; return ;
} }
...@@ -261,27 +258,34 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetAttributeVa ...@@ -261,27 +258,34 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetAttributeVa
for (i = 0; i < ckAttributesLength; i++) { for (i = 0; i < ckAttributesLength; i++) {
ckBufferLength = sizeof(CK_BYTE) * ckpAttributes[i].ulValueLen; ckBufferLength = sizeof(CK_BYTE) * ckpAttributes[i].ulValueLen;
ckpAttributes[i].pValue = (void *) malloc(ckBufferLength); ckpAttributes[i].pValue = (void *) malloc(ckBufferLength);
if (ckpAttributes[i].pValue == NULL) {
freeCKAttributeArray(ckpAttributes, i);
JNU_ThrowOutOfMemoryError(env, 0);
return;
}
ckpAttributes[i].ulValueLen = ckBufferLength; ckpAttributes[i].ulValueLen = ckBufferLength;
} }
/* now get the attributes with all values */ /* now get the attributes with all values */
rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength); rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength);
/* copy back the values to the Java attributes */ if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
for (i = 0; i < ckAttributesLength; i++) { /* copy back the values to the Java attributes */
jAttribute = ckAttributePtrToJAttribute(env, &(ckpAttributes[i])); for (i = 0; i < ckAttributesLength; i++) {
(*env)->SetObjectArrayElement(env, jTemplate, i, jAttribute); jAttribute = ckAttributePtrToJAttribute(env, &(ckpAttributes[i]));
} if (jAttribute == NULL) {
freeCKAttributeArray(ckpAttributes, ckAttributesLength);
for(i=0; i<ckAttributesLength; i++) { return;
if(ckpAttributes[i].pValue != NULL_PTR) { }
free(ckpAttributes[i].pValue); (*env)->SetObjectArrayElement(env, jTemplate, i, jAttribute);
if ((*env)->ExceptionCheck(env)) {
freeCKAttributeArray(ckpAttributes, ckAttributesLength);
return;
}
} }
} }
free(ckpAttributes); freeCKAttributeArray(ckpAttributes, ckAttributesLength);
TRACE0("FINISHED\n"); TRACE0("FINISHED\n");
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return ; }
} }
#endif #endif
...@@ -312,15 +316,11 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SetAttributeVa ...@@ -312,15 +316,11 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SetAttributeVa
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
ckObjectHandle = jLongToCKULong(jObjectHandle); ckObjectHandle = jLongToCKULong(jObjectHandle);
jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength); jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
if ((*env)->ExceptionCheck(env)) { return; }
rv = (*ckpFunctions->C_SetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength); rv = (*ckpFunctions->C_SetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength);
for(i=0; i<ckAttributesLength; i++) { freeCKAttributeArray(ckpAttributes, ckAttributesLength);
if(ckpAttributes[i].pValue != NULL_PTR) {
free(ckpAttributes[i].pValue);
}
}
free(ckpAttributes);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
...@@ -355,15 +355,11 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObjectsIni ...@@ -355,15 +355,11 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObjectsIni
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength); jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
if ((*env)->ExceptionCheck(env)) { return; }
rv = (*ckpFunctions->C_FindObjectsInit)(ckSessionHandle, ckpAttributes, ckAttributesLength); rv = (*ckpFunctions->C_FindObjectsInit)(ckSessionHandle, ckpAttributes, ckAttributesLength);
for(i=0; i<ckAttributesLength; i++) { freeCKAttributeArray(ckpAttributes, ckAttributesLength);
if(ckpAttributes[i].pValue != NULL_PTR) {
free(ckpAttributes[i].pValue);
}
}
free(ckpAttributes);
TRACE0("FINISHED\n"); TRACE0("FINISHED\n");
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
...@@ -397,14 +393,18 @@ JNIEXPORT jlongArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObje ...@@ -397,14 +393,18 @@ JNIEXPORT jlongArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObje
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
ckMaxObjectLength = jLongToCKULong(jMaxObjectCount); ckMaxObjectLength = jLongToCKULong(jMaxObjectCount);
ckpObjectHandleArray = (CK_OBJECT_HANDLE_PTR) malloc(sizeof(CK_OBJECT_HANDLE) * ckMaxObjectLength); ckpObjectHandleArray = (CK_OBJECT_HANDLE_PTR) malloc(sizeof(CK_OBJECT_HANDLE) * ckMaxObjectLength);
if (ckpObjectHandleArray == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return NULL;
}
rv = (*ckpFunctions->C_FindObjects)(ckSessionHandle, ckpObjectHandleArray, ckMaxObjectLength, &ckActualObjectCount); rv = (*ckpFunctions->C_FindObjects)(ckSessionHandle, ckpObjectHandleArray, ckMaxObjectLength, &ckActualObjectCount);
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jObjectHandleArray = ckULongArrayToJLongArray(env, ckpObjectHandleArray, ckActualObjectCount);
}
jObjectHandleArray = ckULongArrayToJLongArray(env, ckpObjectHandleArray, ckActualObjectCount);
free(ckpObjectHandleArray); free(ckpObjectHandleArray);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; }
return jObjectHandleArray ; return jObjectHandleArray ;
} }
#endif #endif
......
/* /*
* Portions Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. * Portions Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
*/ */
/* Copyright (c) 2002 Graz University of Technology. All rights reserved. /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
...@@ -97,6 +97,10 @@ JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1OpenSession ...@@ -97,6 +97,10 @@ JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1OpenSession
#ifndef NO_CALLBACKS #ifndef NO_CALLBACKS
if (jNotify != NULL) { if (jNotify != NULL) {
notifyEncapsulation = (NotifyEncapsulation *) malloc(sizeof(NotifyEncapsulation)); notifyEncapsulation = (NotifyEncapsulation *) malloc(sizeof(NotifyEncapsulation));
if (notifyEncapsulation == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return 0L;
}
notifyEncapsulation->jApplicationData = (jApplication != NULL) notifyEncapsulation->jApplicationData = (jApplication != NULL)
? (*env)->NewGlobalRef(env, jApplication) ? (*env)->NewGlobalRef(env, jApplication)
: NULL; : NULL;
...@@ -118,7 +122,18 @@ JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1OpenSession ...@@ -118,7 +122,18 @@ JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1OpenSession
TRACE0(" ... "); TRACE0(" ... ");
rv = (*ckpFunctions->C_OpenSession)(ckSlotID, ckFlags, ckpApplication, ckNotify, &ckSessionHandle); rv = (*ckpFunctions->C_OpenSession)(ckSlotID, ckFlags, ckpApplication, ckNotify, &ckSessionHandle);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
#ifndef NO_CALLBACKS
if (notifyEncapsulation != NULL) {
if (notifyEncapsulation->jApplicationData != NULL) {
(*env)->DeleteGlobalRef(env, jApplication);
}
(*env)->DeleteGlobalRef(env, jNotify);
free(notifyEncapsulation);
}
#endif /* NO_CALLBACKS */
return 0L;
}
TRACE0("got session"); TRACE0("got session");
TRACE1(", SessionHandle=%u", ckSessionHandle); TRACE1(", SessionHandle=%u", ckSessionHandle);
...@@ -163,7 +178,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CloseSession ...@@ -163,7 +178,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CloseSession
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
rv = (*ckpFunctions->C_CloseSession)(ckSessionHandle); rv = (*ckpFunctions->C_CloseSession)(ckSessionHandle);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
#ifndef NO_CALLBACKS #ifndef NO_CALLBACKS
notifyEncapsulation = removeNotifyEntry(env, ckSessionHandle); notifyEncapsulation = removeNotifyEntry(env, ckSessionHandle);
...@@ -208,7 +223,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CloseAllSessio ...@@ -208,7 +223,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CloseAllSessio
ckSlotID = jLongToCKULong(jSlotID); ckSlotID = jLongToCKULong(jSlotID);
rv = (*ckpFunctions->C_CloseAllSessions)(ckSlotID); rv = (*ckpFunctions->C_CloseAllSessions)(ckSlotID);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
#ifndef NO_CALLBACKS #ifndef NO_CALLBACKS
/* Remove all notify callback helper objects. */ /* Remove all notify callback helper objects. */
...@@ -250,10 +265,9 @@ JNIEXPORT jobject JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetSessionI ...@@ -250,10 +265,9 @@ JNIEXPORT jobject JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetSessionI
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
rv = (*ckpFunctions->C_GetSessionInfo)(ckSessionHandle, &ckSessionInfo); rv = (*ckpFunctions->C_GetSessionInfo)(ckSessionHandle, &ckSessionInfo);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jSessionInfo = ckSessionInfoPtrToJSessionInfo(env, &ckSessionInfo);
jSessionInfo = ckSessionInfoPtrToJSessionInfo(env, &ckSessionInfo); }
return jSessionInfo ; return jSessionInfo ;
} }
#endif #endif
...@@ -274,7 +288,7 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetOpera ...@@ -274,7 +288,7 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetOpera
CK_SESSION_HANDLE ckSessionHandle; CK_SESSION_HANDLE ckSessionHandle;
CK_BYTE_PTR ckpState; CK_BYTE_PTR ckpState;
CK_ULONG ckStateLength; CK_ULONG ckStateLength;
jbyteArray jState; jbyteArray jState = NULL;
CK_RV rv; CK_RV rv;
CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
...@@ -283,17 +297,20 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetOpera ...@@ -283,17 +297,20 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetOpera
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
rv = (*ckpFunctions->C_GetOperationState)(ckSessionHandle, NULL_PTR, &ckStateLength); rv = (*ckpFunctions->C_GetOperationState)(ckSessionHandle, NULL_PTR, &ckStateLength);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; }
ckpState = (CK_BYTE_PTR) malloc(ckStateLength); ckpState = (CK_BYTE_PTR) malloc(ckStateLength);
if (ckpState == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return NULL;
}
rv = (*ckpFunctions->C_GetOperationState)(ckSessionHandle, ckpState, &ckStateLength); rv = (*ckpFunctions->C_GetOperationState)(ckSessionHandle, ckpState, &ckStateLength);
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jState = ckByteArrayToJByteArray(env, ckpState, ckStateLength); jState = ckByteArrayToJByteArray(env, ckpState, ckStateLength);
}
free(ckpState); free(ckpState);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; }
return jState ; return jState ;
} }
#endif #endif
...@@ -325,6 +342,8 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SetOperationSt ...@@ -325,6 +342,8 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SetOperationSt
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jByteArrayToCKByteArray(env, jOperationState, &ckpState, &ckStateLength); jByteArrayToCKByteArray(env, jOperationState, &ckpState, &ckStateLength);
if ((*env)->ExceptionCheck(env)) { return; }
ckEncryptionKeyHandle = jLongToCKULong(jEncryptionKeyHandle); ckEncryptionKeyHandle = jLongToCKULong(jEncryptionKeyHandle);
ckAuthenticationKeyHandle = jLongToCKULong(jAuthenticationKeyHandle); ckAuthenticationKeyHandle = jLongToCKULong(jAuthenticationKeyHandle);
...@@ -332,7 +351,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SetOperationSt ...@@ -332,7 +351,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SetOperationSt
free(ckpState); free(ckpState);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
...@@ -362,12 +381,13 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Login ...@@ -362,12 +381,13 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Login
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
ckUserType = jLongToCKULong(jUserType); ckUserType = jLongToCKULong(jUserType);
jCharArrayToCKCharArray(env, jPin, &ckpPinArray, &ckPinLength); jCharArrayToCKCharArray(env, jPin, &ckpPinArray, &ckPinLength);
if ((*env)->ExceptionCheck(env)) { return; }
rv = (*ckpFunctions->C_Login)(ckSessionHandle, ckUserType, ckpPinArray, ckPinLength); rv = (*ckpFunctions->C_Login)(ckSessionHandle, ckUserType, ckpPinArray, ckPinLength);
free(ckpPinArray); free(ckpPinArray);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
...@@ -391,7 +411,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Logout ...@@ -391,7 +411,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Logout
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
rv = (*ckpFunctions->C_Logout)(ckSessionHandle); rv = (*ckpFunctions->C_Logout)(ckSessionHandle);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
...@@ -410,10 +430,14 @@ void putNotifyEntry(JNIEnv *env, CK_SESSION_HANDLE hSession, NotifyEncapsulation ...@@ -410,10 +430,14 @@ void putNotifyEntry(JNIEnv *env, CK_SESSION_HANDLE hSession, NotifyEncapsulation
NotifyListNode *currentNode, *newNode; NotifyListNode *currentNode, *newNode;
if (notifyEncapsulation == NULL) { if (notifyEncapsulation == NULL) {
return ; return;
} }
newNode = (NotifyListNode *) malloc(sizeof(NotifyListNode)); newNode = (NotifyListNode *) malloc(sizeof(NotifyListNode));
if (newNode == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return;
}
newNode->hSession = hSession; newNode->hSession = hSession;
newNode->notifyEncapsulation = notifyEncapsulation; newNode->notifyEncapsulation = notifyEncapsulation;
newNode->next = NULL; newNode->next = NULL;
...@@ -578,9 +602,10 @@ CK_RV notifyCallback( ...@@ -578,9 +602,10 @@ CK_RV notifyCallback(
jEvent = ckULongToJLong(event); jEvent = ckULongToJLong(event);
ckNotifyClass = (*env)->FindClass(env, CLASS_NOTIFY); ckNotifyClass = (*env)->FindClass(env, CLASS_NOTIFY);
assert(ckNotifyClass != 0); if (ckNotifyClass == NULL) { return rv; }
jmethod = (*env)->GetMethodID(env, ckNotifyClass, "CK_NOTIFY", "(JJLjava/lang/Object;)V"); jmethod = (*env)->GetMethodID(env, ckNotifyClass, "CK_NOTIFY", "(JJLjava/lang/Object;)V");
assert(jmethod != 0); if (jmethod == NULL) { return rv; }
(*env)->CallVoidMethod(env, notifyEncapsulation->jNotifyObject, jmethod, (*env)->CallVoidMethod(env, notifyEncapsulation->jNotifyObject, jmethod,
jSessionHandle, jEvent, notifyEncapsulation->jApplicationData); jSessionHandle, jEvent, notifyEncapsulation->jApplicationData);
...@@ -588,10 +613,14 @@ CK_RV notifyCallback( ...@@ -588,10 +613,14 @@ CK_RV notifyCallback(
pkcs11Exception = (*env)->ExceptionOccurred(env); pkcs11Exception = (*env)->ExceptionOccurred(env);
if (pkcs11Exception != NULL) { if (pkcs11Exception != NULL) {
/* TBD: clear the pending exception with ExceptionClear? */
/* The was an exception thrown, now we get the error-code from it */ /* The was an exception thrown, now we get the error-code from it */
pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION); pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
if (pkcs11ExceptionClass == NULL) { return rv; }
jmethod = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J"); jmethod = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
assert(jmethod != 0); if (jmethod == NULL) { return rv; }
errorCode = (*env)->CallLongMethod(env, pkcs11Exception, jmethod); errorCode = (*env)->CallLongMethod(env, pkcs11Exception, jmethod);
rv = jLongToCKULong(errorCode); rv = jLongToCKULong(errorCode);
} }
......
/* /*
* Portions Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved. * Portions Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
*/ */
/* Copyright (c) 2002 Graz University of Technology. All rights reserved. /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
...@@ -77,15 +77,16 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignInit ...@@ -77,15 +77,16 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignInit
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jMechanismToCKMechanism(env, jMechanism, &ckMechanism); jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
if ((*env)->ExceptionCheck(env)) { return; }
ckKeyHandle = jLongToCKULong(jKeyHandle); ckKeyHandle = jLongToCKULong(jKeyHandle);
rv = (*ckpFunctions->C_SignInit)(ckSessionHandle, &ckMechanism, ckKeyHandle); rv = (*ckpFunctions->C_SignInit)(ckSessionHandle, &ckMechanism, ckKeyHandle);
if(ckMechanism.pParameter != NULL_PTR) { if (ckMechanism.pParameter != NULL_PTR) {
free(ckMechanism.pParameter); free(ckMechanism.pParameter);
} }
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
...@@ -117,14 +118,23 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Sign ...@@ -117,14 +118,23 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Sign
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jByteArrayToCKByteArray(env, jData, &ckpData, &ckDataLength); jByteArrayToCKByteArray(env, jData, &ckpData, &ckDataLength);
if ((*env)->ExceptionCheck(env)) { return NULL; }
/* START standard code */ /* START standard code */
/* first determine the length of the signature */ /* first determine the length of the signature */
rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength, NULL_PTR, &ckSignatureLength); rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength, NULL_PTR, &ckSignatureLength);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
free(ckpData);
return NULL;
}
ckpSignature = (CK_BYTE_PTR) malloc(ckSignatureLength * sizeof(CK_BYTE)); ckpSignature = (CK_BYTE_PTR) malloc(ckSignatureLength * sizeof(CK_BYTE));
if (ckpSignature == NULL) {
free(ckpData);
JNU_ThrowOutOfMemoryError(env, 0);
return NULL;
}
/* now get the signature */ /* now get the signature */
rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength, ckpSignature, &ckSignatureLength); rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength, ckpSignature, &ckSignatureLength);
...@@ -134,22 +144,31 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Sign ...@@ -134,22 +144,31 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Sign
/* START workaround code for operation abort bug in pkcs#11 of Datakey and iButton */ /* START workaround code for operation abort bug in pkcs#11 of Datakey and iButton */
/* /*
ckpSignature = (CK_BYTE_PTR) malloc(256 * sizeof(CK_BYTE)); ckpSignature = (CK_BYTE_PTR) malloc(256 * sizeof(CK_BYTE));
if (ckpSignature == NULL) {
free(ckpData);
JNU_ThrowOutOfMemoryError(env, 0);
return NULL;
}
rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength, ckpSignature, &ckSignatureLength); rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength, ckpSignature, &ckSignatureLength);
if (rv == CKR_BUFFER_TOO_SMALL) { if (rv == CKR_BUFFER_TOO_SMALL) {
free(ckpSignature); free(ckpSignature);
ckpSignature = (CK_BYTE_PTR) malloc(ckSignatureLength * sizeof(CK_BYTE)); ckpSignature = (CK_BYTE_PTR) malloc(ckSignatureLength * sizeof(CK_BYTE));
if (ckpSignature == NULL) {
free(ckpData);
JNU_ThrowOutOfMemoryError(env, 0);
return NULL;
}
rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength, ckpSignature, &ckSignatureLength); rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength, ckpSignature, &ckSignatureLength);
} }
*/ */
/* END workaround code */ /* END workaround code */
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jSignature = ckByteArrayToJByteArray(env, ckpSignature, ckSignatureLength); jSignature = ckByteArrayToJByteArray(env, ckpSignature, ckSignatureLength);
}
free(ckpData); free(ckpData);
free(ckpSignature); free(ckpSignature);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; }
return jSignature ; return jSignature ;
} }
#endif #endif
...@@ -189,14 +208,22 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignUpdate ...@@ -189,14 +208,22 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignUpdate
bufP = BUF; bufP = BUF;
} else { } else {
bufLen = min(MAX_HEAP_BUFFER_LEN, jInLen); bufLen = min(MAX_HEAP_BUFFER_LEN, jInLen);
bufP = (CK_BYTE_PTR)malloc((size_t)bufLen); bufP = (CK_BYTE_PTR) malloc((size_t)bufLen);
if (bufP == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return;
}
} }
while (jInLen > 0) { while (jInLen > 0) {
jsize chunkLen = min(bufLen, jInLen); jsize chunkLen = min(bufLen, jInLen);
(*env)->GetByteArrayRegion(env, jIn, jInOfs, chunkLen, (jbyte *)bufP); (*env)->GetByteArrayRegion(env, jIn, jInOfs, chunkLen, (jbyte *)bufP);
if ((*env)->ExceptionCheck(env)) {
if (bufP != BUF) { free(bufP); }
return;
}
rv = (*ckpFunctions->C_SignUpdate)(ckSessionHandle, bufP, chunkLen); rv = (*ckpFunctions->C_SignUpdate)(ckSessionHandle, bufP, chunkLen);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
if (bufP != BUF) { if (bufP != BUF) {
free(bufP); free(bufP);
} }
...@@ -206,9 +233,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignUpdate ...@@ -206,9 +233,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignUpdate
jInLen -= chunkLen; jInLen -= chunkLen;
} }
if (bufP != BUF) { if (bufP != BUF) { free(bufP); }
free(bufP);
}
} }
#endif #endif
...@@ -244,15 +269,18 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignFina ...@@ -244,15 +269,18 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignFina
rv = (*ckpFunctions->C_SignFinal)(ckSessionHandle, bufP, &ckSignatureLength); rv = (*ckpFunctions->C_SignFinal)(ckSessionHandle, bufP, &ckSignatureLength);
if (rv == CKR_BUFFER_TOO_SMALL) { if (rv == CKR_BUFFER_TOO_SMALL) {
bufP = (CK_BYTE_PTR) malloc(ckSignatureLength); bufP = (CK_BYTE_PTR) malloc(ckSignatureLength);
if (bufP == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return NULL;
}
rv = (*ckpFunctions->C_SignFinal)(ckSessionHandle, bufP, &ckSignatureLength); rv = (*ckpFunctions->C_SignFinal)(ckSessionHandle, bufP, &ckSignatureLength);
} }
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) { if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
jSignature = ckByteArrayToJByteArray(env, bufP, ckSignatureLength); jSignature = ckByteArrayToJByteArray(env, bufP, ckSignatureLength);
} }
if (bufP != BUF) { if (bufP != BUF) { free(bufP); }
free(bufP);
}
return jSignature; return jSignature;
} }
#endif #endif
...@@ -280,11 +308,13 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignRecoverIni ...@@ -280,11 +308,13 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignRecoverIni
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jMechanismToCKMechanism(env, jMechanism, &ckMechanism); jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
if ((*env)->ExceptionCheck(env)) { return; }
ckKeyHandle = jLongToCKULong(jKeyHandle); ckKeyHandle = jLongToCKULong(jKeyHandle);
rv = (*ckpFunctions->C_SignRecoverInit)(ckSessionHandle, &ckMechanism, ckKeyHandle); rv = (*ckpFunctions->C_SignRecoverInit)(ckSessionHandle, &ckMechanism, ckKeyHandle);
if(ckMechanism.pParameter != NULL_PTR) { if (ckMechanism.pParameter != NULL_PTR) {
free(ckMechanism.pParameter); free(ckMechanism.pParameter);
} }
...@@ -323,26 +353,38 @@ JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignRecover ...@@ -323,26 +353,38 @@ JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignRecover
if (jInLen <= MAX_STACK_BUFFER_LEN) { if (jInLen <= MAX_STACK_BUFFER_LEN) {
inBufP = INBUF; inBufP = INBUF;
} else { } else {
inBufP = (CK_BYTE_PTR)malloc((size_t)jInLen); inBufP = (CK_BYTE_PTR) malloc((size_t)jInLen);
if (inBufP == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
} }
(*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP); (*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP);
if ((*env)->ExceptionCheck(env)) {
if (inBufP != INBUF) { free(inBufP); }
return 0;
}
rv = (*ckpFunctions->C_SignRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckSignatureLength); rv = (*ckpFunctions->C_SignRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckSignatureLength);
/* re-alloc larger buffer if it fits into our Java buffer */ /* re-alloc larger buffer if it fits into our Java buffer */
if ((rv == CKR_BUFFER_TOO_SMALL) && (ckSignatureLength <= jIntToCKULong(jOutLen))) { if ((rv == CKR_BUFFER_TOO_SMALL) && (ckSignatureLength <= jIntToCKULong(jOutLen))) {
outBufP = (CK_BYTE_PTR) malloc(ckSignatureLength); outBufP = (CK_BYTE_PTR) malloc(ckSignatureLength);
if (outBufP == NULL) {
if (inBufP != INBUF) {
free(inBufP);
}
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
rv = (*ckpFunctions->C_SignRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckSignatureLength); rv = (*ckpFunctions->C_SignRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckSignatureLength);
} }
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) { if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
(*env)->SetByteArrayRegion(env, jOut, jOutOfs, ckSignatureLength, (jbyte *)outBufP); (*env)->SetByteArrayRegion(env, jOut, jOutOfs, ckSignatureLength, (jbyte *)outBufP);
} }
if (inBufP != INBUF) { if (inBufP != INBUF) { free(inBufP); }
free(inBufP); if (outBufP != OUTBUF) { free(outBufP); }
}
if (outBufP != OUTBUF) {
free(outBufP);
}
return ckSignatureLength; return ckSignatureLength;
} }
#endif #endif
...@@ -370,6 +412,8 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyInit ...@@ -370,6 +412,8 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyInit
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jMechanismToCKMechanism(env, jMechanism, &ckMechanism); jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
if ((*env)->ExceptionCheck(env)) { return; }
ckKeyHandle = jLongToCKULong(jKeyHandle); ckKeyHandle = jLongToCKULong(jKeyHandle);
rv = (*ckpFunctions->C_VerifyInit)(ckSessionHandle, &ckMechanism, ckKeyHandle); rv = (*ckpFunctions->C_VerifyInit)(ckSessionHandle, &ckMechanism, ckKeyHandle);
...@@ -378,7 +422,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyInit ...@@ -378,7 +422,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyInit
free(ckMechanism.pParameter); free(ckMechanism.pParameter);
} }
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
...@@ -409,7 +453,13 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Verify ...@@ -409,7 +453,13 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Verify
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jByteArrayToCKByteArray(env, jData, &ckpData, &ckDataLength); jByteArrayToCKByteArray(env, jData, &ckpData, &ckDataLength);
if ((*env)->ExceptionCheck(env)) { return; }
jByteArrayToCKByteArray(env, jSignature, &ckpSignature, &ckSignatureLength); jByteArrayToCKByteArray(env, jSignature, &ckpSignature, &ckSignatureLength);
if ((*env)->ExceptionCheck(env)) {
free(ckpData);
return;
}
/* verify the signature */ /* verify the signature */
rv = (*ckpFunctions->C_Verify)(ckSessionHandle, ckpData, ckDataLength, ckpSignature, ckSignatureLength); rv = (*ckpFunctions->C_Verify)(ckSessionHandle, ckpData, ckDataLength, ckpSignature, ckSignatureLength);
...@@ -417,7 +467,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Verify ...@@ -417,7 +467,7 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Verify
free(ckpData); free(ckpData);
free(ckpSignature); free(ckpSignature);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
...@@ -456,26 +506,31 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyUpdate ...@@ -456,26 +506,31 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyUpdate
bufP = BUF; bufP = BUF;
} else { } else {
bufLen = min(MAX_HEAP_BUFFER_LEN, jInLen); bufLen = min(MAX_HEAP_BUFFER_LEN, jInLen);
bufP = (CK_BYTE_PTR)malloc((size_t)bufLen); bufP = (CK_BYTE_PTR) malloc((size_t)bufLen);
if (bufP == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return;
}
} }
while (jInLen > 0) { while (jInLen > 0) {
jsize chunkLen = min(bufLen, jInLen); jsize chunkLen = min(bufLen, jInLen);
(*env)->GetByteArrayRegion(env, jIn, jInOfs, chunkLen, (jbyte *)bufP); (*env)->GetByteArrayRegion(env, jIn, jInOfs, chunkLen, (jbyte *)bufP);
if ((*env)->ExceptionCheck(env)) {
if (bufP != BUF) { free(bufP); }
return;
}
rv = (*ckpFunctions->C_VerifyUpdate)(ckSessionHandle, bufP, chunkLen); rv = (*ckpFunctions->C_VerifyUpdate)(ckSessionHandle, bufP, chunkLen);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
if (bufP != BUF) { if (bufP != BUF) { free(bufP); }
free(bufP);
}
return; return;
} }
jInOfs += chunkLen; jInOfs += chunkLen;
jInLen -= chunkLen; jInLen -= chunkLen;
} }
if (bufP != BUF) { if (bufP != BUF) { free(bufP); }
free(bufP);
}
} }
#endif #endif
...@@ -502,13 +557,14 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyFinal ...@@ -502,13 +557,14 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyFinal
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jByteArrayToCKByteArray(env, jSignature, &ckpSignature, &ckSignatureLength); jByteArrayToCKByteArray(env, jSignature, &ckpSignature, &ckSignatureLength);
if ((*env)->ExceptionCheck(env)) { return; }
/* verify the signature */ /* verify the signature */
rv = (*ckpFunctions->C_VerifyFinal)(ckSessionHandle, ckpSignature, ckSignatureLength); rv = (*ckpFunctions->C_VerifyFinal)(ckSessionHandle, ckpSignature, ckSignatureLength);
free(ckpSignature); free(ckpSignature);
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
...@@ -535,15 +591,17 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyRecoverI ...@@ -535,15 +591,17 @@ JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyRecoverI
ckSessionHandle = jLongToCKULong(jSessionHandle); ckSessionHandle = jLongToCKULong(jSessionHandle);
jMechanismToCKMechanism(env, jMechanism, &ckMechanism); jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
if ((*env)->ExceptionCheck(env)) { return; }
ckKeyHandle = jLongToCKULong(jKeyHandle); ckKeyHandle = jLongToCKULong(jKeyHandle);
rv = (*ckpFunctions->C_VerifyRecoverInit)(ckSessionHandle, &ckMechanism, ckKeyHandle); rv = (*ckpFunctions->C_VerifyRecoverInit)(ckSessionHandle, &ckMechanism, ckKeyHandle);
if(ckMechanism.pParameter != NULL_PTR) { if (ckMechanism.pParameter != NULL_PTR) {
free(ckMechanism.pParameter); free(ckMechanism.pParameter);
} }
if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; } if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
} }
#endif #endif
...@@ -578,26 +636,38 @@ JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyRecover ...@@ -578,26 +636,38 @@ JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1VerifyRecover
if (jInLen <= MAX_STACK_BUFFER_LEN) { if (jInLen <= MAX_STACK_BUFFER_LEN) {
inBufP = INBUF; inBufP = INBUF;
} else { } else {
inBufP = (CK_BYTE_PTR)malloc((size_t)jInLen); inBufP = (CK_BYTE_PTR) malloc((size_t)jInLen);
if (inBufP == NULL) {
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
} }
(*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP); (*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)inBufP);
if ((*env)->ExceptionCheck(env)) {
if (inBufP != INBUF) { free(inBufP); }
return 0;
}
rv = (*ckpFunctions->C_VerifyRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckDataLength); rv = (*ckpFunctions->C_VerifyRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckDataLength);
/* re-alloc larger buffer if it fits into our Java buffer */ /* re-alloc larger buffer if it fits into our Java buffer */
if ((rv == CKR_BUFFER_TOO_SMALL) && (ckDataLength <= jIntToCKULong(jOutLen))) { if ((rv == CKR_BUFFER_TOO_SMALL) && (ckDataLength <= jIntToCKULong(jOutLen))) {
outBufP = (CK_BYTE_PTR) malloc(ckDataLength); outBufP = (CK_BYTE_PTR) malloc(ckDataLength);
if (outBufP == NULL) {
if (inBufP != INBUF) { free(inBufP); }
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
rv = (*ckpFunctions->C_VerifyRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckDataLength); rv = (*ckpFunctions->C_VerifyRecover)(ckSessionHandle, inBufP, jInLen, outBufP, &ckDataLength);
} }
if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) { if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
(*env)->SetByteArrayRegion(env, jOut, jOutOfs, ckDataLength, (jbyte *)outBufP); (*env)->SetByteArrayRegion(env, jOut, jOutOfs, ckDataLength, (jbyte *)outBufP);
} }
if (inBufP != INBUF) { if (inBufP != INBUF) { free(inBufP); }
free(inBufP); if (outBufP != OUTBUF) { free(outBufP); }
}
if (outBufP != OUTBUF) {
free(outBufP);
}
return ckDataLength; return ckDataLength;
} }
#endif #endif
/* /*
* Portions Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved. * Portions Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
*/ */
/* Copyright (c) 2002 Graz University of Technology. All rights reserved. /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
...@@ -154,6 +154,7 @@ ...@@ -154,6 +154,7 @@
#include "pkcs11.h" #include "pkcs11.h"
#include <jni.h> #include <jni.h>
#include <jni_util.h>
#define MAX_STACK_BUFFER_LEN (4 * 1024) #define MAX_STACK_BUFFER_LEN (4 * 1024)
#define MAX_HEAP_BUFFER_LEN (64 * 1024) #define MAX_HEAP_BUFFER_LEN (64 * 1024)
...@@ -277,12 +278,14 @@ ...@@ -277,12 +278,14 @@
*/ */
jlong ckAssertReturnValueOK(JNIEnv *env, CK_RV returnValue); jlong ckAssertReturnValueOK(JNIEnv *env, CK_RV returnValue);
void throwPKCS11RuntimeException(JNIEnv *env, jstring jmessage);
void throwFileNotFoundException(JNIEnv *env, jstring jmessage);
void throwIOException(JNIEnv *env, const char *message); void throwIOException(JNIEnv *env, const char *message);
void throwIOExceptionUnicodeMessage(JNIEnv *env, const short *message); void throwPKCS11RuntimeException(JNIEnv *env, const char *message);
void throwDisconnectedRuntimeException(JNIEnv *env); void throwDisconnectedRuntimeException(JNIEnv *env);
/* function to free CK_ATTRIBUTE array
*/
void freeCKAttributeArray(CK_ATTRIBUTE_PTR attrPtr, int len);
/* funktions to convert Java arrays to a CK-type array and the array length */ /* funktions to convert Java arrays to a CK-type array and the array length */
void jBooleanArrayToCKBBoolArray(JNIEnv *env, const jbooleanArray jArray, CK_BBOOL **ckpArray, CK_ULONG_PTR ckLength); void jBooleanArrayToCKBBoolArray(JNIEnv *env, const jbooleanArray jArray, CK_BBOOL **ckpArray, CK_ULONG_PTR ckLength);
...@@ -438,3 +441,15 @@ extern jobject notifyListLock; ...@@ -438,3 +441,15 @@ extern jobject notifyListLock;
extern jobject jInitArgsObject; extern jobject jInitArgsObject;
extern CK_C_INITIALIZE_ARGS_PTR ckpGlobalInitArgs; extern CK_C_INITIALIZE_ARGS_PTR ckpGlobalInitArgs;
#endif /* NO_CALLBACKS */ #endif /* NO_CALLBACKS */
#ifdef P11_MEMORYDEBUG
#include <stdlib.h>
/* Simple malloc/free dumper */
void *p11malloc(size_t c, char *file, int line);
void p11free(void *p, char *file, int line);
#define malloc(c) (p11malloc((c), __FILE__, __LINE__))
#define free(c) (p11free((c), __FILE__, __LINE__))
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册