javacommons/gcfprotocols/https/src.s60/securityinfoimpl.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2002-2003 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 #include <badesca.h>
       
    19 
       
    20 #include "mnativesecureconnectioninformation.h"
       
    21 #include "com_nokia_mj_impl_https_SecurityInfoImpl.h"
       
    22 #include "s60commonutils.h"
       
    23 #include "logger.h"
       
    24 
       
    25 
       
    26 using namespace java::util;
       
    27 
       
    28 // Constants that define the date/time '00:00, 1 Jan 1970' when used to create a TTime object
       
    29 const TUint JavaUpperTimeFor1970 = 14474675;
       
    30 const TUint JavaLowerTimeFor1970 = 254771200;
       
    31 
       
    32 /* Takes a jlong which is the number of milliseconds from the Java epoch time
       
    33 of 00:00 1st Jan 1970 and converts it into a EPOC TTime object */
       
    34 TTime CreateEpocTTime(jlong aJavaTime)
       
    35 {
       
    36     // Convert jlong to a TInt64
       
    37     TInt64 timeNum = *reinterpret_cast<TInt64*>(&aJavaTime);
       
    38     // Create a TTime object that represents the Java Date 'epoch' time of 00:00, 1 Jan 1970
       
    39     TInt64 javaEpocTimeNum = MAKE_TINT64(JavaUpperTimeFor1970, JavaLowerTimeFor1970);
       
    40     TTime time(javaEpocTimeNum);
       
    41     TTimeIntervalMicroSeconds delta(timeNum * 1000);
       
    42     return time + delta;
       
    43 }
       
    44 
       
    45 /* Takes a TTime and returns the number of milliseconds since the Java epoch time
       
    46 of 00:00 1st Jan 1970 */
       
    47 jlong CreateJavaTime(TTime aEpocTime)
       
    48 {
       
    49     // Create a TTime object that represents the Java Date 'epoch' time of 00:00, 1 Jan 1970
       
    50     TInt64 javaEpocTimeNum = MAKE_TINT64(JavaUpperTimeFor1970, JavaLowerTimeFor1970);
       
    51     TTime javaEpochTime(javaEpocTimeNum);
       
    52     // Find difference in microseconds between 'epoch' and EPOC date and adjust to milliseconds
       
    53     TTimeIntervalMicroSeconds microInterval = aEpocTime.MicroSecondsFrom(javaEpochTime);
       
    54     TInt64 intervalNum = microInterval.Int64();
       
    55     intervalNum /= 1000;
       
    56     jlong jInterval = *reinterpret_cast<jlong*>(&intervalNum);
       
    57     return jInterval;
       
    58 }
       
    59 
       
    60 void AddToJavaStringArrayL(JNIEnv& aJni, jobjectArray& aContainer, TInt aPosition, const TDesC& aString)
       
    61 {
       
    62     jstring javaString = S60CommonUtils::NativeToJavaString(aJni, aString);
       
    63     if (javaString == NULL)
       
    64         return;
       
    65 
       
    66     aJni.SetObjectArrayElement(aContainer, aPosition, javaString);
       
    67     aJni.DeleteLocalRef(javaString);
       
    68 }
       
    69 
       
    70 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_https_SecurityInfoImpl__1createSecurityInfo(
       
    71     JNIEnv* aJni,
       
    72     jobject,
       
    73     jint aSecureConnectionHandle,
       
    74     jobjectArray aStringResultArray,
       
    75     jlongArray aLongResultArray)
       
    76 {
       
    77     //Java unhand uses a reinterpret_cast which messes up the vtables
       
    78     //as we are casting to a mixin.
       
    79     LOG(ESOCKET,EInfo,"++ jni _createSecurityInfo");
       
    80     MNativeSecureConnectionInformation* endPoint = reinterpret_cast<MNativeSecureConnectionInformation*>(aSecureConnectionHandle);
       
    81     TTime start = TTime();
       
    82     TTime end = TTime();
       
    83     CDesCArraySeg* stringResult = NULL;
       
    84 
       
    85 
       
    86     stringResult = new CDesCArraySeg(com_nokia_mj_impl_https_SecurityInfoImpl_DATA_STRING_NUMBER);
       
    87 
       
    88     if (!stringResult)
       
    89         return KErrNoMemory;
       
    90 
       
    91     TInt error = KErrNone;
       
    92 
       
    93     if (endPoint)
       
    94     {
       
    95         TRAP(error, endPoint->GetSecurityInfoL(*stringResult, start, end););
       
    96     }
       
    97 
       
    98     if (error == KErrNone)
       
    99     {
       
   100         jlong longResult = CreateJavaTime(start);
       
   101         aJni->SetLongArrayRegion(aLongResultArray,
       
   102                                  com_nokia_mj_impl_https_SecurityInfoImpl_CERT_NOT_BEFORE,
       
   103                                  1, &longResult);
       
   104         longResult = CreateJavaTime(end);
       
   105         aJni->SetLongArrayRegion(aLongResultArray,
       
   106                                  com_nokia_mj_impl_https_SecurityInfoImpl_CERT_NOT_AFTER,
       
   107                                  1, &longResult);
       
   108         for (TInt i = 0; i < stringResult->Count() && error == KErrNone; ++i)
       
   109         {
       
   110             TRAP(error, AddToJavaStringArrayL(*aJni, aStringResultArray, i, (*stringResult)[i]));
       
   111         }
       
   112         delete stringResult;
       
   113     }
       
   114     //Ignore the error if there is no cert, Java will throw a Certificate Exception
       
   115     if (error==KErrNotFound)
       
   116         error=KErrNone;
       
   117     LOG(ESOCKET,EInfo,"-- jni _createSecurityInfo");
       
   118     return error;
       
   119 }
       
   120 
       
   121 
       
   122 
       
   123