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.
19 #include <cmssignerinfo.h>
23 #include <cmssigneridentifier.h>
26 #include "pkcs7asn1.h"
29 // Implementation of CCmsSignerInfo
31 CCmsSignerInfo* CCmsSignerInfo::NewL(const TDesC8& aDataToBeSigned,
33 const CDSAPrivateKey& aKey,
34 CCmsSignerIdentifier* aSignerIdentifier,
35 CX509AlgorithmIdentifier* aDigestAlgorithm,
36 CX509AlgorithmIdentifier* aSignatureAlgorithm)
38 CCmsSignerInfo* self = NewLC(aDataToBeSigned,
44 CleanupStack::Pop(self);
48 CCmsSignerInfo* CCmsSignerInfo::NewLC(const TDesC8& aDataToBeSigned,
50 const CDSAPrivateKey& aKey,
51 CCmsSignerIdentifier* aSignerIdentifier,
52 CX509AlgorithmIdentifier* aDigestAlgorithm,
53 CX509AlgorithmIdentifier* aSignatureAlgorithm)
55 if (!aSignerIdentifier||!aDigestAlgorithm||!aSignatureAlgorithm)
57 User::Leave(KErrArgument);
59 CCmsSignerInfo* self = new (ELeave) CCmsSignerInfo();
60 CleanupStack::PushL(self);
61 self->ConstructL(aDataToBeSigned, aIsHash, aKey, aSignerIdentifier, aDigestAlgorithm, aSignatureAlgorithm);
65 CCmsSignerInfo* CCmsSignerInfo::NewL(const TDesC8& aDataToBeSigned,
67 const CRSAPrivateKey& aKey,
68 CCmsSignerIdentifier* aSignerIdentifier,
69 CX509AlgorithmIdentifier* aDigestAlgorithm,
70 CX509AlgorithmIdentifier* aSignatureAlgorithm)
72 CCmsSignerInfo* self = NewLC(aDataToBeSigned,
78 CleanupStack::Pop(self);
82 CCmsSignerInfo* CCmsSignerInfo::NewLC(const TDesC8& aDataToBeSigned,
84 const CRSAPrivateKey& aKey,
85 CCmsSignerIdentifier* aSignerIdentifier,
86 CX509AlgorithmIdentifier* aDigestAlgorithm,
87 CX509AlgorithmIdentifier* aSignatureAlgorithm)
89 if (!aSignerIdentifier||!aDigestAlgorithm||!aSignatureAlgorithm)
91 User::Leave(KErrArgument);
94 CCmsSignerInfo* self = new (ELeave) CCmsSignerInfo();
95 CleanupStack::PushL(self);
96 self->ConstructL(aDataToBeSigned, aIsHash, aKey, aSignerIdentifier, aDigestAlgorithm, aSignatureAlgorithm);
100 CCmsSignerInfo* CCmsSignerInfo::NewL(const TDesC8& aRawData)
102 CCmsSignerInfo* self = NewLC(aRawData);
103 CleanupStack::Pop(self);
107 CCmsSignerInfo* CCmsSignerInfo::NewLC(const TDesC8& aRawData)
109 CCmsSignerInfo* self = new (ELeave) CCmsSignerInfo();
110 CleanupStack::PushL(self);
111 self->ConstructL(aRawData);
115 CCmsSignerInfo::CCmsSignerInfo()
119 CCmsSignerInfo::~CCmsSignerInfo()
121 delete iDigestAlgorithm;
122 delete iSignatureAlgorithm;
123 delete iSignatureValue;
124 delete iSignerIdentifier;
127 void CCmsSignerInfo::ConstructL(const TDesC8& aDataToBeSigned,
129 const CDSAPrivateKey& aKey,
130 CCmsSignerIdentifier* aSignerIdentifier,
131 CX509AlgorithmIdentifier* aDigestAlgorithm,
132 CX509AlgorithmIdentifier* aSignatureAlgorithm)
135 if (aSignatureAlgorithm->Algorithm()!=EDSA)
137 User::Leave(KErrArgument);
139 iSignatureValue=CmsUtils::CreateSignatureL(aDataToBeSigned, aIsHash, aDigestAlgorithm->Algorithm(), aKey);
141 iSignerIdentifier=aSignerIdentifier;
142 iDigestAlgorithm=aDigestAlgorithm;
143 iSignatureAlgorithm=aSignatureAlgorithm;
144 //find out the CMS signer info version
145 if (iSignerIdentifier->SignerIdentifierType() == CCmsSignerIdentifier::EIssuerAndSerialNumber)
149 else if (iSignerIdentifier->SignerIdentifierType() == CCmsSignerIdentifier::ESubjectKeyIdentifier)
156 void CCmsSignerInfo::ConstructL(const TDesC8& aDataToBeSigned,
158 const CRSAPrivateKey& aKey,
159 CCmsSignerIdentifier* aSignerIdentifier,
160 CX509AlgorithmIdentifier* aDigestAlgorithm,
161 CX509AlgorithmIdentifier* aSignatureAlgorithm)
163 if (aSignatureAlgorithm->Algorithm()!=ERSA)
165 User::Leave(KErrArgument);
167 iSignatureValue=CmsUtils::CreateSignatureL(aDataToBeSigned, aIsHash, aDigestAlgorithm->Algorithm(), aKey);
168 iSignerIdentifier=aSignerIdentifier;
169 iDigestAlgorithm=aDigestAlgorithm;
170 iSignatureAlgorithm=aSignatureAlgorithm;
171 //find out the CMS signer info version
172 if (iSignerIdentifier->SignerIdentifierType() == CCmsSignerIdentifier::EIssuerAndSerialNumber)
176 else if (iSignerIdentifier->SignerIdentifierType() == CCmsSignerIdentifier::ESubjectKeyIdentifier)
182 void CCmsSignerInfo::ConstructL(const TDesC8& aRawData)
184 CArrayPtr<TASN1DecGeneric>* signerInfo = PKCS7ASN1::DecodeSequenceLC(aRawData, 5, 7);
185 TASN1DecInteger decInt;
188 iVersion = decInt.DecodeDERShortL(*signerInfo->At(pos++));
189 if (iVersion<0 || iVersion>4)
191 User::Leave(KErrArgument);
194 DecodeSignerIdentifierL(signerInfo->At(pos++)->Encoding());
196 iDigestAlgorithm = CX509AlgorithmIdentifier::NewL(signerInfo->At(pos++)->Encoding());
198 if(signerInfo->At(pos)->Tag() == 0 && signerInfo->At(pos)->Class() == EContextSpecific)
200 // authenticated attributes not supported at this time
201 iSignedAttributesPresent=ETrue;
204 iSignatureAlgorithm = CX509AlgorithmIdentifier::NewL(signerInfo->At(pos++)->Encoding());
205 DecodeEncryptedDigestL(signerInfo->At(pos++)->Encoding());
207 if(pos < signerInfo->Count() && signerInfo->At(pos)->Tag() == 1 && signerInfo->At(pos)->Class() == EContextSpecific)
209 // unauthenticated attributes not supported at this time
210 iUnsignedAttributesPresent=ETrue;
214 CleanupStack::PopAndDestroy(signerInfo);
217 CASN1EncSequence* CCmsSignerInfo::EncodeASN1DERLC() const
219 // the root sequence contains the signed object
220 CASN1EncSequence* root = CASN1EncSequence::NewLC();
223 CASN1EncInt* version=CASN1EncInt::NewLC(iVersion);
224 root->AddAndPopChildL(version);
227 CASN1EncBase* sid=EncodeSignerIdentifierLC();
228 root->AddAndPopChildL(sid);
230 //Encode Digest Algoritm
231 CASN1EncSequence* digAlg=iDigestAlgorithm->EncodeASN1DERLC();
232 root->AddAndPopChildL(digAlg);
234 //Encode signature Algoritm
235 CASN1EncSequence* sigAlg=iSignatureAlgorithm->EncodeASN1DERLC();
236 root->AddAndPopChildL(sigAlg);
238 //Encode signature value
239 CASN1EncOctetString* sigEnc=CASN1EncOctetString::NewLC(iSignatureValue->Des());
240 root->AddAndPopChildL(sigEnc);
245 void CCmsSignerInfo::DecodeEncryptedDigestL(const TDesC8& aRawData)
247 CmsUtils::DecodeOctetStringL(aRawData, iSignatureValue);
250 void CCmsSignerInfo::DecodeSignerIdentifierL(const TDesC8& aRawData)
252 iSignerIdentifier=CCmsSignerIdentifier::NewL(aRawData);
255 CASN1EncBase* CCmsSignerInfo::EncodeSignerIdentifierLC() const
257 return iSignerIdentifier->EncodeASN1DERLC();
260 EXPORT_C TInt CCmsSignerInfo::Version() const
265 EXPORT_C TBool CCmsSignerInfo::IsSignedAttributesPresent() const
267 return iSignedAttributesPresent;
270 EXPORT_C TBool CCmsSignerInfo::IsUnsignedAttributesPresent() const
272 return iUnsignedAttributesPresent;
275 EXPORT_C const CX509AlgorithmIdentifier& CCmsSignerInfo::DigestAlgorithm() const
277 return *iDigestAlgorithm;
280 EXPORT_C const CX509AlgorithmIdentifier& CCmsSignerInfo::SignatureAlgorithm() const
282 return *iSignatureAlgorithm;
285 EXPORT_C const TPtrC8 CCmsSignerInfo::SignatureValue() const
287 return *iSignatureValue;
290 EXPORT_C const CCmsSignerIdentifier& CCmsSignerInfo::SignerIdentifier() const
292 return *iSignerIdentifier;