os/security/cryptoservices/certificateandkeymgmt/inc/pkcs10.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) 2002-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 * Declares classes for producing PKCS#10 certificate requests.
    16 *
    17 */
    18 
    19 
    20 /**
    21  @file
    22  @publishedPartner
    23  @released 
    24 */
    25 
    26 #ifndef __PKCS10_H__
    27 #define __PKCS10_H__
    28 
    29 #include <e32base.h>
    30 #include <asn1enc.h>
    31 #include <mctkeystore.h>
    32 #include <signed.h>
    33 
    34 class CX500DistinguishedName;
    35 class CASN1EncBase;
    36 class CPKCS10Attributes;
    37 class CPKCS10KeyHelper;
    38 class CMessageDigest;
    39 class TX509KeyEncoder;
    40 
    41 /** 
    42  * Class for making PKCS#10 Certificate Request objects.
    43  *
    44  * Keys are specified by a cryptotokens key info object - this means that this
    45  * API can only be used to generate cert requests for keys that are held in a
    46  * keystore on the device.
    47  * 
    48  */
    49 class CPKCS10Request : public CActive
    50 	{
    51 public:
    52 	/**
    53 	 * Creates a new PKCS#10 request object.
    54 	 * 
    55 	 * @param aDN X500 distinguished name of the entity provided by caller.
    56 	 *     Stored in iDN member variable. Ownership is not transferred.
    57 	 * @param aKeyInfo The key info object of the key to sign the request with.
    58 	 * 	   Does not take ownership.
    59 	 * @param aAttr (Optional) The PKCS10 attributes to include in the request.
    60 	 * 	   Takes ownership.
    61 	 * @return A pointer to the newly allocated object.
    62 	 */
    63 	IMPORT_C static CPKCS10Request* NewL(const CX500DistinguishedName& aDN,
    64 										 const CCTKeyInfo& aKeyInfo,
    65 										 CPKCS10Attributes* aAttr = NULL);
    66 	
    67 	/**
    68 	 * Creates a new PKCS#10 request object.
    69 	 * 
    70 	 * @param aDN X500 distinguished name of the entity provided by caller.
    71 	 *     Stored in iDN member variable. Ownership is not transferred.
    72 	 * @param aKeyInfo The key info object of the key to sign the request with.
    73 	 * 	   Does not take ownership.
    74 	 * @param aAttr (Optional) The PKCS10 attributes to include in the request.
    75 	 * 	   Takes ownership.
    76 	 * @return A pointer to the newly allocated object that is left on the
    77 	 * 	   cleanup stack.
    78 	 */
    79 	IMPORT_C static CPKCS10Request* NewLC(const CX500DistinguishedName& aDN,
    80 										 const CCTKeyInfo& aKeyInfo,
    81 										 CPKCS10Attributes* aAttr = NULL);
    82 
    83 	/**
    84 	 * Destructs PKCS#10 object, deletes encoding buffer and attributes.
    85 	 */
    86 	IMPORT_C virtual ~CPKCS10Request();
    87 
    88 public:
    89 
    90 	/**
    91 	 * Set the attributes to be encoded in the request. It replaces existing
    92 	 * attributes, if any.
    93 	 * @param aAttr The attributes - this object takes ownership.
    94 	 */
    95 	IMPORT_C void SetAttributes(CPKCS10Attributes* aAttr);
    96 
    97 	/**
    98 	 * Set the digest algorithm to use when signing the request.  If this method
    99 	 * is not called, the default SHA-1 is used. 
   100 	 *
   101 	 * @param aDigest	For RSA keys, one of EMD2, EMD5 or ESHA1.  
   102 	 * 					For DSA keys, ESHA1 is the only permitted value.
   103 	 * @leave KErrArgument if the specified algorithm is not supported.
   104 	 */
   105 	IMPORT_C void SetDigestAlgL(TAlgorithmId aDigest);
   106 
   107 	/**
   108 	 * Set the distinguished name of the entity. It replaces existing
   109 	 * name, if any.
   110 	 * @param aDN X500 distinguished name of the entity provided by caller.
   111 	 *     Stored in iDN member variable. Ownership is not transferred.
   112 	 */
   113 	IMPORT_C void SetDistinguishedNameL(const CX500DistinguishedName& aDN);
   114 
   115 	/**
   116 	 * Set the information of the key to sign with. It replaces existing
   117 	 * key info, if any.
   118 	 * @param aKeyInfo The key info object of the key to sign the request with.
   119 	 * 	   Does not take ownership.
   120 	 */
   121 	IMPORT_C void SetKeyInfoL(const CCTKeyInfo& aKeyInfo);
   122 
   123 	/**
   124 	 * Create the ASN.1 DER encoding of the certificate request.  This is an
   125 	 * asynchronous method. The Cancel() method can be called to cancel an
   126 	 * outstanding request. This method can be called repeatedly to create 
   127 	 * certificate requests after setting the various parameters. However an
   128 	 * outstanding request must complete or be cancelled before calling this 
   129 	 * method again.
   130 	 * 
   131 	 * 
   132 	 * @param aResult	On successful completion, this points to a newly
   133 	 * 					allocated buffer containing the encoded certificate request.
   134 	 * @param aStatus	Asynchronous status notification 
   135 	 */	
   136 	IMPORT_C void CreateEncoding(HBufC8*& aResult, TRequestStatus& aStatus);
   137 	
   138 private:
   139 
   140 	virtual void RunL();
   141 	virtual TInt RunError(TInt aErr);
   142 	virtual void DoCancel();
   143 
   144 	enum TState
   145 		{
   146 		EIdle,
   147 		EInitialize,
   148 		EGetKeyStore,
   149 		EGetPublicKey,
   150 		EOpenSigner,
   151 		ESign
   152 		};
   153 
   154 private:
   155 	/** Private constructor that initializes essential member variables. */
   156 	CPKCS10Request(const CX500DistinguishedName* aDN,
   157 				   const CCTKeyInfo* aKeyInfo,
   158 				   CPKCS10Attributes* aAttr);
   159 
   160 	// Methods making ASN.1 encoding objects
   161 
   162 	/**	
   163 	 * Performs the actual ASN.1 encoding of the request without signing it.
   164 	 * certRequestInfo is what gets signed with private key.
   165 	 * @return Pointer to a newly allocated CASN1EncSequence object.
   166 	 */
   167 	CASN1EncSequence* MakeCertRequestInfoEncLC();
   168 
   169 	/**
   170 	 * Encodes desired certificate attributes into ASN1. Takes whatever 
   171 	 * attributes are in the iAttributes and adds them below a 
   172 	 * sequence. If there are no attributes stored, leaves the set empty.
   173 	 * 
   174 	 * The structure of the attribute node is as follows:
   175 	 * @code
   176      * Context-specific[0]
   177      *    SEQUENCE-OF
   178      *      OID of the organization
   179      *      SET-OF
   180      *        SEQUENCE-OF (stored in iAttributes)
   181      *          SEQUENCE-OF
   182      *            OID of attribute
   183      *            OCTET STRING value
   184      *          SEQUENCE-OF
   185      *            OID of attribute
   186      *            OCTET STRING value
   187      *          ...
   188 	 * @endcode
   189 	 * @return Pointer to a newly allocated encoding object containing 
   190 	 *     desired certificate attributes.
   191 	 */
   192 	CASN1EncBase* MakeAttrEncLC();
   193 
   194 	/**
   195 	 * Generates data to be signed.
   196 	 */
   197 	void EncodeTBSDataL();
   198 
   199 	void CreateFinalEncodingL();
   200 
   201 	void Reset();
   202 
   203 private:
   204 	const CX500DistinguishedName*	iDN;
   205 	const CCTKeyInfo* 				iKeyInfo;
   206 	CPKCS10Attributes*				iAttributes;
   207 	TAlgorithmId					iDigestId;
   208 	TRequestStatus*					iClientStatus;
   209 	TState 							iState;
   210 	HBufC8**						iResult;
   211 	MCTKeyStore*					iKeyStore;
   212 	CPKCS10KeyHelper*				iKeyHelper;
   213 	HBufC8*							iExportedKey;
   214 	HBufC8*							iTBSData;
   215 	};
   216 
   217 #endif