X509.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using System.Threading;
  5. namespace wolfSSL.CSharp
  6. {
  7. public class X509
  8. {
  9. private const string wolfssl_dll = "wolfssl.dll";
  10. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  11. private extern static int wolfSSL_X509_get_pubkey_buffer(IntPtr x509, IntPtr buf, IntPtr bufSz);
  12. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  13. private extern static IntPtr wolfSSL_X509_get_der(IntPtr x509, IntPtr bufSz);
  14. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  15. private extern static void wolfSSL_X509_free(IntPtr x509);
  16. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  17. private extern static int wc_DerToPem(IntPtr der, int derSz, IntPtr pem, int pemSz, int type);
  18. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  19. private extern static IntPtr wolfSSL_X509_get_name_oneline(IntPtr x509Name, IntPtr buf, int bufSz);
  20. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  21. private extern static IntPtr wolfSSL_X509_get_subject_name(IntPtr x509);
  22. [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
  23. private extern static IntPtr wolfSSL_X509_get_issuer_name(IntPtr x509);
  24. private IntPtr x509;
  25. private int type;
  26. private bool isDynamic;
  27. /* public properties */
  28. public string Issuer;
  29. public string Subject;
  30. /* enum from wolfssl */
  31. private readonly int CERT_TYPE = 0;
  32. /// <summary>
  33. /// Creates a new X509 class
  34. /// </summary>
  35. /// <param name="x509">Pointer to wolfSSL structure</param>
  36. /// <param name="isDynamic">Should the lower level x509 be free'd? </param>
  37. public X509(IntPtr x509, bool isDynamic)
  38. {
  39. IntPtr ret;
  40. this.type = wolfssl.SSL_FILETYPE_PEM;
  41. this.x509 = x509;
  42. ret = wolfSSL_X509_get_name_oneline(
  43. wolfSSL_X509_get_issuer_name(this.x509), IntPtr.Zero, 0);
  44. this.Issuer = Marshal.PtrToStringAnsi(ret);
  45. ret = wolfSSL_X509_get_name_oneline(
  46. wolfSSL_X509_get_subject_name(this.x509), IntPtr.Zero, 0);
  47. this.Subject = Marshal.PtrToStringAnsi(ret);
  48. this.isDynamic = isDynamic;
  49. }
  50. /// <summary>
  51. /// Free up the C level WOLFSSL_X509 struct if needed
  52. /// </summary>
  53. ~X509()
  54. {
  55. if (this.isDynamic)
  56. {
  57. wolfSSL_X509_free(this.x509);
  58. }
  59. }
  60. /// <summary>
  61. /// Used for getting the public key buffer
  62. /// </summary>
  63. /// <returns>DER public key on success</returns>
  64. public byte[] GetPublicKey()
  65. {
  66. if (this.x509 == IntPtr.Zero)
  67. {
  68. return null;
  69. }
  70. try
  71. {
  72. IntPtr bufSz;
  73. IntPtr buf;
  74. int keySz = 0;
  75. int ret;
  76. byte[] key = null;
  77. bufSz = Marshal.AllocHGlobal(4); /* pointer to 4 bytes */
  78. ret = wolfSSL_X509_get_pubkey_buffer(this.x509, IntPtr.Zero, bufSz);
  79. if (ret == wolfssl.SUCCESS)
  80. {
  81. keySz = Marshal.ReadInt32(bufSz, 0);
  82. buf = Marshal.AllocHGlobal(keySz);
  83. ret = wolfSSL_X509_get_pubkey_buffer(this.x509, buf, bufSz);
  84. if (ret == wolfssl.SUCCESS)
  85. {
  86. key = new byte[keySz];
  87. Marshal.Copy(buf, key, 0, keySz);
  88. }
  89. Marshal.FreeHGlobal(buf);
  90. }
  91. Marshal.FreeHGlobal(bufSz);
  92. return key;
  93. }
  94. catch (Exception e)
  95. {
  96. wolfssl.log(wolfssl.ERROR_LOG, "error getting public key" + e.ToString());
  97. return null;
  98. }
  99. }
  100. /// <summary>
  101. /// Gets the X509 buffer
  102. /// </summary>
  103. /// <returns>X509 buffer on success</returns>
  104. public byte[] Export(int type)
  105. {
  106. if (this.x509 == IntPtr.Zero)
  107. return null;
  108. try
  109. {
  110. IntPtr bufSz;
  111. IntPtr buf;
  112. byte[] ret = null;
  113. bufSz = Marshal.AllocHGlobal(4); /* pointer to 4 bytes */
  114. buf = wolfSSL_X509_get_der(this.x509, bufSz);
  115. if (buf != IntPtr.Zero)
  116. {
  117. int derSz = Marshal.ReadInt32(bufSz, 0);
  118. if (type == wolfssl.SSL_FILETYPE_ASN1)
  119. {
  120. ret = new byte[derSz];
  121. Marshal.Copy(buf, ret, 0, derSz);
  122. }
  123. else if (type == wolfssl.SSL_FILETYPE_PEM)
  124. {
  125. int pemSz;
  126. pemSz = wc_DerToPem(buf, derSz, IntPtr.Zero, 0, CERT_TYPE);
  127. if (pemSz > 0)
  128. {
  129. IntPtr pem = Marshal.AllocHGlobal(pemSz);
  130. pemSz = wc_DerToPem(buf, derSz, pem, pemSz, CERT_TYPE);
  131. ret = new byte[pemSz];
  132. Marshal.Copy(pem, ret, 0, pemSz);
  133. Marshal.FreeHGlobal(pem);
  134. }
  135. }
  136. else
  137. {
  138. wolfssl.log(wolfssl.ERROR_LOG, "unsupported export type");
  139. }
  140. Marshal.FreeHGlobal(bufSz);
  141. return ret;
  142. }
  143. {
  144. wolfssl.log(wolfssl.ERROR_LOG, "unable to get buffer");
  145. }
  146. Marshal.FreeHGlobal(bufSz);
  147. return ret;
  148. }
  149. catch (Exception e)
  150. {
  151. wolfssl.log(wolfssl.ERROR_LOG, "error getting x509 DER" + e.ToString());
  152. return null;
  153. }
  154. }
  155. /// <summary>
  156. /// Gets the X509 buffer using this.type set (default PEM)
  157. /// </summary>
  158. /// <returns>X509 buffer on success</returns>
  159. public byte[] Export()
  160. {
  161. return Export(this.type);
  162. }
  163. /// <summary>
  164. /// Gets the X509 format
  165. /// </summary>
  166. /// <returns>X509 format on success</returns>
  167. public string GetFormat()
  168. {
  169. if (this.type == wolfssl.SSL_FILETYPE_PEM)
  170. {
  171. return "PEM";
  172. }
  173. if (this.type == wolfssl.SSL_FILETYPE_ASN1)
  174. {
  175. return "DER";
  176. }
  177. return "Unknown";
  178. }
  179. }
  180. }