os/security/cryptoservices/asnpkcs/inc/asnpkcs.h
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) 2003-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
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#ifndef __ASNPKCS_H__
sl@0
    20
#define __ASNPKCS_H__
sl@0
    21
sl@0
    22
#include <e32std.h>
sl@0
    23
#include <e32base.h>
sl@0
    24
#include <s32file.h>
sl@0
    25
#include <signed.h>
sl@0
    26
#include <x509cert.h>
sl@0
    27
#include <mctkeystore.h>
sl@0
    28
sl@0
    29
/**
sl@0
    30
 * @file
sl@0
    31
 * @publishedPartner 
sl@0
    32
 * @released
sl@0
    33
 */
sl@0
    34
sl@0
    35
class CASN1EncSequence;
sl@0
    36
class CPBEncryptParms;
sl@0
    37
sl@0
    38
/**
sl@0
    39
 * This class provides the means to encode PKCS#5 parameters 
sl@0
    40
 * into an ASN1 sequence as specified in the PKCS#5 specifications.
sl@0
    41
 * 
sl@0
    42
 */
sl@0
    43
class TASN1EncPKCS5
sl@0
    44
	{
sl@0
    45
public:
sl@0
    46
	/**
sl@0
    47
	 * Returns an ASN1 sequence encoding the given PKCS#5 PBE parameters.
sl@0
    48
	 * The ASN1 syntax used is specified in the PKCS#5 v2.0  specifications. 
sl@0
    49
	 * Refer to the specs for a detailed description of the returned sequence.
sl@0
    50
	 *
sl@0
    51
	 * This class is used, for instance, by TASN1EncPKCS8 to specify the PBE
sl@0
    52
	 * parameters of encrypted private keys.
sl@0
    53
	 *
sl@0
    54
	 * @param aParms The PBE parameters to be encoded
sl@0
    55
	 *
sl@0
    56
	 * @return An ASN1 sequence encoding the given PBE parameters.
sl@0
    57
	 */
sl@0
    58
	IMPORT_C static CASN1EncSequence* EncodeDERL(const CPBEncryptParms& aParms);
sl@0
    59
	};
sl@0
    60
sl@0
    61
sl@0
    62
/**
sl@0
    63
 * This class provides the means to decode an ASN1 sequence encoding
sl@0
    64
 * PKCS#5 PBE parameters.
sl@0
    65
 * 
sl@0
    66
 */
sl@0
    67
class TASN1DecPKCS5
sl@0
    68
	{
sl@0
    69
public:
sl@0
    70
	/**
sl@0
    71
	 * Decodes a ASN1 sequence encoding PKCS#5 PBE parameters.
sl@0
    72
	 * The ASN1 syntax is specified in the PKCS#5 v2.0  specifications.
sl@0
    73
	 *
sl@0
    74
	 * @param aBinaryData A descriptor containing the ASN1 data in binary format.
sl@0
    75
	 *
sl@0
    76
	 * @return The decoded PBE parameters.
sl@0
    77
	 */
sl@0
    78
	IMPORT_C static CPBEncryptParms* DecodeDERL(const TDesC8& aBinaryData);
sl@0
    79
	};
sl@0
    80
sl@0
    81
//!
sl@0
    82
//!	Converts stored key data and key info to PKCS8 and returns ASN1 encoding thereof
sl@0
    83
//!	
sl@0
    84
class CDecPKCS8Data;
sl@0
    85
sl@0
    86
/// The minimum number of bytes necessary to determine that data is cleartext pkcs8
sl@0
    87
const TInt KIsPKCS8DataMinLength = 24;
sl@0
    88
sl@0
    89
/// The minimum number of bytes necessary to determine that data is encrypted pkcs8
sl@0
    90
const TInt KIsEncryptedPKCS8DataMinLength = 36;
sl@0
    91
sl@0
    92
/**
sl@0
    93
 * Provides the means to decode PKCS#8 encoded private keys.
sl@0
    94
 * 
sl@0
    95
 */
sl@0
    96
