mscorlib(4.0.0.0) API with additions
X500DistinguishedName.cs
3 
5 {
7  public sealed class X500DistinguishedName : AsnEncodedData
8  {
9  private string m_distinguishedName;
10 
13  public string Name
14  {
15  get
16  {
17  if (m_distinguishedName == null)
18  {
19  m_distinguishedName = Decode(X500DistinguishedNameFlags.Reversed);
20  }
21  return m_distinguishedName;
22  }
23  }
24 
25  internal X500DistinguishedName(CAPIBase.CRYPTOAPI_BLOB encodedDistinguishedNameBlob)
26  : base(new Oid(), encodedDistinguishedNameBlob)
27  {
28  }
29 
32  public X500DistinguishedName(byte[] encodedDistinguishedName)
33  : base(new Oid(), encodedDistinguishedName)
34  {
35  }
36 
39  public X500DistinguishedName(AsnEncodedData encodedDistinguishedName)
40  : base(encodedDistinguishedName)
41  {
42  }
43 
46  public X500DistinguishedName(X500DistinguishedName distinguishedName)
47  : base(distinguishedName)
48  {
49  m_distinguishedName = distinguishedName.Name;
50  }
51 
54  public X500DistinguishedName(string distinguishedName)
55  : this(distinguishedName, X500DistinguishedNameFlags.Reversed)
56  {
57  }
58 
62  public X500DistinguishedName(string distinguishedName, X500DistinguishedNameFlags flag)
63  : base(new Oid(), Encode(distinguishedName, flag))
64  {
65  m_distinguishedName = distinguishedName;
66  }
67 
72  public unsafe string Decode(X500DistinguishedNameFlags flag)
73  {
74  uint dwStrType = 3 | MapNameToStrFlag(flag);
75  byte[] rawData = m_rawData;
76  byte[] array = rawData;
77  fixed (byte* value = array)
78  {
79  CAPIBase.CRYPTOAPI_BLOB cRYPTOAPI_BLOB = default(CAPIBase.CRYPTOAPI_BLOB);
80  IntPtr pName = new IntPtr(&cRYPTOAPI_BLOB);
81  cRYPTOAPI_BLOB.cbData = (uint)rawData.Length;
82  cRYPTOAPI_BLOB.pbData = new IntPtr(value);
83  uint num = CAPISafe.CertNameToStrW(65537u, pName, dwStrType, SafeLocalAllocHandle.InvalidHandle, 0u);
84  if (num == 0)
85  {
86  throw new CryptographicException(-2146762476);
87  }
88  using (SafeLocalAllocHandle safeLocalAllocHandle = CAPI.LocalAlloc(64u, new IntPtr(2 * num)))
89  {
90  if (CAPISafe.CertNameToStrW(65537u, pName, dwStrType, safeLocalAllocHandle, num) == 0)
91  {
92  throw new CryptographicException(-2146762476);
93  }
94  return Marshal.PtrToStringUni(safeLocalAllocHandle.DangerousGetHandle());
95  }
96  }
97  }
98 
103  public override string Format(bool multiLine)
104  {
105  if (m_rawData == null || m_rawData.Length == 0)
106  {
107  return string.Empty;
108  }
109  return CAPI.CryptFormatObject(1u, multiLine ? 1u : 0u, new IntPtr(7L), m_rawData);
110  }
111 
112  private unsafe static byte[] Encode(string distinguishedName, X500DistinguishedNameFlags flag)
113  {
114  if (distinguishedName == null)
115  {
116  throw new ArgumentNullException("distinguishedName");
117  }
118  uint pcbEncoded = 0u;
119  uint dwStrType = 3 | MapNameToStrFlag(flag);
120  if (!CAPISafe.CertStrToNameW(65537u, distinguishedName, dwStrType, IntPtr.Zero, IntPtr.Zero, ref pcbEncoded, IntPtr.Zero))
121  {
123  }
124  byte[] array = new byte[pcbEncoded];
125  byte[] array2 = array;
126  fixed (byte* value = array2)
127  {
128  if (!CAPISafe.CertStrToNameW(65537u, distinguishedName, dwStrType, IntPtr.Zero, new IntPtr(value), ref pcbEncoded, IntPtr.Zero))
129  {
130  throw new CryptographicException(Marshal.GetLastWin32Error());
131  }
132  }
133  return array;
134  }
135 
136  private static uint MapNameToStrFlag(X500DistinguishedNameFlags flag)
137  {
138  uint num = 29169u;
139  if (((int)flag & (int)(~num)) != 0)
140  {
141  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SR.GetString("Arg_EnumIllegalVal"), new object[1]
142  {
143  "flag"
144  }));
145  }
146  uint num2 = 0u;
147  if (flag != 0)
148  {
149  if ((flag & X500DistinguishedNameFlags.Reversed) == X500DistinguishedNameFlags.Reversed)
150  {
151  num2 |= 0x2000000;
152  }
153  if ((flag & X500DistinguishedNameFlags.UseSemicolons) == X500DistinguishedNameFlags.UseSemicolons)
154  {
155  num2 |= 0x40000000;
156  }
157  else if ((flag & X500DistinguishedNameFlags.UseCommas) == X500DistinguishedNameFlags.UseCommas)
158  {
159  num2 |= 0x4000000;
160  }
161  else if ((flag & X500DistinguishedNameFlags.UseNewLines) == X500DistinguishedNameFlags.UseNewLines)
162  {
163  num2 |= 0x8000000;
164  }
165  if ((flag & X500DistinguishedNameFlags.DoNotUsePlusSign) == X500DistinguishedNameFlags.DoNotUsePlusSign)
166  {
167  num2 |= 0x20000000;
168  }
169  if ((flag & X500DistinguishedNameFlags.DoNotUseQuotes) == X500DistinguishedNameFlags.DoNotUseQuotes)
170  {
171  num2 |= 0x10000000;
172  }
173  if ((flag & X500DistinguishedNameFlags.ForceUTF8Encoding) == X500DistinguishedNameFlags.ForceUTF8Encoding)
174  {
175  num2 |= 0x80000;
176  }
177  if ((flag & X500DistinguishedNameFlags.UseUTF8Encoding) == X500DistinguishedNameFlags.UseUTF8Encoding)
178  {
179  num2 |= 0x40000;
180  }
181  else if ((flag & X500DistinguishedNameFlags.UseT61Encoding) == X500DistinguishedNameFlags.UseT61Encoding)
182  {
183  num2 |= 0x20000;
184  }
185  }
186  return num2;
187  }
188  }
189 }
The exception that is thrown when an error occurs during a cryptographic operation.
X500DistinguishedNameFlags
Specifies characteristics of the X.500 distinguished name.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Represents Abstract Syntax Notation One (ASN.1)-encoded data.
Represents a cryptographic object identifier. This class cannot be inherited.
Definition: Oid.cs:6
X500DistinguishedName(AsnEncodedData encodedDistinguishedName)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
X500DistinguishedName(X500DistinguishedName distinguishedName)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
Definition: __Canon.cs:3
Represents the distinguished name of an X509 certificate. This class cannot be inherited.
string Name
Gets the comma-delimited distinguished name from an X500 certificate.
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
unsafe string Decode(X500DistinguishedNameFlags flag)
Decodes a distinguished name using the characteristics specified by the flag parameter.
override string Format(bool multiLine)
Returns a formatted version of an X500 distinguished name for printing or for output to a text window...
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
static unsafe string PtrToStringUni(IntPtr ptr, int len)
Allocates a managed T:System.String and copies a specified number of characters from an unmanaged Uni...
Definition: Marshal.cs:103
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
Definition: IntPtr.cs:20
X500DistinguishedName(string distinguishedName)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
X500DistinguishedName(byte[] encodedDistinguishedName)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
X500DistinguishedName(string distinguishedName, X500DistinguishedNameFlags flag)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....