First public contribution.
2 * Copyright (c) 2006-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.
25 #ifndef CMSSIGNEDOBJECT_H
26 #define CMSSIGNEDOBJECT_H
30 #include <cmscertchoice.h>
35 class CCmsSignerIdentifier;
36 class CCmsContentInfo;
37 class CEncapsulatedContentInfo;
38 class CX509Certificate;
39 class CX509AlgorithmIdentifier;
40 class CCmsCertificateChoice;
41 class CASN1EncSequence;
44 const TInt KCmsMaxSignedDataElements = 6;
46 A representation of a RFC2630 (signed data) entity.
48 class CCmsSignedObject : public CSignedObject
53 Index of CMS object fields.
58 Index of version field
63 Index of digest algorithm set field
65 EDigestAlgorithms = 1,
68 Index of encapsulated content info field
70 EEncapsulatedContentInfo = 2,
73 Index of certificate set field
78 Index of revocation list field
83 Index of signer info set field
89 Creates a CMS signed data object as defined in RFC2630. The CMS signed data created by
90 this API contains no signer info. SignL() method can be called to add more signer info.
91 @param aType The type of the encapsulated content.
92 @param aIsDetached A boolean indicating whether the encapsulated data is detached.
93 @param aContentData The encapsulated data.
94 If aIsDetached is EFalse, aContentData must not be KNullDesC8. Otherwise this API leave
96 If aIsDetached is ETrue, aContentData can be KNullDesC8. But user must provide hash
97 value when later calling SignL(). Otherwise SignL() leaves with KErrArguement.
98 @return The fully constructed object.
100 IMPORT_C static CCmsSignedObject* NewL(TCmsContentInfoType aType,
102 const TDesC8& aContentData);
105 Creates a CMS signed data object as defined in RFC2630, and leaves the object on the cleanup stack.
106 this API contains no signer info. SignL() method can be called to add more signer info.
107 @param aType The type of the encapsulated content.
108 @param aIsDetached A boolean indicating whether the encapsulated data is detached.
109 @param aContentData The encapsulated data.
110 If aIsDetached is EFalse, aContentData must not be KNullDesC8. Otherwise this API leave
112 If aIsDetached is ETrue, aContentData can be KNullDesC8. But user must provide hash
113 value when later calling SignL(). Otherwise SignL() leaves with KErrArguement.
114 @return The fully constructed object.
116 IMPORT_C static CCmsSignedObject* NewLC(TCmsContentInfoType aType,
118 const TDesC8& aContentData);
121 Creates a CMS signed data object as defined in RFC2630. This API only creates detached signed data
122 as no data content is provided. The CMS signed data created by this API contains one signer info.
123 SignL() method can be called to add more signer info.
124 @param aType Encapsulated Content data type.
125 @param aHashValue The hash value of the data content to be signed.
126 @param aDigestAlgorithm The digest algorithm used to create the hash.
127 @param aKey The DSA private key used to sign.
128 @param aCert The signer's certificate.
129 @param aAddCertificate A boolean indicating whether the signer's certificate is added to the signed data object.
130 @return The fully constructed object.
132 IMPORT_C static CCmsSignedObject* NewL(TCmsContentInfoType aType,
133 const TDesC8& aHashValue,
134 TAlgorithmId aDigestAlgorithm,
135 const CDSAPrivateKey& aKey,
136 const CX509Certificate& aCert,
137 TBool aAddCertificate);
140 Creates a CMS signed data object as defined in RFC2630 and leaves the object on the cleanup stack.
141 This API only creates detached signed data as no data content is provided. The CMS signed data
142 created by this API contains one signer info. SignL() method can be called to add more signer info.
143 @param aType Encapsulated Content data type.
144 @param aHashValue The hash value of the data content to be signed.
145 @param aDigestAlgorithm The digest algorithm used to create the hash.
146 @param aKey The DSA private key used to sign.
147 @param aCert The signer's certificate.
148 @param aAddCertificate A boolean indicating whether the signer's certificate is added to the signed data object.
149 @return The fully constructed object.
151 IMPORT_C static CCmsSignedObject* NewLC(TCmsContentInfoType aType,
152 const TDesC8& aHashValue,
153 TAlgorithmId aDigestAlgorithm,
154 const CDSAPrivateKey& aKey,
155 const CX509Certificate& aCert,
156 TBool aAddCertificate);
159 Creates a CMS signed data object as defined in RFC2630. This API only creates detached signed data
160 as no data content is provided. The CMS signed data created by this API contains one signer info.
161 SignL() method can be called to add more signer info.
162 @param aType Encapsulated Content data type.
163 @param aHashValue The hash value of the data content to be signed.
164 @param aDigestAlgorithm The digest algorithm used to create the hash.
165 @param aKey The RSA private key used to sign.
166 @param aCert aCert The signer's certificate.
167 @param aAddCertificate A boolean indicating whether the signer's certificate is added to the signed data object.
168 @return The fully constructed object.
170 IMPORT_C static CCmsSignedObject* NewL(TCmsContentInfoType aType,
171 const TDesC8& aHashValue,
172 TAlgorithmId aDigestAlgorithm,
173 const CRSAPrivateKey& aKey,
174 const CX509Certificate& aCert,
175 TBool aAddCertificate);
178 Creates a CMS signed data object as defined in RFC2630 and leaves the object on the cleanup stack.
179 This API only creates detached signed data as no data content is provided. The CMS signed data
180 created by this API contains one signer info. SignL() method can be called to add more signer info.
181 @param aType Encapsulated Content data type.
182 @param aHashValue The hash value of the data content to be signed.
183 @param aDigestAlgorithm The digest algorithm used to create the hash.
184 @param aKey The RSA private key used to sign.
185 @param aCert The signer's certificate.
186 @param aAddCertificate A boolean indicating whether the signer's certificate is added to the signed data object.
187 @return The fully constructed object.
189 IMPORT_C static CCmsSignedObject* NewLC(TCmsContentInfoType aType,
190 const TDesC8& aHashValue,
191 TAlgorithmId aDigestAlgorithm,
192 const CRSAPrivateKey& aKey,
193 const CX509Certificate& aCert,
194 TBool aAddCertificate);
197 Creates a CMS signed data object as defined in RFC2630.
198 @param aContentInfo The CMS content info that contains the encoded signed object.
199 @return The fully constructed object.
201 IMPORT_C static CCmsSignedObject* NewL(const CCmsContentInfo& aContentInfo);
204 Creates a CMS signed data object as defined in RFC2630 and leaves it on the cleanup stack.
205 @param aContentInfo The CMS content info that contains the encoded signed object.
206 @return The fully constructed object.
208 IMPORT_C static CCmsSignedObject* NewLC(const CCmsContentInfo& aContentInfo);
212 Creates one signature and adds it to the Signer info list. The signing certificate
213 is added to the certificate list if the last boolean parameter aAddCertificate is true and
214 it does not exist in the list. The digest algorithm is added to the digest algorithm list if it
215 does not exist in the list. Calling this API multiple times will create multiple signatures.
216 @param aHashValue The hash value to be signed. If this is an empty string,
217 the content data to be signed must have been passed in via
218 NewL method and hash value will be calculated by the implementation
220 @param aDigestAlgorithm The digest algorithm used to create the hash.
221 @param aKey the DSA private key used to sign.
222 @param aCert the signer's certificate.
223 @param aAddCertificate A boolean indicating whether the signer's certificate is added to the signed data object.
224 @leave KErrArgument if no hash nor data content is provided.
226 IMPORT_C void SignL(const TDesC8& aHashValue,
227 TAlgorithmId aDigestAlgorithm,
228 const CDSAPrivateKey& aKey,
229 const CX509Certificate& aCert,
230 TBool aAddCertificate);
234 Creates one signature and adds it to the Signer info list. The signing certificate
235 is added to the certificate list if the last boolean parameter aAddCertificate is true and
236 it does not exist in the list. The digest algorithm is added to the digest algorithm list if it
237 does not exist in the list. Calling this API multiple times will create multiple signatures.
238 @param aHashValue The hash value to be signed. If this is an empty string,
239 the content data to be signed must have been passed in via
240 NewL method and hash value will be calculated by the implementation
242 @param aDigestAlgorithm The digest algorithm used to create the hash.
243 @param aKey the RSA private key used to sign.
244 @param aCert the signer's certificate.
245 @param aAddCertificate A boolean indicating whether the signer's certificate is added to the signed data object.
246 @leave KErrArgument if no hash nor data content is provided.
248 IMPORT_C void SignL(const TDesC8& aHashValue,
249 TAlgorithmId aDigestAlgorithm,
250 const CRSAPrivateKey& aKey,
251 const CX509Certificate& aCert,
252 TBool aAddCertificate);
257 IMPORT_C ~CCmsSignedObject();
260 virtual from signedobject class
262 IMPORT_C virtual const TPtrC8* DataElementEncoding(const TUint aIndex) const;
263 IMPORT_C virtual void InternalizeL(RReadStream& aStream) ;
264 IMPORT_C virtual const TPtrC8 SignedDataL() const;
267 Returns whether the certificate list exists.
268 @return Boolean indicating whether the certificate list exists.
270 IMPORT_C TBool IsCertificateSetPresent() const;
273 Returns whether the certificate revocation list exists.
274 @return Boolean indicating whether the certificate Revocation list exists.
276 IMPORT_C TBool IsCertificateRevocationListsPresent() const;
279 Returns the version of this CMS signed object.
280 @return The version of this CMS signed object.
282 IMPORT_C TInt Version() const;
285 Returns the employed algorithm list.
286 @return The employed algorithm list reference.
288 IMPORT_C const RPointerArray<CX509AlgorithmIdentifier>& DigestAlgorithms() const;
291 Returns the certificates list.
292 @return The certificates list reference.
294 IMPORT_C const RPointerArray<CCmsCertificateChoice>& Certificates() const;
297 Returns the encapsulated content info of this signed object.
298 @return The encapsulated content info reference.
300 IMPORT_C const CEncapsulatedContentInfo& ContentInfo() const;
304 Retrieves the list of SignerInfo objects.
305 @return The signer info list reference.
307 IMPORT_C const RPointerArray<CCmsSignerInfo>& SignerInfo() const;
311 Creates the ASN1 sequence of this CMS signed object and leaves it on the cleanup stack.
312 @return ASN1 sequence of this object.
314 IMPORT_C CASN1EncSequence* EncodeASN1DERLC() const;
317 Appends the X509 certificate to the certificate list.
318 @param aCert The X509 certificate to be appended.
320 IMPORT_C void AddCertificateL(const CX509Certificate& aCert);
324 Appends an encoded attribute certificate to the certificate list.
325 @param aCert The encoded certificate to be appended.
326 @param aType The type of the encoded certificate..
328 IMPORT_C void AddCertificateL(const TDesC8& aCert, CCmsCertificateChoice::TCertificateType aType);
331 Validates the signer and creates the certificate chain for that signer. This API is used to validate attached signature.
332 @param aSignerInfo The signer to be validated.
333 @param aCertChainEncoding The certificate chain. This is created and pushed onto the cleanup stack by the function.
334 @leave KErrNotFound There is no matching certificate.
335 @return Boolean that identifies whether the signer can be validated.
337 IMPORT_C TBool ValidateSignerLC(const CCmsSignerInfo& aSignerInfo, HBufC8*& aCertChainEncoding);
340 Validates the signer and creates the certificate chain for that signer. This API is used to validate attached signature.
341 @param aSignerInfo The signer to be validated.
342 @param aCertificates The certificate list provided by the user to validate the signature.
343 @param aCertChainEncoding The certificate chain. This is created and pushed onto the cleanup stack by the function.
344 @return Boolean that identifies whether the signer can be validated.
345 @leave KErrNotFound There is no matching certificate.
347 IMPORT_C TBool ValidateSignerLC(const CCmsSignerInfo& aSignerInfo, const RPointerArray<CX509Certificate>& aCertificates, HBufC8*& aCertChainEncoding);
352 Validates the signer and creates the certificate chain for that signer. This API is used to validate detached signature.
353 @param aSignerInfo The signer to be validated.
354 @param aCertChainEncoding The certificate chain. This is created and pushed onto the cleanup stack by the function.
355 @param aIsHash The flag represent if the next parameter is the hash of the data content.
356 @param aContentDataOrHash the descriptor that contains the data content or its hash
357 @leave KErrNotFound There is no matching certificate.
358 @return Boolean that identifies whether the signer can be validated.
360 IMPORT_C TBool ValidateSignerLC(const CCmsSignerInfo& aSignerInfo, HBufC8*& aCertChainEncoding, TBool aIsHash, const TDesC8& aContentDataOrHash);
363 Validates the signer and creates the certificate chain for that signer. This API is used to validate detached signature.
364 @param aSignerInfo The signer to be validated.
365 @param aCertificates The certificate list provided by the user to validate the signature.
366 @param aCertChainEncoding The certificate chain. This is created and pushed onto the cleanup stack by the function.
367 @param aIsHash The flag represent if the next parameter is the hash of the data content.
368 @param aContentDataOrHash the descriptor that contains the data content or its hash
369 @return Boolean that identifies whether the signer can be validated.
370 @leave KErrNotFound There is no matching certificate.
372 IMPORT_C TBool ValidateSignerLC(const CCmsSignerInfo& aSignerInfo, const RPointerArray<CX509Certificate>& aCertificates, HBufC8*& aCertChainEncoding, TBool aIsHash, const TDesC8& aContentDataOrHash);
384 Second phase constructor for decoding a CMS signed data object
385 @param aContentInfo the content info which contains the CMS signed data.
387 void ConstructL(const CCmsContentInfo& aContentInfo);
390 Second phase constructor for constructing a CMS signed data object from data content.
391 @param aType the encapsulated content info type.
392 @param aIsDetached if the CMS signed data does not contains the data content being signed.
393 @param aContentData the content data descriptor.
395 void ConstructL(TCmsContentInfoType aType, TBool aIsDetached, const TDesC8& aContentData);
398 Second phase constructor for constructing a CMS signed data object from data content hash
399 @param aType the encapsulated content info type.
400 @param aHashValue the hash of the data content to create the signature.
401 @param aDigestAlgorithm the digest algorithm.
402 @param aKey the DSA private to create signature.
403 @param aCert the signer's certficate
404 @param aAddCertificate a flag to represent if the signer's certificate is added to certificate set.
406 void ConstructL(TCmsContentInfoType aType,
407 const TDesC8& aHashValue,
408 TAlgorithmId aDigestAlgorithm,
409 const CDSAPrivateKey& aKey,
410 const CX509Certificate& aCert,
411 TBool aAddCertificate);
413 Second phase constructor for constructing a CMS signed data object from data content hash
414 @param aType the encapsulated content info type.
415 @param aHashValue the hash of the data content to create the signature.
416 @param aDigestAlgorithm the digest algorithm.
417 @param aKey the RSA private to create signature.
418 @param aCert the signer's certficate
419 @param aAddCertificate a flag to represent if the signer's certificate is added to certificate set.
421 void ConstructL(TCmsContentInfoType aType,
422 const TDesC8& aHashValue,
423 TAlgorithmId aDigestAlgorithm,
424 const CRSAPrivateKey& aKey,
425 const CX509Certificate& aCert,
426 TBool aAddCertificate);
428 Append the algorithm to the algoritm list
429 @param aDigestAlgorithm the algorithm ID.
431 void AddDigestAlgorithmL(TAlgorithmId aDigestAlgorithm);
434 Build the signer's identifier from the signer's certificate. If the signer's certificate
435 contains the subject identifier extension, the signer identifier is subject id extension.
436 otherwise, the signer identifier is isuuer name and serial number.
437 @param aCert the signer's certificate.
438 @return a CMS signer identifier instance pointer
440 CCmsSignerIdentifier* BuildSignerIdentifierLC(const CX509Certificate& aCert);
443 Build the signer list, algorithm list and certificate list in the CMS signer data.
444 @param aDigestAlgorithm the digest algorithm identifier.
445 @param aIsHash A flag the represent if the next descriptor is the hash value rather that original data
446 @param aValue the data content or its hash.
447 @param aKey the DSA private used to sign.
448 @param aCert the signer's certificate
449 @param aAddCertificate the flag to represent if the certificate is added to the certificate set
451 void BuildSignerInfoCertListAndAlgoritmListL(TAlgorithmId aDigestAlgorithm,
453 const TDesC8& aValue,
454 const CDSAPrivateKey& aKey,
455 const CX509Certificate& aCert,
456 TBool aAddCertificate);
459 Build the signer list, algorithm list and certificate list in the CMS signer data.
460 @param aDigestAlgorithm the digest algorithm identifier.
461 @param aIsHash A flag the represent if the next descriptor is the hash value rather that original data
462 @param aValue the data content or its hash.
463 @param aKey the RSA private used to sign.
464 @param aCert the signer's certificate
465 @param aAddCertificate the flag to represent if the certificate is added to the certificate set
467 void BuildSignerInfoCertListAndAlgoritmListL(TAlgorithmId aDigestAlgorithm,
469 const TDesC8& aValue,
470 const CRSAPrivateKey& aKey,
471 const CX509Certificate& aCert,
472 TBool aAddCertificate);
474 Initialise the signed data base class members for the validation process.
475 @param aRawData the raw data of the CMS signed data.
477 void InitSignedObjectL(const TDesC8& aRawData);
481 Decode the CMS Signer data.
482 @param aRawData the raw data of the CMS signed data.
484 void DecodeSignedDataL(const TDesC8& aRawData);
487 Decode the digest algorithm set.
488 @param the raw data of the algorithm list.
490 void DecodeDigestAlgorithmsL(const TDesC8& aRawData);
493 Decode the encapsulated content info
494 @param the raw data of the encapsulated content info.
496 void DecodeEncapsulatedContentInfoL(const TDesC8& aRawData);
499 Decode the certificate set.
500 @param the raw data of the certificate list
502 void DecodeCertificatesL(const TDesC8& aRawData);
505 Decode the certificate revocation set. Not implemented now!
506 @param the raw data of the certificate revocation list.
508 void DecodeRevocationListsL(const TDesC8& aRawData);
511 Decode the signer info set.
512 @param the raw data of the certificate revocation list.
514 void DecodeSignerInfoL(const TDesC8& aRawData);
517 Encode the certificate set
518 @return the encoding of the certificate set
520 CASN1EncBase* EncodeCertificatesLC() const;
523 Encode the algorithm set
524 @return the encoding of the digest algorithm set
526 CASN1EncBase* EncodeAlgorithmsLC() const;
529 Encode the signer info set
530 @return the encoding of the certificate set
532 CASN1EncBase* EncodeSignerInfoLC() const;
535 Validate the signature by the given certificate.
536 @param aSignerInfo the signer info reference contains the signature
537 @param aEndEntityCert the certificate used to create the signature.
538 @return if the signature can be validated
540 TBool ValidateSignatureL(const CCmsSignerInfo& aSignerInfo, const CX509Certificate& aEndEntityCert);
543 This function is called when validating a detached CMS signed object.
544 It sets the data content being signed so that the signed data can be validated.
545 @param aContentData The data content being signed.
547 void SetContentData(const TDesC8& aContentData);
550 This function is called when validating a detached CMS signed object.
551 It sets the hash being signed so that the signed data can be validated.
552 @param aHash The hash being signed.
554 void SetHash(const TDesC8& aHash);
559 Reprents if the certificate set is present
561 TBool iIsCertificateSetPresent;
564 Reprents if the certificate revocationlisy is present
566 TBool iIsCertificateRevocationListsPresent;
569 Version of the Signed object
576 RPointerArray<CX509AlgorithmIdentifier> iDigestAlgorithms;
579 Encapsulated Content List
581 CEncapsulatedContentInfo* iContentInfo;
586 RPointerArray<CCmsCertificateChoice> iCertificates;
591 RPointerArray<CCmsSignerInfo> iSignerInfo;
594 Array of Encoded fields
596 TFixedArray<TPtrC8*, KCmsMaxSignedDataElements> iDataElements;
599 The data content being signed
604 The Hash being signed
610 #endif //CMSSIGNEDOBJECT_H