コード例 #1
0
        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);
        }
コード例 #2
0
ファイル: CertificatePal.cs プロジェクト: yamina/Calamari
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
ファイル: CertificatePal.cs プロジェクト: yamina/Calamari
        /// <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);
        }
コード例 #5
0
ファイル: CertificatePal.cs プロジェクト: yamina/Calamari
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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)));
                }
            }
        }
コード例 #8
0
ファイル: X509Native.cs プロジェクト: tk4218/clrsecurity
        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);
        }
コード例 #9
0
ファイル: X509Native.cs プロジェクト: tk4218/clrsecurity
        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)));
                }
            }
        }
コード例 #10
0
 internal static extern bool CertSetCertificateContextProperty(SafeCertContextHandle pCertContext,
                                                               CertificateProperty dwPropId,
                                                               CertSetPropertyFlags dwFlags,
                                                               [In] ref CRYPT_KEY_PROV_INFO pvData);
コード例 #11
0
 internal static extern bool CertGetCertificateContextProperty(IntPtr pCertContext, CertificateProperty dwPropId,
                                                               [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pvData, [In, Out] ref int pcbData);
コード例 #12
0
 public static extern bool CertSetCertificateContextProperty(
     SafeCertContextHandle pCertContext,
     CertificateProperty dwPropId,
     CertificateSetPropertyFlags dwFlags,
     [In] SafeNCryptKeyHandle keyHandle);
コード例 #13
0
 public static extern bool CertGetCertificateContextProperty(
     SafeCertContextHandle pCertContext,
     CertificateProperty dwPropId,
     [Out] out IntPtr pvData,
     [In, Out] ref int pcbData);
コード例 #14
0
 internal static extern bool CertSetCertificateContextProperty(
     IntPtr pCertContext,
     CertificateProperty propertyId,
     uint dwFlags,
     IntPtr pvData
     );
コード例 #15
0
ファイル: CertHelper.cs プロジェクト: suwatch/AzureCLI
 private static extern bool CertSetCertificateContextProperty(SafeCertContextHandle pCertContext,
                                                               CertificateProperty dwPropId,
                                                               CertificatePropertySetFlags dwFlags,
                                                               [In] ref CERT_KEY_CONTEXT pvData);
コード例 #16
0
        /// <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;
        }
コード例 #17
0
ファイル: X509Native.cs プロジェクト: tk4218/clrsecurity
 internal static extern bool CertSetCertificateContextProperty(SafeCertContextHandle pCertContext,
                                                               CertificateProperty dwPropId,
                                                               CertificatePropertySetFlags dwFlags,
                                                               [In] ref CERT_KEY_CONTEXT pvData);
コード例 #18
0
 internal static extern bool CertSetCertificateContextProperty(IntPtr pCertContext, CertificateProperty propertyId, uint dwFlags, IntPtr pvData);
コード例 #19
0
        /// <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);
        }