os/security/cryptoservices/asnpkcs/test/tpkcs8enc/pkcs8Tester.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* Test program for PKCS8 Encoding
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
/**
sl@0
    21
 @file
sl@0
    22
*/
sl@0
    23
sl@0
    24
#include <e32test.h>
sl@0
    25
#include <e32std.h>
sl@0
    26
#include <U32STD.H>
sl@0
    27
#include <asn1enc.h>
sl@0
    28
#include <asnpkcs.h>
sl@0
    29
#include <pbe.h>
sl@0
    30
#include <pbebase.h>
sl@0
    31
#include <pbedata.h>
sl@0
    32
#include <random.h>
sl@0
    33
#include <asymmetrickeys.h> 
sl@0
    34
#include <bigint.h>
sl@0
    35
#include <securityerr.h>
sl@0
    36
  
sl@0
    37
#include "pkcs8tester.h"
sl@0
    38
sl@0
    39
const TInt KErrEmptyKeyFile = -99990;
sl@0
    40
sl@0
    41
/**
sl@0
    42
 * Extracts the nPos command line argument.
sl@0
    43
 */
sl@0
    44
 HBufC* Pkcs8Utils::GetArgumentL(TInt nPos)
sl@0
    45
        {
sl@0
    46
		HBufC *argv = HBufC::NewLC(User::CommandLineLength());
sl@0
    47
		TPtr cmd(argv->Des());
sl@0
    48
		User::CommandLine(cmd);
sl@0
    49
        TLex arguments(cmd);
sl@0
    50
sl@0
    51
        // finds nth parameter
sl@0
    52
        while(nPos && !arguments.Eos())
sl@0
    53
                {
sl@0
    54
                TPtrC token = arguments.NextToken();
sl@0
    55
                if(token.Length() > 0)
sl@0
    56
                        nPos--;
sl@0
    57
                }
sl@0
    58
sl@0
    59
        HBufC* result = NULL;
sl@0
    60
        if(!arguments.Eos())
sl@0
    61
                {
sl@0
    62
                TPtrC testfile(arguments.NextToken());
sl@0
    63
sl@0
    64
                if(testfile.Length() > 0)
sl@0
    65
                        result = testfile.AllocL();
sl@0
    66
                };
sl@0
    67
sl@0
    68
        // no parameter found, but must return something so..
sl@0
    69
        if(!result)
sl@0
    70
                result = HBufC::NewL(0);
sl@0
    71
sl@0
    72
        CleanupStack::PopAndDestroy(argv);
sl@0
    73
sl@0
    74
        return result;
sl@0
    75
        }
sl@0
    76
        
sl@0
    77
 CDecPKCS8Data* Pkcs8Utils::getPkcsDataL(TDesC& fileName)
sl@0
    78
	{
sl@0
    79
	// First import a PKCS8 private key from file
sl@0
    80
	RFs fs;
sl@0
    81
	CleanupClosePushL(fs);
sl@0
    82
	User::LeaveIfError(fs.Connect());
sl@0
    83
sl@0
    84
	RFile file;
sl@0
    85
	CleanupClosePushL(file);	
sl@0
    86
	TInt r = file.Open(fs, fileName, EFileRead);
sl@0
    87
	User::LeaveIfError(r);
sl@0
    88
        
sl@0
    89
	TInt fileSize = 0;
sl@0
    90
	User::LeaveIfError(file.Size(fileSize));
sl@0
    91
sl@0
    92
	if (fileSize <= 0)
sl@0
    93
	{
sl@0
    94
		User::Leave(KErrEmptyKeyFile);
sl@0
    95
	}
sl@0
    96
sl@0
    97
	HBufC8* buf = HBufC8::NewMaxLC(fileSize);	
sl@0
    98
	TPtr8 data(buf->Des());
sl@0
    99
	data.FillZ();
sl@0
   100
	User::LeaveIfError(file.Read(data, fileSize));
sl@0
   101
        
sl@0
   102
	CDecPKCS8Data* decoded = TASN1DecPKCS8::DecodeDERL(buf->Des());	
sl@0
   103
sl@0
   104
	CleanupStack::PopAndDestroy(buf);
sl@0
   105
	CleanupStack::PopAndDestroy(&file);
sl@0
   106
	CleanupStack::PopAndDestroy(&fs);
sl@0
   107
	//CleanupStack::PopAndDestroy(fileName);
sl@0
   108
	
sl@0
   109
	return decoded;
sl@0
   110
	}       
