NetMQCertificate.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NaCl;
  5. namespace NetMQ
  6. {
  7. /// <summary>
  8. /// Certificate to be used for the Curve encryption
  9. /// </summary>
  10. public class NetMQCertificate
  11. {
  12. // Z85 codec, taken from 0MQ RFC project, implements RFC32 Z85 encoding
  13. // Maps base 256 to base 85
  14. private static string Encoder = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#";
  15. // Maps base 85 to base 256
  16. // We chop off lower 32 and higher 128 ranges
  17. // 0xFF denotes invalid characters within this range
  18. private static byte[] Decoder = {
  19. 0xFF, 0x44, 0xFF, 0x54, 0x53, 0x52, 0x48, 0xFF, 0x4B, 0x4C, 0x46, 0x41,
  20. 0xFF, 0x3F, 0x3E, 0x45, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21. 0x08, 0x09, 0x40, 0xFF, 0x49, 0x42, 0x4A, 0x47, 0x51, 0x24, 0x25, 0x26,
  22. 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
  23. 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x4D,
  24. 0xFF, 0x4E, 0x43, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
  25. 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
  26. 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x4F, 0xFF, 0x50, 0xFF, 0xFF};
  27. /// <summary>
  28. /// Encodes Z85 string
  29. /// </summary>
  30. /// <param name="data">Data</param>
  31. private string Z85Encode(byte[] data)
  32. {
  33. byte byte_nbr = 0;
  34. UInt32 value = 0;
  35. string? dest = null;
  36. while (byte_nbr<data.Length)
  37. {
  38. // Accumulate value in base 256 (binary)
  39. value = value* 256 + data[byte_nbr++];
  40. if (byte_nbr % 4 == 0)
  41. {
  42. // Output value in base 85
  43. UInt32 divisor = 85 * 85 * 85 * 85;
  44. while (divisor != 0)
  45. {
  46. dest += Encoder[(int)(value / divisor % 85)];
  47. divisor /= 85;
  48. }
  49. value = 0;
  50. }
  51. }
  52. return dest ?? "";
  53. }
  54. /// <summary>
  55. /// Decodes Z85 string
  56. /// </summary>
  57. /// <param name="key">key in Z85 format</param>
  58. /// <exception cref="ArgumentException">If key in invalid</exception>
  59. private byte[] Z85Decode(string key)
  60. {
  61. UInt32 char_nbr = 0;
  62. UInt32 value = 0;
  63. var dest_ = new List<byte>();
  64. foreach (var cha in key)
  65. {
  66. // Accumulate value in base 85
  67. if (UInt32.MaxValue / 85 < value)
  68. {
  69. // Invalid z85 encoding, represented value exceeds 0xffffffff
  70. throw new ArgumentException("Invalid key bad encoding");
  71. }
  72. value *= 85;
  73. char_nbr++;
  74. var index = cha - 32;
  75. if (index >= Decoder.Length)
  76. {
  77. // Invalid z85 encoding, character outside range
  78. throw new ArgumentException("Invalid key character");
  79. }
  80. UInt32 summand = Decoder[index];
  81. if (summand == 0xFF || summand > (UInt32.MaxValue - value))
  82. {
  83. // Invalid z85 encoding, invalid character or represented value exceeds 0xffffffff
  84. throw new ArgumentException("Invalid key character");
  85. }
  86. value += summand;
  87. if (char_nbr % 5 == 0)
  88. {
  89. // Output value in base 256
  90. UInt32 divisor = 256 * 256 * 256;
  91. while (divisor != 0)
  92. {
  93. dest_.Add((byte)(value / divisor % 256));
  94. divisor /= 256;
  95. }
  96. value = 0;
  97. }
  98. }
  99. return dest_.ToArray();
  100. }
  101. /// <summary>
  102. /// Create a Certificate with a random secret key and a derived public key for the curve encryption
  103. /// </summary>
  104. public NetMQCertificate()
  105. {
  106. SecretKey = new byte[32];
  107. PublicKey = new byte[32];
  108. Curve25519XSalsa20Poly1305.KeyPair(SecretKey, PublicKey);
  109. }
  110. /// <summary>
  111. /// Create a certificate from secret key and public key
  112. /// </summary>
  113. /// <param name="secretKey">Secret key</param>
  114. /// <param name="publicKey">Public key</param>
  115. /// <exception cref="ArgumentException">If secretKey or publicKey are not 32-bytes long</exception>
  116. public NetMQCertificate(byte[] secretKey, byte[] publicKey)
  117. {
  118. if (secretKey.Length != 32)
  119. throw new ArgumentException("secretKey must be 32 bytes length");
  120. if (publicKey.Length != 32)
  121. throw new ArgumentException("publicKey must be 32 bytes length");
  122. SecretKey = secretKey;
  123. PublicKey = publicKey;
  124. }
  125. /// <summary>
  126. /// Create a certificate from secret key and public key
  127. /// </summary>
  128. /// <param name="secretKey">Secret key</param>
  129. /// <param name="publicKey">Public key</param>
  130. /// <exception cref="ArgumentException">If secretKey or publicKey are not 40-chars long</exception>
  131. public NetMQCertificate(string secretKey, string publicKey)
  132. {
  133. if (secretKey.Length != 40)
  134. throw new ArgumentException("secretKey must be 40 char long");
  135. if (publicKey.Length != 40)
  136. throw new ArgumentException("publicKey must be 40 char long");
  137. SecretKey = Z85Decode(secretKey);
  138. PublicKey = Z85Decode(publicKey);
  139. }
  140. private NetMQCertificate(byte[] key, bool isSecret)
  141. {
  142. if (key.Length != 32)
  143. throw new ArgumentException("key must be 32 bytes length");
  144. if (isSecret)
  145. {
  146. SecretKey = key;
  147. PublicKey = Curve25519.ScalarMultiplicationBase(key);
  148. }
  149. else
  150. PublicKey = key;
  151. }
  152. private NetMQCertificate(string keystr, bool isSecret)
  153. {
  154. if (keystr.Length != 40)
  155. throw new ArgumentException("key must be 40 bytes length");
  156. var key = Z85Decode(keystr);
  157. if (isSecret)
  158. {
  159. SecretKey = key;
  160. PublicKey = Curve25519.ScalarMultiplicationBase(key);
  161. }
  162. else
  163. PublicKey = key;
  164. }
  165. /// <summary>
  166. /// Create a certificate from secret key, public key is derived from the secret key
  167. /// </summary>
  168. /// <param name="secretKey">Secret Key</param>
  169. /// <exception cref="ArgumentException">If secret key is not 32-bytes long</exception>
  170. /// <returns>The newly created certificate</returns>
  171. public static NetMQCertificate CreateFromSecretKey(byte[] secretKey)
  172. {
  173. return new NetMQCertificate(secretKey, true);
  174. }
  175. /// <summary>
  176. /// Create a certificate from secret key, public key is derived from the secret key
  177. /// </summary>
  178. /// <param name="secretKey">Secret Key</param>
  179. /// <exception cref="ArgumentException">If secret key is not 32-bytes long</exception>
  180. /// <returns>The newly created certificate</returns>
  181. public NetMQCertificate FromSecretKey(byte[] secretKey)
  182. {
  183. return new NetMQCertificate(secretKey, true);
  184. }
  185. /// <summary>
  186. /// Create a certificate from secret key, public key is derived from the secret key
  187. /// </summary>
  188. /// <param name="secretKey">Secret Key</param>
  189. /// <exception cref="ArgumentException">If secret key is not 40-chars long</exception>
  190. /// <returns>The newly created certificate</returns>
  191. public static NetMQCertificate CreateFromSecretKey(string secretKey)
  192. {
  193. return new NetMQCertificate(secretKey, true);
  194. }
  195. /// <summary>
  196. /// Create a certificate from secret key, public key is derived from the secret key
  197. /// </summary>
  198. /// <param name="secretKey">Secret Key</param>
  199. /// <exception cref="ArgumentException">If secret key is not 40-chars long</exception>
  200. /// <returns>The newly created certificate</returns>
  201. public NetMQCertificate FromSecretKey(string secretKey)
  202. {
  203. return new NetMQCertificate(secretKey, true);
  204. }
  205. /// <summary>
  206. /// Create a public key only certificate.
  207. /// </summary>
  208. /// <param name="publicKey">Public key</param>
  209. /// <exception cref="ArgumentException">If public key is not 32-bytes long</exception>
  210. /// <returns>The newly created certificate</returns>
  211. public static NetMQCertificate FromPublicKey(byte[] publicKey)
  212. {
  213. return new NetMQCertificate(publicKey, false);
  214. }
  215. /// <summary>
  216. /// Create a public key only certificate.
  217. /// </summary>
  218. /// <param name="publicKey">Public key</param>
  219. /// <exception cref="ArgumentException">If public key is not 40-chars long</exception>
  220. /// <returns>The newly created certificate</returns>
  221. public static NetMQCertificate FromPublicKey(string publicKey)
  222. {
  223. return new NetMQCertificate(publicKey, false);
  224. }
  225. /// <summary>
  226. /// Curve Secret key
  227. /// </summary>
  228. public byte[]? SecretKey { get; private set; }
  229. /// <summary>
  230. /// Curve Secret key, encoded.
  231. /// </summary>
  232. public string? SecretKeyZ85 => SecretKey != null ? Z85Encode(SecretKey) : null;
  233. /// <summary>
  234. /// Returns true if the certificate also includes a secret key
  235. /// </summary>
  236. public bool HasSecretKey => SecretKey != null;
  237. /// <summary>
  238. /// Curve Public key.
  239. /// </summary>
  240. public byte[] PublicKey { get; private set; }
  241. /// <summary>
  242. /// Curve Public key, encoded.
  243. /// </summary>
  244. public string PublicKeyZ85 => Z85Encode(PublicKey);
  245. }
  246. }