Ejemplo n.º 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);
                }
            }
        }
Ejemplo n.º 2
0
 public static void TestName(TestContext ctx, BtlsX509Name actual, CertificateNameInfo expected, string label)
 {
     ctx.Expect(actual.GetString(), Is.EqualTo(expected.String), label + ".String");
     ctx.Expect(actual.GetHash(), Is.EqualTo(expected.Hash), label + ".Hash");
     ctx.Expect(actual.GetHashOld(), Is.EqualTo(expected.HashOld), label + ".HashOld");
     ctx.Expect(actual.GetRawData(false), Is.EqualTo(expected.RawData), label + ".RawData");
     ctx.Expect(actual.GetRawData(true), Is.EqualTo(expected.RawDataCanon), label + ".RawDataCanon");
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DistinguishedName"/> class with a given <see cref="CertificateNameInfo"/> instance.
        /// </summary>
        /// <param name="cni">The CertificateNameInfo instance to initialize from.</param>
        /// <exception cref="CertificateException">An error occurs while initializeing the DistinguishedName object.</exception>
        private void Initialize(CertificateNameInfo cni)
        {
            if (cni.cRDN <= 0)
            {
                throw new CertificateException("Certificate does not have a subject relative distinguished name.");
            }
            RelativeDistinguishedName cr;
            RdnAttribute cra;

            for (int i = 0; i < cni.cRDN; i++)
            {
                cr = (RelativeDistinguishedName)Marshal.PtrToStructure(new IntPtr(cni.rgRDN.ToInt64() + i * Marshal.SizeOf(typeof(RelativeDistinguishedName))), typeof(RelativeDistinguishedName));
                for (int j = 0; j < cr.cRDNAttr; j++)
                {
                    cra = (RdnAttribute)Marshal.PtrToStructure(new IntPtr(cr.rgRDNAttr.ToInt64() + j * Marshal.SizeOf(typeof(RdnAttribute))), typeof(RdnAttribute));
                    m_List.Add(new NameAttribute(Marshal.PtrToStringAnsi(cra.pszObjId), Marshal.PtrToStringUni(cra.pbData)));
                }
            }
        }
Ejemplo n.º 4
0
		/// <summary>
		/// Initializes a new instance of the <see cref="DistinguishedName"/> class with a given <see cref="CertificateNameInfo"/> instance.
		/// </summary>
		/// <param name="cni">The CertificateNameInfo instance to initialize from.</param>
		/// <exception cref="CertificateException">An error occurs while initializeing the DistinguishedName object.</exception>
		private void Initialize(CertificateNameInfo cni) {
			if (cni.cRDN <= 0)
				throw new CertificateException("Certificate does not have a subject relative distinguished name.");
			RelativeDistinguishedName cr;
			RdnAttribute cra;
			for(int i = 0; i < cni.cRDN; i++) {
				cr = (RelativeDistinguishedName)Marshal.PtrToStructure(new IntPtr(cni.rgRDN.ToInt64() + i * Marshal.SizeOf(typeof(RelativeDistinguishedName))), typeof(RelativeDistinguishedName));
				for(int j = 0; j < cr.cRDNAttr; j++) {
					cra = (RdnAttribute)Marshal.PtrToStructure(new IntPtr(cr.rgRDNAttr.ToInt64() + j * Marshal.SizeOf(typeof(RdnAttribute))), typeof(RdnAttribute));
					m_List.Add(new NameAttribute(Marshal.PtrToStringAnsi(cra.pszObjId), Marshal.PtrToStringUni(cra.pbData)));
				}
			}
		}
Ejemplo n.º 5
0
		/// <summary>
		/// Initializes a new instance of the <see cref="DistinguishedName"/> class.
		/// </summary>
		/// <param name="cni">A <see cref="CertificateNameInfo"/> instance that's used to initialize the object.</param>
		internal DistinguishedName(CertificateNameInfo cni) : this() {
			Initialize(cni);
		}
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DistinguishedName"/> class.
 /// </summary>
 /// <param name="cni">A <see cref="CertificateNameInfo"/> instance that's used to initialize the object.</param>
 internal DistinguishedName(CertificateNameInfo cni) : this()
 {
     Initialize(cni);
 }