sl@0
   111
 
sl@0
   112
 void Pkcs8Utils::WriteSequenceL(const TDesC& fn, CASN1EncSequence& seq)
sl@0
   113
{
sl@0
   114
	HBufC8* seqBufc = HBufC8::NewLC(seq.LengthDER());
sl@0
   115
	TPtr8 seqBuf(seqBufc->Des());
sl@0
   116
	seqBuf.SetLength(seq.LengthDER());
sl@0
   117
	TUint pos = 0;
sl@0
   118
	
sl@0
   119
	seq.WriteDERL(seqBuf, pos);
sl@0
   120
sl@0
   121
sl@0
   122
	RFs fs2;
sl@0
   123
	CleanupClosePushL(fs2);
sl@0
   124
	User::LeaveIfError(fs2.Connect());
sl@0
   125
sl@0
   126
	RFile file2;
sl@0
   127
	CleanupClosePushL(file2);	
sl@0
   128
	TRAPD(err, file2.Create(fs2, fn, EFileRead));
sl@0
   129
	if (err==KErrNotFound) 
sl@0
   130
	{
sl@0
   131
		User::LeaveIfError(err);
sl@0
   132
	}
sl@0
   133
	file2.Close();		
sl@0
   134
		
sl@0
   135
	RFileWriteStream writer;
sl@0
   136
	writer.PushL();
sl@0
   137
 
sl@0
   138
    	User::LeaveIfError(writer.Open(fs2, fn, EFileWrite));  
sl@0
   139
	
sl@0
   140
	writer.WriteL(seqBuf);
sl@0
   141
	writer.Close();
sl@0
   142
sl@0
   143
	CleanupStack::PopAndDestroy(4, seqBufc);		
sl@0
   144
}
sl@0
   145
sl@0
   146
sl@0
   147
        
sl@0
   148
LOCAL_D void DoTestsDSAEncryptedL(CDecPKCS8Data& decoded)
sl@0
   149
	{
sl@0
   150
	CPKCS8KeyPairDSA* keypair = static_cast<CPKCS8KeyPairDSA*>(decoded.KeyPairData());
sl@0
   151
sl@0
   152
	const CDSAPrivateKey& priv = keypair->PrivateKey();
sl@0
   153
	
sl@0
   154
	TPBPassword pass(_L("")); 
sl@0
   155
	
sl@0
   156
	CSystemRandom* rand = CSystemRandom::NewLC();
sl@0
   157
	HBufC8* saltc = HBufC8::NewMaxLC(16);
sl@0
   158
	TPtr8 salt(saltc->Des());
sl@0
   159
	salt.FillZ(); 
sl@0
   160
	TRAPD(err, rand->GenerateBytesL(salt));
sl@0
   161
	if((err != KErrNone) && (err != KErrNotSecure))
sl@0
   162
		User::Leave(err);
sl@0
   163
sl@0
   164
	HBufC8* ivc = HBufC8::NewMaxLC(8);
sl@0
   165
	
sl@0
   166
	TPtr8 iv(ivc->Des());
sl@0
   167
	iv.FillZ(); 
sl@0
   168
	//rand->GenerateBytesL(iv);
sl@0
   169
	
sl@0
   170
	CPBEncryptParms* params = CPBEncryptParms::NewLC(ECipher3DES_CBC, salt, iv, 2048);
sl@0
   171
	CPBEncryptElement* encryptElement = CPBEncryptElement::NewLC(pass, *params);
sl@0
   172
	CPBEncryptor* encryptor = encryptElement->NewEncryptLC();
sl@0
   173
sl@0
   174
	CASN1EncSequence* seq =  TASN1EncPKCS8::EncodeEncryptedL(priv, *encryptor, *params);
sl@0
   175
	
sl@0
   176
	CleanupStack::PopAndDestroy(encryptor);
sl@0
   177
	CleanupStack::PopAndDestroy(encryptElement);
sl@0
   178
	CleanupStack::PopAndDestroy(params);
sl@0
   179
	CleanupStack::PopAndDestroy(ivc);
sl@0
   180
	CleanupStack::PopAndDestroy(saltc);
sl@0
   181
	CleanupStack::PopAndDestroy(rand);	
sl@0
   182
sl@0
   183
	CleanupStack::PushL(seq);
sl@0
   184
	_LIT(fn, "ExportedDSA_Encrypted.txt");
sl@0
   185
	Pkcs8Utils::WriteSequenceL(fn, *seq);
sl@0
   186
	CleanupStack::PopAndDestroy(seq);	
sl@0
   187
	}
