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

Loading…
Cancel
Save