class TASN1DecPKCS8
sl@0
    97
	{
sl@0
    98
public:
sl@0
    99
	/**
sl@0
   100
	 * Decodes DER encoded ASN1 data representing a PKCS#8 clear text private key.
sl@0
   101
	 * See the PKCS#8 specifications for the ASN1 syntax.
sl@0
   102
	 *
sl@0
   103
	 * @param aBinaryData A descriptor containing the ASN1 data.
sl@0
   104
	 *
sl@0
   105
	 * @return A pointer to a CDecPKCS8Data object containing the decoded private key.
sl@0
   106
	 */
sl@0
   107
	IMPORT_C static CDecPKCS8Data* DecodeDERL(const TDesC8& aBinaryData);
sl@0
   108
sl@0
   109
	/**
sl@0
   110
	 * Decodes DER encoded ASN1 data representing a PKCS#8 encrypted  private key.
sl@0
   111
	 * See the PKCS#8 specifications for the ASN1 syntax.
sl@0
   112
	 *
sl@0
   113
	 * @param aBinaryData A descriptor containing the ASN1 data.
sl@0
   114
	 * @param aPassword The password to decrypt the key.
sl@0
   115
	 *
sl@0
   116
	 * @return A pointer to a CDecPKCS8Data object containing the decoded private key.
sl@0
   117
	 */
sl@0
   118
	IMPORT_C static CDecPKCS8Data* DecodeEncryptedDERL(const TDesC8& aBinaryData, const TDesC8& aPassword);
sl@0
   119
sl@0
   120
	/**
sl@0
   121
	 * Determines if some binary data is a pkcs#8 clear text private key.
sl@0
   122
	 * 
sl@0
   123
	 * @param aBinaryData A descriptor containing the data.  This must be at
sl@0
   124
	 * least KIsPKCS8DataMinLength bytes long.
sl@0
   125
	 *
sl@0
   126
	 * @return ETrue if binary data is pkcs#8 clear text private key or EFalse if it is not.
sl@0
   127
	 */
sl@0
   128
	IMPORT_C static TBool IsPKCS8Data(const TDesC8& aBinaryData);
sl@0
   129
sl@0
   130
	/**
sl@0
   131
	 * Determines if some binary data is an encrypted pkcs#8 private key.
sl@0
   132
	 * 
sl@0
   133
	 * @param aBinaryData A descriptor containing the data.
sl@0
   134
	 *
sl@0
   135
	 * @return ETrue if binary data is an encrypted pkcs#8 private key or EFalse if it is not.
sl@0
   136
	 */
sl@0
   137
	IMPORT_C static TBool IsEncryptedPKCS8Data(const TDesC8& aBinaryData);
sl@0
   138
sl@0
   139
private:
sl@0
   140
	static TBool IsASN1Sequence(const TDesC8& aBinaryData, TInt& aPos);
sl@0
   141
	static TBool IsExpectedData(const TDesC8& aBinaryData, TInt& aPos, const TDesC8& aExpectedData);
sl@0
   142
};
sl@0
   143
	
sl@0
   144
sl@0
   145
//!
sl@0
   146
//!	Server side object decodes a PKCS8 data object incoming from client
sl@0
   147
//!	On construction, decodes the data to determine version, key
sl@0
   148
//!	algorithm and gives access to the key data by creating the appropriate
sl@0
   149
//!	MPKCS8DecodedKeyPairData object for the algorithm
sl@0
   150
//!	
sl@0
   151
//!	PrivateKeyInfo ::= SEQUENCE {
sl@0
   152
//!	version Version,
sl@0
   153
//!	privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
sl@0
   154
//!	privateKey PrivateKey,
sl@0
   155
//!	attributes [0] IMPLICIT Attributes OPTIONAL }
sl@0
   156
//!	
sl@0
   157
//!	Version ::= INTEGER
sl@0
   158
//!	PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
sl@0
   159
//!	PrivateKey ::= OCTET STRING
sl@0
   160
//!	Attributes ::= SET OF Attribute
sl@0
   161
//!
sl@0
   162
