Identity.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #ifndef IDENTITY_H__
  2. #define IDENTITY_H__
  3. #include <inttypes.h>
  4. #include <string.h>
  5. #include <string>
  6. #include <memory>
  7. #include <atomic>
  8. #include <vector>
  9. #include "Base.h"
  10. #include "Signature.h"
  11. #include "CryptoKey.h"
  12. namespace i2p
  13. {
  14. namespace data
  15. {
  16. typedef Tag<32> IdentHash;
  17. inline std::string GetIdentHashAbbreviation (const IdentHash& ident)
  18. {
  19. return ident.ToBase64 ().substr (0, 4);
  20. }
  21. struct Keys
  22. {
  23. uint8_t privateKey[256];
  24. uint8_t signingPrivateKey[20];
  25. uint8_t publicKey[256];
  26. uint8_t signingKey[128];
  27. };
  28. const uint8_t CERTIFICATE_TYPE_NULL = 0;
  29. const uint8_t CERTIFICATE_TYPE_HASHCASH = 1;
  30. const uint8_t CERTIFICATE_TYPE_HIDDEN = 2;
  31. const uint8_t CERTIFICATE_TYPE_SIGNED = 3;
  32. const uint8_t CERTIFICATE_TYPE_MULTIPLE = 4;
  33. const uint8_t CERTIFICATE_TYPE_KEY = 5;
  34. struct Identity
  35. {
  36. uint8_t publicKey[256];
  37. uint8_t signingKey[128];
  38. uint8_t certificate[3]; // byte 1 - type, bytes 2-3 - length
  39. Identity () = default;
  40. Identity (const Keys& keys) { *this = keys; };
  41. Identity& operator=(const Keys& keys);
  42. size_t FromBuffer (const uint8_t * buf, size_t len);
  43. IdentHash Hash () const;
  44. };
  45. Keys CreateRandomKeys ();
  46. const size_t DEFAULT_IDENTITY_SIZE = sizeof (Identity); // 387 bytes
  47. const uint16_t CRYPTO_KEY_TYPE_ELGAMAL = 0;
  48. const uint16_t CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC = 1;
  49. const uint16_t CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC_TEST = 65280; // TODO: remove later
  50. const uint16_t CRYPTO_KEY_TYPE_ECIES_GOSTR3410_CRYPTO_PRO_A_SHA256_AES256CBC = 65281; // TODO: use GOST R 34.11 instead SHA256 and GOST 28147-89 instead AES
  51. const uint16_t SIGNING_KEY_TYPE_DSA_SHA1 = 0;
  52. const uint16_t SIGNING_KEY_TYPE_ECDSA_SHA256_P256 = 1;
  53. const uint16_t SIGNING_KEY_TYPE_ECDSA_SHA384_P384 = 2;
  54. const uint16_t SIGNING_KEY_TYPE_ECDSA_SHA512_P521 = 3;
  55. const uint16_t SIGNING_KEY_TYPE_RSA_SHA256_2048 = 4;
  56. const uint16_t SIGNING_KEY_TYPE_RSA_SHA384_3072 = 5;
  57. const uint16_t SIGNING_KEY_TYPE_RSA_SHA512_4096 = 6;
  58. const uint16_t SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519 = 7;
  59. const uint16_t SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519ph = 8; // not implemented
  60. const uint16_t SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256 = 9;
  61. const uint16_t SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512 = 10; // approved by FSB
  62. const uint16_t SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519 = 11; // for LeaseSet2 only
  63. typedef uint16_t SigningKeyType;
  64. typedef uint16_t CryptoKeyType;
  65. class IdentityEx
  66. {
  67. public:
  68. IdentityEx ();
  69. IdentityEx (const uint8_t * publicKey, const uint8_t * signingKey,
  70. SigningKeyType type = SIGNING_KEY_TYPE_DSA_SHA1, CryptoKeyType cryptoType = CRYPTO_KEY_TYPE_ELGAMAL);
  71. IdentityEx (const uint8_t * buf, size_t len);
  72. IdentityEx (const IdentityEx& other);
  73. IdentityEx (const Identity& standard);
  74. ~IdentityEx ();
  75. IdentityEx& operator=(const IdentityEx& other);
  76. IdentityEx& operator=(const Identity& standard);
  77. size_t FromBuffer (const uint8_t * buf, size_t len);
  78. size_t ToBuffer (uint8_t * buf, size_t len) const;
  79. size_t FromBase64(const std::string& s);
  80. std::string ToBase64 () const;
  81. const Identity& GetStandardIdentity () const { return m_StandardIdentity; };
  82. const IdentHash& GetIdentHash () const { return m_IdentHash; };
  83. const uint8_t * GetEncryptionPublicKey () const { return m_StandardIdentity.publicKey; };
  84. uint8_t * GetEncryptionPublicKeyBuffer () { return m_StandardIdentity.publicKey; };
  85. std::shared_ptr<i2p::crypto::CryptoKeyEncryptor> CreateEncryptor (const uint8_t * key) const;
  86. size_t GetFullLen () const { return m_ExtendedLen + DEFAULT_IDENTITY_SIZE; };
  87. size_t GetSigningPublicKeyLen () const;
  88. const uint8_t * GetSigningPublicKeyBuffer () const; // returns NULL for P521
  89. size_t GetSigningPrivateKeyLen () const;
  90. size_t GetSignatureLen () const;
  91. bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const;
  92. SigningKeyType GetSigningKeyType () const;
  93. bool IsRSA () const; // signing key type
  94. CryptoKeyType GetCryptoKeyType () const;
  95. void DropVerifier () const; // to save memory
  96. bool operator == (const IdentityEx & other) const { return GetIdentHash() == other.GetIdentHash(); }
  97. void RecalculateIdentHash(uint8_t * buff=nullptr);
  98. static i2p::crypto::Verifier * CreateVerifier (SigningKeyType keyType);
  99. static std::shared_ptr<i2p::crypto::CryptoKeyEncryptor> CreateEncryptor (CryptoKeyType keyType, const uint8_t * key);
  100. private:
  101. void CreateVerifier () const;
  102. void UpdateVerifier (i2p::crypto::Verifier * verifier) const;
  103. private:
  104. Identity m_StandardIdentity;
  105. IdentHash m_IdentHash;
  106. mutable std::unique_ptr<i2p::crypto::Verifier> m_Verifier;
  107. mutable std::atomic_bool m_IsVerifierCreated; // make sure we don't create twice
  108. size_t m_ExtendedLen;
  109. uint8_t * m_ExtendedBuffer;
  110. };
  111. class PrivateKeys // for eepsites
  112. {
  113. public:
  114. PrivateKeys () = default;
  115. PrivateKeys (const PrivateKeys& other) { *this = other; };
  116. PrivateKeys (const Keys& keys) { *this = keys; };
  117. PrivateKeys& operator=(const Keys& keys);
  118. PrivateKeys& operator=(const PrivateKeys& other);
  119. ~PrivateKeys () = default;
  120. std::shared_ptr<const IdentityEx> GetPublic () const { return m_Public; };
  121. const uint8_t * GetPrivateKey () const { return m_PrivateKey; };
  122. const uint8_t * GetSigningPrivateKey () const { return m_SigningPrivateKey; };
  123. size_t GetSignatureLen () const; // might not match identity
  124. bool IsOfflineSignature () const { return m_TransientSignatureLen > 0; };
  125. uint8_t * GetPadding();
  126. void RecalculateIdentHash(uint8_t * buf=nullptr) { m_Public->RecalculateIdentHash(buf); }
  127. void Sign (const uint8_t * buf, int len, uint8_t * signature) const;
  128. size_t GetFullLen () const;
  129. size_t FromBuffer (const uint8_t * buf, size_t len);
  130. size_t ToBuffer (uint8_t * buf, size_t len) const;
  131. size_t FromBase64(const std::string& s);
  132. std::string ToBase64 () const;
  133. std::shared_ptr<i2p::crypto::CryptoKeyDecryptor> CreateDecryptor (const uint8_t * key) const;
  134. static std::shared_ptr<i2p::crypto::CryptoKeyDecryptor> CreateDecryptor (CryptoKeyType cryptoType, const uint8_t * key);
  135. static PrivateKeys CreateRandomKeys (SigningKeyType type = SIGNING_KEY_TYPE_DSA_SHA1, CryptoKeyType cryptoType = CRYPTO_KEY_TYPE_ELGAMAL);
  136. static void GenerateSigningKeyPair (SigningKeyType type, uint8_t * priv, uint8_t * pub);
  137. static void GenerateCryptoKeyPair (CryptoKeyType type, uint8_t * priv, uint8_t * pub); // priv and pub are 256 bytes long
  138. static i2p::crypto::Signer * CreateSigner (SigningKeyType keyType, const uint8_t * priv);
  139. // offline keys
  140. PrivateKeys CreateOfflineKeys (SigningKeyType type, uint32_t expires) const;
  141. const std::vector<uint8_t>& GetOfflineSignature () const { return m_OfflineSignature; };
  142. private:
  143. void CreateSigner () const;
  144. void CreateSigner (SigningKeyType keyType) const;
  145. private:
  146. std::shared_ptr<IdentityEx> m_Public;
  147. uint8_t m_PrivateKey[256];
  148. uint8_t m_SigningPrivateKey[128]; // assume private key doesn't exceed 128 bytes
  149. mutable std::unique_ptr<i2p::crypto::Signer> m_Signer;
  150. std::vector<uint8_t> m_OfflineSignature; // non zero length, if applicable
  151. size_t m_TransientSignatureLen = 0;
  152. size_t m_TransientSigningPrivateKeyLen = 0;
  153. };
  154. // kademlia
  155. struct XORMetric
  156. {
  157. union
  158. {
  159. uint8_t metric[32];
  160. uint64_t metric_ll[4];
  161. };
  162. void SetMin () { memset (metric, 0, 32); };
  163. void SetMax () { memset (metric, 0xFF, 32); };
  164. bool operator< (const XORMetric& other) const { return memcmp (metric, other.metric, 32) < 0; };
  165. };
  166. IdentHash CreateRoutingKey (const IdentHash& ident);
  167. XORMetric operator^(const IdentHash& key1, const IdentHash& key2);
  168. // destination for delivery instuctions
  169. class RoutingDestination
  170. {
  171. public:
  172. RoutingDestination () {};
  173. virtual ~RoutingDestination () {};
  174. virtual std::shared_ptr<const IdentityEx> GetIdentity () const = 0;
  175. virtual void Encrypt (const uint8_t * data, uint8_t * encrypted, BN_CTX * ctx) const = 0; // encrypt data for
  176. virtual bool IsDestination () const = 0; // for garlic
  177. const IdentHash& GetIdentHash () const { return GetIdentity ()->GetIdentHash (); };
  178. };
  179. class LocalDestination
  180. {
  181. public:
  182. virtual ~LocalDestination() {};
  183. virtual bool Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx) const = 0;
  184. virtual std::shared_ptr<const IdentityEx> GetIdentity () const = 0;
  185. const IdentHash& GetIdentHash () const { return GetIdentity ()->GetIdentHash (); };
  186. };
  187. }
  188. }
  189. #endif