Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DistinguishedName"/> class.
        /// </summary>
        /// <param name="input">A pointer to a buffer that's used to initialize the object.</param>
        /// <param name="length">The length of the buffer.</param>
        /// <exception cref="CertificateException">Could not decode the buffer.</exception>
        internal DistinguishedName(IntPtr input, int length) : this()
        {
            int size = 0;

            SspiProvider.CryptDecodeObject(SecurityConstants.PKCS_7_ASN_ENCODING | SecurityConstants.X509_ASN_ENCODING, new IntPtr(SecurityConstants.X509_UNICODE_NAME), input, length, 0, IntPtr.Zero, ref size);
            if (size <= 0)
            {
                throw new CertificateException("Unable to decode the name of the certificate.");
            }
            IntPtr buffer = Marshal.AllocHGlobal(size);

            if (SspiProvider.CryptDecodeObject(SecurityConstants.PKCS_7_ASN_ENCODING | SecurityConstants.X509_ASN_ENCODING, new IntPtr(SecurityConstants.X509_UNICODE_NAME), input, length, 0, buffer, ref size) == 0)
            {
                throw new CertificateException("Unable to decode the name of the certificate.");
            }
            try {
                CertificateNameInfo cni = (CertificateNameInfo)Marshal.PtrToStructure(buffer, typeof(CertificateNameInfo));
                Initialize(cni);
            } catch (CertificateException ce) {
                throw ce;
            } catch (Exception e) {
                throw new CertificateException("Could not get the certificate distinguished name.", e);
            } finally {
                if (buffer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(buffer);
                }
            }
        }