sl@0: /*
sl@0: * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: * All rights reserved.
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of the License "Eclipse Public License v1.0"
sl@0: * which accompanies this distribution, and is available
sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: *
sl@0: * Initial Contributors:
sl@0: * Nokia Corporation - initial contribution.
sl@0: *
sl@0: * Contributors:
sl@0: *
sl@0: * Description: 
sl@0: * X.509 key classes and utility classes for key encoding/decoding.
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: /**
sl@0:  @file
sl@0:  @internalTechnology 
sl@0: */
sl@0: 
sl@0: #if !defined (X509KEYENCODER_H)
sl@0: #define X509KEYENCODER_H
sl@0: 
sl@0: #include <e32base.h>
sl@0: #include <e32std.h>
sl@0: #include <asymmetrickeys.h>
sl@0: #include <asymmetric.h>
sl@0: #include <hash.h>
sl@0: #include <bigint.h>
sl@0: #include <signed.h>
sl@0: 
sl@0: // Forward declarations
sl@0: class CASN1EncBase;
sl@0: class CASN1EncContainer;
sl@0: class CASN1EncSequence;
sl@0: class CASN1EncBitString;
sl@0: 
sl@0: class TX509KeyEncoder
sl@0: /**
sl@0:  * Abstract class that is the base class for RSA and DSA key encoder classes.
sl@0:  * These classes are used to encode the X509 ASN.1 types AlgorithmIdentifier and
sl@0:  * SubjectPublicKeyInfo.
sl@0:  *
sl@0:  * This class is part of the pkcs10 API, and will be changed or removed in a
sl@0:  * future release.  You should not use it.
sl@0:  * 
sl@0:  */
sl@0: 	{
sl@0: public:
sl@0: 	/**
sl@0: 	 * Constructor that takes an algorithm identifier and saves it into the 
sl@0: 	 * corresponding member variable. It is then used in the
sl@0: 	 * EncodeSignatureAlgorithm() function.
sl@0: 	 * 
sl@0: 	 * @param aDigestAlg	Digest algorithm to use. Currently the following 
sl@0: 	 * 						algorithms are supported: MD2, MD5, and SHA-1.
sl@0: 	 */
sl@0: 	TX509KeyEncoder(TAlgorithmId aDigestAlg);
sl@0: 
sl@0: 	/**
sl@0: 	 * Produces the SubjectPublicKeyInfo encoding. 
sl@0: 	 * 
sl@0: 	 * The encoding has the following ASN.1 format:
sl@0: 	 * @code
sl@0: 	 * SubjectPublicKeyInfo {ALGORITHM : IOSet} ::= SEQUENCE {
sl@0: 	 *     algorithm        AlgorithmIdentifier {{IOSet}},
sl@0: 	 *     subjectPublicKey BIT STRING
sl@0: 	 *     }
sl@0: 	 * @endcode
sl@0: 	 */
sl@0: 	IMPORT_C virtual CASN1EncBase* EncodeKeyLC() const = 0;
sl@0: 
sl@0: 	/**
sl@0: 	 * Produces the AlgorithmIdentifier encoding.
sl@0: 	 * 
sl@0: 	 * @return	ASN.1 sequence containing signature algorithm
sl@0: 	 */
sl@0: 	IMPORT_C virtual CASN1EncSequence* EncodeSignatureAlgorithmLC() const = 0;
sl@0: 
sl@0: 	/**
sl@0: 	 * Produces the DigestAlgorithmIdentifier encoder.
sl@0: 	 *
sl@0: 	 * The encoding has the following ASN.1 format
sl@0: 	 * @code
sl@0:      *   DigestAlgorithmIdentifier ::= SEQUENCE {
sl@0:      *     algorithm AlgorithmIdentifier,
sl@0:      *     parameters ANY DEFINED BY algorithm OPTIONAL }
sl@0:      *
sl@0:      *   AlgorithmIdentifier ::= OBJECT IDENTIFIER
sl@0: 	 * @endcode
sl@0:  	 *
sl@0: 	 * @return Appropriate ASN.1 sequence of type <code>DigestAlgorithmIdentifier</code>
sl@0: 	 */
sl@0: 	IMPORT_C virtual CASN1EncSequence* EncodeDigestAlgorithmLC() const;
sl@0: 
sl@0: protected:
sl@0: 	/** Digest algorithm to use. */
sl@0: 	TAlgorithmId iDigestAlg;
sl@0: 	};
sl@0: 
sl@0: class TX509RSAKeyEncoder : public TX509KeyEncoder
sl@0: /**
sl@0:  * Subclasses TC509KeyEncoder to provides key encoding capability for RSA public keys.
sl@0:  * 
sl@0:  * This class is part of the pkcs10 API, and will be changed or removed in a
sl@0:  * future release.  You should not use it.
sl@0:  * 
sl@0:  */
sl@0: 	{
sl@0: public:
sl@0: 	/**
sl@0: 	 * Constructs a RSA key pair encoder, saving reference to the passed 
sl@0: 	 * key pair in the member variable.
sl@0: 	 * 
sl@0: 	 * @param aPublicKey	RSA public key to use for encoding.
sl@0: 	 * @param aDigestAlg	Digest algorithm to use.
sl@0: 	 */
sl@0: 	IMPORT_C TX509RSAKeyEncoder(const CRSAPublicKey& aPublicKey, TAlgorithmId aDigestAlg);
sl@0: 
sl@0: 	/**
sl@0: 	 * Produces the SubjectPublicKeyInfo encoding. 
sl@0: 	 * 
sl@0: 	 * The resulting encoding has the following form:
sl@0: 	 * @code
sl@0: 	 *     SEQUENCE-OF
sl@0: 	 *         SEQUENCE-OF
sl@0: 	 *             OID of the encryption algorithm (KRSA)
sl@0: 	 *             NULL
sl@0: 	 *         BIT STRING encoded public key.
sl@0: 	 * @endcode
sl@0: 	 * 
sl@0: 	 * @return	DER-encoded public key information, placed on the cleanup stack.
sl@0: 	 */
sl@0: 	IMPORT_C virtual CASN1EncBase* EncodeKeyLC() const;
sl@0: 
sl@0: 	/**
sl@0: 	 * Produces the AlgorithmIdentifier encoding.
sl@0:   	 * 
sl@0: 	 * This has the following form:
sl@0: 	 * @code
sl@0: 	 *     SEQUENCE-OF
sl@0: 	 *         OID signature-algorithm
sl@0: 	 *         NULL
sl@0: 	 * @endcode
sl@0: 	 * 
sl@0: 	 * @return	ASN.1 sequence containing signature algorithm encoding, 
sl@0: 	 * 			placed on the cleanup stack.
sl@0: 	 */
sl@0: 	IMPORT_C virtual CASN1EncSequence* EncodeSignatureAlgorithmLC() const;
sl@0: 
sl@0: private:
sl@0: 	/**
sl@0: 	 * Saved reference to the RSA public key to be used for encoding.
sl@0: 	 */
sl@0: 	const CRSAPublicKey& iPublicKey;
sl@0: 	};
sl@0: 
sl@0: class TX509DSAKeyEncoder : public TX509KeyEncoder
sl@0: /**
sl@0:  * Provides key encoding and signing capability using a DSA public key.
sl@0:  * 
sl@0:  * This class is part of the pkcs10 API, and will be changed or removed in a
sl@0:  * future release.  You should not use it.
sl@0:  * 
sl@0:  */
sl@0: 	{
sl@0: public:
sl@0: 	/**
sl@0: 	 * Constructs a DSA key pair encoder, saving reference to the passed 
sl@0: 	 * public key in the member variable.
sl@0: 	 * 
sl@0: 	 * @param aKeyPublic	DSA public key to use for encoding.
sl@0: 	 * @param aDigestAlg	Digest algorithm to use.
sl@0: 	 */
sl@0: 	IMPORT_C TX509DSAKeyEncoder(const CDSAPublicKey& aKeyPublic, 
sl@0: 								TAlgorithmId aDigestAlg);
sl@0: 
sl@0: 	/**
sl@0: 	 * Produces the SubjectPublicKeyInfo encoding. 
sl@0:   	 * 
sl@0: 	 * The ASN.1 encoding of a DSA key has the following form:
sl@0: 	 * @code
sl@0: 	 * SEQUENCE-OF
sl@0: 	 *     SEQUENCE-OF
sl@0: 	 *         OID dsa (1.2.840.10040.4.1)
sl@0: 	 *         SEQUENCE-OF
sl@0: 	 *             INTEGER p
sl@0: 	 *             INTEGER q
sl@0: 	 *             INTEGER g
sl@0: 	 *     BIT STRING
sl@0: 	 *         INTEGER public value (y)
sl@0: 	 * @endcode
sl@0: 	 * 
sl@0: 	 * @return DER-encoded public key information, placed on the cleanup stack.
sl@0: 	 */
sl@0: 	IMPORT_C virtual CASN1EncBase* EncodeKeyLC() const;
sl@0: 
sl@0: 	/**
sl@0: 	 * Produces the AlgorithmIdentifier encoding.
sl@0:    	 * 
sl@0: 	 * This has the following form:
sl@0: 	 * @code
sl@0: 	 * SEQUENCE-OF
sl@0: 	 *     OID dsa-signature-oid
sl@0: 	 *     SEQUENCE-OF dsa-params
sl@0: 	 *         INTEGER p
sl@0: 	 *         INTEGER q
sl@0: 	 *         INTEGER g
sl@0: 	 * @endcode
sl@0: 	 * 
sl@0: 	 * @return	ASN.1 sequence containing signature algorithm encoding, 
sl@0: 	 *     		placed on the cleanup stack.
sl@0: 	 */
sl@0: 	IMPORT_C virtual CASN1EncSequence* EncodeSignatureAlgorithmLC() const;
sl@0: 
sl@0: private:
sl@0: 	/**
sl@0: 	 * Saved reference to the DSA public key to be used for encoding.
sl@0: 	 */
sl@0: 	const CDSAPublicKey& iPublicKey;
sl@0: 	};
sl@0: 
sl@0: #endif