pkiutilities/DeviceToken/Src/KeyStore/Server/DevCertKeyStreamUtils.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2006 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 DevCertKeyStreamUtils
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <asymmetrickeys.h>
       
    20 #include "DevCertKeyStreamUtils.h"
       
    21 #include "DevCertKeyEncryptor.h"
       
    22 
       
    23 // ======== GLOBAL FUNCTIONS ========
       
    24 
       
    25 // ---------------------------------------------------------------------------
       
    26 // ExternalizeL()
       
    27 // RSA Public Key
       
    28 // ---------------------------------------------------------------------------
       
    29 //
       
    30 void ExternalizeL(const CRSAPublicKey& aKey, RWriteStream& aStream)
       
    31     {
       
    32     aStream << aKey.N() << aKey.E();
       
    33     }
       
    34 
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // ExternalizeL()
       
    38 // RSA Private Key
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 void ExternalizeL(const CRSAPrivateKey& aData, RWriteStream& aStream)
       
    42     {
       
    43     CDevCertKeyEncryptor* encryptor = CDevCertKeyEncryptor::NewLC();
       
    44     if( encryptor->IsPluginExistL() )
       
    45         {
       
    46         MKeyEncryptor* encrypt = encryptor->CreateImplementationL();  
       
    47 
       
    48         //aStream << aData.N();
       
    49         EncryptAndStoreL(aData.N(), aStream, encrypt );
       
    50 
       
    51         // Check the incoming RSA private key (standard or CRT) 
       
    52         TRSAPrivateKeyType keyType = aData.PrivateKeyType();
       
    53         aStream.WriteInt32L((TInt32)keyType);
       
    54 
       
    55         if (EStandard==keyType)
       
    56             {
       
    57             const CRSAPrivateKeyStandard& key = static_cast<const CRSAPrivateKeyStandard&>(aData);
       
    58             //aStream << key.D();
       
    59             EncryptAndStoreL(key.D(), aStream, encrypt );
       
    60             }
       
    61         else if (EStandardCRT==keyType)
       
    62             {
       
    63             const CRSAPrivateKeyCRT& key = static_cast<const CRSAPrivateKeyCRT&>(aData);
       
    64             //aStream << key.P() << key.Q() << key.DP() << key.DQ() << key.QInv();
       
    65             EncryptAndStoreL(key.P(), aStream, encrypt );
       
    66             EncryptAndStoreL(key.Q(), aStream, encrypt );
       
    67             EncryptAndStoreL(key.DP(), aStream, encrypt );
       
    68             EncryptAndStoreL(key.DQ(), aStream, encrypt );
       
    69             EncryptAndStoreL(key.QInv(), aStream, encrypt );
       
    70             }
       
    71         else
       
    72             { 
       
    73             User::Leave(KErrNotSupported);
       
    74             }   
       
    75         }
       
    76     else
       
    77         {
       
    78         aStream << aData.N();
       
    79 
       
    80         // Check the incoming RSA private key (standard or CRT) 
       
    81         TRSAPrivateKeyType keyType = aData.PrivateKeyType();
       
    82         aStream.WriteInt32L((TInt32)keyType);
       
    83 
       
    84         if (EStandard==keyType)
       
    85             {
       
    86             const CRSAPrivateKeyStandard& key = static_cast<const CRSAPrivateKeyStandard&>(aData);
       
    87             aStream << key.D();
       
    88             }
       
    89         else if (EStandardCRT==keyType)
       
    90             {
       
    91             const CRSAPrivateKeyCRT& key = static_cast<const CRSAPrivateKeyCRT&>(aData);
       
    92             aStream << key.P() << key.Q() << key.DP() << key.DQ() << key.QInv();
       
    93             }
       
    94         else
       
    95             { 
       
    96             User::Leave(KErrNotSupported);
       
    97             }
       
    98         } 
       
    99     CleanupStack::PopAndDestroy( encryptor );        
       
   100     }
       
   101 
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // ExternalizeL()
       
   105 // DSA Public Key
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 void ExternalizeL(const CDSAPublicKey& aKey, RWriteStream& aStream)
       
   109     {
       
   110     aStream << aKey.P() << aKey.Q() << aKey.G() << aKey.Y();
       
   111     }
       
   112 
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // ExternalizeL()
       
   116 // DSA Private Key
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 void ExternalizeL(const CDSAPrivateKey& aKey, RWriteStream& aStream)
       
   120     {
       
   121     CDevCertKeyEncryptor* encryptor = CDevCertKeyEncryptor::NewLC();
       
   122     if( encryptor->IsPluginExistL() )
       
   123         {
       
   124         MKeyEncryptor* encrypt = encryptor->CreateImplementationL();  
       
   125         //aStream << aKey.P() << aKey.Q() << aKey.G() << aKey.X();
       
   126         EncryptAndStoreL(aKey.P(), aStream, encrypt );
       
   127         EncryptAndStoreL(aKey.Q(), aStream, encrypt );
       
   128         EncryptAndStoreL(aKey.G(), aStream, encrypt );
       
   129         EncryptAndStoreL(aKey.X(), aStream, encrypt );
       
   130         }
       
   131     else
       
   132         {
       
   133         aStream << aKey.P() << aKey.Q() << aKey.G() << aKey.X();  
       
   134         }
       
   135     CleanupStack::PopAndDestroy(encryptor);        
       
   136     }
       
   137 
       
   138 
       
   139 // ---------------------------------------------------------------------------
       
   140 // CreateL()
       
   141 // RSA Public key
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 void CreateL(RReadStream& aStream, CRSAPublicKey*& aOut)
       
   145     {
       
   146     RInteger N, keyPublicExp;
       
   147     CreateLC(aStream, N);
       
   148     CreateLC(aStream, keyPublicExp);
       
   149 
       
   150     aOut = CRSAPublicKey::NewL(N, keyPublicExp);
       
   151 
       
   152     CleanupStack::Pop(2, &N); // keyPublicExp, N
       
   153     }
       
   154 
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // CreateL()
       
   158 // RSA Private key
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void CreateL(RReadStream& aStream, CRSAPrivateKey*& aOut)
       
   162     {
       
   163     CDevCertKeyEncryptor* encryptor = CDevCertKeyEncryptor::NewLC();
       
   164     if( encryptor->IsPluginExistL() )
       
   165         {
       
   166         MKeyEncryptor* encrypt = encryptor->CreateImplementationL();
       
   167 
       
   168         RInteger privateN;
       
   169         DecryptAndCreateLC(aStream, privateN, encrypt);
       
   170 
       
   171         TRSAPrivateKeyType keyType = EStandard;
       
   172         keyType = (TRSAPrivateKeyType)aStream.ReadInt32L();
       
   173 
       
   174         if (EStandard==keyType)
       
   175             {
       
   176             RInteger D;
       
   177             DecryptAndCreateLC(aStream, D, encrypt);
       
   178 
       
   179             aOut = CRSAPrivateKeyStandard::NewL(privateN, D);
       
   180 
       
   181             CleanupStack::Pop(&D);
       
   182             }
       
   183         else if (EStandardCRT==keyType)
       
   184             {
       
   185             RInteger p, q, dP, dQ, qInv;
       
   186             DecryptAndCreateLC(aStream, p, encrypt);
       
   187             DecryptAndCreateLC(aStream, q, encrypt);
       
   188             DecryptAndCreateLC(aStream, dP, encrypt);
       
   189             DecryptAndCreateLC(aStream, dQ, encrypt);
       
   190             DecryptAndCreateLC(aStream, qInv, encrypt);
       
   191 
       
   192             aOut = CRSAPrivateKeyCRT::NewL(privateN, p, q, dP, dQ, qInv);
       
   193 
       
   194             CleanupStack::Pop(5, &p);
       
   195             }
       
   196         else
       
   197             {
       
   198             User::Leave(KErrNotSupported);
       
   199             }
       
   200 
       
   201         CleanupStack::Pop(&privateN);
       
   202         }
       
   203     else
       
   204         {
       
   205         RInteger privateN;
       
   206         CreateLC(aStream, privateN);
       
   207 
       
   208         TRSAPrivateKeyType keyType = EStandard;
       
   209         keyType = (TRSAPrivateKeyType)aStream.ReadInt32L();
       
   210 
       
   211         if (EStandard==keyType)
       
   212             {
       
   213             RInteger D;
       
   214             CreateLC(aStream, D);
       
   215 
       
   216             aOut = CRSAPrivateKeyStandard::NewL(privateN, D);
       
   217 
       
   218             CleanupStack::Pop(&D);
       
   219             }
       
   220         else if (EStandardCRT==keyType)
       
   221             {
       
   222             RInteger p, q, dP, dQ, qInv;
       
   223             CreateLC(aStream, p);
       
   224             CreateLC(aStream, q);
       
   225             CreateLC(aStream, dP);
       
   226             CreateLC(aStream, dQ);
       
   227             CreateLC(aStream, qInv);
       
   228 
       
   229             aOut = CRSAPrivateKeyCRT::NewL(privateN, p, q, dP, dQ, qInv);
       
   230 
       
   231             CleanupStack::Pop(5, &p);
       
   232             }
       
   233         else
       
   234             {
       
   235             User::Leave(KErrNotSupported);
       
   236             }
       
   237 
       
   238         CleanupStack::Pop(&privateN); 
       
   239         } 
       
   240     CleanupStack::PopAndDestroy(encryptor);       
       
   241     }
       
   242 
       
   243 
       
   244 // ---------------------------------------------------------------------------
       
   245 // CreateL()
       
   246 // DSA Public key
       
   247 // ---------------------------------------------------------------------------
       
   248 //
       
   249 void CreateL(RReadStream& aStream, CDSAPublicKey*& aOut)
       
   250     {
       
   251     RInteger P, Q, G, Y;
       
   252     CreateLC(aStream, P);
       
   253     CreateLC(aStream, Q);
       
   254     CreateLC(aStream, G);
       
   255     CreateLC(aStream, Y);
       
   256 
       
   257     aOut = CDSAPublicKey::NewL(P, Q, G, Y);
       
   258 
       
   259     CleanupStack::Pop(4, &P);
       
   260     }
       
   261 
       
   262 
       
   263 // ---------------------------------------------------------------------------
       
   264 // CreateL()
       
   265 // DSA Private key
       
   266 // ---------------------------------------------------------------------------
       
   267 //
       
   268 void CreateL(RReadStream& aStream, CDSAPrivateKey*& aOut)
       
   269     {
       
   270     CDevCertKeyEncryptor* encryptor = CDevCertKeyEncryptor::NewLC();
       
   271     if( encryptor->IsPluginExistL() )
       
   272         {
       
   273         MKeyEncryptor* encrypt = encryptor->CreateImplementationL();
       
   274         RInteger P, Q, G, X;
       
   275         DecryptAndCreateLC(aStream, P, encrypt);
       
   276         DecryptAndCreateLC(aStream, Q, encrypt);
       
   277         DecryptAndCreateLC(aStream, G, encrypt);
       
   278         DecryptAndCreateLC(aStream, X, encrypt);
       
   279 
       
   280         aOut = CDSAPrivateKey::NewL(P, Q, G, X);
       
   281 
       
   282         CleanupStack::Pop(4, &P);
       
   283         }
       
   284     else
       
   285         {
       
   286         RInteger P, Q, G, X;
       
   287         CreateLC(aStream, P);
       
   288         CreateLC(aStream, Q);
       
   289         CreateLC(aStream, G);
       
   290         CreateLC(aStream, X);
       
   291 
       
   292         aOut = CDSAPrivateKey::NewL(P, Q, G, X);
       
   293 
       
   294         CleanupStack::Pop(4, &P); 
       
   295         }
       
   296     CleanupStack::PopAndDestroy(encryptor);        
       
   297     }
       
   298 
       
   299 //EOF
       
   300