internal X509CertificateContextProperty(X509Certificate2 cert, X509CertificatePropertyType propID) { if (IntPtr.Zero.Equals(cert.Handle)) { throw new UninitializedObjectException(); } Certificate = cert; PropertyName = propID; }
internal X509CertificateContextProperty(X509Certificate2 cert, X509CertificatePropertyType propID, Byte[] bytes) : this(cert, propID) { switch (propID) { // DWORD case X509CertificatePropertyType.AccessState: case X509CertificatePropertyType.KeySpec: case X509CertificatePropertyType.PublicKeyLength: case X509CertificatePropertyType.PublicKeyCngLength: initializeDword(bytes); break; // string case X509CertificatePropertyType.CNGSignatureHashAlgorithm: case X509CertificatePropertyType.PvkFile: case X509CertificatePropertyType.FriendlyName: case X509CertificatePropertyType.Description: case X509CertificatePropertyType.AutoenrollmentTemplateName: case X509CertificatePropertyType.StatusInfo: case X509CertificatePropertyType.RequestOriginatorMachine: case X509CertificatePropertyType.OcspCachePrefix: initializeString(bytes); break; // ASN.1 case X509CertificatePropertyType.EnhancedKeyUsage: case X509CertificatePropertyType.RootProgramCertificatePolicies: case X509CertificatePropertyType.CTLNextUpdateLocation: case X509CertificatePropertyType.OcspResponse: initializeAsn1(bytes); break; case X509CertificatePropertyType.CEPEnrollmentInfo: case X509CertificatePropertyType.EnrollmentInfo: initializeStruct(bytes); break; // byte[] default: initializeHash(bytes); break; } }
internal X509CertificateContextProperty(X509Certificate2 cert, X509CertificatePropertyType propID, IntPtr data) : this(cert, propID) { initializeStruct(data); }
/// <summary> /// Gets a specified certificate context property. /// </summary> /// <param name="cert">Certificate.</param> /// <param name="propID">Property ID to retrieve.</param> /// <exception cref="ArgumentNullException"> /// <strong>cert</strong> parameter is null reference. /// </exception> /// <exception cref="UninitializedObjectException"> /// Certificate object is not initialized and is empty. /// </exception> /// <exception cref="Exception"> /// Requested context property is not found for the current certificate object. /// </exception> /// <returns>Specified certificate context property.</returns> public static X509CertificateContextProperty GetCertificateContextProperty(this X509Certificate2 cert, X509CertificatePropertyType propID) { if (cert == null) { throw new ArgumentNullException(nameof(cert)); } if (IntPtr.Zero.Equals(cert.Handle)) { throw new UninitializedObjectException(); } UInt32 pcbData = 0; switch (propID) { case X509CertificatePropertyType.Handle: case X509CertificatePropertyType.KeyContext: case X509CertificatePropertyType.ProviderInfo: if (!Crypt32.CertGetCertificateContextProperty(cert.Handle, propID, IntPtr.Zero, ref pcbData)) { throw new Exception("No such property."); } IntPtr ptr = Marshal.AllocHGlobal((Int32)pcbData); Crypt32.CertGetCertificateContextProperty(cert.Handle, propID, ptr, ref pcbData); try { return(new X509CertificateContextProperty(cert, propID, ptr)); } finally { Marshal.FreeHGlobal(ptr); } // byte[] default: if (!Crypt32.CertGetCertificateContextProperty(cert.Handle, propID, null, ref pcbData)) { throw new Exception("No such property."); } Byte[] bytes = new Byte[pcbData]; Crypt32.CertGetCertificateContextProperty(cert.Handle, propID, bytes, ref pcbData); return(new X509CertificateContextProperty(cert, propID, bytes)); } }
internal static extern Boolean CertSetCertificateContextProperty( [In] IntPtr pCertContext, [In] X509CertificatePropertyType dwPropId, [In] UInt32 dwFlags, [In] IntPtr pvData );
internal static extern Boolean CertGetCertificateContextProperty( [In] IntPtr pCertContext, [In] X509CertificatePropertyType dwPropId, [Out] Byte[] pvData, [In, Out] ref UInt32 pcbData );