Update contrib.
2 * Copyright (c) 2001-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.
16 * The unified certificate store implementation
27 #ifndef __UNIFIEDCERTSTORE_H__
28 #define __UNIFIEDCERTSTORE_H__
35 #include <e32property.h>
37 #include <mcertstore.h>
38 #include <ct/rmpointerarray.h>
39 #include <ct/rcpointerarray.h>
46 * Publish and Subscribe - UnifiedCertSTore Category
47 * Aliased here to System Category to prevent SC break.
50 const TUid KUnifiedCertStorePropertyCat = {KUidSystemCategoryValue};
54 * Publish and subscribe key for UnifiedCertSTore.
56 enum TUnifiedCertStoreKey
60 * The Publish and subscribe key for the certstore changes.
61 * If the client of the UnifiedCertstore needs to be notified when
62 * certificate addition, removal and trust&application setting occurs,
63 * the client needs to subscribe to KUnifiedCertStorePropertyCat and
64 * EUnifiedCertStoreFlag.
66 * Aliased here to KUidUnifiedCertstore flag to avoid SC break.
68 EUnifiedCertStoreFlag = KUidUnifiedCertstoreFlag
73 * The Unique ID for unknown hardware certstore, used as the input parameter when it is to be filtered.
74 * @deprecated. Used only for Data compatibility.
77 const TInt KUnknownHardwareCertStore = 0;
82 * The Unique ID for mutable software certstore, used as the input parameter when it is to be filtered.
86 const TInt KThirdPartyCertStore = 1;
91 * The Unique ID for SIM certstore, used as the input parameter when it is to be filtered.
95 const TInt KSIMCertStore = 2;
99 * The Unique ID for WIM certstore, used as the input parameter when it is to be filtered.
103 const TInt KWIMCertStore = 3;
107 * The Unique ID for UICC certstore, used as the input parameter when it is to be filtered.
110 const TInt KUICCCertStore = 4;
114 * The Unique ID for immutable software certstore, used as the input parameter when it is to be filtered.
118 const TInt KManufactureCertStore = 5;
120 // Forward declarations
122 class MCTWritableCertStore;
123 class MCTTokenInterface;
127 class CCertAttributeFilter;
128 class CCTTokenTypeInfo;
129 class TCTTokenObjectHandle;
130 class CCheckedCertStore;
132 // This class is forward declared to avoid including its definition in this
133 // exported header file because it must only be used internally.
134 class CUnifiedCertStoreWorkingVars;
135 class CX500DistinguishedName;
138 * The unified certificate store.
140 * This class provides a certificate store whose contents are the sum of the
141 * contents of all certificate store implementations on the device. It is
142 * intended as the single point of access for clients wishing to use certificate
145 * Since this class is intended for widespread use, capability checks relating
146 * to certificate access are documented here even though the checks are actually
147 * made in the individual cert store implementations.
150 NONSHARABLE_CLASS(CUnifiedCertStore) : public CActive, public MCertStore
154 * Creates a new CUnifiedCertStore
156 * @param aFs A file server session. It must already be open.
157 * @param aOpenForWrite ETrue if the stores must be opened with write access
158 * (e.g. for adding certificates) and EFalse if the user
159 * only needs read-only access.
160 * @return A pointer to an instance of the CUnifiedCertStore class.
162 IMPORT_C static CUnifiedCertStore* NewL(RFs& aFs, TBool aOpenForWrite);
165 * Creates a new CUnifiedCertStore and pushes it on the cleanup stack.
167 * @param aFs A file server session. It must already be open.
168 * @param aOpenForWrite ETrue if the stores must be opened with write access
169 * (e.g. for adding certificates) and EFalse if the user
170 * only needs read-only access.
171 * @return A pointer to an instance of the CUnifiedCertStore class.
173 IMPORT_C static CUnifiedCertStore* NewLC(RFs& aFs, TBool aOpenForWrite);
175 * Creates a new CUnifiedCertStore with the sequence filter, so that multiple certstores that are managed
176 * by it will be filtered and ordered.
178 * @param aFs A file server session. It must already be open.
179 * @param aOpenForWrite ETrue if the stores must be opened with write access
180 * (e.g. for adding certificates) and EFalse if the user
181 * only needs read-only access. Ownership is taken.
182 * @param aOrderFilter An array of the unique sequence IDs specifying CertStore ordering.
183 * @return A pointer to an instance of the CUnifiedCertStore class.
185 IMPORT_C static CUnifiedCertStore* NewL(RFs& aFs,
187 RArray<TInt>& aOrderFilter);
189 * Creates a new CUnifiedCertStore with the sequence filter, so that multiple certstores that are managed
190 * by it will be filtered and ordered, and it is pushed on the cleanup stack.
192 * @param aFs A file server session. It must already be open.
193 * @param aOpenForWrite ETrue if the stores must be opened with write access
194 * (e.g. for adding certificates) and EFalse if the user
195 * only needs read-only access. Ownership is taken.
196 * @param aOrderFilter An array of the unique sequence IDs specifying CertStore ordering.
197 * @return A pointer to an instance of the CUnifiedCertStore class.
199 IMPORT_C static CUnifiedCertStore* NewLC(RFs& aFs,
201 RArray<TInt>& aOrderFilter);
204 * The destructor destroys all the resources owned by this object.
206 IMPORT_C ~CUnifiedCertStore();
209 * Initializes the manager.
211 * It must be called after the manager has been constructed
212 * and before any call to the manager functions.
214 * This is an asynchronous request.
216 * @param aStatus The request status object; contains the result of the Initialize()
217 * request when complete. Set to KErrCancel if any outstanding request is cancelled.
219 IMPORT_C void Initialize(TRequestStatus& aStatus);
222 * Cancels an ongoing Initialize() operation.
224 * The operation completes with KErrCancel.
226 IMPORT_C void CancelInitialize();
228 public: // Implementation of MCertStore interface
230 /** Lists all certificates that satisfy the supplied filter.
232 * @param aCertInfos An array that the returned certificates are added to .
233 * @param aFilter A filter to restrict which certificates are returned.
234 * @param aStatus The request status object.
237 virtual void List(RMPointerArray<CCTCertInfo>& aCertInfos,
238 const CCertAttributeFilter& aFilter, TRequestStatus& aStatus);
239 virtual void CancelList();
240 virtual void GetCert(CCTCertInfo*& aCertInfo, const TCTTokenObjectHandle& aHandle,
241 TRequestStatus& aStatus);
242 virtual void CancelGetCert();
244 /** Gets the list of applications . Applications are represented by UIDs .
246 * @param aCertInfos An array of certificates .
247 * @param aApplications An array that the returned application UIDs are added to.
248 * @param aStatus The request status object.
251 virtual void Applications(const CCTCertInfo& aCertInfo,
252 RArray<TUid>& aApplications, TRequestStatus &aStatus);
253 virtual void CancelApplications();
254 virtual void IsApplicable(const CCTCertInfo& aCertInfo, TUid aApplication,
255 TBool& aIsApplicable, TRequestStatus& aStatus);
256 virtual void CancelIsApplicable();
257 virtual void Trusted(const CCTCertInfo& aCertInfo, TBool& aTrusted,
258 TRequestStatus& aStatus);
259 virtual void CancelTrusted();
260 virtual void Retrieve(const CCTCertInfo& aCertInfo, TDes8& aEncodedCert,
261 TRequestStatus& aStatus);
262 virtual void CancelRetrieve();
264 public: // Functions defined in MCTWritableCertStore except Add functions
267 * Removes a certificate.
269 * @param aCertInfo The certificate to be removed.
270 * @param aStatus The request status object; contains the result of the Remove()
271 * request when complete. Set to KErrCancel if an outstanding request is cancelled.
273 * @capability WriteUserData This requires the WriteUserData capability when
274 * applied to user certificates.
275 * @capability WriteDeviceData This requires the WriteDeviceData capability
276 * when applied to CA certificates.
277 * @leave KErrPermissionDenied If the caller doesn't have the required
280 IMPORT_C void Remove(const CCTCertInfo& aCertInfo, TRequestStatus& aStatus);
283 * Cancels an ongoing Remove() operation.
285 * The operation completes with KErrCancel.
287 IMPORT_C void CancelRemove();
290 * Replaces the current applicability settings with the settings in the
293 * This should only be called for CA certificates - it has no meaning for
296 * @param aCertInfo The certificate whose applicability should be updated.
297 * @param aApplications The new applicability settings. Ownership of this
298 * remains with the caller, and it must remain valid for the
299 * lifetime of the call.
300 * @param aStatus The request status object; contains the result of the SetApplicability()
301 * request when complete. Set to KErrCancel, if an outstanding request is cancelled.
303 * @capability WriteDeviceData This requires the WriteDeviceData capability.
304 * @leave KErrPermissionDenied If the caller doesn't have the required capabilities.
306 IMPORT_C void SetApplicability(const CCTCertInfo& aCertInfo,
307 const RArray<TUid>& aApplications, TRequestStatus &aStatus);
310 * Cancels an ongoing SetApplicability() operation.
312 * The operation completes with KErrCancel.
314 IMPORT_C void CancelSetApplicability();
317 * Changes the trust settings.
319 * A CA certificate is trusted if the user is willing to use it for authenticating
320 * servers. It has no meaning with other types of certificates.
322 * @param aCertInfo The certificate to be updated.
323 * @param aTrusted ETrue, if trusted; EFalse, otherwise.
324 * @param aStatus The request status object; contains the result of the SetTrust()
325 * request when complete. Set to KErrCancel, if an outstanding request is cancelled.
327 * @capability WriteDeviceData This requires the WriteDeviceData capability.
328 * @leave KErrPermissionDenied If the caller doesn't have the required capabilities.
330 IMPORT_C void SetTrust(const CCTCertInfo& aCertInfo, TBool aTrusted,
331 TRequestStatus& aStatus);
334 * Cancels an ongoing SetTrust() operation.
336 * The operation completes with KErrCancel.
338 IMPORT_C void CancelSetTrust();
342 * Lists all certificates that have a particular subject DN.
344 * @param aCertInfos An array that the returned certificates are added to
345 * @param aFilter A filter to restrict which certificates are returned.
346 * @param aIssuer Only certificates with this issuer DN will be returned
347 * @param aStatus Asynchronous request status.
349 IMPORT_C void List(RMPointerArray<CCTCertInfo>& aCertInfos,
350 const CCertAttributeFilter& aFilter,
351 const TDesC8& aIssuer,
352 TRequestStatus& aStatus);
355 * Lists all certificates that have a particular issuer.
357 * @param aCertInfos An array that the returned certificates are added to
358 * @param aFilter A filter to restrict which certificates are returned.
359 * @param aIssuers Only certificates with this issuer will be returned
360 * @param aStatus Asynchronous request status.
362 IMPORT_C void List(RMPointerArray<CCTCertInfo>& aCertInfos,
363 const CCertAttributeFilter& aFilter,
364 RPointerArray<const TDesC8> aIssuers,
365 TRequestStatus& aStatus);
368 * Retrieves a certificate as a parsed object.
370 * This will only work for certificates that have a CCertificate-derived
371 * representation, in other words X509 and WTLS certificates. If called for
372 * a URL certificate, KErrNotSupported is returned.
374 * @param aCertInfo The certificate to retrieve
375 * @param aCert The returned certificate. This object can safely be up-cast
376 * to a CX509Certificate or CWTLSCertificate if it's known that
377 * that is the certificate format.
378 * @param aStatus Asynchronous request status.
380 * @capability ReadUserData This requires the ReadUserData capability when
381 * applied to user certificates, as these may contain
382 * sensitive user data.
383 * @leave KErrPermissionDenied If called for a user certificate when the
384 * caller doesn't have the ReadUserData capability.
386 IMPORT_C void Retrieve(const CCTCertInfo& aCertInfo, CCertificate*& aCert,
387 TRequestStatus& aStatus);
390 * Gets the number of certificate stores.
392 * @return The total number of certificate stores.
394 IMPORT_C TInt CertStoreCount() const;
397 * Gets a particular certificate store.
399 * @param aIndex The index of the required certificate store.
400 * A number between 0 and CertStoreCount() - 1.
401 * @return The certificate store.
403 IMPORT_C MCTCertStore& CertStore(TInt aIndex);
406 * Gets the number of writeable certificate stores.
408 * @return The number of writeable certificate stores.
410 IMPORT_C TInt WritableCertStoreCount() const;
413 * Gets a particular writeable certificate store.
415 * @param aIndex The index of the required certificate store.
416 * A number between 0 and WriteableCertStoreCount() - 1.
417 * @return The writeable certificate store.
419 IMPORT_C MCTWritableCertStore& WritableCertStore(TInt aIndex);
422 * Gets the number of read-only certificate stores.
424 * @return The number of read-only certificate stores.
426 IMPORT_C TInt ReadOnlyCertStoreCount() const;
429 * Gets a particular read-only certificate store.
431 * @param aIndex The index of the required certificate store.
432 * A number between 0 and ReadOnlyCertStoreCount() - 1.
433 * @return The read-only certificate store.
435 IMPORT_C MCTCertStore& ReadOnlyCertStore(TInt aIndex);
442 EInitializeGetTokenList,
444 EInitializeGetWritableInterface,
445 EInitializeGetReadableInterface,
446 EInitializeGetReadableInterfaceFinished,
469 CUnifiedCertStore(RFs& aFs, TBool aOpenForWrite);
470 void ConstructL(RArray<TInt>& aOrderFilter);
473 TInt RunError(TInt aError);
475 // Implementations for asynchronous operations
477 void ListL(RMPointerArray<CCTCertInfo>& aCertInfos,
478 const CCertAttributeFilter& aFilter);
479 void ListL(RMPointerArray<CCTCertInfo>& aCertInfos,
480 const CCertAttributeFilter& aFilter,
481 RPointerArray<const TDesC8> aIssuers);
482 void RetrieveL(const CCTCertInfo& aCertInfo, CCertificate*& aCert);
483 void GetCertL(CCTCertInfo*& aCertInfo, const TCTTokenObjectHandle& aHandle);
484 void ApplicationsL(const CCTCertInfo& aCertInfo, RArray<TUid>& aApplications);
485 void IsApplicableL(const CCTCertInfo& aCertInfo, TUid aApplication,
486 TBool& aIsApplicable);
487 void TrustedL(const CCTCertInfo& aCertInfo, TBool& aTrusted);
488 void RetrieveDataL(const CCTCertInfo& aCertInfo, TDes8& aEncodedCert);
489 void RemoveL(const CCTCertInfo& aCertInfo);
490 void SetApplicabilityL(const CCTCertInfo& aCertInfo,
491 const RArray<TUid>& aApplications);
492 void SetTrustL(const CCTCertInfo& aCertInfo, TBool aTrusted);
495 void AllocWorkingVarsL();
496 void BeginAsyncOp(TRequestStatus& aStatus, TState aState);
497 void DestroyTemporaryMembers();
498 MCTCertStore* GetCertStore(const TCTTokenObjectHandle& aHandle);
499 void FindCertStoreL(const TCTTokenObjectHandle& aHandle);
500 void FindWritableCertStoreL(const TCTTokenObjectHandle& aHandle);
501 TCompareResults CompareCertInfoDN(const CCTCertInfo* aCertInfo);
502 TBool MatchL(const CX500DistinguishedName& aName) const;
503 void Complete(TInt aError);
504 void CancelOutstandingRequest();
506 // Filters CertStores according to specified order.
507 void ApplyOrderingL(RCPointerArray<CCTTokenTypeInfo>& aTokenTypes);
509 void FilterTokenTypesL(RCPointerArray<CCTTokenTypeInfo>& aSearchTokenTypes,
510 RCPointerArray<CCTTokenTypeInfo>& aTempTokenTypes,
511 TInt aOrderAttribute);
515 * A file server session, this is not logically a part of this class
516 * but is needed for the client store and the file certstore.
520 TBool iOpenedForWrite;
522 RPointerArray<CCheckedCertStore> iReadOnlyCertStores;
523 RPointerArray<CCheckedCertStore> iWritableCertStores;
524 RPointerArray<CCheckedCertStore> iCertStores;
526 TBool iCurrentlyDoingReadOnly;
529 * This a TokenType retrieved from the iTokenTypes array.
530 * We use this to get a list of Tokens and to open these Tokens.
532 MCTTokenType* iTokenType;
535 * This is the list of Tokens for one of the Token Types of iTokenTypes.
537 RCPointerArray<HBufC> iTokens;
539 /** All the UIDs of hardware token types */
540 RArray<TUid> iHardwareTypeUids;
542 * This is used as an index for the iTokens array when we try
543 * to get an interface to each of the tokens.
548 * A Token interface. We will use the interface to get a readable or writable
549 * certstore interface. The value is updated at EInitializeGetToken and used
550 * at EInitializeGetWritableInterface.
555 * We use this to (temporarily) store the interface we obtained from iToken.
556 * It will be inserted in iWritableCertStores or iCertStores.
558 MCTTokenInterface* iTokenInterface;
561 The index of the plugin certstore that is being processed
566 * This is the status of the caller of an asynchronous function. It must be set
567 * to KRequestPending by the function while doing the processing.
569 TRequestStatus* iClientStatus;
573 TBool iIsInitialized;
576 * This member holds all the variables that are only used to store temporary results
577 * while performing a given operation. It must be initialized at the start of the
578 * operation and deleted at the end of it whether the opeartion completes successfully
579 * or not. When no operation is being performed it must be 0.
581 CUnifiedCertStoreWorkingVars* iWorkingVars;
584 * The cert store in use by an outstanding async operation.
586 MCTCertStore *iCurrentCertStore;
589 * The writable cert store in use by an outstanding async operation.
591 MCTWritableCertStore *iCurrentWritableCertStore;
593 // Padding to keep class size constant
597 // An array of Uids specifying Token Type ordering
598 RArray<TInt> iOrderAttributes;
600 // Publish and subscribe property which is used to notify the
601 // cerificate addition,removal and application&trust setting.
602 RProperty iPSCertstoreChangeProperty;