First public contribution.
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
31 #include <securitydefs.h>
38 /** Enumerates the identity of the algorithm.
43 /** An RSA algorithm. */
45 /** A DSA algorithm. */
47 /** A DH algorithm. */
49 /** A MD2 algorithm. */
51 /** A MD5 algorithm. */
53 /** A SHA-1 algorithm. */
55 /** A SHA-224 algorithm. */
57 /** A SHA-256 algorithm. */
59 /** A SHA-384 algorithm. */
61 /** A SHA-512 algorithm. */
65 class CValidityPeriod : public CBase
66 /** The period for which the certificate is valid.
71 /** Tests whether the specified date and time is within the validity period.
73 * @param aTime The date and time to be tested.
74 * @return ETrue, if the date and time is within the validity period;
75 * EFalse, otherwise. */
76 IMPORT_C TBool Valid(const TTime& aTime) const;
78 /** Gets the start of the validity period.
80 * @return The start date and time. */
81 IMPORT_C const TTime& Start() const;
83 /** Gets the end of the validity period.
85 * @return The end date and time. */
86 IMPORT_C const TTime& Finish() const;
90 * @param aValidityPeriod The validity period object to be copied. */
91 IMPORT_C CValidityPeriod(const CValidityPeriod& aValidityPeriod);
94 /** Default constructor. */
95 IMPORT_C CValidityPeriod();
97 /** The start time of the validity period. */
100 /** The end time of the validity period. */
104 class CAlgorithmIdentifier : public CBase
105 /** Contains an algorithm ID and any encoded parameters required by that algorithm.
107 * An object of this type creates and owns a heap descriptor to contain the encoded
113 /** Creates a new algorithm ID object copied from an existing object.
115 * @param aAlgorithmIdentifier The algorithm ID object to be copied.
116 * @return A pointer to the new algorithm ID object. */
117 IMPORT_C static CAlgorithmIdentifier* NewL(const CAlgorithmIdentifier& aAlgorithmIdentifier);
119 /** Creates a new algorithm ID object copied from an existing object, and puts
120 * a pointer to the new object onto the cleanup stack.
122 * @param aAlgorithmIdentifier The algorithm ID object to be copied.
123 * @return A pointer to the new algorithm ID object. */
124 IMPORT_C static CAlgorithmIdentifier* NewLC(const CAlgorithmIdentifier& aAlgorithmIdentifier);
126 /** Creates a new algorithm ID object.
128 * @param aAlgorithmId The algorithm ID.
129 * @param aEncodedParams The encoded parameters.
130 * @return A pointer to the new algorithm ID object. */
131 IMPORT_C static CAlgorithmIdentifier* NewL(TAlgorithmId& aAlgorithmId, const TDesC8& aEncodedParams);
133 /** Creates a new algorithm ID object, and puts a pointer to the new object onto
136 * @param aAlgorithmId The algorithm ID.
137 * @param aEncodedParams The encoded parameters.
138 * @return A pointer to the new algorithm ID object. */
139 IMPORT_C static CAlgorithmIdentifier* NewLC(TAlgorithmId& aAlgorithmId, const TDesC8& aEncodedParams);
141 /** Tests whether this algorithm identifier object is equal to the specified algorithm
144 * @param aAlgorithmIdentifier The algorithm identifier object to be compared.
145 * @return ETrue, if this algorithm identifier object is equal to the specified
146 * algorithm identifier object; EFalse otherwise. */
147 IMPORT_C TBool operator == (const CAlgorithmIdentifier& aAlgorithmIdentifier) const;
149 /** Gets the algorithm identifier.
151 * @return The algorithm identifier. */
152 IMPORT_C TAlgorithmId Algorithm() const; //ID for the algorithm
154 /** Gets the encoded parameters for the algorithm identifier.
156 * Note that this object owns the heap descriptor that owns the encoded parameters.
158 * @return The encoded parameters. */
159 IMPORT_C TPtrC8 EncodedParams() const; //the encoded parameters
163 * Frees all resources owned by the object. */
164 IMPORT_C ~CAlgorithmIdentifier();
167 /** Default constructor. */
168 IMPORT_C CAlgorithmIdentifier();
170 /** Constructor taking the specified parameters.
172 * @param aAlgorithmId The algorithm ID. */
173 IMPORT_C CAlgorithmIdentifier(TAlgorithmId& aAlgorithmId);
175 /** Second-phase constructor taking an existing algorithm identifier object.
177 * @param aAlgorithmIdentifier The algorithm identifier object. */
178 IMPORT_C virtual void ConstructL(const CAlgorithmIdentifier& aAlgorithmIdentifier);
180 /** Second-phase constructor taking encoded parameters.
182 * @param aEncodedParams The encoded parameters. */
183 IMPORT_C virtual void ConstructL(const TDesC8& aEncodedParams);
185 /** The algorithm ID. */
186 TAlgorithmId iAlgorithmId;
188 /** The encoded parameters for the algorithm ID. */
189 HBufC8* iEncodedParams;
192 class CSigningAlgorithmIdentifier : public CBase
193 /** Contains two CAlgorithmIdentifier objects for comparison purposes.
195 * Implements an equality operator.
200 /** Constructs a new Signing Algorithm Identifier object, copying an existing Signing
201 * Algorithm Identifier object.
203 * @param aSigningAlgorithmIdentifier The Signing Algorithm Identifier object.
204 * @return The new Signing Algorithm Identifier object. */
205 IMPORT_C static CSigningAlgorithmIdentifier* NewL(const CSigningAlgorithmIdentifier& aSigningAlgorithmIdentifier);
207 /** Constructs a new Signing Algorithm Identifier object, copying an existing Signing
208 * Algorithm Identifier object, and puts a pointer to it onto the cleanup stack.
210 * @param aSigningAlgorithmIdentifier The Signing Algorithm Identifier object.
211 * @return The new Signing Algorithm Identifier object. */
212 IMPORT_C static CSigningAlgorithmIdentifier* NewLC(const CSigningAlgorithmIdentifier& aSigningAlgorithmIdentifier);
214 /** Tests whether the Signing Algorithm Identifier object is equal to the specified
215 * Signing Algorithm Identifier object.
217 * @param aSigningAlgorithmIdentifier The Signing Algorithm Identifier object to be compared.
218 * @return ETrue, if this object's Signing Algorithm Identifier value
219 * is equal to the specified Signing Algorithm Identifier
220 * object's value; EFalse, otherwise. */
221 IMPORT_C TBool operator == (const CSigningAlgorithmIdentifier& aSigningAlgorithmIdentifier) const;
223 /** Gets the signature ID of the asymmetric algorithm.
225 * @return The signature ID of the asymmetric algorithm. */
226 IMPORT_C const CAlgorithmIdentifier& AsymmetricAlgorithm() const;
228 /** Gets the signature ID of the digest algorithm.
230 * @return The signature ID of the digest algorithm. */
231 IMPORT_C const CAlgorithmIdentifier& DigestAlgorithm() const;
235 * Frees all resources owned by the object, prior to its destruction. */
236 IMPORT_C ~CSigningAlgorithmIdentifier();
239 /** Second-phase constructor.
242 void ConstructL(const CSigningAlgorithmIdentifier& aSigningAlgorithmIdentifier);
244 /** The signature ID of the asymmetric algorithm. */
245 CAlgorithmIdentifier* iAsymmetricAlgorithm;
247 /** The signature ID of the digest algorithm. */
248 CAlgorithmIdentifier* iDigestAlgorithm;
251 class CSubjectPublicKeyInfo : public CBase
252 /** A base class for a container that holds information about a subject public key.
254 * It contains the algorithm ID, the encoded public key and the encoded parameters.
258 //algorithm ID + encoded public key + encoded parameters
261 /** Creates a new subject public key object copied from an existing object.
263 * @param aSubjectPublicKeyInfo The subject public key object to be copied.
264 * @return A pointer to the new public key object. */
265 IMPORT_C static CSubjectPublicKeyInfo* NewL(const CSubjectPublicKeyInfo& aSubjectPublicKeyInfo);
267 /** Creates a new subject public key object copied from an existing object and
268 * puts a pointer to the new object onto the cleanup stack.
270 * @param aSubjectPublicKeyInfo The subject public key object to be copied.
271 * @return A pointer to the new public key object. */
272 IMPORT_C static CSubjectPublicKeyInfo* NewLC(const CSubjectPublicKeyInfo& aSubjectPublicKeyInfo);
274 /** Gets the algorithm ID.
276 * @return The algorithm ID. */
277 IMPORT_C TAlgorithmId AlgorithmId() const;
279 /** Gets the encoded parameters required by the algorithm.
281 * @return A non-modifiable pointer descriptor representing the encoded parameters. */
282 IMPORT_C const TPtrC8 EncodedParams() const;
284 /** Gets the encoded public key data.
286 * @return A non-modifiable pointer descriptor representing the encoded public
288 IMPORT_C const TPtrC8 KeyData() const;
292 * Frees all resources owned by the object. */
293 IMPORT_C ~CSubjectPublicKeyInfo();
295 /** Second-phase constructor.
297 * @param aSubjectPublicKeyInfo The subject public key object to be copied. */
298 IMPORT_C virtual void ConstructL(const CSubjectPublicKeyInfo& aSubjectPublicKeyInfo);
300 /** The algorithm ID. */
301 CAlgorithmIdentifier* iAlgId;
303 /** A heap descriptor representing the encoded key data. */
304 HBufC8* iEncodedKeyData;
307 class CRSASignatureResult : public CBase
308 /** The RSA public key algorithm signature result.
311 * @li CWTLSRSASignatureResult
312 * @li CPKCS1SignatureResult.
314 * @see TKeyFactory::RSASignatureResultL()
319 /** Tests whether the signature result is valid.
321 * @param aResult The signature result.
322 * @return ETrue if the signature result is valid, otherwise EFalse. */
323 IMPORT_C virtual TBool VerifyL(const TDesC8& aResult) = 0;
327 * Frees all resources owned by the object. */
328 IMPORT_C ~CRSASignatureResult();
330 /** Compares this RSA Signature Result object with the specified RSA Signature
331 * Result object for equality.
333 * @param aResult The RSA Signature Result object to be compared.
334 * @return ETrue, if they are the same; EFalse, otherwise. */
335 IMPORT_C TBool operator == (const CRSASignatureResult& aResult) const;
337 /** The digest algorithm ID. */
338 CAlgorithmIdentifier* iDigestAlgorithm;
340 /** A heap descriptor representing the digest algorithm. */
346 /** Constructs the public key objects used for signature verification from their
347 * encoded binary form.
352 /** Gets the RSA public key.
354 * @param aEncoding A non-modifiable descriptor representing the entire encoding.
355 * @return The RSA Public key. */
356 virtual CRSAPublicKey* RSAPublicKeyL(const TDesC8& aEncoding) const = 0;
358 /** Gets the RSA signature result.
360 * @param aDigestAlgorithm The algorithm ID.
361 * @param aDigest A non-modifiable descriptor representing the digest algorithm.
362 * @return The RSA signature result. */
363 virtual CRSASignatureResult* RSASignatureResultL(const CAlgorithmIdentifier& aDigestAlgorithm, TDesC8& aDigest) const = 0;
365 /** Gets the DSA public key.
367 * @param aParams The DSA parameters
368 * @param aEncoding A non-modifiable descriptor representing the entire encoding.
369 * @return The DSA public key. */
370 virtual CDSAPublicKey* DSAPublicKeyL(const CDSAParameters& aParams, const TDesC8& aEncoding) const = 0;
372 /** Gets the digital DSA signature given an encoding key.
374 * @param aEncoding A non-modifiable descriptor representing the entire encoding.
375 * @return The DSA signature. */
376 virtual CDSASignature* DSASignatureL(const TDesC8& aEncoding) const = 0;
378 /** Gets the DSA parameters.
380 * @param aEncoding A non-modifiable descriptor representing the entire encoding.
381 * @return The DSA parameters. */
382 virtual CDSAParameters* DSAParametersL(const TDesC8& aEncoding) const = 0;
383 // New function for TKeyFactory API
384 virtual CDSAPublicKey* DSAPublicKeyL(const TDesC8& aParamsEncoding, const TDesC8& aEncoding) const = 0;
387 class CSigningKeyParameters : public CBase
388 /** Contains the parameter information required by some signing algorithms.
390 * The DSA signing algorithm needs parameters as well as a key. Currently, this
391 * class only contains DSA parameters.
396 /** Creates a new signing key parameters object.
398 * @return A pointer to the new signing key parameters object. */
399 IMPORT_C static CSigningKeyParameters* NewL();
401 /** Creates a new signing key parameters object and puts a pointer to the new object
402 * onto the cleanup stack.
404 * @return A pointer to the new signing key parameters object. */
405 IMPORT_C static CSigningKeyParameters* NewLC();
407 /** Creates a new signing key parameters object copied from an existing object.
409 * @param aParameters The signing key parameters object to be copied.
410 * @return A pointer to the new parameters object. */
411 IMPORT_C static CSigningKeyParameters* NewL(const CSigningKeyParameters& aParameters);
413 /** Creates a new signing key parameters object copied from an existing object
414 * and puts a pointer to the new object onto the cleanup stack.
416 * @param aParameters The signing key parameters object to be copied.
417 * @return A pointer to the new signing key parameters object. */
418 IMPORT_C static CSigningKeyParameters* NewLC(const CSigningKeyParameters& aParameters);
422 * Frees all resources owned by the object. */
423 IMPORT_C ~CSigningKeyParameters();
425 /** Sets the DSA parameters.
427 * @param aParams The DSA parameters. */
428 IMPORT_C void SetDSAParamsL(const CDSAParameters& aParams);
430 /** Gets the DSA parameters.
432 * @return The DSA parameters.
435 const CDSAParameters* DSAParams() const;
437 CSigningKeyParameters();
438 void ConstructL(const CSigningKeyParameters& aParameters);
439 CDSAParameters* iDSAParams;
443 class CSignedObject : public CBase
444 /** Base class for certificates.
449 /** Verifies a signature using the specified encoded key.
451 * @param aEncodedKey The encoded key.
452 * @return ETrue if the signature is valid, otherwise EFalse. */
453 IMPORT_C TBool VerifySignatureL(const TDesC8& aEncodedKey) const;
455 /** Verifies a signature using the specified encoded key and hash.
457 * @param aEncodedKey The encoded key.
458 * @param aHash The hash of the data to be validated.
459 * @return ETrue if the signature is valid, otherwise EFalse. */
460 IMPORT_C TBool VerifySignatureL(const TDesC8& aEncodedKey, const TDesC8& aHash) const;
462 /** Gets the digital signature.
464 * @return A non-modifiable pointer descriptor representing the digital signature. */
465 IMPORT_C const TPtrC8 Signature() const;
467 /** Gets the signed data.
469 * @return A non-modifiable pointer descriptor representing the signed data. */
470 IMPORT_C virtual const TPtrC8 SignedDataL() const = 0;
472 /** Gets the fingerprint.
474 * The fingerprint returned is the SHA1 hash of the encoding of the entire object.
476 * @return A non-modifiable pointer descriptor representing the finger print. */
477 IMPORT_C const TPtrC8 Fingerprint() const;
479 /** Gets the entire encoding.
481 * @return A non-modifiable pointer descriptor representing the entire encoding. */
482 IMPORT_C const TPtrC8 Encoding() const;
484 /** Gets the signing algorithm ID used.
486 * @return The signing algorithm ID. */
487 IMPORT_C const CSigningAlgorithmIdentifier& SigningAlgorithm() const;
489 /** Externalises the encoding of the entire object to a write stream.
491 * The fingerprint and the signed data can be regenerated after restoration.
493 * The presence of this function means that the standard templated operator<<()
494 * can be used to externalise objects of this class.
496 * @param aStream Stream to which the object should be externalised. */
497 IMPORT_C virtual void ExternalizeL(RWriteStream& aStream) const;
499 /** Internalises the encoded object from a read stream.
501 * The class makes use of a specification-specific parser class for extracting
502 * the various elements, that is provided by a subclass of CSignedObject. For
503 * this reason this function is pure virtual.
505 * The presence of this function means that the standard templated operator>>()
506 * can be used to internalise objects of this class.
508 * @param aStream Stream from which the contents of the field should be internalised. */
509 IMPORT_C virtual void InternalizeL(RReadStream& aStream) = 0;
511 /** Sets the signing key parameters.
513 * @param aParameters The signing key parameters. */
514 IMPORT_C void SetParametersL(const CSigningKeyParameters& aParameters);
516 /** Gets the encoded data for the specified encoded data element, in the (to be
517 * signed) tbsCertificate data structure, of the signed object.
519 * @param aIndex The encoded data element position in the tbsCertificate data
520 * structure. See the enumeration: CX509Certificate::Anonymous.
521 * @return The encoded data for the specified data element of the signed object. */
522 IMPORT_C virtual const TPtrC8* DataElementEncoding(const TUint aIndex) const = 0;
526 * Frees all resources owned by the object. */
527 IMPORT_C ~CSignedObject();
530 /** Verifies a RSA signature using the specified encoded key.
532 * @param aEncodedKey The encoded key.
533 * @return ETrue if the signature is valid, otherwise EFalse.
536 TBool VerifyRSASignatureL(const TDesC8& aEncodedKey) const;
539 TBool VerifyRSASignatureL(const TDesC8& aEncodedKey, const TDesC8& aHash) const;
541 /** A pointer to a key factory object. */
542 TKeyFactory* iKeyFactory;
544 /** A heap descriptor representing the entire encoding. */
547 /** The digital signature. */
552 * The SHA1 hash of the encoding of the entire object. */
553 HBufC8* iFingerprint;
555 /** The signing key parameters */
556 CSigningKeyParameters* iParameters;
558 /** The signing algorithm ID. */
559 CSigningAlgorithmIdentifier* iSigningAlgorithm;
562 class CCertificate : public CSignedObject
563 /** A data structure that binds a public key to a given individual.
565 * A certificate is a signed object, and adds a serial number, a validity period
566 * and a subject public key.
568 * This is a base class for classes that implement certificates of particular types.
575 * Frees all resources owned by the object. */
576 IMPORT_C ~CCertificate();
578 /** Gets the subject public key information.
580 * @return The subject public key information. */
581 IMPORT_C const CSubjectPublicKeyInfo& PublicKey() const;
583 /** Gets the serial number.
585 * @return A non-modifiable pointer descriptor representing the serial number. */
586 IMPORT_C const TPtrC8 SerialNumber() const;
588 /** Gets the validity period.
590 * @return The validity period. */
591 IMPORT_C const CValidityPeriod& ValidityPeriod() const;
593 /** Tests whether a certificate is self-signed.
595 * @return ETrue, if it is self-signed; EFalse, otherwise. */
596 IMPORT_C virtual TBool IsSelfSignedL() const = 0;
598 /** Gets the subject.
600 * @return A heap descriptor representing the subject. */
601 IMPORT_C virtual HBufC* SubjectL() const = 0;
605 * @return A heap descriptor representing the issuer. */
606 IMPORT_C virtual HBufC* IssuerL() const = 0;
608 /** Gets the key identifier.
610 * @return The key identifier. */
611 IMPORT_C virtual TKeyIdentifier KeyIdentifierL() const;
614 /** The serial number. */
615 HBufC8* iSerialNumber;
617 /** The validity period. */
618 CValidityPeriod* iValidityPeriod;
620 /** The subject public key information. */
621 CSubjectPublicKeyInfo* iSubjectPublicKeyInfo;