class MPKCS8DecodedKeyPairData;	//	Forward declare
sl@0
   163
sl@0
   164
sl@0
   165
/**
sl@0
   166
 * This class provides the means to decode PKCS#8 encoded private keys.
sl@0
   167
 * 
sl@0
   168
 */
sl@0
   169
class CDecPKCS8Data : public CBase
sl@0
   170
{
sl@0
   171
public:
sl@0
   172
	/**
sl@0
   173
	 * @internalComponent
sl@0
   174
	 * 
sl@0
   175
	 * Decodes a ASN1 sequence encoding PKCS#8 encrypted  private key.
sl@0
   176
	 *
sl@0
   177
	 * @param aData A descriptor containing the data.
sl@0
   178
	 *
sl@0
   179
	 * @return A pointer to a CDecPKCS8Data object containing the decoded private key.
sl@0
   180
	 */
sl@0
   181
	static CDecPKCS8Data* NewL(const TDesC8& aData);
sl@0
   182
	
sl@0
   183
public:
sl@0
   184
	/** 
sl@0
   185
     * Destructor
sl@0
   186
     */
sl@0
   187
	virtual ~CDecPKCS8Data();
sl@0
   188
	
sl@0
   189
public:
sl@0
   190
	/*
sl@0
   191
	 * Returns the version number of the certificate.
sl@0
   192
	 *
sl@0
   193
	 * @return Version number of the certificate.
sl@0
   194
	 */
sl@0
   195
	inline TInt Version() const;
sl@0
   196
	
sl@0
   197
	/*
sl@0
   198
	 * Return the algorithm identifier.
sl@0
   199
	 *
sl@0
   200
	 * @return algorithm identifier.
sl@0
   201
	 */
sl@0
   202
	inline TAlgorithmId Algorithm() const;
sl@0
   203
	
sl@0
   204
	/*
sl@0
   205
	 * Returns the key pair data. This depends on the value returned by CDecPKCS8Data::Algorithm()
sl@0
   206
	 *
sl@0
   207
	 * @return either RSA or DSA to M class key pair data. 
sl@0
   208
	 */
sl@0
   209
	inline MPKCS8DecodedKeyPairData* KeyPairData() const;
sl@0
   210
	
sl@0
   211
	/*
sl@0
   212
	 * Returns a DER-encoded set of PKCS8 attributes (use TASN1DecSet to decode)
sl@0
   213
	 *
sl@0
   214
	 * @return a PKCS8 attributes
sl@0
   215
	 */
sl@0
   216
	inline const TDesC8& PKCS8Attributes() const;
sl@0
   217
	
sl@0
   218
protected:
sl@0
   219
	/** @internalComponent */
sl@0
   220
	CDecPKCS8Data();
sl@0
   221
	/** @internalComponent */
sl@0
   222
	void ConstructL(const TDesC8& aData);
sl@0
   223
	
sl@0
   224
private:	//	No copying
sl@0
   225
	CDecPKCS8Data(const CDecPKCS8Data&);
sl@0
   226
	CDecPKCS8Data& operator=(CDecPKCS8Data&);
sl@0
   227
	
sl@0
   228
private:
sl@0
   229
	TInt iVersion;
sl@0
   230
	TAlgorithmId iAlgorithmID;
sl@0
   231
	MPKCS8DecodedKeyPairData* iKeyPairData;
sl@0
   232
	HBufC8* iAttributes;
sl@0
   233
};
sl@0
   234
sl@0
   235
sl@0
   236
//!	Mixin class for generic actions to be performed on a keypair
sl@0
   237
//!	
sl@0
   238
//!
sl@0
   239
class MPKCS8DecodedKeyPairData
sl@0
   240
{
sl@0
   241
public:
sl@0
   242
	/** 
sl@0
   243
	 * Gets a key identifier
sl@0
   244
	 *
sl@0
   245
     * @param aKeyIdentifier A descriptor containing a key identifier (SHA1 hash of modulus)
sl@0
   246
     */
sl@0
   247
	virtual void GetKeyIdentifierL(TKeyIdentifier& aKeyIdentifier) const = 0;
sl@0
   248
	virtual TUint KeySize() const = 0;	
sl@0
   249
	virtual void Release() = 0;
sl@0
   250
	
sl@0
   251
protected:
sl@0
   252
	virtual ~MPKCS8DecodedKeyPairData();
sl@0
   253
};
sl@0
   254
