os/security/cryptoservices/certificateandkeymgmt/inc/pkcs12bags.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2005-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 the License "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 
    18 
    19 /**
    20  @file
    21  @publishedPartner
    22  @released
    23 */
    24 
    25 #ifndef __PKCS12_BAGS_H__
    26 #define __PKCS12_BAGS_H__
    27 
    28 #include <asn1dec.h>
    29 #include <signed.h>
    30 #include <asnpkcs.h>
    31 #include <pkcs12kdf.h>
    32 #include <pkcs12safebag.h>
    33 #include <pkcs12attribute.h>
    34 #include <pkcs7contentinfo_v2.h>
    35 #include <pkcs7encrypteddataobject.h>
    36 
    37 namespace PKCS12
    38 {
    39 /** Object identifier for KeyBag */
    40 _LIT(KPkcs12KeyBagOID, "1.2.840.113549.1.12.10.1.1"); 	
    41 
    42 /** Object identifier for ShroudedKeyBag */
    43 _LIT(KPkcs12ShroudedKeyBagOID, "1.2.840.113549.1.12.10.1.2"); 
    44 
    45 /** Object identifier for CertBag */
    46 _LIT(KPkcs12CertBagOID, "1.2.840.113549.1.12.10.1.3"); 
    47 
    48 /** Object identifier for CrlBag */
    49 _LIT(KPkcs12CrlBagOID, "1.2.840.113549.1.12.10.1.4"); 
    50 
    51 /** Object identifier for SecretBag */
    52 _LIT(KPkcs12SecretBagOID, "1.2.840.113549.1.12.10.1.5"); 
    53 
    54 /** Object identifier for SafeContentsBag */
    55 _LIT(KPkcs12SafeContentsBagOID, "1.2.840.113549.1.12.10.1.6");
    56 
    57 /** Object identifier for x509 certificate */
    58 _LIT(KX509CertificateOID,"1.2.840.113549.1.9.22.1"); 
    59 
    60 /**
    61  This class decodes the KeyBag present in the SafeBag.
    62  It has a method to get the PrivatKeyInfo
    63  */
    64 class CDecPkcs12KeyBag : public CDecPkcs12SafeBag
    65 	{
    66 public:
    67     /**
    68 	 Creates a new PKCS#12KeyBag object.
    69 	  
    70 	 @param  aSafeBagData Contains a PKCS#12 SafeBag Structure.
    71 	 @return A pointer to the newly allocated object.
    72 	 @leave  KErrAgrument if the data is not a sequence or class tag name 
    73 	  		 is not Universal and if iPrivateKeyInfo is NULL.
    74 	 */
    75 	IMPORT_C static CDecPkcs12KeyBag* NewL(const TDesC8& aSafeBagData);
    76 	
    77     /**
    78      The PrivateKey information present in the KeyBag.
    79      The returned ASN1 sequence respects the following grammar:
    80       
    81  	 	PrivateKeyInfo ::= SEQUENCE {
    82  	 	version Version,
    83  	 	privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
    84  	 	privateKey PrivateKey,
    85  	 	attributes [0] IMPLICIT Attributes OPTIONAL }
    86  	 	
    87  	 	Version ::= INTEGER
    88  	 	PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
    89  	 	PrivateKey ::= OCTET STRING
    90  	 	Attributes ::= SET OF Attribute
    91      
    92   	 @return A pointer to a CDecPKCS8Data object. Ownership of memory is transferred 
    93   	 to the caller.
    94   	 @see    CDecPKCS8Data
    95 	 */
    96 	IMPORT_C CDecPKCS8Data* PrivateKeyInfoL() const;
    97 	
    98 	/**
    99 	 Destructor.
   100 	 */
   101 	virtual ~CDecPkcs12KeyBag();
   102 	
   103 private:
   104 	/**
   105      Decodes the entire KeyBag structure.
   106      @param aSafeBagData Contains a PKCS#12 SafeBag Structure.
   107      @leave  KErrAgrument if the data is not a sequence or class tag name 
   108 	  		 is not Universal and if iPrivateKeyInfo is NULL.
   109 	 @see	 TASN1DecPKCS8		 
   110      */
   111 	void ConstructL(const TDesC8& aSafeBagData);
   112     
   113     /**
   114 	 Constructor.
   115 	 */
   116 	CDecPkcs12KeyBag();
   117 	
   118 	/**
   119 	 Copy Constructor.
   120 	 @param aDecPkcs12keyBag A CDecPkcs12KeyBag object.
   121 	 */
   122 	CDecPkcs12KeyBag(const CDecPkcs12KeyBag& aDecPkcs12keyBag);
   123 	
   124 	/**
   125 	 Assignment operator.
   126 	 @param aDecPkcs12keyBag A CDecPkcs12KeyBag object.
   127 	 @return A CDecPkcs12KeyBag class.
   128 	 */
   129 	CDecPkcs12KeyBag& operator=(const CDecPkcs12KeyBag& aDecPkcs12keyBag);
   130 };
   131 
   132 
   133 /**
   134  This class decodes the ShroudedKeyBag present in the SafeBag.
   135  It has a method to get the PrivatKeyInfo
   136  */
   137 class CDecPkcs12ShroudedKeyBag : public CDecPkcs12SafeBag
   138 	{
   139 public:
   140    /**
   141 	Creates a new PKCS#12KeyBag object.
   142 	 
   143 	@param aSafeBagData Contains a PKCS#12 shroudedKeyBag structure.
   144 	@return A pointer to the newly allocated object.
   145 	@leave  KErrAgrument if the data is not safeBag structure.
   146 	*/
   147 	IMPORT_C static CDecPkcs12ShroudedKeyBag* NewL(const TDesC8& aSafeBagData);
   148 	
   149    /**
   150     The PrivateKey Information present in the ShroudKeyBag. 
   151     Below is the ASN.1 sequence.
   152     
   153     ContentEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
   154  
   155  	AlgorithmIdentifier: A type that identifies an algorithm (by object identifier) and associated parameters
   156  	AlgorithmIdentifier ::= SEQUENCE 
   157                         {
   158                         algorithm   ALGORITHM.&id({SupportedAlgorithms}),
   159                         parameters  ALGORITHM.&Type({SupportedAlgorithms}{@ algorithm}) OPTIONAL
   160                         }    
   161                         
   162 	@return A pointer to a CDecPKCS8Data object. Ownership is transferred to the caller.
   163 	@leave  KErrAgrument if the data is not a sequence or class tag name 
   164 	  		 is not Universal.
   165 	@leave  KErrNotSupported if otherthan PKCS12 pbeIds. 
   166 	@leave  KErrGeneral	   if the decryption of the final part length is less than 0.
   167 	@see    TASN1DecPKCS5, CPBEncryptElement, PKCS12KDF, CPBDecryptor, CDecPKCS8Data.
   168 	*/
   169 	IMPORT_C CDecPKCS8Data* PrivateKeyInfoL(TDesC& aPassword) const;
   170 		
   171 	/**
   172 	 Destructor.
   173 	 */
   174 	virtual ~CDecPkcs12ShroudedKeyBag();
   175 	
   176 private:
   177 	/**
   178      This decodes the entire KeyBag structure.
   179      @param  aSafeBagData Contains a PKCS#12 shroudedKeyBag Structure.
   180      @leave  KErrAgrument if the data is not a sequence or class tag name 
   181 	  		 is not Universal.
   182 	 @see	 TASN1DecPKCS8
   183      */
   184 	void ConstructL(const TDesC8& aSafeBagData);
   185     
   186     /**
   187 	 Constructor.
   188 	 */
   189 	CDecPkcs12ShroudedKeyBag();
   190 	
   191 	/**
   192 	 Copy Constructor.
   193 	 @param aDecPkcs12ShroudedKeyBag A CDecPkcs12ShroudedKeyBag object.
   194 	 @return A CDecPkcs12ShroudedKeyBag class.
   195 	 */
   196 	CDecPkcs12ShroudedKeyBag(const CDecPkcs12ShroudedKeyBag& aDecPkcs12ShroudedKeyBag);
   197 	
   198 	/**
   199 	 Assignment operator.
   200 	 @param aDecPkcs12ShroudedKeyBag A CDecPkcs12ShroudedKeyBag object.
   201 	 */
   202 	CDecPkcs12ShroudedKeyBag& operator=(const CDecPkcs12ShroudedKeyBag& aDecPkcs12ShroudedKeyBag);
   203 	};
   204 
   205 
   206 /**
   207  This class decodes the CertBag 
   208  It has methods to get the CertId and the CertValue present in the CertBag
   209  The X509Certificate() method returns the x509 certificate 
   210  in case the CertId is x509
   211  */
   212 class CDecPkcs12CertBag : public CDecPkcs12SafeBag
   213 	{
   214 public:
   215 	/**
   216 	 Creates a new PKCS#12CertBag object.
   217 	  
   218 	 @param aCertBagData contains a PKCS#12 CertBag Structure.
   219 	 @leave KErrArgument if the data is not a sequence or class tag name 
   220 	  		 is not Universal.
   221      @leave	KErrNotSupported if otherthan X509 certificate is present.
   222 	 @return A pointer to the newly allocated object.
   223 	 */
   224 	 IMPORT_C static CDecPkcs12CertBag* NewL(const TDesC8& aCertBagData);
   225 	 
   226 	/**
   227 	 This method returns the OID present in the certId field of CertBag sequence.	 	 	 	 
   228 	 @return Returns OID present in the certId feild of CertBag sequence
   229 	 */
   230 	 IMPORT_C const TDesC& CertId() const;
   231 	 
   232 	/**
   233 	 This method returns the DER encoded certValue present in the CertBag sequence.	 	 
   234 	 @return The DER encoded certValue present in the CertBag sequence.
   235 	 @see  X509Certificate
   236 	 */
   237 	 IMPORT_C const TDesC8& CertValue() const;
   238 	 
   239 	/**
   240 	 This method returns the decoded x509 certificate.	 
   241 	 @return Returns a pointer to a CX509Certificate object if the certificate of type 
   242 	 X509; otherwise, null is returned. Ownership of memory is transferred to the caller.			 
   243 	 @see CertValue
   244 	 */
   245 	 IMPORT_C CX509Certificate* X509CertificateL() const;
   246 	 
   247 	 /**
   248 	 Destructor.
   249 	 */
   250 	virtual ~CDecPkcs12CertBag();
   251 private:
   252 	/**
   253      This decodes the entire CertBag structure.
   254      @param aCertBagData contains a PKCS#12 CertBag Structure.
   255      @leave KErrArgument if the data is not a sequence or class tag name 
   256 	  		 is not Universal.      
   257      @see	CDecPkcs12SafeBag, CX509Certificate.
   258      */
   259 	 void ConstructL(const TDesC8& aCertBagData);
   260 	        
   261     /**
   262 	 Constructor.
   263 	 */
   264 	 CDecPkcs12CertBag();
   265 	
   266 	/**
   267 	 Copy Constructor.
   268 	 @param aDecPkcs12CertBag A CDecPkcs12CertBag object.
   269 	 */
   270 	CDecPkcs12CertBag(const CDecPkcs12CertBag& aDecPkcs12CertBag);
   271 	
   272 	/**
   273 	 Assignment operator.
   274 	 @param aDecPkcs12CertBag A CDecPkcs12CertBag object.
   275 	 @return A CDecPkcs12CertBag class.
   276 	 */
   277 	CDecPkcs12CertBag& operator=(const CDecPkcs12CertBag& aDecPkcs12CertBag);
   278      
   279 private:
   280     /** Contains Object identifier indicating the certificate type*/
   281 	HBufC* iCertId;
   282 	
   283 	/** Contains the certificate which is encoded and is an OCTET String */
   284 	TPtrC8 iCertValue;	
   285 	};
   286 
   287 /**
   288  Decodes the SafeContents bag present within a SafeBag and 
   289  returns an array of Safebags present within this SafeContents bag.
   290  */
   291 class CDecPkcs12SafeContentsBag : public CDecPkcs12SafeBag
   292 	{
   293 public:
   294     /**
   295 	 Creates a new PKCS#12SafeContentsBag object.
   296 	  
   297 	 @param aSafeContentsBagData Contains a PKCS#12 SafeBag structure.
   298 	 @return A pointer to the newly allocated object.
   299 	 @leave KErrArgument if the data is not a sequence or class tag name 
   300 	  		 is not Universal.
   301 	 */
   302 	IMPORT_C static CDecPkcs12SafeContentsBag* NewL(const TDesC8& aSafeContentsBagData);
   303 	/**
   304 	 The SafeContents Bag contains one or more Safe Bags in it.This 
   305 	 method returns the reference to all these SafeBags.
   306 	 
   307 	 @return An array of SafeBags present within the SafeContentsBag
   308 	 */
   309 	IMPORT_C const RPointerArray<CDecPkcs12SafeBag>& SafeBags() const; 
   310 	
   311 	/**
   312 	 Destructor.
   313 	 */
   314 	virtual ~CDecPkcs12SafeContentsBag();
   315 	
   316 private:
   317     /**
   318      This decodes the entire SafeContentsBag structure.
   319      @param aSafeContentsBagData Contains a PKCS#12 SafeBag structure.
   320      @leave KErrArgument if the data is not a sequence or class tag name 
   321 	  		 is not Universal.
   322 	 @see   CDecPkcs12SafeBag
   323      */
   324 	void ConstructL(const TDesC8& aSafeContentsBagData);     
   325 	
   326 	/**
   327 	 Constructor.
   328 	 */
   329 	 CDecPkcs12SafeContentsBag();
   330 		
   331 	/**
   332 	 Copy Constructor.
   333 	 @param aDecPkcs12SafeContentsBag A CDecPkcs12SafeContentsBag object.
   334 	 */
   335 	CDecPkcs12SafeContentsBag(const CDecPkcs12SafeContentsBag& aDecPkcs12SafeContentsBag);
   336 	
   337 	/**
   338 	 Assignment operator.
   339 	 @param aDecPkcs12SafeContentsBag A CDecPkcs12SafeContentsBag object.
   340 	 @return A reference to CDecPkcs12SafeContentsBag class.
   341 	 */
   342 	CDecPkcs12SafeContentsBag& operator=(const CDecPkcs12SafeContentsBag& aDecPkcs12SafeContentsBag);
   343     
   344 private:   
   345      /** Contains an array of SafeBags present within the SafeContents Bag */
   346 	 RPointerArray<CDecPkcs12SafeBag> iSafeBags;
   347 	 };	
   348 	 
   349 class CDecPkcs12SafeContents : public CBase
   350 	{
   351 public:
   352 	/**
   353 	 Creates a new CDecPkcs12SafeContents object for plain Data.
   354 	  
   355 	 @param  aSafeContentsBagData Contains a PKCS#7 ContentInfo Structure.
   356 	 @return A pointer to the newly allocated object.
   357 	 @leave  KErrArgument if the data is not a sequence or class tag name 
   358 	  		 is not Universal.
   359 	 */
   360 	IMPORT_C static CDecPkcs12SafeContents* NewL(const CPKCS7ContentInfo& aSafeContentsBagData);
   361 	
   362 	/**
   363 	 Creates a new CDecPkcs12SafeContents object for Encrypted Data.
   364 	  
   365 	 @param  aSafeContentsBagData Contains a PKCS#7 ContentInfo Structure.
   366 	 @param	 aPassword			  aPassword is the password used for decryption. 
   367 	 @return A pointer to the newly allocated object.
   368 	 @leave  KErrArgument if the data is not a sequence or class tag name 
   369 	  		 is not Universal.
   370 	 */
   371 	IMPORT_C static CDecPkcs12SafeContents* NewL(const CPKCS7ContentInfo& aSafeContentsBagData, const TDesC& aPassword);
   372 
   373 	/**
   374 	 Creates a new CDecPkcs12SafeContents object for Enveloped Data.
   375 	 The class doesn't support the public key privacy mode  if the 
   376 	 ContentInfo contains an EnvelopedData object.Client should decrypt 
   377 	 the Envelope Data. A recipient opens the envelope by decrypting the 
   378 	 one of the encrypted content-encryption keys with the recipient's 
   379 	 private key and decrypts the encrypted content with the recovered 
   380 	 content-encryption key and pass the plain data. 
   381 	 @param  aSafeContentsBagData Contains a PKCS#7 ContentInfo Structure ContentData.
   382 	 @return A pointer to the newly allocated object.
   383 	 @leave  KErrArgument if the data is not a sequence or class tag name 
   384 	  		 is not Universal.
   385 	 */
   386 	IMPORT_C static CDecPkcs12SafeContents* NewL(const TDesC8& aSafeContentsBagData);
   387 	
   388 	/**
   389 	 The method returns the plain data.
   390 	 @return A pointer to descriptor containing decrypted data.
   391 	 		 Returns NULL pointer if the decrypted data is not present.		 
   392 	 */
   393 	IMPORT_C const TDesC8* DecryptedData() const;
   394 	
   395 	/**
   396 	 The method returns array of safebags objects.
   397 	 @return A pointer to array of safeBag objects.
   398 	 */
   399 	IMPORT_C const RPointerArray<CDecPkcs12SafeBag>& SafeContentsBags() const;
   400 	
   401 	/**
   402 	 Destructor.
   403 	 */
   404 	virtual ~CDecPkcs12SafeContents();
   405 	
   406 private:
   407 	/**
   408 	 These objects represents the SafeBag Sequences present in the 
   409 	 SafeContents Bag Sequence.
   410 	 
   411 	 @param aSafeContent Contains data to decode the bags. 
   412 	 @leave KErrArgument if the aSafeContent is not a Sequence and class tag is not Universal.
   413 	 @leave	KErrNotSupported if any bag otherthan keyBag, shroudedKeyBag,CertBag,CRLBag,SecretBag
   414 	 						 and SafeContentBag.
   415 	 @see   CDecPkcs12SafeBag
   416 	 */
   417 	void ConstructL(const TDesC8& aSafeContent);
   418 	
   419 	/**
   420 	 This method decrypts the encrypted information. 
   421 	 @param		aContentInfo Contains a PKCS#7 ContentInfo Structure.
   422 	 @param     aPassword is the password used for decryption.
   423 	 */
   424 	void DecodeEncryptedDataL(const CPKCS7ContentInfo& aContentInfo, const TDesC& aPassword);
   425 	
   426 	/**
   427 	 Constructor.
   428 	 */
   429 	CDecPkcs12SafeContents();
   430 	
   431 	/**
   432 	 Copy Constructor.
   433 	 @param aDecPkcs12SafeContents A CDecPkcs12SafeContents object.
   434 	 */
   435 	CDecPkcs12SafeContents(const CDecPkcs12SafeContents& aDecPkcs12SafeContents);
   436 	
   437 	/**
   438 	 Assignment operator.
   439 	 @param aDecPkcs12SafeContents A CDecPkcs12SafeContents object.
   440 	 @return A reference to CDecPkcs12SafeContents class.
   441 	 */
   442 	CDecPkcs12SafeContents& operator=(const CDecPkcs12SafeContents& aDecPkcs12SafeContents);
   443 	
   444 
   445 private:
   446 	/** Contains an array of SafeBags objects */
   447 	RPointerArray<CDecPkcs12SafeBag> iSafeBags;
   448 	
   449 	/** Contains plain data */
   450 	HBufC8* iDecryptedData;
   451 	
   452 	};
   453 } // namespace PKCS12
   454 #endif // __PKCS12BAGS_H__