sl@0
   188
sl@0
   189
sl@0
   190
LOCAL_D void DoTestsDSAL(CDecPKCS8Data& decoded)
sl@0
   191
{
sl@0
   192
	CPKCS8KeyPairDSA* keypair = static_cast<CPKCS8KeyPairDSA*>(decoded.KeyPairData());
sl@0
   193
sl@0
   194
	const CDSAPrivateKey& priv = keypair->PrivateKey();
sl@0
   195
	
sl@0
   196
	// Now Export it
sl@0
   197
	CASN1EncSequence* seq =  TASN1EncPKCS8::EncodeL(priv);
sl@0
   198
sl@0
   199
	CleanupStack::PushL(seq);
sl@0
   200
	Pkcs8Utils::WriteSequenceL(_L("ExportedDSA.txt"), *seq);
sl@0
   201
	CleanupStack::PopAndDestroy(seq);	
sl@0
   202
}
sl@0
   203
	
sl@0
   204
LOCAL_D void DoTestsRSAL(CDecPKCS8Data& decoded)
sl@0
   205
{
sl@0
   206
	CPKCS8KeyPairRSA* keypair = static_cast<CPKCS8KeyPairRSA*>(decoded.KeyPairData());
sl@0
   207
sl@0
   208
	const CRSAPrivateKeyCRT& priv = static_cast<const CRSAPrivateKeyCRT&>(keypair->PrivateKey());
sl@0
   209
sl@0
   210
	const CRSAPublicKey& pub = keypair->PublicKey();
sl@0
   211
	
sl@0
   212
	// Now Export it
sl@0
   213
	CASN1EncSequence* seq =  TASN1EncPKCS8::EncodeL(priv, pub);
sl@0
   214
sl@0
   215
	CleanupStack::PushL(seq);
sl@0
   216
	Pkcs8Utils::WriteSequenceL(_L("ExportedRSA.txt"), *seq);
sl@0
   217
	CleanupStack::PopAndDestroy(seq);	
sl@0
   218
}
sl@0
   219
sl@0
   220
sl@0
   221
LOCAL_D void DoTestsRSAEncryptedL(CDecPKCS8Data& decoded)
sl@0
   222
{
sl@0
   223
	CPKCS8KeyPairRSA* keypair = static_cast<CPKCS8KeyPairRSA*>(decoded.KeyPairData());
sl@0
   224
sl@0
   225
	const CRSAPrivateKeyCRT& priv = static_cast<const CRSAPrivateKeyCRT&>(keypair->PrivateKey());
sl@0
   226
	const CRSAPublicKey& pub = keypair->PublicKey();
sl@0
   227
	
sl@0
   228
	TPBPassword pass(_L("")); 
sl@0
   229
	
sl@0
   230
	CSystemRandom* rand = CSystemRandom::NewLC();
sl@0
   231
	HBufC8* saltc = HBufC8::NewMaxLC(16);
sl@0
   232
	TPtr8 salt(saltc->Des());
sl@0
   233
	salt.FillZ(); 
sl@0
   234
	TRAPD(err, rand->GenerateBytesL(salt));
sl@0
   235
	if((err != KErrNone) && (err != KErrNotSecure))
sl@0
   236
		User::Leave(err);
sl@0
   237
sl@0
   238
	HBufC8* ivc = HBufC8::NewMaxLC(8);
sl@0
   239
	
sl@0
   240
	TPtr8 iv(ivc->Des());
sl@0
   241
	iv.FillZ(); 
sl@0
   242
	TRAP(err, rand->GenerateBytesL(iv));
sl@0
   243
	if((err != KErrNone) && (err != KErrNotSecure))
sl@0
   244
		User::Leave(err);
sl@0
   245
	
sl@0
   246
	CPBEncryptParms* params = CPBEncryptParms::NewLC(ECipher3DES_CBC, salt, iv, 2048);
sl@0
   247
	CPBEncryptElement* encryptElement = CPBEncryptElement::NewLC(pass, *params);
sl@0
   248
	CPBEncryptor* encryptor = encryptElement->NewEncryptLC();
sl@0
   249
sl@0
   250
	CASN1EncSequence* seq =  TASN1EncPKCS8::EncodeEncryptedL(priv, pub, *encryptor, *params);
sl@0
   251
	
sl@0
   252
	CleanupStack::PopAndDestroy(encryptor);
sl@0
   253
	CleanupStack::PopAndDestroy(encryptElement);
sl@0
   254
	CleanupStack::PopAndDestroy(params);
sl@0
   255
	CleanupStack::PopAndDestroy(ivc);
sl@0
   256
	CleanupStack::PopAndDestroy(saltc);
sl@0
   257
	CleanupStack::PopAndDestroy(rand);	
sl@0
   258
sl@0
   259
	CleanupStack::PushL(seq);
sl@0
   260
	_LIT(fn, "ExportedRSA_Encrypted.txt");
sl@0
   261
	Pkcs8Utils::WriteSequenceL(fn, *seq);
sl@0
   262
	CleanupStack::PopAndDestroy(seq);	
sl@0
   263
}
sl@0
   264