sl@0
   255
class CRSAPublicKey;
sl@0
   256
class CRSAPrivateKey;
sl@0
   257
class TASN1DecGeneric;
sl@0
   258
sl@0
   259
//!	Represents an RSA key pair and provides the means to externalize it to
sl@0
   260
//!	a stream and generate a key identifier (SHA1 hash of modulus)
sl@0
   261
//!
sl@0
   262
class CPKCS8KeyPairRSA : public CBase, public MPKCS8DecodedKeyPairData
sl@0
   263
{
sl@0
   264
public:
sl@0
   265
	/**
sl@0
   266
	 * @internalComponent
sl@0
   267
	 *
sl@0
   268
	 * Constructs the ASN1 PKCS#8 RSA private key.	 
sl@0
   269
 	 *
sl@0
   270
 	 * @param aSource A descriptor containing the key identifier
sl@0
   271
 	 *
sl@0
   272
 	 * @return A pointer to a MPKCS8DecodedKeyPairData object containing the decoded private key.
sl@0
   273
	 */ 
sl@0
   274
	static MPKCS8DecodedKeyPairData* NewL(const TASN1DecGeneric& aSource);
sl@0
   275
	
sl@0
   276
public:
sl@0
   277
	/** 
sl@0
   278
     * Destructor
sl@0
   279
     */
sl@0
   280
	virtual ~CPKCS8KeyPairRSA();
sl@0
   281
	
sl@0
   282
public:
sl@0
   283
	/** 
sl@0
   284
	 * Gets a key identifier
sl@0
   285
	 *
sl@0
   286
     * @param aKeyIdentifier A descriptor containing a key identifier
sl@0
   287
     */
sl@0
   288
	virtual void GetKeyIdentifierL(TKeyIdentifier& aKeyIdentifier) const;	
sl@0
   289
	virtual TUint KeySize() const;	
sl@0
   290
	virtual void Release();
sl@0
   291
	
sl@0
   292
	/** 
sl@0
   293
	 * Contructs a RSA Public Key
sl@0
   294
	 *
sl@0
   295
 	 * @return A RSA Public Key
sl@0
   296
     */
sl@0
   297
	inline const CRSAPublicKey& PublicKey() const;
sl@0
   298
		
sl@0
   299
	/** 
sl@0
   300
	 * Contructs a RSA Private Key
sl@0
   301
	 *
sl@0
   302
 	 * @return A RSA Private Key
sl@0
   303
     */
sl@0
   304
	inline const CRSAPrivateKey& PrivateKey() const;
sl@0
   305
	
sl@0
   306
protected:
sl@0
   307
	CPKCS8KeyPairRSA(){}	
sl@0
   308
	/** @internalComponent */
sl@0
   309
	void ConstructL(const TASN1DecGeneric& aSource);
sl@0
   310
	
sl@0
   311
private:
sl@0
   312
	CRSAPublicKey* iPublicKey;
sl@0
   313
	CRSAPrivateKey* iPrivateKey;
sl@0
   314
};
sl@0
   315
sl@0
   316
sl@0
   317
class CDSAPublicKey;
sl@0
   318
class CDSAPrivateKey;
sl@0
   319
sl@0
   320
//!	Represents a DSA key pair and provides the means to externalize it to
sl@0
   321
//!	a stream and generate a key identifier
sl@0
   322
//!
sl@0
   323
