javacommons/security/src.s60/fileutils.cpp
branchRCL_3
changeset 14 04becd199f91
child 24 6c158198356e
equal deleted inserted replaced
13:f5050f1da672 14: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 "fileutils.h"
       
    20 #include "securityutils.h"
       
    21 #include <f32file.h>
       
    22 #include <openssl/sha.h>
       
    23 #include "javajniutils.h"
       
    24 #include "com_nokia_mj_impl_security_midp_authentication_AuthenticationModule.h"
       
    25 #include <caf/caf.h>
       
    26 #include <caf/content.h>
       
    27 #include <caf/data.h>
       
    28 #include "javajniutils.h"
       
    29 
       
    30 using namespace java::security;
       
    31 
       
    32 char * FileUtils::computeDigest(const char* aFileName)
       
    33 {
       
    34     return computeDigest(aFileName, false);
       
    35 }
       
    36 
       
    37 char * FileUtils::computeDigest(const char* aFileName, bool drmContent)
       
    38 {
       
    39     RFs fs;
       
    40     TInt err = fs.Connect();
       
    41     char * digest = NULL;
       
    42     RFile file;
       
    43     ContentAccess::CContent* cafContent = NULL;
       
    44     ContentAccess::CData* cafData = NULL;
       
    45     if (err == KErrNone)
       
    46     {
       
    47         int len = strlen(aFileName);
       
    48         HBufC * fileName = HBufC::New(len);
       
    49         TPtr fileNamePtr = fileName->Des();
       
    50         TPtr8 ptr8((TUint8 *)aFileName, len);
       
    51         ptr8.SetLength(len);
       
    52         fileNamePtr.Copy(ptr8);
       
    53         if (drmContent)
       
    54         {
       
    55             TRAP(err,
       
    56                  cafContent = ContentAccess::CContent::NewL(fileNamePtr);
       
    57                  cafData = cafContent->OpenContentL(ContentAccess::EPeek););
       
    58         }
       
    59         else
       
    60         {
       
    61             err = file.Open(fs, fileNamePtr, EFileShareReadersOrWriters);
       
    62         }
       
    63         delete fileName;
       
    64         fileName = NULL;
       
    65         if (err == KErrNone)
       
    66         {
       
    67             // figure out the size of the file
       
    68             TInt size;
       
    69             if (drmContent)
       
    70             {
       
    71                 TRAP(err, cafData->DataSizeL(size));
       
    72             }
       
    73             else
       
    74             {
       
    75                 err = file.Size(size);
       
    76             }
       
    77             if (err == KErrNone)
       
    78             {
       
    79                 // if the size of the file is less than the size of the chunks,
       
    80                 // then do the hash calculation in on go
       
    81                 unsigned char * computed_digest = NULL;
       
    82                 HBufC8* buf = NULL;
       
    83                 if (size > 0 && size <= SHA_1_HASH_CHUNK_LEN)
       
    84                 {
       
    85                     // do the hash calculation in one go
       
    86                     TRAPD(err, buf = HBufC8::NewL(size););
       
    87                     if (err == KErrNone)
       
    88                     {
       
    89                         TPtr8 ptr = buf->Des();
       
    90                         if (drmContent)
       
    91                         {
       
    92                             cafData->Read(ptr, size);
       
    93                         }
       
    94                         else
       
    95                         {
       
    96                             err = file.Read(ptr, size);
       
    97                         }
       
    98                         if (err == KErrNone)
       
    99                         {
       
   100                             computed_digest = new unsigned char[SHA_1_DIGEST_LEN];
       
   101                             SHA1((const unsigned char *)buf->Ptr(), size, computed_digest);
       
   102                         }
       
   103                         delete buf;
       
   104                         buf = NULL;
       
   105                     }
       
   106 
       
   107                 }
       
   108                 else
       
   109                 {
       
   110                     // do the hash calculation in chunks
       
   111                     SHA_CTX c;
       
   112                     SHA1_Init(&c);
       
   113                     TRAPD(err, buf = HBufC8::NewL(SHA_1_HASH_CHUNK_LEN););
       
   114                     if (err == KErrNone)
       
   115                     {
       
   116                         TPtr8 ptr = buf->Des();
       
   117                         if (drmContent)
       
   118                         {
       
   119                             cafData->Read(ptr, SHA_1_HASH_CHUNK_LEN);
       
   120                         }
       
   121                         else
       
   122                         {
       
   123                             err = file.Read(ptr, SHA_1_HASH_CHUNK_LEN);
       
   124                         }
       
   125                         while (err == KErrNone
       
   126                                 && ptr.Length() > 0)
       
   127                         {
       
   128                             SHA1_Update(&c, (const unsigned char *)buf->Ptr(), ptr.Length());
       
   129                             if (drmContent)
       
   130                             {
       
   131                                 cafData->Read(ptr, SHA_1_HASH_CHUNK_LEN);
       
   132                             }
       
   133                             else
       
   134                             {
       
   135                                 err = file.Read(ptr, SHA_1_HASH_CHUNK_LEN);
       
   136                             }
       
   137                         }
       
   138                         delete buf;
       
   139                         buf = NULL;
       
   140                         if (err == KErrNone)
       
   141                         {
       
   142                             // put it all together
       
   143                             computed_digest = new unsigned char[SHA_1_DIGEST_LEN];
       
   144                             SHA1_Final(computed_digest, &c);
       
   145                         }
       
   146                     }
       
   147                 }
       
   148 
       
   149                 // format it as hex
       
   150                 if (computed_digest != NULL)
       
   151                 {
       
   152                     digest = new char[2*SHA_1_DIGEST_LEN + 1];
       
   153                     char * tmp = digest;
       
   154                     for (int i=0; i<SHA_1_DIGEST_LEN; i++)
       
   155                     {
       
   156                         sprintf(tmp, "%02X", computed_digest[i]);
       
   157                         tmp = tmp + 2;
       
   158                     }
       
   159                     delete[] computed_digest;
       
   160                     computed_digest = NULL;
       
   161                     digest[2*SHA_1_DIGEST_LEN] = '\0';
       
   162                     tmp = NULL;
       
   163                 }
       
   164             }
       
   165             if (drmContent)
       
   166             {
       
   167                 if (cafData)
       
   168                 {
       
   169                     delete cafData;
       
   170                     cafData = NULL;
       
   171                 }
       
   172                 if (cafContent)
       
   173                 {
       
   174                     delete cafContent;
       
   175                     cafContent = NULL;
       
   176                 }
       
   177             }
       
   178             else
       
   179             {
       
   180                 file.Close();
       
   181             }
       
   182         }
       
   183         fs.Close();
       
   184     }
       
   185     return digest;
       
   186 }
       
   187 
       
   188 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_security_midp_authentication_AuthenticationModule__1drmDecryptAndComputeHash
       
   189 (JNIEnv * env, jobject, jstring appJARPath)
       
   190 {
       
   191     jboolean isCopy;
       
   192     const char* jarPath(env->GetStringUTFChars(appJARPath, &isCopy));
       
   193     char * jarHashValue = FileUtils::computeDigest(jarPath, true);
       
   194     env->ReleaseStringUTFChars(appJARPath, jarPath);
       
   195     if (jarHashValue != NULL)
       
   196     {
       
   197         jstring hash = env->NewStringUTF(jarHashValue);
       
   198         delete[] jarHashValue;
       
   199         jarHashValue = NULL;
       
   200         return hash;
       
   201     }
       
   202     return NULL;
       
   203 }