sl@0
   265
sl@0
   266
/**
sl@0
   267
 * 1st parameter dsa cleartext key
sl@0
   268
 * 2nd parameter dsa encrypted key
sl@0
   269
 * 3rd parameter rsa cleartext key
sl@0
   270
 * 4th parameter rsa encrypted key
sl@0
   271
 */	
sl@0
   272
LOCAL_D void DoTestsL()
sl@0
   273
{
sl@0
   274
	HBufC* fileName = Pkcs8Utils::GetArgumentL(0);
sl@0
   275
	CleanupStack::PushL(fileName);
sl@0
   276
	
sl@0
   277
	CDecPKCS8Data* decoded = Pkcs8Utils::getPkcsDataL(*fileName);
sl@0
   278
	CleanupStack::PopAndDestroy(fileName);	
sl@0
   279
sl@0
   280
	CleanupStack::PushL(decoded);
sl@0
   281
	DoTestsDSAL(*decoded);
sl@0
   282
sl@0
   283
	DoTestsDSAEncryptedL(*decoded);
sl@0
   284
	CleanupStack::PopAndDestroy(decoded);	
sl@0
   285
sl@0
   286
	 fileName = Pkcs8Utils::GetArgumentL(1);
sl@0
   287
	CleanupStack::PushL(fileName);	
sl@0
   288
sl@0
   289
	decoded = Pkcs8Utils::getPkcsDataL(*fileName);
sl@0
   290
	CleanupStack::PopAndDestroy(fileName);	
sl@0
   291
sl@0
   292
	CleanupStack::PushL(decoded);
sl@0
   293
	DoTestsRSAL(*decoded);
sl@0
   294
sl@0
   295
	DoTestsRSAEncryptedL(*decoded);
sl@0
   296
	CleanupStack::PopAndDestroy(decoded);	
sl@0
   297
}
sl@0
   298
sl@0
   299
LOCAL_C void  ProcessL()
sl@0
   300
	{
sl@0
   301
	HBufC* arg = Pkcs8Utils::GetArgumentL(0);
sl@0
   302
	_LIT(attr, "-attribute");
sl@0
   303
	if (arg->Find(attr)!=KErrNotFound)
sl@0
   304
		{
sl@0
   305
		TRAPD(err, GenerateAndSaveL(*Pkcs8Utils::GetArgumentL(1), *Pkcs8Utils::GetArgumentL(2)));		
sl@0
   306
		}
sl@0
   307
	else 
sl@0
   308
		{
sl@0
   309
		TRAPD(err, DoTestsL());		
sl@0
   310
		}
sl@0
   311
	delete arg;	
sl@0
   312
	}
sl@0
   313
sl@0
   314
sl@0
   315
sl@0
   316
/**
sl@0
   317
* Command line parameters are
sl@0
   318
* [0] -attribute [1] pkcs8 original file name [2] output file name
sl@0
   319
* or
sl@0
   320
* ...
sl@0
   321
*/
sl@0
   322
GLDEF_C TInt E32Main() // main function called by E32
sl@0
   323
    {
sl@0
   324
	__UHEAP_MARK;
sl@0
   325
	CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
sl@0
   326
	
sl@0
   327
	TRAPD(err, ProcessL());
sl@0
   328
	
sl@0
   329
	delete cleanup; // destroy clean-up stack
sl@0
   330
	__UHEAP_MARKEND;
sl@0
   331
	return 0; // and return
sl@0
   332
    }