class CPKCS8KeyPairDSA : public CBase, public MPKCS8DecodedKeyPairData
sl@0
   324
{
sl@0
   325
public:
sl@0
   326
	/** 
sl@0
   327
	 * @internalComponent
sl@0
   328
	 * 
sl@0
   329
	 * Contructs the ASN1 PKCS#8 DSA private key
sl@0
   330
	 *
sl@0
   331
	 * @param aParamsData A block of PKCS#8 parameters data for DER data to decode
sl@0
   332
	 *
sl@0
   333
	 * @param aSource A descriptor containing a key identifier		
sl@0
   334
	 *
sl@0
   335
	 * @return A pointer to MPKCS8DecodedKeyPairData object containing the decoded key.
sl@0
   336
	 */
sl@0
   337
	static MPKCS8DecodedKeyPairData* NewL(const TDesC8& aParamsData, const TASN1DecGeneric& aSource);
sl@0
   338
	
sl@0
   339
public:
sl@0
   340
	/** 
sl@0
   341
     * Destructor
sl@0
   342
     */
sl@0
   343
	virtual ~CPKCS8KeyPairDSA();
sl@0
   344
	
sl@0
   345
public:
sl@0
   346
	/** 
sl@0
   347
	 * Gets a key identifier
sl@0
   348
	 *
sl@0
   349
     * @param aKeyIdentifier A descriptor containing a key identifier
sl@0
   350
     */
sl@0
   351
	virtual void GetKeyIdentifierL(TKeyIdentifier& aKeyIdentifier) const;
sl@0
   352
	virtual TUint KeySize() const;
sl@0
   353
	virtual void Release();
sl@0
   354
	
sl@0
   355
	/** 
sl@0
   356
	 * Contructs a DSA Public Key
sl@0
   357
	 *
sl@0
   358
 	 * @return A DSA Public Key
sl@0
   359
     */	
sl@0
   360
	inline const CDSAPublicKey& PublicKey() const;
sl@0
   361
	
sl@0
   362
	/** 
sl@0
   363
	 * Contructs a RSA Private Key
sl@0
   364
	 *
sl@0
   365
 	 * @return A RSA Private Key
sl@0
   366
     */	
sl@0
   367
	inline const CDSAPrivateKey& PrivateKey() const;
sl@0
   368
	
sl@0
   369
protected:
sl@0
   370
	CPKCS8KeyPairDSA(){}
sl@0
   371
	/** @internalComponent */
sl@0
   372
	void ConstructL(const TDesC8& aParamsData, const TASN1DecGeneric& aSource);
sl@0
   373
	
sl@0
   374
private:
sl@0
   375
	CDSAPublicKey* iPublicKey;
sl@0
   376
	CDSAPrivateKey* iPrivateKey;
sl@0
   377
};
sl@0
   378
sl@0
   379
class CRSAPrivateKeyCRT;
sl@0
   380
class CASN1EncOctetString;
sl@0
   381
class CASN1EncBase;
sl@0
   382
class CPBEncryptParms;
sl@0
   383
class CPBEncryptor; 
sl@0
   384
class RInteger;
sl@0
   385
sl@0
   386
/**
sl@0
   387
 * Encodes the given private key using the pkcs#8 standard.
sl@0
   388
 *
sl@0
   389
 * The returned ASN1 sequence respects the following grammar:
sl@0
   390
 * 
sl@0
   391
 *	PrivateKeyInfo ::= SEQUENCE {
sl@0
   392
 *	version Version,
sl@0
   393
 *	privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
sl@0
   394
 *	privateKey PrivateKey,
sl@0
   395
 *	attributes [0] IMPLICIT Attributes OPTIONAL }
sl@0
   396
 *	
sl@0
   397
 *	Version ::= INTEGER
sl@0
   398
 *	PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
sl@0
   399
 *	PrivateKey ::= OCTET STRING
sl@0
   400
 *	Attributes ::= SET OF Attribute
sl@0
   401
 *
sl@0
   402
 * The PrivateKeyAlgorithmIdentifier format it depends on the 
sl@0
   403
 * specific algorithm it represents. For RSA is specified in 
sl@0
   404
 * the PKCS#1 document, for DSA in PKCS#11.
sl@0
   405
 *
sl@0
   406
 * Or, in the case of encrypted private keys:
sl@0
   407
 *
sl@0
   408
 *	EncryptedPrivateKeyInfo ::= SEQUENCE {
sl@0
   409
 *	encryptionAlgorithm EncryptionAlgorithmIdentifier,
sl@0
   410
 *	encryptedData EncryptedData }
sl@0
   411
 *	
sl@0
   412
 *	EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
sl@0
   413
 *	EncryptedData ::= OCTET STRING
sl@0
   414
 *
sl@0
   415
 * AlgorithmIdentifier is the ASN1 sequence defined in the
sl@0
   416
 * PKCS#5 standard. 
sl@0
   417
 */	
