javacommons/security/src/utils/storagehandler.cpp
branchRCL_3
changeset 19 04becd199f91
child 23 98ccebc37403
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2009 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 #include "storagehandler.h"
       
    18 #include "javacommonutils.h"
       
    19 #include "javastoragenames.h"
       
    20 #include "javastorage.h"
       
    21 #include "logger.h"
       
    22 
       
    23 using namespace std;
       
    24 using namespace java::storage;
       
    25 using namespace java::security;
       
    26 using namespace java::util;
       
    27 
       
    28 StorageHandler::StorageHandler()
       
    29 {
       
    30     iStorage = JavaStorage::createInstance();
       
    31     iStorage->open(JAVA_DATABASE_NAME);
       
    32 }
       
    33 
       
    34 void StorageHandler::readValidCerts(const Uid& aUid, std::list<int>& aCerts)
       
    35 {
       
    36     JavaStorageEntry attr;
       
    37     JavaStorageApplicationEntry_t entry;
       
    38 
       
    39     attr.setEntry(ID, aUid.toString());
       
    40     entry.insert(attr);
       
    41 
       
    42     attr.setEntry(VALID_CERTS, L"");
       
    43     entry.insert(attr);
       
    44 
       
    45     JavaStorageApplicationList_t foundApps;
       
    46 
       
    47     iStorage->search(MIDP_PACKAGE_TABLE, entry, foundApps);
       
    48 
       
    49     wstring certIndexes = L"";
       
    50     findEntry(foundApps, VALID_CERTS, certIndexes);
       
    51 
       
    52     wstring::size_type startIdx = 0;
       
    53     wstring::size_type endIdx = 0;
       
    54     const char delim = ',';
       
    55 
       
    56     while (wstring::npos != startIdx || wstring::npos != endIdx)
       
    57     {
       
    58         endIdx = certIndexes.find(delim, startIdx);
       
    59         if (endIdx == wstring::npos) break;    // Not found
       
    60 
       
    61         if (startIdx != endIdx)  // Skip 1,,2 case
       
    62         {
       
    63             addToValidCerts(certIndexes.substr(
       
    64                                 startIdx, (endIdx - startIdx)), aCerts);
       
    65         }
       
    66         startIdx = endIdx + 1;
       
    67     }
       
    68 
       
    69     // Read last one
       
    70     if ((startIdx = certIndexes.find_last_of(delim)) != wstring::npos)
       
    71     {
       
    72         addToValidCerts(certIndexes.substr(startIdx + 1,
       
    73                                            (certIndexes.size() - startIdx)), aCerts);
       
    74     }
       
    75 
       
    76     // If only one cert index without delimiter
       
    77     if (aCerts.size() == 0 && certIndexes.size() > 0)
       
    78     {
       
    79         addToValidCerts(certIndexes, aCerts);
       
    80     }
       
    81 }
       
    82 
       
    83 void StorageHandler::getChainFromIndex(
       
    84     const JavaStorageApplicationList_t& aAttributes,
       
    85     const int aIndex,
       
    86     std::string& aChain)
       
    87 {
       
    88     int i = 1;
       
    89     wstring chain = L"";
       
    90     bool foundPart = true;
       
    91     const wstring attrPrefix = L"MIDlet-Certificate-";
       
    92     JavaStorageEntry attr;
       
    93 
       
    94     do
       
    95     {
       
    96         wstring indexedAttrName = attrPrefix;
       
    97         indexedAttrName.append(JavaCommonUtils::intToWstring(aIndex))
       
    98         .append(L"-").append(JavaCommonUtils::intToWstring(i));
       
    99 
       
   100         attr.setEntry(NAME, indexedAttrName);
       
   101 
       
   102         JavaStorageApplicationEntry_t findEntry;
       
   103         findEntry.insert(attr);
       
   104 
       
   105         bool found = false;
       
   106         JavaStorageApplicationList_t::const_iterator appIter;
       
   107 
       
   108         for (appIter = aAttributes.begin()
       
   109                        ; appIter != aAttributes.end()
       
   110                 ; appIter++)
       
   111         {
       
   112             JavaStorageApplicationEntry_t::const_iterator entryFinder
       
   113             = (*appIter).find(attr);
       
   114 
       
   115             if (entryFinder != (*appIter).end()) // Correct one found.
       
   116             {
       
   117                 attr.setEntry(VALUE, L"");
       
   118                 entryFinder = (*appIter).find(attr); // Get its value.
       
   119 
       
   120                 if (entryFinder != (*appIter).end())
       
   121                 {
       
   122                     chain.append((*entryFinder).entryValue());
       
   123                 }
       
   124 
       
   125                 found = true;
       
   126                 break;
       
   127             }
       
   128         }
       
   129 
       
   130         if (!found)
       
   131         {
       
   132             foundPart = false;
       
   133         }
       
   134 
       
   135         i++;
       
   136     }
       
   137     while (foundPart);
       
   138 
       
   139     char* tempStr = JavaCommonUtils::wstringToUtf8(chain);
       
   140     aChain.append(tempStr);
       
   141     delete [] tempStr;
       
   142 }
       
   143 void StorageHandler::findEntry(const JavaStorageApplicationList_t& queryResult,
       
   144                                const std::wstring& eName,
       
   145                                std::wstring& eValue)
       
   146 {
       
   147     if (queryResult.size() > 0)
       
   148     {
       
   149         JavaStorageApplicationEntry_t entry = queryResult.front();
       
   150         JavaStorageEntry findPattern;
       
   151         findPattern.setEntry(eName, L"");
       
   152         JavaStorageApplicationEntry_t::const_iterator findIterator =
       
   153             entry.find(findPattern);
       
   154         if (findIterator != entry.end())
       
   155         {
       
   156             eValue = findIterator->entryValue();
       
   157         }
       
   158     }
       
   159 }
       
   160 
       
   161 void StorageHandler::addToValidCerts(
       
   162     const wstring& aValue, list<int>& aCerts)
       
   163 {
       
   164     try
       
   165     {
       
   166         aCerts.push_back(JavaCommonUtils::wstringToInt(aValue));
       
   167     }
       
   168     catch (ExceptionBase eb)
       
   169     {
       
   170         WLOG1WSTR(EJavaStorage,
       
   171                   "Invalid cert index skipped: %s", aValue);
       
   172     }
       
   173 }
       
   174 
       
   175 StorageHandler::~StorageHandler()
       
   176 {
       
   177     if (NULL != iStorage)
       
   178     {
       
   179         iStorage->close();
       
   180         delete iStorage;
       
   181         iStorage = NULL;
       
   182     }
       
   183 }