esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
keychain.hpp
Go to the documentation of this file.
1#ifndef NDNPH_CLI_KEYCHAIN_HPP
2#define NDNPH_CLI_KEYCHAIN_HPP
3
4#include "../keychain/ec.hpp"
5#include "../keychain/keychain.hpp"
6#include "io.hpp"
7
8namespace ndnph {
9namespace cli {
10
12inline KeyChain&
14 static KeyChain keyChain;
15 static bool ready = false;
16 if (!ready) {
17 const char* env = getenv("NDNPH_KEYCHAIN");
18 if (env == nullptr) {
19 fprintf(stderr, "ndnph::cli::openKeyChain missing NDNPH_KEYCHAIN environment variable\n");
20 exit(1);
21 }
22
23 ready = keyChain.open(env);
24 if (!ready) {
25 fprintf(stderr, "ndnph::cli::openKeyChain error\n");
26 exit(1);
27 }
28 }
29 return keyChain;
30}
31
33inline std::string
34checkKeyChainId(const std::string& id) {
35 bool ok = std::all_of(id.begin(), id.end(), [](char ch) {
36 return static_cast<bool>(std::islower(ch)) || static_cast<bool>(std::isdigit(ch));
37 });
38 if (id.empty() || !ok) {
39 fprintf(stderr,
40 "ndnph::cli::checkKeyChainId(%s) id must be non-empty and only contain digits and "
41 "lower-case letters\n",
42 id.data());
43 exit(1);
44 }
45 return id;
46}
47
49inline void
50loadKey(Region& region, const std::string& id, EcPrivateKey& pvt, EcPublicKey& pub) {
51 if (!ec::load(openKeyChain(), id.data(), region, pvt, pub)) {
52 fprintf(stderr, "ndnph::cli::loadKey(%s) not found in KeyChain\n", id.data());
53 exit(1);
54 }
55}
56
58inline Data
59loadCertificate(Region& region, const std::string& id) {
60 auto cert = openKeyChain().certs.get(id.data(), region);
61 if (!cert) {
62 fprintf(stderr, "ndnph::cli::loadCertificate(%s) not found in KeyChain\n", id.data());
63 exit(1);
64 }
65 return cert;
66}
67
69inline Data
70inputCertificate(Region& region, EcPublicKey* pub = nullptr, std::istream& is = std::cin) {
71 auto data = region.create<Data>();
72 if (!data || !input(region, data, is) ||
73 !(pub == nullptr ? certificate::isCertificate(data) : pub->import(region, data))) {
74 fprintf(stderr, "ndnph::cli::inputCertificate parse cert error\n");
75 exit(1);
76 }
77 return data;
78}
79
80} // namespace cli
81} // namespace ndnph
82
83#endif // NDNPH_CLI_KEYCHAIN_HPP
Data packet.
Definition data.hpp:136
Data get(const char *id, Region &region)
Definition keychain.hpp:26
File based key and certificate store.
Definition keychain.hpp:36
bool open(Arg &&... arg)
Open the FileStore backend in both key store and certificate store.
Definition keychain.hpp:46
KeyChainCerts certs
Definition keychain.hpp:52
Region-based memory allocator thats owns memory of NDNph objects.
Definition region.hpp:9
RefType create(Arg &&... arg)
Allocate and create an object, and return its reference.
Definition region.hpp:90
EC private key.
Definition ec.hpp:180
EC public key.
Definition ec.hpp:65
bool isCertificate(const Data &data)
Determine if the Data packet is a certificate.
Definition certificate.hpp:170
void loadKey(Region &region, const std::string &id, EcPrivateKey &pvt, EcPublicKey &pub)
Load a key from the KeyChain.
Definition keychain.hpp:50
std::string checkKeyChainId(const std::string &id)
Check KeyChain object ID has the proper format.
Definition keychain.hpp:34
KeyChain & openKeyChain()
Open KeyChain according to `NDNPH_KEYCHAIN` environ.
Definition keychain.hpp:13
bool input(Region &region, T &target, std::istream &is=std::cin)
Read and decode from input stream.
Definition io.hpp:15
Data loadCertificate(Region &region, const std::string &id)
Load a certificate from the KeyChain.
Definition keychain.hpp:59
Data inputCertificate(Region &region, EcPublicKey *pub=nullptr, std::istream &is=std::cin)
Load a certificate in binary format from input stream.
Definition keychain.hpp:70
bool load(KeyChain &keyChain, const char *id, Region &region, EcPrivateKey &pvt, EcPublicKey &pub)
Load key pair from KeyChain.
Definition ec.hpp:301
Definition fs.hpp:33