Example #1
0
        public static unsafe string CertGetNameString(
            SafeCertContextHandle certContext,
            CertNameType certNameType,
            CertNameFlags certNameFlags,
            CertNameStringType strType)
        {
            int cchCount = CertGetNameString(certContext, certNameType, certNameFlags, strType, null, 0);

            if (cchCount == 0)
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            Span <char> buffer = cchCount <= 256 ? stackalloc char[cchCount] : new char[cchCount];

            fixed(char *ptr = &MemoryMarshal.GetReference(buffer))
            {
                if (CertGetNameString(certContext, certNameType, certNameFlags, strType, ptr, cchCount) == 0)
                {
                    throw Marshal.GetLastWin32Error().ToCryptographicException();
                }

                Debug.Assert(buffer[cchCount - 1] == '\0');
                return(new string(buffer.Slice(0, cchCount - 1)));
            }
        }
        public static unsafe string CertGetNameString(
            SafeCertContextHandle certContext,
            CertNameType certNameType,
            CertNameFlags certNameFlags,
            CertNameStringType strType)
        {
            const int sizeof_wchar_t = 4;
            int       cchCount       = CertGetNameString(certContext, certNameType, certNameFlags, strType, null, 0);

            if (cchCount == 0)
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            Span <byte> buffer = cchCount <= 256 ? stackalloc byte[cchCount * sizeof_wchar_t] : new byte[cchCount * sizeof_wchar_t];

            fixed(byte *ptr = &MemoryMarshal.GetReference(buffer))
            {
                if (CertGetNameString(certContext, certNameType, certNameFlags, strType, ptr, cchCount) == 0)
                {
                    throw Marshal.GetLastWin32Error().ToCryptographicException();
                }

                Debug.Assert(buffer[(cchCount - 1) * sizeof_wchar_t] == '\0');
                // return new string(buffer.Slice(0, cchCount - 1));
                return(System.Text.Encoding.UTF32.GetString(buffer.Slice(0, (cchCount - 1) * sizeof_wchar_t).ToArray()));
            }
        }
Example #3
0
 public static extern int CertGetNameString(
     IntPtr pCertContext,
     CertNameType dwType,
     int dwFlags,
     IntPtr pvTypePara,
     IntPtr pszNameString,
     int cchNameString);
 public static extern bool CertStrToName(
     X509Encoding dwCertEncodingType,
     String pszX500,
     CertNameType dwStrType,
     IntPtr pvReserved,
     [In, Out] byte[] pbEncoded,
     ref int pcbEncoded,
     IntPtr other);
Example #5
0
        public static string GetCertNameAttribute(IntPtr pCert, CertNameType nameType)
        {
            IntPtr pName  = IntPtr.Zero;
            int    length = 0;

            length = CertGetNameString(pCert, nameType, 0, IntPtr.Zero, pName, length);
            if (length == 1)
            {
                return("");
            }

            pName = Marshal.AllocHGlobal(length * 2);

            length = CertGetNameString(pCert, nameType, 0, IntPtr.Zero, pName, length);

            string name = Marshal.PtrToStringUni(pName, length - 1);

            Marshal.FreeHGlobal(pName);

            return(name);
        }
Example #6
0
        private static String GetCertNameInfo(SafeCertContextHandle pCertContext, CertNameType dwNameType, CertNameFlags dwNameFlags)
        {
            Debug.Assert(dwNameType != CertNameType.CERT_NAME_ATTR_TYPE);

            CertNameStringType stringType = CertNameStringType.CERT_X500_NAME_STR | CertNameStringType.CERT_NAME_STR_REVERSE_FLAG;

            int cch = Interop.crypt32.CertGetNameString(pCertContext, dwNameType, dwNameFlags, ref stringType, null, 0);

            if (cch == 0)
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            StringBuilder sb = new StringBuilder(cch);

            if (0 == Interop.crypt32.CertGetNameString(pCertContext, dwNameType, dwNameFlags, ref stringType, sb, cch))
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            return(sb.ToString());
        }
Example #7
0
        public string GetNameInfo(X509NameType nameType, bool forIssuer)
        {
            CertNameType            certNameType  = MapNameType(nameType);
            CertNameFlags           certNameFlags = forIssuer ? CertNameFlags.CERT_NAME_ISSUER_FLAG : CertNameFlags.None;
            CertNameStrTypeAndFlags strType       = CertNameStrTypeAndFlags.CERT_X500_NAME_STR | CertNameStrTypeAndFlags.CERT_NAME_STR_REVERSE_FLAG;

            int cchCount = Interop.crypt32.CertGetNameString(_certContext, certNameType, certNameFlags, ref strType, null, 0);

            if (cchCount == 0)
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            StringBuilder sb = new StringBuilder(cchCount);

            if (Interop.crypt32.CertGetNameString(_certContext, certNameType, certNameFlags, ref strType, sb, cchCount) == 0)
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            return(sb.ToString());
        }
 public static extern int CertGetNameString(SafeCertContextHandle pCertContext, CertNameType dwType, CertNameFlags dwFlags, [In] ref CertNameStringType pvPara, [Out] StringBuilder pszNameString, int cchNameString);
Example #9
0
 private static unsafe partial int CertGetNameString(SafeCertContextHandle pCertContext, CertNameType dwType, CertNameFlags dwFlags, in CertNameStringType pvTypePara, char *pszNameString, int cchNameString);
Example #10
0
 private static unsafe string GetCertNameInfo(SafeCertContextHandle pCertContext, CertNameType dwNameType, CertNameFlags dwNameFlags)
 {
     Debug.Assert(dwNameType != CertNameType.CERT_NAME_ATTR_TYPE);
     return(Interop.crypt32.CertGetNameString(
                pCertContext,
                dwNameType,
                dwNameFlags,
                CertNameStringType.CERT_X500_NAME_STR | CertNameStringType.CERT_NAME_STR_REVERSE_FLAG));
 }
Example #11
0
 public static extern int CertGetNameString(SafeCertContextHandle pCertContext, CertNameType dwType, CertNameFlags dwFlags, [In] ref CertNameStringType pvTypePara, [Out] StringBuilder pszNameString, int cchNameString);
 private static extern unsafe int CertGetNameString(SafeCertContextHandle pCertContext, CertNameType dwType, CertNameFlags dwFlags, in CertNameStringType pvTypePara, byte *pszNameString, int cchNameString);
 internal static extern int CertNameToStr(X509Encoding dwCertEncodingType, ref CRYPT_DATA_BLOB pName, CertNameType dwStrType, [In, Out] char[] psz, int csz);
Example #14
0
        private static string GetCertNameInfo(SafeCertContextHandle pCertContext, CertNameType dwNameType, CertNameFlags dwNameFlags)
        {
            Debug.Assert(dwNameType != CertNameType.CERT_NAME_ATTR_TYPE);

            CertNameStringType stringType = CertNameStringType.CERT_X500_NAME_STR | CertNameStringType.CERT_NAME_STR_REVERSE_FLAG;

            int cch = Interop.crypt32.CertGetNameString(pCertContext, dwNameType, dwNameFlags, ref stringType, null, 0);
            if (cch == 0)
                throw Marshal.GetLastWin32Error().ToCryptographicException();

            StringBuilder sb = new StringBuilder(cch);
            if (0 == Interop.crypt32.CertGetNameString(pCertContext, dwNameType, dwNameFlags, ref stringType, sb, cch))
                throw Marshal.GetLastWin32Error().ToCryptographicException();

            return sb.ToString();
        }