javaextensions/satsa/crypto/src/stsdsasignature.cpp
changeset 21 2a9601315dfc
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Implementation of STSDSASignature
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <x509cert.h>
       
    21 #include <asymmetric.h>
       
    22 
       
    23 #include "stsconstants.h"
       
    24 #include "stsdsasignature.h"
       
    25 
       
    26 #include "javajniutils.h"
       
    27 #include "logger.h"
       
    28 
       
    29 namespace java
       
    30 {
       
    31 namespace satsa
       
    32 {
       
    33 
       
    34 
       
    35 
       
    36 STSDSASignature::STSDSASignature():mDSA(0), mpkey(0)
       
    37 {
       
    38 
       
    39 }
       
    40 
       
    41 
       
    42 inline int STSDSASignature::Construct(const std::wstring aPadding)
       
    43 {
       
    44     // Create digest object according to aAlgorithm
       
    45     if (aPadding == STSDigestSHA1)
       
    46     {
       
    47         md = EVP_dss1();
       
    48     }
       
    49     else if (aPadding == STSDigestMD2)
       
    50     {
       
    51         md = EVP_md2();
       
    52     }
       
    53     else if (aPadding == STSDigestMD5)
       
    54     {
       
    55         md = EVP_md5();
       
    56     }
       
    57     else
       
    58     {
       
    59         // Algorithm was not found
       
    60         return KSTSErrDigest;
       
    61     }
       
    62     if (md == NULL)
       
    63     {
       
    64         return KSTSErrDigest;
       
    65     }
       
    66     else
       
    67     {
       
    68 
       
    69         mdctx = new EVP_MD_CTX;
       
    70         if (mdctx != NULL)
       
    71         {
       
    72             EVP_MD_CTX_init(mdctx);
       
    73             return 0;
       
    74         }
       
    75         else
       
    76         {
       
    77             return KSTSErrDigest;
       
    78         }
       
    79     }
       
    80 
       
    81 }
       
    82 
       
    83 
       
    84 STSDSASignature* STSDSASignature::Create(const std::wstring aPadding,
       
    85         int* errCode)
       
    86 {
       
    87     LOG(ESATSA, EInfo, "STSDSASignature::Create+");
       
    88     STSDSASignature* self = new STSDSASignature;
       
    89     if (self == NULL)
       
    90     {
       
    91         // object was not created successfully
       
    92         ELOG(ESATSA,
       
    93              "STSDSASignature::Create: object was not created successfully");
       
    94         *errCode = KSTSErrNoMemory;
       
    95         return NULL;
       
    96     }
       
    97     else
       
    98     {
       
    99         // Object created, call the second level constructor
       
   100         *errCode = self->Construct(aPadding);
       
   101         if (*errCode == 0)
       
   102         {
       
   103             return self;
       
   104         }
       
   105         else
       
   106         {
       
   107             delete self;
       
   108             return NULL;
       
   109         }
       
   110     }
       
   111 }
       
   112 
       
   113 
       
   114 STSDSASignature::~STSDSASignature()
       
   115 {
       
   116 
       
   117     if (0 != mpkey)
       
   118     {
       
   119         EVP_PKEY_free(mpkey);
       
   120     }
       
   121     if (0 != mDSA)
       
   122     {
       
   123         delete mDSA;
       
   124         mDSA = 0;
       
   125     }
       
   126 
       
   127 }
       
   128 
       
   129 int STSDSASignature::InitVerify(JNIEnv* aJni, jstring aKeyAlgorithm,
       
   130                                 jstring aKeyFormat, jbyteArray aKeyEncoded)
       
   131 {
       
   132     LOG(ESATSA, EInfo, "STSDSASignature::InitVerify+");
       
   133     int retVal = 0;
       
   134 
       
   135     // Check key validity
       
   136     // Key must be created for used algorithm
       
   137     std::wstring key_algorithm;
       
   138     try
       
   139     {
       
   140         key_algorithm = java::util::JniUtils::jstringToWstring(aJni,
       
   141                         aKeyAlgorithm);
       
   142     }
       
   143     catch (...)
       
   144     {
       
   145         ELOG(ESATSA, "caught exception. Return error code");
       
   146         return (KSTSErrInvalidKey);
       
   147     }
       
   148     if (key_algorithm != STSAlgorithmDSA)
       
   149     {
       
   150         ELOG(ESATSA, "Invalid Key");
       
   151         return (KSTSErrInvalidKey);
       
   152     }
       
   153     // Format: Only X509 keys are supported
       
   154     std::wstring key_format;
       
   155     try
       
   156     {
       
   157         key_format = java::util::JniUtils::jstringToWstring(aJni, aKeyFormat);
       
   158     }
       
   159     catch (...)
       
   160     {
       
   161         ELOG(ESATSA, "caught exception. Return error code");
       
   162         return (KSTSErrInvalidKey);
       
   163     }
       
   164     if (key_format != STSKeyFormatX509)
       
   165     {
       
   166         ELOG(ESATSA, "Wrong Key Format");
       
   167         return (KSTSErrInvalidKey);
       
   168     }
       
   169 
       
   170     // Read the encoded key from the jbytearray
       
   171     int key_length = aJni->GetArrayLength(aKeyEncoded);
       
   172     const unsigned char *key = new unsigned char[key_length];
       
   173     aJni->GetByteArrayRegion(aKeyEncoded, 0, key_length, (signed char*) key);
       
   174 
       
   175     mDSA = d2i_DSA_PUBKEY(&mDSA, &key, (long) key_length);
       
   176     if (mDSA == NULL)
       
   177     {
       
   178         ELOG(ESATSA, "Key is not ok");
       
   179         return KSTSErrInvalidKey;
       
   180     }
       
   181 
       
   182     mpkey = EVP_PKEY_new();
       
   183     EVP_PKEY_set1_DSA(mpkey, mDSA);
       
   184 
       
   185     retVal = EVP_VerifyInit_ex(mdctx, md, NULL);
       
   186     LOG(ESATSA, EInfo, "STSDSASignature::InitVerify--");
       
   187     return retVal;
       
   188 
       
   189 }
       
   190 
       
   191 int STSDSASignature::Verify(JNIEnv* aJni, jbyteArray aSignature)
       
   192 {
       
   193     LOG(ESATSA, EInfo, "STSDSASignature::Verify+");
       
   194     int result;
       
   195     if (mpkey == NULL)
       
   196     {
       
   197         // Not initialized
       
   198         ELOG(ESATSA, "STSDSASignature::Verify: Key is not initialized");
       
   199         return KSTSErrSignature;
       
   200     }
       
   201     if (!mIsUpdated)
       
   202     {
       
   203         return false;
       
   204     }
       
   205 
       
   206     if (mdctx == NULL)
       
   207     {
       
   208         // If data is not updated TCK requires that exception is not thrown and
       
   209         // false is returned.
       
   210         ELOG(ESATSA, "STSDSASignature::Verify:Data is not upadted");
       
   211         return false;
       
   212     }
       
   213 
       
   214     // Read the signature data from the jbytearray
       
   215     int siglen = aJni->GetArrayLength(aSignature);
       
   216     const unsigned char *sigbuf = new unsigned char[siglen];
       
   217     aJni->GetByteArrayRegion(aSignature, 0, siglen, (signed char*) sigbuf);
       
   218 
       
   219     result = EVP_VerifyFinal(mdctx, sigbuf, siglen, mpkey);
       
   220     mIsUpdated = false;
       
   221 
       
   222     // reset the digest
       
   223     const EVP_MD* md = NULL;
       
   224     md = EVP_MD_CTX_md(mdctx);
       
   225     EVP_MD_CTX_init(mdctx);
       
   226     EVP_DigestInit_ex(mdctx, md, NULL);
       
   227     if (0 == result)
       
   228     {
       
   229         return false;
       
   230     }
       
   231     else if (1 == result)
       
   232     {
       
   233         return true;
       
   234     }
       
   235     else
       
   236     {
       
   237         return KSTSErrSignature;
       
   238     }
       
   239 
       
   240 }
       
   241 
       
   242 int STSDSASignature::Update(JNIEnv* aJni, jbyteArray aData, jint aOffset,
       
   243                             jint aLength)
       
   244 {
       
   245     LOG(ESATSA, EInfo, "STSDSASignature::Update+");
       
   246     if (mpkey == NULL)
       
   247     {
       
   248         ELOG(ESATSA, "STSDSASignature::Update: Key is not initialized");
       
   249         // Not initialized
       
   250         return KSTSErrSignature;
       
   251     }
       
   252 
       
   253     // Read the signature data from the jbytearray
       
   254     const unsigned char *data = new unsigned char[aLength];
       
   255     aJni->GetByteArrayRegion(aData, aOffset, aLength, (signed char*) data);
       
   256 
       
   257     int result = EVP_VerifyUpdate(mdctx, (const void *) data,
       
   258                                   (unsigned int) aLength);
       
   259 
       
   260     if (!mIsUpdated)
       
   261     {
       
   262         mIsUpdated = true;
       
   263     }
       
   264 
       
   265     LOG(ESATSA, EInfo, "STSDSASignature::Update--");
       
   266     return result;
       
   267 
       
   268 }
       
   269 } // namespace satsa
       
   270 } // namespace java
       
   271 
       
   272