My Project 3.2.0
C++ Distributed Hash Table
Loading...
Searching...
No Matches
crypto.h
1/*
2 * Copyright (C) 2014-2023 Savoir-faire Linux Inc.
3 * Author : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
4 * Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include "infohash.h"
23#include "utils.h"
24#include "rng.h"
25
26extern "C" {
27#include <gnutls/gnutls.h>
28#include <gnutls/abstract.h>
29#include <gnutls/x509.h>
30#include <gnutls/ocsp.h>
31}
32
33#include <vector>
34#include <memory>
35#include <atomic>
36#include <mutex>
37#include <string_view>
38
39#ifdef _WIN32
40#include <iso646.h>
41#endif
42
43namespace dht {
44
48namespace crypto {
49
50class OPENDHT_PUBLIC CryptoException : public std::runtime_error {
51public:
52 explicit CryptoException(const std::string& str) : std::runtime_error(str) {};
53 explicit CryptoException(const char* str) : std::runtime_error(str) {};
54 CryptoException(const CryptoException& e) noexcept = default;
55 CryptoException& operator=(const CryptoException&) noexcept = default;
56};
57
61class OPENDHT_PUBLIC DecryptError : public CryptoException {
62public:
63 explicit DecryptError(const std::string& str) : CryptoException(str) {};
64 explicit DecryptError(const char* str) : CryptoException(str) {};
65 DecryptError(const DecryptError& e) noexcept = default;
66 DecryptError& operator=(const DecryptError&) noexcept = default;
67};
68
69struct PrivateKey;
70struct Certificate;
71class RevocationList;
72
73using Identity = std::pair<std::shared_ptr<PrivateKey>, std::shared_ptr<Certificate>>;
74
78struct OPENDHT_PUBLIC PublicKey
79{
80 PublicKey();
81
85 PublicKey(gnutls_pubkey_t k) : pk(k) {}
86
88 PublicKey(const uint8_t* dat, size_t dat_size);
89 PublicKey(const Blob& pk) : PublicKey(pk.data(), pk.size()) {}
90 PublicKey(std::string_view pk) : PublicKey((const uint8_t*)pk.data(), pk.size()) {}
91 PublicKey(PublicKey&& o) noexcept : pk(o.pk) { o.pk = nullptr; }
92
93 ~PublicKey();
94 explicit operator bool() const { return pk; }
95 bool operator ==(const PublicKey& o) const {
96 return pk == o.pk || getLongId() == o.getLongId();
97 }
98 bool operator !=(const PublicKey& o) const {
99 return !(*this == o);
100 }
101
102 PublicKey& operator=(PublicKey&& o) noexcept;
103
107 const InfoHash& getId() const;
108
112 const PkId& getLongId() const;
113
114 bool checkSignature(const uint8_t* data, size_t data_len, const uint8_t* signature, size_t signature_len) const;
115 inline bool checkSignature(const Blob& data, const Blob& signature) const {
116 return checkSignature(data.data(), data.size(), signature.data(), signature.size());
117 }
118
119 Blob encrypt(const uint8_t* data, size_t data_len) const;
120 inline Blob encrypt(const Blob& data) const {
121 return encrypt(data.data(), data.size());
122 }
123 inline Blob encrypt(std::string_view data) const {
124 return encrypt((const uint8_t*)data.data(), data.size());
125 }
126 void pack(Blob& b) const;
127 int pack(uint8_t* out, size_t* out_len) const;
128 void unpack(const uint8_t* dat, size_t dat_size);
129
130 std::string toString() const;
131
132 template <typename Packer>
133 void msgpack_pack(Packer& p) const
134 {
135 Blob b;
136 pack(b);
137 p.pack_bin(b.size());
138 p.pack_bin_body((const char*)b.data(), b.size());
139 }
140
141 void msgpack_unpack(const msgpack::object& o);
142
143 gnutls_digest_algorithm_t getPreferredDigest() const;
144
145 gnutls_pubkey_t pk {nullptr};
146private:
147 mutable InfoHash cachedId_ {};
148 mutable PkId cachedLongId_ {};
149 mutable std::atomic_bool idCached_ {false};
150 mutable std::atomic_bool longIdCached_ {false};
151
152 PublicKey(const PublicKey&) = delete;
153 PublicKey& operator=(const PublicKey&) = delete;
154 void encryptBloc(const uint8_t* src, size_t src_size, uint8_t* dst, size_t dst_size) const;
155};
156
160struct OPENDHT_PUBLIC PrivateKey
161{
162 PrivateKey();
163 //PrivateKey(gnutls_privkey_t k) : key(k) {}
164
168 PrivateKey(gnutls_x509_privkey_t k);
169
170 PrivateKey(PrivateKey&& o) noexcept;
171 PrivateKey& operator=(PrivateKey&& o) noexcept;
172
173 PrivateKey(const uint8_t* src, size_t src_size, const char* password = nullptr);
174 PrivateKey(const Blob& src, const std::string& password = {}) : PrivateKey(src.data(), src.size(), password.c_str()) {}
175 PrivateKey(std::string_view src, const std::string& password = {}) : PrivateKey((const uint8_t*)src.data(), src.size(), password.c_str()) {}
176
177 ~PrivateKey();
178 explicit operator bool() const { return key; }
179
180 const PublicKey& getPublicKey() const;
181 const std::shared_ptr<PublicKey>& getSharedPublicKey() const;
182
183 int serialize(uint8_t* out, size_t* out_len, const std::string& password = {}) const;
184 Blob serialize(const std::string& password = {}) const;
185
190 Blob sign(const uint8_t* data, size_t data_len) const;
191 inline Blob sign(std::string_view dat) const { return sign((const uint8_t*)dat.data(), dat.size()); }
192 inline Blob sign(const Blob& dat) const { return sign(dat.data(), dat.size()); }
193
199 Blob decrypt(const uint8_t* cypher, size_t cypher_len) const;
200 Blob decrypt(const Blob& cypher) const { return decrypt(cypher.data(), cypher.size()); }
201
208 static PrivateKey generate(unsigned key_length = 4096);
209 static PrivateKey generateEC();
210
211 gnutls_privkey_t key {};
212 gnutls_x509_privkey_t x509_key {};
213private:
214 PrivateKey(const PrivateKey&) = delete;
215 PrivateKey& operator=(const PrivateKey&) = delete;
216 Blob decryptBloc(const uint8_t* src, size_t src_size) const;
217
218 mutable std::mutex publicKeyMutex_ {};
219 mutable std::shared_ptr<PublicKey> publicKey_ {};
220};
221
222class OPENDHT_PUBLIC RevocationList
223{
224 using clock = std::chrono::system_clock;
225 using time_point = clock::time_point;
226 using duration = clock::duration;
227public:
228 RevocationList();
229 RevocationList(const Blob& b);
230 RevocationList(RevocationList&& o) noexcept : crl(o.crl) { o.crl = nullptr; }
231 ~RevocationList();
232
233 RevocationList& operator=(RevocationList&& o) { crl = o.crl; o.crl = nullptr; return *this; }
234
235 void pack(Blob& b) const;
236 void unpack(const uint8_t* dat, size_t dat_size);
237 Blob getPacked() const {
238 Blob b;
239 pack(b);
240 return b;
241 }
242
243 template <typename Packer>
244 void msgpack_pack(Packer& p) const
245 {
246 Blob b = getPacked();
247 p.pack_bin(b.size());
248 p.pack_bin_body((const char*)b.data(), b.size());
249 }
250
251 void msgpack_unpack(const msgpack::object& o);
252
253 void revoke(const Certificate& crt, time_point t = time_point::min());
254
255 bool isRevoked(const Certificate& crt) const;
256
261 void sign(const PrivateKey&, const Certificate&, duration validity_period = {});
262 void sign(const Identity& id) { sign(*id.first, *id.second); }
263
264 bool isSignedBy(const Certificate& issuer) const;
265
266 std::string toString() const;
267
272
274 std::string getIssuerName() const;
275
277 std::string getIssuerUID() const;
278
279 time_point getUpdateTime() const;
280 time_point getNextUpdateTime() const;
281
282 gnutls_x509_crl_t get() { return crl; }
283 gnutls_x509_crl_t getCopy() const {
284 if (not crl)
285 return nullptr;
286 auto copy = RevocationList(getPacked());
287 gnutls_x509_crl_t ret = copy.crl;
288 copy.crl = nullptr;
289 return ret;
290 }
291
292private:
293 gnutls_x509_crl_t crl {};
294 RevocationList(const RevocationList&) = delete;
295 RevocationList& operator=(const RevocationList&) = delete;
296};
297
298enum class NameType { UNKNOWN = 0, RFC822, DNS, URI, IP };
299
300class OPENDHT_PUBLIC CertificateRequest {
301public:
302 CertificateRequest();
303 CertificateRequest(const uint8_t* data, size_t size);
304 CertificateRequest(std::string_view src) : CertificateRequest((const uint8_t*)src.data(), src.size()) {}
305 CertificateRequest(const Blob& data) : CertificateRequest(data.data(), data.size()) {}
306
307 CertificateRequest(CertificateRequest&& o) noexcept : request(std::move(o.request)) {
308 o.request = nullptr;
309 }
310 CertificateRequest& operator=(CertificateRequest&& o) noexcept;
311
312 ~CertificateRequest();
313
314 void setName(const std::string& name);
315 void setUID(const std::string& name);
316 void setAltName(NameType type, const std::string& name);
317
318 std::string getName() const;
319 std::string getUID() const;
320
321 void sign(const PrivateKey& key, const std::string& password = {});
322
323 bool verify() const;
324
325 Blob pack() const;
326 std::string toString() const;
327
328 gnutls_x509_crq_t get() const { return request; }
329private:
330 CertificateRequest(const CertificateRequest& o) = delete;
331 CertificateRequest& operator=(const CertificateRequest& o) = delete;
332 gnutls_x509_crq_t request {nullptr};
333};
334
335class OPENDHT_PUBLIC OcspRequest
336{
337public:
338 OcspRequest(gnutls_ocsp_req_t r) : request(r) {}
339 OcspRequest(const uint8_t* dat_ptr, size_t dat_size);
340 OcspRequest(std::string_view dat): OcspRequest((const uint8_t*)dat.data(), dat.size()) {}
341 ~OcspRequest();
342
343 /*
344 * Get OCSP Request in readable format.
345 */
346 std::string toString(const bool compact = true) const;
347
348 Blob pack() const;
349 Blob getNonce() const;
350private:
351 gnutls_ocsp_req_t request;
352};
353
354class OPENDHT_PUBLIC OcspResponse
355{
356public:
357 OcspResponse(const uint8_t* dat_ptr, size_t dat_size);
358 OcspResponse(std::string_view response) : OcspResponse((const uint8_t*)response.data(), response.size()) {}
359 ~OcspResponse();
360
361 Blob pack() const;
362 /*
363 * Get OCSP Response in readable format.
364 */
365 std::string toString(const bool compact = true) const;
366
367 /*
368 * Get OCSP response certificate status.
369 * Return certificate status.
370 * http://www.gnu.org/software/gnutls/reference/gnutls-ocsp.html#gnutls-ocsp-cert-status-t
371 */
372 gnutls_ocsp_cert_status_t getCertificateStatus() const;
373
374 /*
375 * Verify OCSP response and return OCSP status.
376 * Throws CryptoException in case of error in the response.
377 * http://www.gnu.org/software/gnutls/reference/gnutls-ocsp.html#gnutls-ocsp-verify-reason-t
378 */
379 gnutls_ocsp_cert_status_t verifyDirect(const Certificate& crt, const Blob& nonce);
380
381private:
382 gnutls_ocsp_resp_t response;
383};
384
385struct OPENDHT_PUBLIC Certificate {
386 Certificate() noexcept {}
387
391 Certificate(gnutls_x509_crt_t crt) noexcept : cert(crt) {}
392
393 Certificate(Certificate&& o) noexcept
394 : cert(o.cert)
395 , issuer(std::move(o.issuer))
396 , publicKey_(std::move(o.publicKey_))
397 { o.cert = nullptr; };
398
403 Certificate(const Blob& crt);
404 Certificate(const uint8_t* dat, size_t dat_size) : cert(nullptr) {
405 unpack(dat, dat_size);
406 }
407 Certificate(std::string_view pem) : Certificate((const uint8_t*)pem.data(), pem.size()) {}
408
413 template<typename Iterator>
414 Certificate(const Iterator& begin, const Iterator& end) {
415 unpack(begin, end);
416 }
417
422 template<typename Iterator>
423 Certificate(const std::vector<std::pair<Iterator, Iterator>>& certs) {
424 unpack(certs);
425 }
426
427 Certificate& operator=(Certificate&& o) noexcept;
428 ~Certificate();
429
430 void pack(Blob& b) const;
431 void unpack(const uint8_t* dat, size_t dat_size);
432 Blob getPacked() const {
433 Blob b;
434 pack(b);
435 return b;
436 }
437
446 template<typename Iterator>
447 void unpack(const Iterator& begin, const Iterator& end)
448 {
449 std::shared_ptr<Certificate> tmp_subject {};
450 std::shared_ptr<Certificate> first {};
451 for (Iterator icrt = begin; icrt < end; ++icrt) {
452 auto tmp_crt = std::make_shared<Certificate>(*icrt);
453 if (tmp_subject)
454 tmp_subject->issuer = tmp_crt;
455 tmp_subject = std::move(tmp_crt);
456 if (!first)
457 first = tmp_subject;
458 }
459 *this = first ? std::move(*first) : Certificate();
460 }
461
473 template<typename Iterator>
474 void unpack(const std::vector<std::pair<Iterator, Iterator>>& certs)
475 {
476 std::shared_ptr<Certificate> tmp_issuer;
477 // reverse iteration
478 for (auto li = certs.rbegin(); li != certs.rend(); ++li) {
479 Certificate tmp_crt;
480 gnutls_x509_crt_init(&tmp_crt.cert);
481 const gnutls_datum_t crt_dt {(uint8_t*)&(*li->first), (unsigned)(li->second-li->first)};
482 int err = gnutls_x509_crt_import(tmp_crt.cert, &crt_dt, GNUTLS_X509_FMT_PEM);
483 if (err != GNUTLS_E_SUCCESS)
484 err = gnutls_x509_crt_import(tmp_crt.cert, &crt_dt, GNUTLS_X509_FMT_DER);
485 if (err != GNUTLS_E_SUCCESS)
486 throw CryptoException(std::string("Could not read certificate - ") + gnutls_strerror(err));
487 tmp_crt.issuer = tmp_issuer;
488 tmp_issuer = std::make_shared<Certificate>(std::move(tmp_crt));
489 }
490 *this = tmp_issuer ? std::move(*tmp_issuer) : Certificate();
491 }
492
493 template <typename Packer>
494 void msgpack_pack(Packer& p) const
495 {
496 Blob b;
497 pack(b);
498 p.pack_bin(b.size());
499 p.pack_bin_body((const char*)b.data(), b.size());
500 }
501
502 void msgpack_unpack(const msgpack::object& o);
503
504 explicit operator bool() const { return cert; }
505 const PublicKey& getPublicKey() const;
506 const std::shared_ptr<PublicKey>& getSharedPublicKey() const;
507
509 const InfoHash& getId() const;
511 const PkId& getLongId() const;
512
513 Blob getSerialNumber() const;
514
516 std::string getName() const;
517
519 std::string getUID() const;
520
522 std::string getIssuerName() const;
523
525 std::string getIssuerUID() const;
526
528 std::vector<std::pair<NameType, std::string>> getAltNames() const;
529
530 std::chrono::system_clock::time_point getActivation() const;
531 std::chrono::system_clock::time_point getExpiration() const;
532
537 bool isCA() const;
538
543 std::string toString(bool chain = true) const;
544
545 std::string print() const;
546
551 void revoke(const PrivateKey&, const Certificate&);
552
556 std::vector<std::shared_ptr<RevocationList>> getRevocationLists() const;
557
562 void addRevocationList(std::shared_ptr<RevocationList>);
563
564 static Certificate generate(const PrivateKey& key, const std::string& name = "dhtnode", const Identity& ca = {}, bool is_ca = false, int64_t validity = 0);
565 static Certificate generate(const CertificateRequest& request, const Identity& ca, int64_t validity = 0);
566
567 gnutls_x509_crt_t getCopy() const {
568 if (not cert)
569 return nullptr;
570 auto copy = Certificate(getPacked());
571 gnutls_x509_crt_t ret = copy.cert;
572 copy.cert = nullptr;
573 return ret;
574 }
575
576 std::vector<gnutls_x509_crt_t>
577 getChain(bool copy = false) const
578 {
579 if (not cert)
580 return {};
581 std::vector<gnutls_x509_crt_t> crts;
582 for (auto c = this; c; c = c->issuer.get())
583 crts.emplace_back(copy ? c->getCopy() : c->cert);
584 return crts;
585 }
586
587 std::pair<
588 std::vector<gnutls_x509_crt_t>,
589 std::vector<gnutls_x509_crl_t>
590 >
591 getChainWithRevocations(bool copy = false) const
592 {
593 if (not cert)
594 return {};
595 std::vector<gnutls_x509_crt_t> crts;
596 std::vector<gnutls_x509_crl_t> crls;
597 for (auto c = this; c; c = c->issuer.get()) {
598 crts.emplace_back(copy ? c->getCopy() : c->cert);
599 crls.reserve(crls.size() + c->revocation_lists.size());
600 for (const auto& crl : c->revocation_lists)
601 crls.emplace_back(copy ? crl->getCopy() : crl->get());
602 }
603 return {crts, crls};
604 }
605
606 gnutls_digest_algorithm_t getPreferredDigest() const;
607
608 /*
609 * Generate OCSP request.
610 * Return GnuTLS error code.
611 * https://www.gnutls.org/manual/html_node/Error-codes.html
612 */
613 std::pair<std::string, Blob> generateOcspRequest(gnutls_x509_crt_t& issuer);
614
618 void setValidity(const Identity& ca, int64_t validity);
619 void setValidity(const PrivateKey& key, int64_t validity);
620
621 gnutls_x509_crt_t cert {nullptr};
622 std::shared_ptr<Certificate> issuer {};
623 std::shared_ptr<OcspResponse> ocspResponse;
624private:
625 Certificate(const Certificate&) = delete;
626 Certificate& operator=(const Certificate&) = delete;
627 mutable InfoHash cachedId_ {};
628 mutable PkId cachedLongId_ {};
629 mutable std::atomic_bool idCached_ {false};
630 mutable std::atomic_bool longIdCached_ {false};
631
632 struct crlNumberCmp {
633 bool operator() (const std::shared_ptr<RevocationList>& lhs, const std::shared_ptr<RevocationList>& rhs) const {
634 return lhs->getNumber() < rhs->getNumber();
635 }
636 };
637
638 std::set<std::shared_ptr<RevocationList>, crlNumberCmp> revocation_lists;
639
640 mutable std::mutex publicKeyMutex_ {};
641 mutable std::shared_ptr<PublicKey> publicKey_ {};
642};
643
644struct OPENDHT_PUBLIC TrustList
645{
647 int ret;
648 unsigned result;
649 bool hasError() const { return ret < 0; }
650 bool isValid() const { return !hasError() and !(result & GNUTLS_CERT_INVALID); }
651 explicit operator bool() const { return isValid(); }
652 std::string toString() const;
653 OPENDHT_PUBLIC friend std::ostream& operator<< (std::ostream& s, const VerifyResult& h);
654 };
655
656 TrustList();
657 TrustList(TrustList&& o) noexcept : trust(std::move(o.trust)) {
658 o.trust = nullptr;
659 }
660 TrustList& operator=(TrustList&& o) noexcept;
661 ~TrustList();
662 void add(const Certificate& crt);
663 void add(const RevocationList& crl);
664 void remove(const Certificate& crt, bool parents = true);
665 VerifyResult verify(const Certificate& crt) const;
666
667private:
668 TrustList(const TrustList& o) = delete;
669 TrustList& operator=(const TrustList& o) = delete;
670 gnutls_x509_trust_list_t trust {nullptr};
671};
672
680OPENDHT_PUBLIC Identity generateIdentity(const std::string& name, const Identity& ca, unsigned key_length, bool is_ca);
681OPENDHT_PUBLIC Identity generateIdentity(const std::string& name = "dhtnode", const Identity& ca = {}, unsigned key_length = 4096);
682
683OPENDHT_PUBLIC Identity generateEcIdentity(const std::string& name, const Identity& ca, bool is_ca);
684OPENDHT_PUBLIC Identity generateEcIdentity(const std::string& name = "dhtnode", const Identity& ca = {});
685
686OPENDHT_PUBLIC void saveIdentity(const Identity& id, const std::string& path, const std::string& privkey_password = {});
687OPENDHT_PUBLIC Identity loadIdentity(const std::string &path,const std::string &privkey_password = {});
688
697OPENDHT_PUBLIC Blob hash(const Blob& data, size_t hash_length = 512/8);
698
699OPENDHT_PUBLIC void hash(const uint8_t* data, size_t data_length, uint8_t* hash, size_t hash_length);
700
708OPENDHT_PUBLIC Blob stretchKey(std::string_view password, Blob& salt, size_t key_length = 512/8);
709
713OPENDHT_PUBLIC Blob aesEncrypt(const uint8_t* data, size_t data_length, const Blob& key);
714OPENDHT_PUBLIC inline Blob aesEncrypt(const Blob& data, const Blob& key) {
715 return aesEncrypt(data.data(), data.size(), key);
716}
726OPENDHT_PUBLIC Blob aesEncrypt(const Blob& data, std::string_view password, const Blob& salt = {});
727
731OPENDHT_PUBLIC Blob aesDecrypt(const uint8_t* data, size_t data_length, const Blob& key);
732OPENDHT_PUBLIC inline Blob aesDecrypt(const Blob& data, const Blob& key) { return aesDecrypt(data.data(), data.size(), key); }
733OPENDHT_PUBLIC inline Blob aesDecrypt(std::string_view data, const Blob& key) { return aesDecrypt((uint8_t*)data.data(), data.size(), key); }
734
735OPENDHT_PUBLIC Blob aesDecrypt(const uint8_t* data, size_t data_length, std::string_view password);
736OPENDHT_PUBLIC inline Blob aesDecrypt(const Blob& data, std::string_view password) { return aesDecrypt(data.data(), data.size(), password); }
737OPENDHT_PUBLIC inline Blob aesDecrypt(std::string_view data, std::string_view password) { return aesDecrypt((uint8_t*)data.data(), data.size(), password); }
738
742OPENDHT_PUBLIC Blob aesGetKey(const uint8_t* data, size_t data_length, std::string_view password);
743OPENDHT_PUBLIC Blob inline aesGetKey(const Blob& data, std::string_view password) {
744 return aesGetKey(data.data(), data.size(), password);
745}
747OPENDHT_PUBLIC Blob aesGetSalt(const uint8_t* data, size_t data_length);
748OPENDHT_PUBLIC Blob inline aesGetSalt(const Blob& data) {
749 return aesGetSalt(data.data(), data.size());
750}
752OPENDHT_PUBLIC std::string_view aesGetEncrypted(const uint8_t* data, size_t data_length);
753OPENDHT_PUBLIC std::string_view inline aesGetEncrypted(const Blob& data) {
754 return aesGetEncrypted(data.data(), data.size());
755}
756
762OPENDHT_PUBLIC Blob aesBuildEncrypted(const uint8_t* encryptedData, size_t data_length, const Blob& salt);
763OPENDHT_PUBLIC Blob inline aesBuildEncrypted(const Blob& encryptedData, const Blob& salt) {
764 return aesBuildEncrypted(encryptedData.data(), encryptedData.size(), salt);
765}
766OPENDHT_PUBLIC Blob inline aesBuildEncrypted(std::string_view encryptedData, const Blob& salt) {
767 return aesBuildEncrypted((const uint8_t*)encryptedData.data(), encryptedData.size(), salt);
768}
769
770}
771}
std::string getIssuerUID() const
void sign(const PrivateKey &, const Certificate &, duration validity_period={})
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 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:151
std::string getIssuerUID() const
Certificate(const std::vector< std::pair< Iterator, Iterator > > &certs)
Definition crypto.h:423
void unpack(const Iterator &begin, const Iterator &end)
Definition crypto.h:447
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:414
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:474
std::string toString(bool chain=true) 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:391
Blob sign(const uint8_t *data, size_t data_len) const
Blob decrypt(const uint8_t *cypher, size_t cypher_len) const
static PrivateKey generate(unsigned key_length=4096)
PrivateKey(gnutls_x509_privkey_t k)
const InfoHash & getId() const
PublicKey(const uint8_t *dat, size_t dat_size)
PublicKey(gnutls_pubkey_t k)
Definition crypto.h:85
const PkId & getLongId() const