javacommons/security/src.s60/ocspchecker.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "javajniutils.h"
       
    20 #include "com_nokia_mj_impl_security_midp_authentication_OcspChecker.h"
       
    21 #include "ocspclient.h"
       
    22 #include "ocspnativethread.h"
       
    23 #include "fs_methodcall.h"
       
    24 
       
    25 using namespace java::security;
       
    26 using namespace std;
       
    27 using namespace java::util;
       
    28 
       
    29 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_security_midp_authentication_OcspChecker__1createNativeThread
       
    30 (JNIEnv * , jobject)
       
    31 {
       
    32     OcspNativeThread* ocspNativeThread = new OcspNativeThread();
       
    33     return reinterpret_cast<jint>(ocspNativeThread);
       
    34 }
       
    35 
       
    36 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_security_midp_authentication_OcspChecker__1destroyNativeThread
       
    37 (JNIEnv *, jobject, jint aNativeThreadHandle)
       
    38 {
       
    39     if (aNativeThreadHandle < KErrNone)
       
    40     {
       
    41         return;
       
    42     }
       
    43     OcspNativeThread* ocspNativeThread =
       
    44         reinterpret_cast< OcspNativeThread* >(aNativeThreadHandle);
       
    45     if (ocspNativeThread != NULL)
       
    46     {
       
    47         delete ocspNativeThread;
       
    48         ocspNativeThread = NULL;
       
    49     }
       
    50 }
       
    51 
       
    52 LOCAL_C OcspClient* CreateNativePeer(long iap, const char* ocspDefaultUrl)
       
    53 {
       
    54     OcspClient* ocspClient = OcspClient::createInstance(iap, ocspDefaultUrl);
       
    55     return ocspClient;
       
    56 }
       
    57 
       
    58 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_security_midp_authentication_OcspChecker__1createNativePeer
       
    59 (JNIEnv * env, jobject, jint aNativeThreadHandle, jlong iap, jlong /*snap*/, jstring jOcspDefaultUrl)
       
    60 {
       
    61     if (aNativeThreadHandle < KErrNone)
       
    62     {
       
    63         return KErrGeneral;
       
    64     }
       
    65     OcspNativeThread* ocspNativeThread =
       
    66         reinterpret_cast< OcspNativeThread* >(aNativeThreadHandle);
       
    67     OcspClient * ocspClient = NULL;
       
    68     const char* ocspDefaultUrl = NULL;
       
    69     if (jOcspDefaultUrl != NULL)
       
    70     {
       
    71         jboolean isCopy;
       
    72         ocspDefaultUrl = env->GetStringUTFChars(jOcspDefaultUrl, &isCopy);
       
    73         ocspClient = CallMethod(CreateNativePeer, iap, ocspDefaultUrl, ocspNativeThread);
       
    74         env->ReleaseStringUTFChars(jOcspDefaultUrl, ocspDefaultUrl);
       
    75     }
       
    76     else
       
    77     {
       
    78         ocspClient = CallMethod(CreateNativePeer, iap, ocspDefaultUrl, ocspNativeThread);
       
    79     }
       
    80     return reinterpret_cast<int>(ocspClient);
       
    81 }
       
    82 
       
    83 LOCAL_C void DestroyNativePeer(OcspClient* aNativePeer)
       
    84 {
       
    85     if (aNativePeer != NULL)
       
    86     {
       
    87         delete aNativePeer;
       
    88         aNativePeer = NULL;
       
    89     }
       
    90 }
       
    91 
       
    92 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_security_midp_authentication_OcspChecker__1destroyNativePeer
       
    93 (JNIEnv *, jobject, jint aNativeThreadHandle, jint aNativePeerHandle)
       
    94 {
       
    95     if (aNativeThreadHandle < KErrNone
       
    96             || aNativePeerHandle < KErrNone)
       
    97     {
       
    98         return;
       
    99     }
       
   100     OcspNativeThread* ocspNativeThread =
       
   101         reinterpret_cast< OcspNativeThread* >(aNativeThreadHandle);
       
   102     OcspClient* ocspNativePeer =
       
   103         reinterpret_cast< OcspClient* >(aNativePeerHandle);
       
   104     CallMethod(DestroyNativePeer, ocspNativePeer, ocspNativeThread);
       
   105 }
       
   106 
       
   107 LOCAL_C void StartOcspChecks(OcspClient* aOcspClient, const char** certChain, int certChainLen)
       
   108 {
       
   109     return aOcspClient->startOcspCheck(certChain, certChainLen);
       
   110 }
       
   111 
       
   112 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_security_midp_authentication_OcspChecker__1ocspChecks
       
   113 (JNIEnv * env, jobject, jint aNativeThreadHandle, jint aNativePeerHandle, jobjectArray ocspData)
       
   114 {
       
   115     if (aNativeThreadHandle < KErrNone
       
   116             || aNativePeerHandle < KErrNone)
       
   117     {
       
   118         return;
       
   119     }
       
   120     OcspNativeThread* ocspNativeThread =
       
   121         reinterpret_cast< OcspNativeThread* >(aNativeThreadHandle);
       
   122     if (ocspData == NULL)
       
   123     {
       
   124         return;
       
   125     }
       
   126     // do the OCSP check for each of the cert chains
       
   127     OcspClient* ocspNativePeer =
       
   128         reinterpret_cast< OcspClient* >(aNativePeerHandle);
       
   129     jint len = env->GetArrayLength(ocspData);
       
   130     jboolean isCopy;
       
   131     for (int i=0; i<len; i++)
       
   132     {
       
   133         jobject jOcspData = env->GetObjectArrayElement(ocspData, i);
       
   134         jclass ocspDataClass = env->GetObjectClass(jOcspData);
       
   135         jmethodID getCertChainMethod = env->GetMethodID(
       
   136                                            ocspDataClass,"getCertChain", "()[Ljava/lang/String;");
       
   137         jmethodID setIndividualResponsesMethod = env->GetMethodID(
       
   138                     ocspDataClass,"setIndividualResponses", "([I)V");
       
   139         jmethodID setSummaryMethod = env->GetMethodID(
       
   140                                          ocspDataClass,"setSummary", "(I)V");
       
   141         jobjectArray jCertChain = (jobjectArray)env->CallObjectMethod(
       
   142                                       jOcspData, getCertChainMethod);
       
   143         jint certChainLen = env->GetArrayLength(jCertChain);
       
   144         const char** certChain = new const char* [certChainLen];
       
   145         for (int j=0; j<certChainLen; j++)
       
   146         {
       
   147             jstring jCert  = (jstring)env->GetObjectArrayElement(jCertChain, j);
       
   148             const char *cert =  env->GetStringUTFChars(jCert, &isCopy);
       
   149             certChain[j] = cert;
       
   150         }
       
   151         // do the actual OCSP check for the current cert chain
       
   152         CallMethod(StartOcspChecks, ocspNativePeer, certChain, certChainLen, ocspNativeThread);
       
   153         OcspResponse ocspResponse = ocspNativePeer->getOcspCheckResponse();
       
   154         env->CallVoidMethod(jOcspData, setSummaryMethod, ocspResponse.iSummary);
       
   155         if (ocspResponse.iIndividualResponses.size() > 0)
       
   156         {
       
   157             int size = ocspResponse.iIndividualResponses.size();
       
   158             jint* tmp = new jint[size];
       
   159             for (int j=0; j<size; j++)
       
   160             {
       
   161                 tmp[j] = ocspResponse.iIndividualResponses[j];
       
   162             }
       
   163             jintArray individualResponses = (jintArray)env->NewIntArray(size);
       
   164             env->SetIntArrayRegion((jintArray)individualResponses,(jsize)0,(jsize)size,tmp);
       
   165             // set the individual responses
       
   166             env->CallVoidMethod(jOcspData, setIndividualResponsesMethod, individualResponses);
       
   167             delete[] tmp;
       
   168         }
       
   169         // cleanup
       
   170         for (int j=0; j<certChainLen; j++)
       
   171         {
       
   172             jstring jCert  = (jstring)env->GetObjectArrayElement(jCertChain, j);
       
   173             env->ReleaseStringUTFChars(jCert,certChain[j]);
       
   174         }
       
   175         delete[] certChain;
       
   176     }
       
   177 }
       
   178 
       
   179 LOCAL_C void CancelOcspCheck(OcspNativeThread* /*aOcspNativeThread*/, OcspClient* aOcspClient)
       
   180 {
       
   181     aOcspClient->cancelOcspCheck(true);
       
   182 }
       
   183 
       
   184 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_security_midp_authentication_OcspChecker__1cancelOcspChecks
       
   185 (JNIEnv *, jobject, jint aNativeThreadHandle, jint aNativePeerHandle)
       
   186 {
       
   187     if (aNativeThreadHandle < KErrNone
       
   188             || aNativePeerHandle < KErrNone)
       
   189     {
       
   190         return;
       
   191     }
       
   192     OcspNativeThread* ocspNativeThread =
       
   193         reinterpret_cast< OcspNativeThread* >(aNativeThreadHandle);
       
   194     OcspClient* ocspNativePeer =
       
   195         reinterpret_cast< OcspClient* >(aNativePeerHandle);
       
   196     ocspNativePeer->cancelOcspCheck(false);
       
   197     CallMethod(CancelOcspCheck, ocspNativeThread, ocspNativePeer, ocspNativeThread);
       
   198 }