My Project 4.4.0
C++ Distributed Hash Table
Loading...
Searching...
No Matches
crypto.h
1// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "infohash.h"
6#include "utils.h"
7#include "rng.h"
8
9extern "C" {
10#include <gnutls/gnutls.h>
11#include <gnutls/abstract.h>
12#include <gnutls/x509.h>
13#include <gnutls/ocsp.h>
14}
15
16#include <vector>
17#include <memory>
18#include <atomic>
19#include <mutex>
20#include <string_view>
21
22#ifdef _WIN32
23#include <iso646.h>
24#endif
25
26namespace dht {
27
31namespace crypto {
32
33class OPENDHT_PUBLIC CryptoException : public std::runtime_error
34{
35public:
36 explicit CryptoException(const std::string& str)
37 : std::runtime_error(str) {};
38 explicit CryptoException(const char* str)
39 : std::runtime_error(str) {};
40 CryptoException(const CryptoException& e) noexcept = default;
41 CryptoException& operator=(const CryptoException&) noexcept = default;
42};
43
47class OPENDHT_PUBLIC DecryptError : public CryptoException
48{
49public:
50 explicit DecryptError(const std::string& str)
51 : CryptoException(str) {};
52 explicit DecryptError(const char* str)
53 : CryptoException(str) {};
54 DecryptError(const DecryptError& e) noexcept = default;
55 DecryptError& operator=(const DecryptError&) noexcept = default;
56};
57
58struct PrivateKey;
59struct Certificate;
60class RevocationList;
61
62using Identity = std::pair<std::shared_ptr<PrivateKey>, std::shared_ptr<Certificate>>;
63
67struct OPENDHT_PUBLIC PublicKey
68{
69 PublicKey();
70
74 PublicKey(gnutls_pubkey_t k)
75 : pk(k)
76 {}
77
79 PublicKey(const uint8_t* dat, size_t dat_size);
80 PublicKey(const Blob& pk)
81 : PublicKey(pk.data(), pk.size())
82 {}
83 PublicKey(std::string_view pk)
84 : PublicKey((const uint8_t*) pk.data(), pk.size())
85 {}
86 PublicKey(PublicKey&& o) noexcept
87 : pk(o.pk)
88 {
89 o.pk = nullptr;
90 }
91
92 ~PublicKey();
93 explicit operator bool() const { return pk; }
94 bool operator==(const PublicKey& o) const { return pk == o.pk || getLongId() == o.getLongId(); }
95 bool operator!=(const PublicKey& o) const { return !(*this == o); }
96
97 PublicKey& operator=(PublicKey&& o) noexcept;
98
102 const InfoHash& getId() const;
103
107 const PkId& getLongId() const;
108
109 bool checkSignature(const uint8_t* data, size_t data_len, const uint8_t* signature, size_t signature_len) const;
110 inline bool checkSignature(const Blob& data, const Blob& signature) const
111 {
112 return checkSignature(data.data(), data.size(), signature.data(), signature.size());
113 }
114
115 Blob encrypt(const uint8_t* data, size_t data_len) const;
116 inline Blob encrypt(const Blob& data) const { return encrypt(data.data(), data.size()); }
117 inline Blob encrypt(std::string_view data) const { return encrypt((const uint8_t*) data.data(), data.size()); }
118 void pack(Blob& b) const;
119 int pack(uint8_t* out, size_t* out_len) const;
120 void unpack(const uint8_t* dat, size_t dat_size);
121
122 std::string toString() const;
123
124 template<typename Packer>
125 void msgpack_pack(Packer& p) const
126 {
127 Blob b;
128 pack(b);
129 p.pack_bin(b.size());
130 p.pack_bin_body((const char*) b.data(), b.size());
131 }
132
133 void msgpack_unpack(const msgpack::object& o);
134
135 gnutls_digest_algorithm_t getPreferredDigest() const;
136
137 gnutls_pubkey_t pk {nullptr};
138
139private:
140 mutable InfoHash cachedId_ {};
141 mutable PkId cachedLongId_ {};
142 mutable std::atomic_bool idCached_ {false};
143 mutable std::atomic_bool longIdCached_ {false};
144
145 PublicKey(const PublicKey&) = delete;
146 PublicKey& operator=(const PublicKey&) = delete;
147 void encryptBloc(const uint8_t* src, size_t src_size, uint8_t* dst, size_t dst_size) const;
148};
149
153struct OPENDHT_PUBLIC PrivateKey
154{
155 PrivateKey();
156 // PrivateKey(gnutls_privkey_t k) : key(k) {}
157
161 PrivateKey(gnutls_x509_privkey_t k);
162
163 PrivateKey(PrivateKey&& o) noexcept;
164 PrivateKey& operator=(PrivateKey&& o) noexcept;
165
166 PrivateKey(const uint8_t* src, size_t src_size, const char* password = nullptr);
167 PrivateKey(const Blob& src, const std::string& password = {})
168 : PrivateKey(src.data(), src.size(), password.c_str())
169 {}
170 PrivateKey(std::string_view src, const std::string& password = {})
171 : PrivateKey((const uint8_t*) src.data(), src.size(), password.c_str())
172 {}
173
174 ~PrivateKey();
175 explicit operator bool() const { return key; }
176
177 const PublicKey& getPublicKey() const;
178 const std::shared_ptr<PublicKey>& getSharedPublicKey() const;
179
180 int serialize(uint8_t* out, size_t* out_len, const std::string& password = {}) const;
181 Blob serialize(const std::string& password = {}) const;
182
187 Blob sign(const uint8_t* data, size_t data_len) const;
188 inline Blob sign(std::string_view dat) const { return sign((const uint8_t*) dat.data(), dat.size()); }
189 inline Blob sign(const Blob& dat) const { return sign(dat.data(), dat.size()); }
190
196 Blob decrypt(const uint8_t* cypher, size_t cypher_len) const;
197 Blob decrypt(const Blob& cypher) const { return decrypt(cypher.data(), cypher.size()); }
198
206 static PrivateKey generate(unsigned key_length = 4096, gnutls_pk_algorithm_t algo = GNUTLS_PK_RSA);
207
213 static PrivateKey generateEC(gnutls_ecc_curve_t curve = GNUTLS_ECC_CURVE_INVALID);
214
215 gnutls_privkey_t key {};
216 gnutls_x509_privkey_t x509_key {};
217
218private:
219 PrivateKey(const PrivateKey&) = delete;
220 PrivateKey& operator=(const PrivateKey&) = delete;
221 Blob decryptBloc(const uint8_t* src, size_t src_size) const;
222
223 mutable std::mutex publicKeyMutex_;
224 mutable std::shared_ptr<PublicKey> publicKey_ {};
225};
226
227class OPENDHT_PUBLIC RevocationList
228{
229 using clock = std::chrono::system_clock;
230 using time_point = clock::time_point;
231 using duration = clock::duration;
232
233public:
234 RevocationList();
235 RevocationList(const Blob& b);
236 RevocationList(RevocationList&& o) noexcept
237 : crl(o.crl)
238 {
239 o.crl = nullptr;
240 }
241 ~RevocationList();
242
243 RevocationList& operator=(RevocationList&& o)
244 {
245 crl = o.crl;
246 o.crl = nullptr;
247 return *this;
248 }
249
250 void pack(Blob& b) const;
251 void unpack(const uint8_t* dat, size_t dat_size);
252 Blob getPacked() const
253 {
254 Blob b;
255 pack(b);
256 return b;
257 }
258
259 template<typename Packer>
260 void msgpack_pack(Packer& p) const
261 {
262 Blob b = getPacked();
263 p.pack_bin(b.size());
264 p.pack_bin_body((const char*) b.data(), b.size());
265 }
266
267 void msgpack_unpack(const msgpack::object& o);
268
269 void revoke(const Certificate& crt, time_point t = time_point::min());
270
271 bool isRevoked(const Certificate& crt) const;
272
280 void sign(const PrivateKey&, const Certificate&, duration validity_period = {}, uint64_t number = 0);
281 void sign(const Identity& id, duration validity_period = {}, uint64_t number = 0)
282 {
283 sign(*id.first, *id.second, validity_period, number);
284 }
285
286 bool isSignedBy(const Certificate& issuer) const;
287
288 std::string toString() const;
289
294
296 std::string getIssuerName() const;
297
299 std::string getIssuerUID() const;
300
301 time_point getUpdateTime() const;
302 time_point getNextUpdateTime() const;
303
304 gnutls_x509_crl_t get() { return crl; }
305 gnutls_x509_crl_t getCopy() const
306 {
307 if (not crl)
308 return nullptr;
309 auto copy = RevocationList(getPacked());
310 gnutls_x509_crl_t ret = copy.crl;
311 copy.crl = nullptr;
312 return ret;
313 }
314
315private:
316 gnutls_x509_crl_t crl {};
317 RevocationList(const RevocationList&) = delete;
318 RevocationList& operator=(const RevocationList&) = delete;
319};
320
321enum class NameType { UNKNOWN = 0, RFC822, DNS, URI, IP };
322
323class OPENDHT_PUBLIC CertificateRequest
324{
325public:
326 CertificateRequest();
327 CertificateRequest(const uint8_t* data, size_t size);
328 CertificateRequest(std::string_view src)
329 : CertificateRequest((const uint8_t*) src.data(), src.size())
330 {}
331 CertificateRequest(const Blob& data)
332 : CertificateRequest(data.data(), data.size())
333 {}
334
335 CertificateRequest(CertificateRequest&& o) noexcept
336 : request(std::move(o.request))
337 {
338 o.request = nullptr;
339 }
340 CertificateRequest& operator=(CertificateRequest&& o) noexcept;
341
342 ~CertificateRequest();
343
344 void setName(const std::string& name);
345 void setUID(const std::string& name);
346 void setAltName(NameType type, const std::string& name);
347
348 std::string getName() const;
349 std::string getUID() const;
350
351 void sign(const PrivateKey& key, const std::string& password = {});
352
353 bool verify() const;
354
355 Blob pack() const;
356 std::string toString() const;
357
358 gnutls_x509_crq_t get() const { return request; }
359
360private:
361 CertificateRequest(const CertificateRequest& o) = delete;
362 CertificateRequest& operator=(const CertificateRequest& o) = delete;
363 gnutls_x509_crq_t request {nullptr};
364};
365
366class OPENDHT_PUBLIC OcspRequest
367{
368public:
369 OcspRequest(gnutls_ocsp_req_t r)
370 : request(r)
371 {}
372 OcspRequest(const uint8_t* dat_ptr, size_t dat_size);
373 OcspRequest(std::string_view dat)
374 : OcspRequest((const uint8_t*) dat.data(), dat.size())
375 {}
376 ~OcspRequest();
377
378 /*
379 * Get OCSP Request in readable format.
380 */
381 std::string toString(const bool compact = true) const;
382
383 Blob pack() const;
384 Blob getNonce() const;
385
386private:
387 gnutls_ocsp_req_t request;
388};
389
390class OPENDHT_PUBLIC OcspResponse
391{
392public:
393 OcspResponse(const uint8_t* dat_ptr, size_t dat_size);
394 OcspResponse(std::string_view response)
395 : OcspResponse((const uint8_t*) response.data(), response.size())
396 {}
397 ~OcspResponse();
398
399 Blob pack() const;
400 /*
401 * Get OCSP Response in readable format.
402 */
403 std::string toString(const bool compact = true) const;
404
405 /*
406 * Get OCSP response certificate status.
407 * Return certificate status.
408 * http://www.gnu.org/software/gnutls/reference/gnutls-ocsp.html#gnutls-ocsp-cert-status-t
409 */
410 gnutls_ocsp_cert_status_t getCertificateStatus() const;
411
412 /*
413 * Verify OCSP response and return OCSP status.
414 * Throws CryptoException in case of error in the response.
415 * http://www.gnu.org/software/gnutls/reference/gnutls-ocsp.html#gnutls-ocsp-verify-reason-t
416 */
417 gnutls_ocsp_cert_status_t verifyDirect(const Certificate& crt, const Blob& nonce);
418
419private:
420 gnutls_ocsp_resp_t response;
421};
422
423struct OPENDHT_PUBLIC Certificate
424{
425 Certificate() noexcept {}
426
430 Certificate(gnutls_x509_crt_t crt) noexcept
431 : cert(crt)
432 {}
433
434 Certificate(Certificate&& o) noexcept
435 : cert(o.cert)
436 , issuer(std::move(o.issuer))
437 , publicKey_(std::move(o.publicKey_))
438 {
439 o.cert = nullptr;
440 };
441
446 Certificate(const Blob& crt);
447 Certificate(const uint8_t* dat, size_t dat_size)
448 : cert(nullptr)
449 {
450 unpack(dat, dat_size);
451 }
452 Certificate(std::string_view pem)
453 : Certificate((const uint8_t*) pem.data(), pem.size())
454 {}
455
460 template<typename Iterator>
461 Certificate(const Iterator& begin, const Iterator& end)
462 {
463 unpack(begin, end);
464 }
465
470 template<typename Iterator>
471 Certificate(const std::vector<std::pair<Iterator, Iterator>>& certs)
472 {
473 unpack(certs);
474 }
475
476 Certificate& operator=(Certificate&& o) noexcept;
477 ~Certificate();
478
479 void pack(Blob& b) const;
480 void unpack(const uint8_t* dat, size_t dat_size);
481 Blob getPacked() const
482 {
483 Blob b;
484 pack(b);
485 return b;
486 }
487
496 template<typename Iterator>
497 void unpack(const Iterator& begin, const Iterator& end)
498 {
499 std::shared_ptr<Certificate> tmp_subject {};
500 std::shared_ptr<Certificate> first {};
501 for (Iterator icrt = begin; icrt < end; ++icrt) {
502 auto tmp_crt = std::make_shared<Certificate>(*icrt);
503 if (tmp_subject)
504 tmp_subject->issuer = tmp_crt;
505 tmp_subject = std::move(tmp_crt);
506 if (!first)
507 first = tmp_subject;
508 }
509 *this = first ? std::move(*first) : Certificate();
510 }
511
523 template<typename Iterator>
524 void unpack(const std::vector<std::pair<Iterator, Iterator>>& certs)
525 {
526 std::shared_ptr<Certificate> tmp_issuer;
527 // reverse iteration
528 for (auto li = certs.rbegin(); li != certs.rend(); ++li) {
529 Certificate tmp_crt;
530 gnutls_x509_crt_init(&tmp_crt.cert);
531 const gnutls_datum_t crt_dt {(uint8_t*) &(*li->first), (unsigned) (li->second - li->first)};
532 int err = gnutls_x509_crt_import(tmp_crt.cert, &crt_dt, GNUTLS_X509_FMT_PEM);
533 if (err != GNUTLS_E_SUCCESS)
534 err = gnutls_x509_crt_import(tmp_crt.cert, &crt_dt, GNUTLS_X509_FMT_DER);
535 if (err != GNUTLS_E_SUCCESS)
536 throw CryptoException(std::string("Could not read certificate - ") + gnutls_strerror(err));
537 tmp_crt.issuer = tmp_issuer;
538 tmp_issuer = std::make_shared<Certificate>(std::move(tmp_crt));
539 }
540 *this = tmp_issuer ? std::move(*tmp_issuer) : Certificate();
541 }
542
543 template<typename Packer>
544 void msgpack_pack(Packer& p) const
545 {
546 Blob b;
547 pack(b);
548 p.pack_bin(b.size());
549 p.pack_bin_body((const char*) b.data(), b.size());
550 }
551
552 void msgpack_unpack(const msgpack::object& o);
553
554 explicit operator bool() const { return cert; }
555 const PublicKey& getPublicKey() const;
556 const std::shared_ptr<PublicKey>& getSharedPublicKey() const;
557
559 const InfoHash& getId() const;
561 const PkId& getLongId() const;
562
563 Blob getSerialNumber() const;
564
566 std::string getDN() const;
567
569 std::string getName() const;
570
572 std::string getUID() const;
573
575 std::string getIssuerDN() const;
576
578 std::string getIssuerName() const;
579
581 std::string getIssuerUID() const;
582
584 std::vector<std::pair<NameType, std::string>> getAltNames() const;
585
586 std::chrono::system_clock::time_point getActivation() const;
587 std::chrono::system_clock::time_point getExpiration() const;
588
593 bool isCA() const;
594
599 std::string toString(bool chain = true) const;
600
601 std::string print() const;
602
607 void revoke(const PrivateKey&, const Certificate&);
608
612 std::vector<std::shared_ptr<RevocationList>> getRevocationLists() const;
613
618 void addRevocationList(std::shared_ptr<RevocationList>);
619
620 static Certificate generate(const PrivateKey& key,
621 const std::string& name = "dhtnode",
622 const Identity& ca = {},
623 bool is_ca = false,
624 int64_t validity = 0);
625 static Certificate generate(const CertificateRequest& request, const Identity& ca, int64_t validity = 0);
626
627 gnutls_x509_crt_t getCopy() const
628 {
629 if (not cert)
630 return nullptr;
631 auto copy = Certificate(getPacked());
632 gnutls_x509_crt_t ret = copy.cert;
633 copy.cert = nullptr;
634 return ret;
635 }
636
637 std::vector<gnutls_x509_crt_t> getChain(bool copy = false) const
638 {
639 if (not cert)
640 return {};
641 std::vector<gnutls_x509_crt_t> crts;
642 for (auto c = this; c; c = c->issuer.get())
643 crts.emplace_back(copy ? c->getCopy() : c->cert);
644 return crts;
645 }
646
647 std::pair<std::vector<gnutls_x509_crt_t>, std::vector<gnutls_x509_crl_t>> getChainWithRevocations(
648 bool copy = false) const
649 {
650 if (not cert)
651 return {};
652 std::vector<gnutls_x509_crt_t> crts;
653 std::vector<gnutls_x509_crl_t> crls;
654 for (auto c = this; c; c = c->issuer.get()) {
655 crts.emplace_back(copy ? c->getCopy() : c->cert);
656 crls.reserve(crls.size() + c->revocation_lists.size());
657 for (const auto& crl : c->revocation_lists)
658 crls.emplace_back(copy ? crl->getCopy() : crl->get());
659 }
660 return {crts, crls};
661 }
662
663 gnutls_digest_algorithm_t getPreferredDigest() const;
664
665 /*
666 * Generate OCSP request.
667 * Return GnuTLS error code.
668 * https://www.gnutls.org/manual/html_node/Error-codes.html
669 */
670 std::pair<std::string, Blob> generateOcspRequest(gnutls_x509_crt_t& issuer);
671
675 void setValidity(const Identity& ca, int64_t validity);
676 void setValidity(const PrivateKey& key, int64_t validity);
677
678 gnutls_x509_crt_t cert {nullptr};
679 std::shared_ptr<Certificate> issuer {};
680 std::shared_ptr<OcspResponse> ocspResponse;
681
682private:
683 Certificate(const Certificate&) = delete;
684 Certificate& operator=(const Certificate&) = delete;
685 mutable InfoHash cachedId_ {};
686 mutable PkId cachedLongId_ {};
687 mutable std::atomic_bool idCached_ {false};
688 mutable std::atomic_bool longIdCached_ {false};
689
690 struct crlNumberCmp
691 {
692 bool operator()(const std::shared_ptr<RevocationList>& lhs, const std::shared_ptr<RevocationList>& rhs) const
693 {
694 return lhs->getNumber() < rhs->getNumber();
695 }
696 };
697
698 std::set<std::shared_ptr<RevocationList>, crlNumberCmp> revocation_lists;
699
700 mutable std::mutex publicKeyMutex_;
701 mutable std::shared_ptr<PublicKey> publicKey_ {};
702};
703
704struct OPENDHT_PUBLIC TrustList
705{
707 {
708 int ret;
709 unsigned result;
710 bool hasError() const { return ret < 0; }
711 bool isValid() const { return !hasError() and !(result & GNUTLS_CERT_INVALID); }
712 explicit operator bool() const { return isValid(); }
713 OPENDHT_PUBLIC std::string toString() const;
714 OPENDHT_PUBLIC friend std::ostream& operator<<(std::ostream& s, const VerifyResult& h);
715 };
716
717 TrustList();
718 TrustList(TrustList&& o) noexcept
719 : trust(std::move(o.trust))
720 {
721 o.trust = nullptr;
722 }
723 TrustList& operator=(TrustList&& o) noexcept;
724 ~TrustList();
725 void add(const Certificate& crt);
726 void add(const RevocationList& crl);
727 void remove(const Certificate& crt, bool parents = true);
728 VerifyResult verify(const Certificate& crt) const;
729
730private:
731 TrustList(const TrustList& o) = delete;
732 TrustList& operator=(const TrustList& o) = delete;
733 gnutls_x509_trust_list_t trust {nullptr};
734};
735
743OPENDHT_PUBLIC Identity generateIdentity(const std::string& name, const Identity& ca, unsigned key_length, bool is_ca);
744OPENDHT_PUBLIC Identity generateIdentity(const std::string& name = "dhtnode",
745 const Identity& ca = {},
746 unsigned key_length = 4096);
747
748OPENDHT_PUBLIC Identity generateEcIdentity(const std::string& name, const Identity& ca, bool is_ca);
749OPENDHT_PUBLIC Identity generateEcIdentity(const std::string& name = "dhtnode", const Identity& ca = {});
750
751OPENDHT_PUBLIC void saveIdentity(const Identity& id, const std::string& path, const std::string& privkey_password = {});
752OPENDHT_PUBLIC Identity loadIdentity(const std::string& path, const std::string& privkey_password = {});
753
762OPENDHT_PUBLIC Blob hash(const Blob& data, size_t hash_length = 512 / 8);
763
764OPENDHT_PUBLIC void hash(const uint8_t* data, size_t data_length, uint8_t* hash, size_t hash_length);
765
773OPENDHT_PUBLIC Blob stretchKey(std::string_view password, Blob& salt, size_t key_length = 512 / 8);
774
778OPENDHT_PUBLIC Blob aesEncrypt(const uint8_t* data, size_t data_length, const Blob& key);
779OPENDHT_PUBLIC inline Blob
780aesEncrypt(const Blob& data, const Blob& key)
781{
782 return aesEncrypt(data.data(), data.size(), key);
783}
794OPENDHT_PUBLIC Blob aesEncrypt(const Blob& data, std::string_view password, const Blob& salt = {});
795
799OPENDHT_PUBLIC Blob aesDecrypt(const uint8_t* data, size_t data_length, const Blob& key);
800OPENDHT_PUBLIC inline Blob
801aesDecrypt(const Blob& data, const Blob& key)
802{
803 return aesDecrypt(data.data(), data.size(), key);
804}
805OPENDHT_PUBLIC inline Blob
806aesDecrypt(std::string_view data, const Blob& key)
807{
808 return aesDecrypt((uint8_t*) data.data(), data.size(), key);
809}
810
811OPENDHT_PUBLIC Blob aesDecrypt(const uint8_t* data, size_t data_length, std::string_view password);
812OPENDHT_PUBLIC inline Blob
813aesDecrypt(const Blob& data, std::string_view password)
814{
815 return aesDecrypt(data.data(), data.size(), password);
816}
817OPENDHT_PUBLIC inline Blob
818aesDecrypt(std::string_view data, std::string_view password)
819{
820 return aesDecrypt((uint8_t*) data.data(), data.size(), password);
821}
822
826OPENDHT_PUBLIC Blob aesGetKey(const uint8_t* data, size_t data_length, std::string_view password);
827OPENDHT_PUBLIC Blob inline aesGetKey(const Blob& data, std::string_view password)
828{
829 return aesGetKey(data.data(), data.size(), password);
830}
832OPENDHT_PUBLIC Blob aesGetSalt(const uint8_t* data, size_t data_length);
833OPENDHT_PUBLIC Blob inline aesGetSalt(const Blob& data)
834{
835 return aesGetSalt(data.data(), data.size());
836}
838OPENDHT_PUBLIC std::string_view aesGetEncrypted(const uint8_t* data, size_t data_length);
839OPENDHT_PUBLIC std::string_view inline aesGetEncrypted(const Blob& data)
840{
841 return aesGetEncrypted(data.data(), data.size());
842}
843
849OPENDHT_PUBLIC Blob aesBuildEncrypted(const uint8_t* encryptedData, size_t data_length, const Blob& salt);
850OPENDHT_PUBLIC Blob inline aesBuildEncrypted(const Blob& encryptedData, const Blob& salt)
851{
852 return aesBuildEncrypted(encryptedData.data(), encryptedData.size(), salt);
853}
854OPENDHT_PUBLIC Blob inline aesBuildEncrypted(std::string_view encryptedData, const Blob& salt)
855{
856 return aesBuildEncrypted((const uint8_t*) encryptedData.data(), encryptedData.size(), salt);
857}
858
868OPENDHT_PUBLIC Blob webPushEncrypt(const Blob& p256dh,
869 const Blob& auth,
870 const uint8_t* payload,
871 size_t payload_length,
872 const PrivateKey& privKey = {},
873 const Blob& salt = {});
874
875} // namespace crypto
876} // namespace dht
void sign(const PrivateKey &, const Certificate &, duration validity_period={}, uint64_t number=0)
std::string getIssuerUID() const
std::string getIssuerName() const
OPENDHT_PUBLIC Blob aesEncrypt(const uint8_t *data, size_t data_length, const Blob &key)
OPENDHT_PUBLIC Blob hash(const Blob &data, size_t hash_length=512/8)
OPENDHT_PUBLIC Blob aesBuildEncrypted(const uint8_t *encryptedData, size_t data_length, const Blob &salt)
OPENDHT_PUBLIC Identity generateIdentity(const std::string &name, const Identity &ca, unsigned key_length, bool is_ca)
OPENDHT_PUBLIC Blob stretchKey(std::string_view password, Blob &salt, size_t key_length=512/8)
OPENDHT_PUBLIC std::string_view aesGetEncrypted(const uint8_t *data, size_t data_length)
OPENDHT_PUBLIC Blob webPushEncrypt(const Blob &p256dh, const Blob &auth, const uint8_t *payload, size_t payload_length, const PrivateKey &privKey={}, const Blob &salt={})
OPENDHT_PUBLIC Blob aesGetKey(const uint8_t *data, size_t data_length, std::string_view password)
OPENDHT_PUBLIC Blob aesGetSalt(const uint8_t *data, size_t data_length)
OPENDHT_PUBLIC Blob aesDecrypt(const uint8_t *data, size_t data_length, const Blob &key)
std::vector< uint8_t > Blob
Definition utils.h:166
std::string getIssuerUID() const
Certificate(const std::vector< std::pair< Iterator, Iterator > > &certs)
Definition crypto.h:471
void unpack(const Iterator &begin, const Iterator &end)
Definition crypto.h:497
void setValidity(const Identity &ca, int64_t validity)
Certificate(const Blob &crt)
void revoke(const PrivateKey &, const Certificate &)
const PkId & getLongId() const
const InfoHash & getId() const
Certificate(const Iterator &begin, const Iterator &end)
Definition crypto.h:461
std::string getIssuerDN() const
std::vector< std::shared_ptr< RevocationList > > getRevocationLists() const
std::string getIssuerName() const
std::string getUID() const
void unpack(const std::vector< std::pair< Iterator, Iterator > > &certs)
Definition crypto.h:524
std::string toString(bool chain=true) const
std::string getDN() const
void addRevocationList(RevocationList &&)
std::string getName() const
std::vector< std::pair< NameType, std::string > > getAltNames() const
Certificate(gnutls_x509_crt_t crt) noexcept
Definition crypto.h:430
Blob sign(const uint8_t *data, size_t data_len) const
Blob decrypt(const uint8_t *cypher, size_t cypher_len) const
static PrivateKey generateEC(gnutls_ecc_curve_t curve=GNUTLS_ECC_CURVE_INVALID)
PrivateKey(gnutls_x509_privkey_t k)
static PrivateKey generate(unsigned key_length=4096, gnutls_pk_algorithm_t algo=GNUTLS_PK_RSA)
const InfoHash & getId() const
PublicKey(const uint8_t *dat, size_t dat_size)
PublicKey(gnutls_pubkey_t k)
Definition crypto.h:74
const PkId & getLongId() const