sl@0
   418
class TASN1EncPKCS8
sl@0
   419
	{
sl@0
   420
public:
sl@0
   421
	/**
sl@0
   422
	 * Returns the ASN1 PKCS#8 encoding of a RSA private key.
sl@0
   423
	 *
sl@0
   424
	 * The private key syntax for this key type is defined in 
sl@0
   425
	 * the PKCS#1 document. It follows the grammar:
sl@0
   426
	 *
sl@0
   427
	 * RSAPrivateKey ::= SEQUENCE {
sl@0
   428
	 *   version Version,
sl@0
   429
	 *   modulus INTEGER, -- n
sl@0
   430
	 *   publicExponent INTEGER, -- e
sl@0
   431
	 *	 privateExponent INTEGER, -- d
sl@0
   432
	 *	 prime1 INTEGER, -- p
sl@0
   433
	 *	 prime2 INTEGER, -- q
sl@0
   434
	 *	 exponent1 INTEGER, -- d mod (p-1)
sl@0
   435
	 *	 exponent2 INTEGER, -- d mod (q-1)
sl@0
   436
	 *	 coefficient INTEGER, -- (inverse of q) mod p
sl@0
   437
	 *	 otherPrimeInfos OtherPrimeInfos OPTIONAL
sl@0
   438
 	 *	}
sl@0
   439
 	 *
sl@0
   440
 	 * @param aPrivateKey The private key to be encoded (must be in CRT format)
sl@0
   441
 	 * @param aPublicKey The corresponding public key.
sl@0
   442
 	 * @param attributes A set of attributes of the extended information
sl@0
   443
 	 * 					 that is encrypted along with the private-key
sl@0
   444
 	 *					 information.
sl@0
   445
	 *
sl@0
   446
 	 * @return An ASN1 Sequence encoding the key.
sl@0
   447
	 */
sl@0
   448
	IMPORT_C static CASN1EncSequence* EncodeL(const CRSAPrivateKeyCRT& aPrivateKey,
sl@0
   449
	                                          const CRSAPublicKey& aPublicKey, const TDesC8& attributes);
sl@0
   450
	/**
sl@0
   451
	 * Encodes an RSA key in encrypted format.
sl@0
   452
	 * 
sl@0
   453
 	 * @param aPrivateKey The private key to be encoded (must be in CRT format)
sl@0
   454
 	 * @param aPublicKey The corresponding public key.
sl@0
   455
	 * @param aEncryptor The object used to encrypt the data.
sl@0
   456
	 * @param aData The encryption parameters of the given encryptor.
sl@0
   457
	 *              These parameters are stored in the resulting sequence.
sl@0
   458
 	 * @param attributes A set of attributes of the extended information
sl@0
   459
 	 * 					 that is encrypted along with the private-key
sl@0
   460
 	 *					 information.
sl@0
   461
	 *
sl@0
   462
 	 * @return An ASN1 Sequence encoding the encrypted key.
sl@0
   463
	 */
sl@0
   464
	IMPORT_C static CASN1EncSequence* EncodeEncryptedL(const CRSAPrivateKeyCRT& aPrivateKey, 
sl@0
   465
	                                                   const CRSAPublicKey& aPublicKey, 
sl@0
   466
	                                                   CPBEncryptor& aEncryptor, 
sl@0
   467
	                                                   CPBEncryptParms& aData, const TDesC8& attributes);
sl@0
   468
sl@0
   469
	/**
sl@0
   470
	 * Returns the ASN1 PKCS#8 encoding of a DSA private key.
sl@0
   471
	 *
sl@0
   472
	 * The private key syntax for this key type is defined in 
sl@0
   473
	 * the PKCS#11 document. 
sl@0
   474
	 *
sl@0
   475
 	 * @param aPrivateKey The private key to be encoded 
sl@0
   476
 	 * @param attributes A set of attributes of the extended information
sl@0
   477
 	 * 					 that is encrypted along with the private-key
sl@0
   478
 	 *					 information.
sl@0
   479
	 *
sl@0
   480
 	 * @return An ASN1 Sequence encoding the key.
sl@0
   481
	 */
sl@0
   482
	IMPORT_C static CASN1EncSequence* EncodeL(const CDSAPrivateKey& aPrivateKey, const TDesC8& attributes);
sl@0
   483
sl@0
   484
	/**
sl@0
   485
	 * Encodes a DSA key in encrypted format.
sl@0
   486
	 * 
sl@0
   487
 	 * @param aPrivateKey The private key to be encoded.
sl@0
   488
	 * @param aEncryptor The object used to encrypt the data.
sl@0
   489
	 * @param aData The encryption parameters of the given encryptor.
sl@0
   490
	 *              These parameters are stored in the resulting sequence.
sl@0
   491
 	 * @param attributes
sl@0
   492
	 *
sl@0
   493
 	 * @return An ASN1 Sequence encoding the encrypted key.
sl@0
   494
	 */
sl@0
   495
	IMPORT_C static CASN1EncSequence* EncodeEncryptedL(const CDSAPrivateKey& aPrivateKey, 
sl@0
   496
	                                                   CPBEncryptor& aEncryptor, 
sl@0
   497
	                                                   CPBEncryptParms& aData,
sl@0
   498
							  const TDesC8& attributes);
sl@0
   499
sl@0
   500
private:
sl@0
   501
	/**
sl@0
   502
	 *
sl@0
   503
	 * Converts the ASN1 element to an octet string.
sl@0
   504
	 *
sl@0
   505
	 * @param aAsnElement The ASN1 element to be converted
sl@0
   506
	 * 
sl@0
   507
	 * @return An ASN1 Octet string representing the input element.
sl@0
   508
	 */
sl@0
   509
 	static CASN1EncOctetString* ElementToOctetL(CASN1EncBase& aAsnElement);
sl@0
   510
sl@0
   511
	/**
sl@0
   512
 	 * Given a ASN1 sequence representing a private key and a CPBEncryptor object,
sl@0
   513
 	 * it returns an ASN1 octet string containing the key sequence encrypted by
sl@0
   514
 	 * the given encryptor.
sl@0
   515
 	 *
sl@0
   516
 	 * @param aKeySeq The key sequence to be encrypted.
sl@0
   517
  	 * @param aEncryptor The CPBEncryptor object used to encrypt the given key.
sl@0
   518
	 *
sl@0
   519
	 * @return An ASN1 octet string containing the encrypted key.
sl@0
   520
 	 */
sl@0
   521
	static CASN1EncOctetString* EncryptKeySequenceL(CASN1EncSequence& aKeySeq, 
sl@0
   522
	                                                CPBEncryptor& aEncryptor);
sl@0
   523
sl@0
   524
	/**
sl@0
   525
	 *
sl@0
   526
	 * Given a valid key sequence and appropriate PBE encryptors it 
sl@0
   527
	 * encrypts the key and creates a PKCS#8 sequence of type
sl@0
   528
	 * EncryptedPrivateKeyInfo.
sl@0
   529
	 *
sl@0
   530
	 * @param aPrivateKeySequence A ASN1 sequence of the private key to be 
sl@0
   531
	 *                            encrypted. Generally the structure of the
sl@0
   532
	 *                            sequence will depend on the key type.
sl@0
   533
	 * @param aEncryptor The PBE encryptor to be used to encrypt the key.
sl@0
   534
	 * @param aData The PBE encryptor parameters. This information must be 
sl@0
   535
	 *              included in the final ASN1 sequence.
sl@0
   536
	 *
sl@0
   537
	 * @return An ASN1 sequence of type EncryptedPrivateKeyInfo.
sl@0
   538
	 */
sl@0
   539
	static CASN1EncSequence* EncryptedSequenceL(CASN1EncSequence& aPrivateKeySequence, 
sl@0
   540
                                                    CPBEncryptor& aEncryptor, 
sl@0
   541
                                                    CPBEncryptParms& aData);
sl@0
   542
sl@0
   543
	/**
sl@0
   544
	* Given a CRT RSA private key it calculates the RSA private exponent "d".
sl@0
   545
	*
sl@0
   546
	* @param aPrivateKey The RSA private key in CRT format we are interested in.
sl@0
   547
	* @param aPublicKey The RSA public key
sl@0
   548
	*
sl@0
   549
	* @return The RSA private exponent "d".
sl@0
   550
	*/                                                    
sl@0
   551
    static const RInteger CalculateRSAPrivExpL(const CRSAPrivateKeyCRT& aPrivateKey, const CRSAPublicKey& aPublicKey);
sl@0
   552
sl@0
   553
	/**
sl@0
   554
	 * Adds the given DER encoded ASN1 structure to the given sequence. If the structure is KNullDesC8
sl@0
   555
	 * nothing is added. This method is used by the encoder to add the optional pkcs8 attributes to 
sl@0
   556
	 * the ASN1 pkcs8 key it generates. PKCS8 attributes are stored as uninterpreted DER encoded
sl@0
   557
	 * binary data in the keystore.
sl@0
   558
	 *
sl@0
   559
	 * @param aAttribute The DER encoded ASN1 structure.
sl@0
   560
	 * @param aSeq The sequence to which we want to add the attributes.
sl@0
   561
	 */
sl@0
   562
	static void AddAttributesL(CASN1EncSequence& aSeq, const TDesC8& aAttribute);
sl@0
   563
	};
