try to also support C++ < 201103; refs #27

master
Marc Wäckerlin 11 years ago
parent 63e6183831
commit 5b1020aabd
  1. 121
      doc/examples/cryptoki-sign-demo.cxx

@ -5,6 +5,10 @@
// 1 2 3 4 5 6 7 8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
#include <mrw/args.hxx>
#include <mrw/vector.hxx>
#include <cryptoki.hxx>
#include <string>
#include <map>
#include <iostream>
@ -15,96 +19,66 @@
#include <fstream>
#include <streambuf>
#include <chrono>
#include <cryptoki.hxx>
int main(int argc, char** argv) try {
// options
unsigned long r(1);
std::string txt("This is an example.");
std::string lib("libcvP11.so");
std::string slot;
std::string cert;
typedef std::map<std::string,
std::tuple<std::string*, unsigned long*, std::string*, std::string> >
Args;
Args args = {
// option 2 3 4 description
{"-h", Args::mapped_type(0, 0, 0, "same as --help")},
{"--help", Args::mapped_type(0, 0, 0, "show help")},
{"-r", Args::mapped_type(0, &r, 0, "same as --repeat")},
{"--repeat", Args::mapped_type(0, &r, 0, "<number> of repetitions")},
{"-t", Args::mapped_type(0, 0, &txt, "same as --text")},
{"--text", Args::mapped_type(0, 0, &txt, "<file> with text to sign")},
{"-l", Args::mapped_type(&lib, 0, 0, "same as --library")},
{"--library", Args::mapped_type(&lib, 0, 0, "<library> cryptoki to load")},
{"-s", Args::mapped_type(&slot, 0, 0, "same as --slot")},
{"--slot", Args::mapped_type(&slot, 0, 0, "<name> of slot")},
{"-c", Args::mapped_type(&cert, 0, 0, "same as --certificae")},
{"--certificate", Args::mapped_type(&cert, 0, 0, "<cert> name to use")}
// 2: read string from command line
// 3: read unsigned long integer from command line
// 4: read string from file given on command line
};
mrw::args::parse(argc, argv, mrw::args::list()
<<mrw::args::decl("h", "help", "show help",
mrw::args::decl::param_list()
<<mrw::args::help()
<<mrw::args::exit())
<<mrw::args::decl("r", "repeat", "number of repetitions",
mrw::args::decl::param_list()
<<mrw::args::param(r, "number"))
<<mrw::args::decl("t", "text", "text to sign",
mrw::args::decl::param_list()
<<mrw::args::param(txt, "text"))
<<mrw::args::decl("l", "library", "cryptoki lirary to load",
mrw::args::decl::param_list()
<<mrw::args::param(lib, "lib"))
<<mrw::args::decl("s", "slot", "name of slot",
mrw::args::decl::param_list()
<<mrw::args::param(slot, "name"))
<<mrw::args::decl("c","cert", "name of certificate",
mrw::args::decl::param_list()
<<mrw::args::param(cert, "name")));xs
int main(int argc, char** argv) try {
for (auto arg(argv+1); arg<argv+argc; ++arg) {
auto it(args.find(*arg));
if (it!=args.end() &&
(std::get<0>(it->second)!=0 || std::get<1>(it->second)!=0 ||
std::get<2>(it->second)!=0)
&& arg+1<argv+argc) {
if (std::get<0>(it->second)) {
*std::get<0>(it->second) = *++arg;
} else if (std::get<1>(it->second)) {
((std::stringstream&)(std::stringstream()<<*++arg))
>>*std::get<1>(it->second);
} else if (std::get<2>(it->second)) {
std::ifstream t(*++arg);
*std::get<2>(it->second) = std::string
(std::istreambuf_iterator<char>(t),
std::istreambuf_iterator<char>());
}
} else { // argument type 0 or wrong parameter displays help
std::cerr<<"SYNOPSIS"<<std::endl;
std::cerr<<" "<<argv[0]<<" [OPTIONS]"<<std::endl;
std::cerr<<"DESCRIPTION"<<std::endl;
std::cerr<<" sign a text, optionally multiple times for"<<std::endl;
std::cerr<<" performance tests"<<std::endl;
std::cerr<<"OPTIONS"<<std::endl;
std::for_each(args.begin(), args.end(), [](Args::value_type v){
std::cerr<<" "<<std::setw(10)<<std::setfill(' ')
<<v.first<<' '<<std::get<3>(v.second)
<<std::endl;
});
return 1;
}
}
std::cout<<"Sign text "<<r<<" times:"<<std::endl
<<"-----------------------------------------------------"<<std::endl
<<txt<<std::endl
<<"-----------------------------------------------------"<<std::endl;
cryptoki::Init c(lib);
cryptoki::SlotList s(c.slotList());
std::for_each(s.begin(), s.end(), [](cryptoki::Slot s){
cryptoki::SlotInfo si(s.slotinfo());
if (slot.size()&&slot!=si.slotDescription) return;
cryptoki::SlotList sl(c.slotList());
for (cryptoki::SlotList::iterator s(sl.begin()); s!=sl.end(); ++s) {
cryptoki::SlotInfo si(s->slotinfo());
if (slot.size()&&slot!=si.slotDescription) continue;
std::cout<<"Found Slot: "<<si.slotDescription<<std::endl;
cryptoki::TokenInfo ti(s.tokeninfo());
cryptoki::TokenInfo ti(a->tokeninfo());
std::cout<<"Found token: "<<ti.label<<std::endl;
cryptoki::Session session(s);
cryptoki::Session session(*s);
cryptoki::ObjectList certs
(session.find(cryptoki::Attribute(CKA_CLASS)
.from<CK_OBJECT_CLASS>(CKO_CERTIFICATE)));
std::for_each(certs.begin(), certs.end(), [&session](cryptoki::Object c){
std::string label(c.attribute(CKA_LABEL).value);
if (cert.size()&&cert!=label) return;
cryptoki::Attribute id(c.attribute(CKA_ID));
for (cryptoki::ObjectList::iterator c(certs.begin()); c!=certs.end(); ++c) {
std::string label(c->attribute(CKA_LABEL).value);
if (cert.size()&&cert!=label) continue;
cryptoki::Attribute id(c->attribute(CKA_ID));
cryptoki::ObjectList keys
(session.find(cryptoki::Attribute(CKA_CLASS)
.from<CK_OBJECT_CLASS>(CKO_PUBLIC_KEY),
id));
if (!keys.size()) return;
if (!keys.size()) continue;
std::cout<<"Found Certificate: "
<<c.attribute(CKA_LABEL).value<<std::endl;
<<c->attribute(CKA_LABEL).value<<std::endl;
if (!cert.size()) continue;
std::cout<<"Pin: ";
std::string pin;
std::cin>>pin;
@ -117,14 +91,21 @@ int main(int argc, char** argv) try {
return;
}
std::cout<<"Signing ..."<<std::endl;
#ifndef MRW__OLD_PRE11_COMPILER
auto start = std::chrono::system_clock::now();
#endif
for (int i(0); i<r; ++i)
keys[0].sign(txt, CKM_RSA_PKCS);
#ifndef MRW__OLD_PRE11_COMPILER
auto end = std::chrono::system_clock::now();
auto elapsed =std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
auto elapsed(std::chrono::duration_cast<std::chrono::milliseconds>
(end-start));
std::cout<<"Done in "<<elapsed.count()<<"ms"<<std::endl;
});
});
#else
std::cout<<"Done."<<std::endl;
#endif
}
}
return 0;
} catch (std::exception& x) {
std::cerr<<"**** ERROR: "<<x.what()<<std::endl;

Loading…
Cancel
Save