internal static byte[] GetCertificateProperty(IntPtr certificateContext, CertificateProperty property) { Debug.Assert(certificateContext != IntPtr.Zero, "certificateContext != IntPtr.Zero"); byte[] buffer = null; int bufferSize = 0; if (!UnsafeNativeMethods.CertGetCertificateContextProperty(certificateContext, property, buffer, ref bufferSize)) { ErrorCode errorCode = (ErrorCode)Marshal.GetLastWin32Error(); if (errorCode != ErrorCode.MoreData) { throw new CryptographicException((int)errorCode); } } buffer = new byte[bufferSize]; if (!UnsafeNativeMethods.CertGetCertificateContextProperty(certificateContext, property, buffer, ref bufferSize)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(buffer); }
public static byte[] GetCertificateProperty(IntPtr certificateContext, CertificateProperty property) { byte[] buffer = null; var bufferSize = 0; // ReSharper disable once ExpressionIsAlwaysNull if (!CertGetCertificateContextProperty(certificateContext, property, buffer, ref bufferSize)) { // ReSharper disable once InconsistentNaming const int ERROR_MORE_DATA = 0x000000ea; var errorCode = Marshal.GetLastWin32Error(); if (errorCode != ERROR_MORE_DATA) { throw new CryptographicException(errorCode); } } buffer = new byte[bufferSize]; if (!CertGetCertificateContextProperty(certificateContext, property, buffer, ref bufferSize)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(buffer); }
internal static byte[] GetCertificateProperty(SafeCertContextHandle certificateContext, CertificateProperty property) { Debug.Assert(certificateContext != null, "certificateContext != null"); Debug.Assert(!certificateContext.IsClosed && !certificateContext.IsInvalid, "!certificateContext.IsClosed && !certificateContext.IsInvalid"); byte[] buffer = null; int bufferSize = 0; if (!UnsafeNativeMethods.CertGetCertificateContextProperty(certificateContext, property, buffer, ref bufferSize)) { ErrorCode errorCode = (ErrorCode)Marshal.GetLastWin32Error(); if (errorCode != ErrorCode.MoreData) { throw new CryptographicException((int)errorCode); } } buffer = new byte[bufferSize]; if (!UnsafeNativeMethods.CertGetCertificateContextProperty(certificateContext, property, buffer, ref bufferSize)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(buffer); }
/// <summary> /// Get a property of a certificate formatted as a structure /// </summary> public static T GetCertificateProperty <T>(IntPtr certificateContext, CertificateProperty property) where T : struct { var rawProperty = GetCertificateProperty(certificateContext, property); var gcHandle = GCHandle.Alloc(rawProperty, GCHandleType.Pinned); var typedProperty = (T)Marshal.PtrToStructure(gcHandle.AddrOfPinnedObject(), typeof(T)); gcHandle.Free(); return(typedProperty); }
public static bool HasProperty(IntPtr certificateContext, CertificateProperty property) { byte[] buffer = null; var bufferSize = 0; // ReSharper disable once ExpressionIsAlwaysNull var hasProperty = CertGetCertificateContextProperty(certificateContext, property, buffer, ref bufferSize); // ReSharper disable once InconsistentNaming const int ERROR_MORE_DATA = 0x000000ea; return(hasProperty || Marshal.GetLastWin32Error() == ERROR_MORE_DATA); }
internal static bool HasCertificateProperty(IntPtr certificateContext, CertificateProperty property) { Debug.Assert(certificateContext != IntPtr.Zero, "certificateContext != IntPtr.Zero"); byte[] buffer = null; int bufferSize = 0; bool gotProperty = UnsafeNativeMethods.CertGetCertificateContextProperty(certificateContext, property, buffer, ref bufferSize); return(gotProperty || (ErrorCode)Marshal.GetLastWin32Error() == ErrorCode.MoreData); }
internal static T GetCertificateProperty <T>(IntPtr certificateContext, CertificateProperty property) where T : struct { Debug.Assert(certificateContext != IntPtr.Zero, "certificateContext != IntPtr.Zero"); byte[] rawProperty = GetCertificateProperty(certificateContext, property); Debug.Assert(rawProperty.Length >= Marshal.SizeOf(typeof(T)), "Property did not return expected structure"); unsafe { fixed(byte *pRawProperty = &rawProperty[0]) { return((T)Marshal.PtrToStructure(new IntPtr(pRawProperty), typeof(T))); } } }
internal static bool HasCertificateProperty(SafeCertContextHandle certificateContext, CertificateProperty property) { Debug.Assert(certificateContext != null, "certificateContext != null"); Debug.Assert(!certificateContext.IsClosed && !certificateContext.IsInvalid, "!certificateContext.IsClosed && !certificateContext.IsInvalid"); byte[] buffer = null; int bufferSize = 0; bool gotProperty = UnsafeNativeMethods.CertGetCertificateContextProperty(certificateContext, property, buffer, ref bufferSize); return(gotProperty || (ErrorCode)Marshal.GetLastWin32Error() == ErrorCode.MoreData); }
internal static T GetCertificateProperty <T>(SafeCertContextHandle certificateContext, CertificateProperty property) where T : struct { Debug.Assert(certificateContext != null, "certificateContext != null"); Debug.Assert(!certificateContext.IsClosed && !certificateContext.IsInvalid, "!certificateContext.IsClosed && !certificateContext.IsInvalid"); byte[] rawProperty = GetCertificateProperty(certificateContext, property); Debug.Assert(rawProperty.Length >= Marshal.SizeOf(typeof(T)), "Property did not return expected structure"); unsafe { fixed(byte *pRawProperty = &rawProperty[0]) { return((T)Marshal.PtrToStructure(new IntPtr(pRawProperty), typeof(T))); } } }
internal static extern bool CertSetCertificateContextProperty(SafeCertContextHandle pCertContext, CertificateProperty dwPropId, CertSetPropertyFlags dwFlags, [In] ref CRYPT_KEY_PROV_INFO pvData);
internal static extern bool CertGetCertificateContextProperty(IntPtr pCertContext, CertificateProperty dwPropId, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pvData, [In, Out] ref int pcbData);
public static extern bool CertSetCertificateContextProperty( SafeCertContextHandle pCertContext, CertificateProperty dwPropId, CertificateSetPropertyFlags dwFlags, [In] SafeNCryptKeyHandle keyHandle);
public static extern bool CertGetCertificateContextProperty( SafeCertContextHandle pCertContext, CertificateProperty dwPropId, [Out] out IntPtr pvData, [In, Out] ref int pcbData);
internal static extern bool CertSetCertificateContextProperty( IntPtr pCertContext, CertificateProperty propertyId, uint dwFlags, IntPtr pvData );
private static extern bool CertSetCertificateContextProperty(SafeCertContextHandle pCertContext, CertificateProperty dwPropId, CertificatePropertySetFlags dwFlags, [In] ref CERT_KEY_CONTEXT pvData);
/// <summary> /// Gets the property value given the specified property name. /// </summary> /// <param name="propName">The property name</param> /// <returns>The property value object</returns> public object GetProperty(CertificateProperty propName) { object retVal = null; if (m_propertyBag == null) m_propertyBag = new Hashtable(); if (m_propertyBag.Contains(propName)) { retVal = m_propertyBag[propName] as Byte[]; } else { byte[] propValue = new byte[2048]; byte[] propLen = new byte[4]; CryptokiAttribute[] props = new CryptokiAttribute[] { new CryptokiAttribute((CryptokiAttribute.CryptokiType)propName, propValue), new CryptokiAttribute( CryptokiAttribute.CryptokiType.ValueLen, propLen ), }; if(GetAttributeValues(ref props)) { switch(propName) { case CertificateProperty.KeyType: retVal = (KeyType)Utility.ConvertToInt32(propValue); break; case CertificateProperty.SignatureAlgorithm: retVal = (MechanismType)Utility.ConvertToInt32(propValue); break; case CertificateProperty.Issuer: case CertificateProperty.Subject: retVal = new string(UTF8Encoding.UTF8.GetChars(propValue)); break; case CertificateProperty.SerialNumber: case CertificateProperty.RawBytes: { int len = Utility.ConvertToInt32(propLen); if (len < propValue.Length) { byte[] tmp = new byte[len]; Array.Copy(propValue, tmp, len); propValue = tmp; } } retVal = propValue; break; case CertificateProperty.EffectiveDate: case CertificateProperty.ExpirationDate: DATE_TIME_INFO dti = new DATE_TIME_INFO(propValue); retVal = new DateTime(dti.year, dti.month, dti.day, dti.hour, dti.minute, dti.second, dti.msec); break; } } if (retVal != null) { m_propertyBag[propName] = retVal; } } return retVal; }
internal static extern bool CertSetCertificateContextProperty(SafeCertContextHandle pCertContext, CertificateProperty dwPropId, CertificatePropertySetFlags dwFlags, [In] ref CERT_KEY_CONTEXT pvData);
internal static extern bool CertSetCertificateContextProperty(IntPtr pCertContext, CertificateProperty propertyId, uint dwFlags, IntPtr pvData);
/// <summary> /// Gets the property value given the specified property name. /// </summary> /// <param name="propName">The property name</param> /// <returns>The property value object</returns> public object GetProperty(CertificateProperty propName) { object retVal = null; if (m_propertyBag == null) { m_propertyBag = new Hashtable(); } if (m_propertyBag.Contains(propName)) { retVal = m_propertyBag[propName] as Byte[]; } else { byte[] propValue = new byte[2048]; byte[] propLen = new byte[4]; CryptokiAttribute[] props = new CryptokiAttribute[] { new CryptokiAttribute((CryptokiAttribute.CryptokiType)propName, propValue), new CryptokiAttribute(CryptokiAttribute.CryptokiType.ValueLen, propLen), }; if (GetAttributeValues(ref props)) { switch (propName) { case CertificateProperty.KeyType: retVal = (KeyType)Utility.ConvertToInt32(propValue); break; case CertificateProperty.SignatureAlgorithm: retVal = (MechanismType)Utility.ConvertToInt32(propValue); break; case CertificateProperty.Issuer: case CertificateProperty.Subject: retVal = new string(UTF8Encoding.UTF8.GetChars(propValue)); break; case CertificateProperty.SerialNumber: case CertificateProperty.RawBytes: { int len = Utility.ConvertToInt32(propLen); if (len < propValue.Length) { byte[] tmp = new byte[len]; Array.Copy(propValue, tmp, len); propValue = tmp; } } retVal = propValue; break; case CertificateProperty.EffectiveDate: case CertificateProperty.ExpirationDate: DATE_TIME_INFO dti = new DATE_TIME_INFO(propValue); retVal = new DateTime(dti.year, dti.month, dti.day, dti.hour, dti.minute, dti.second, dti.msec); break; } } if (retVal != null) { m_propertyBag[propName] = retVal; } } return(retVal); }