예제 #1
0
파일: x509ui.cs 프로젝트: dox0/DotNet471RS3
 public static void DisplayCertificate(X509Certificate2 certificate, IntPtr hwndParent)
 {
     if (certificate == null)
     {
         throw new ArgumentNullException("certificate");
     }
     DisplayX509Certificate(X509Utils.GetCertContext(certificate), hwndParent);
 }
        internal static SafeCertStoreHandle ExportToMemoryStore(X509Certificate2Collection collection)
        {
            //
            // We need to Assert all StorePermission flags since this is a memory store and we want
            // semi-trusted code to be able to export certificates to a memory store.
            //

            StorePermission sp = new StorePermission(StorePermissionFlags.AllFlags);

            sp.Assert();

            SafeCertStoreHandle safeCertStoreHandle = SafeCertStoreHandle.InvalidHandle;

            // we always want to use CERT_STORE_ENUM_ARCHIVED_FLAG since we want to preserve the collection in this operation.
            // By default, Archived certificates will not be included.

            safeCertStoreHandle = CAPI.CertOpenStore(new IntPtr(CAPI.CERT_STORE_PROV_MEMORY),
                                                     CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING,
                                                     IntPtr.Zero,
                                                     CAPI.CERT_STORE_ENUM_ARCHIVED_FLAG | CAPI.CERT_STORE_CREATE_NEW_FLAG,
                                                     null);

            if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            //
            // We use CertAddCertificateLinkToStore to keep a link to the original store, so any property changes get
            // applied to the original store. This has a limit of 99 links per cert context however.
            //

            foreach (X509Certificate2 x509 in collection)
            {
                if (!CAPI.CertAddCertificateLinkToStore(safeCertStoreHandle,
                                                        X509Utils.GetCertContext(x509),
                                                        CAPI.CERT_STORE_ADD_ALWAYS,
                                                        SafeCertContextHandle.InvalidHandle))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
            }

            return(safeCertStoreHandle);
        }
예제 #3
0
 private static void AddToStore(SafeCertStoreHandle safeCertStoreHandle, X509Certificate2Collection collection)
 {
     //
     // We use CertAddCertificateLinkToStore to keep a link to the original store, so any property changes get
     // applied to the original store. This has a limit of 99 links per cert context however.
     //
     // X509Store.Add(Range) uses CertAddCertificateContextToStore, which would lose information like ephemeral
     // private key associations.
     foreach (X509Certificate2 x509 in collection)
     {
         using (SafeCertContextHandle ctx = X509Utils.GetCertContext(x509)) {
             if (!CAPI.CertAddCertificateLinkToStore(safeCertStoreHandle,
                                                     ctx,
                                                     CAPI.CERT_STORE_ADD_ALWAYS,
                                                     SafeCertContextHandle.InvalidHandle))
             {
                 throw new CryptographicException(Marshal.GetLastWin32Error());
             }
         }
     }
 }