sl@0
   564
sl@0
   565
// Inline function definition //
sl@0
   566
sl@0
   567
inline TInt CDecPKCS8Data::Version() const 
sl@0
   568
	{
sl@0
   569
	return (iVersion);
sl@0
   570
	}
sl@0
   571
sl@0
   572
inline TAlgorithmId CDecPKCS8Data::Algorithm() const 
sl@0
   573
	{
sl@0
   574
	return (iAlgorithmID);
sl@0
   575
	}
sl@0
   576
sl@0
   577
inline MPKCS8DecodedKeyPairData* CDecPKCS8Data::KeyPairData() const 
sl@0
   578
	{
sl@0
   579
	return (iKeyPairData);
sl@0
   580
	}
sl@0
   581
sl@0
   582
sl@0
   583
inline const TDesC8& CDecPKCS8Data::PKCS8Attributes() const 
sl@0
   584
	{
sl@0
   585
	if (iAttributes)
sl@0
   586
		return (*iAttributes);
sl@0
   587
	else
sl@0
   588
		return (KNullDesC8);
sl@0
   589
	}	
sl@0
   590
sl@0
   591
inline const CRSAPublicKey& CPKCS8KeyPairRSA::PublicKey() const
sl@0
   592
	{
sl@0
   593
	return *iPublicKey;
sl@0
   594
	}
sl@0
   595
sl@0
   596
inline const CRSAPrivateKey& CPKCS8KeyPairRSA::PrivateKey() const
sl@0
   597
	{
sl@0
   598
	return *iPrivateKey;
sl@0
   599
	}
sl@0
   600
sl@0
   601
inline const CDSAPublicKey& CPKCS8KeyPairDSA::PublicKey() const
sl@0
   602
	{
sl@0
   603
	return *iPublicKey;
sl@0
   604
	}
sl@0
   605
sl@0
   606
inline const CDSAPrivateKey& CPKCS8KeyPairDSA::PrivateKey() const
sl@0
   607
	{
sl@0
   608
	return *iPrivateKey;
sl@0
   609
	}
sl@0
   610
sl@0
   611
sl@0
   612
sl@0
   613
#endif	//	__ASNPKCS_H__