From 5b1020aabd736fd4b06695f260aee07adcae783f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Fri, 27 Sep 2013 14:36:44 +0000 Subject: [PATCH] try to also support C++ < 201103; refs #27 --- doc/examples/cryptoki-sign-demo.cxx | 183 +++++++++++++--------------- 1 file changed, 82 insertions(+), 101 deletions(-) diff --git a/doc/examples/cryptoki-sign-demo.cxx b/doc/examples/cryptoki-sign-demo.cxx index 4437fe6..4827995 100644 --- a/doc/examples/cryptoki-sign-demo.cxx +++ b/doc/examples/cryptoki-sign-demo.cxx @@ -5,6 +5,10 @@ // 1 2 3 4 5 6 7 8 // 45678901234567890123456789012345678901234567890123456789012345678901234567890 +#include +#include +#include + #include #include #include @@ -15,116 +19,93 @@ #include #include #include -#include -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 > - 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, " of repetitions")}, - {"-t", Args::mapped_type(0, 0, &txt, "same as --text")}, - {"--text", Args::mapped_type(0, 0, &txt, " with text to sign")}, - {"-l", Args::mapped_type(&lib, 0, 0, "same as --library")}, - {"--library", Args::mapped_type(&lib, 0, 0, " cryptoki to load")}, - {"-s", Args::mapped_type(&slot, 0, 0, "same as --slot")}, - {"--slot", Args::mapped_type(&slot, 0, 0, " of slot")}, - {"-c", Args::mapped_type(&cert, 0, 0, "same as --certificae")}, - {"--certificate", Args::mapped_type(&cert, 0, 0, " 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 -}; - int main(int argc, char** argv) try { - for (auto arg(argv+1); arg(it->second)!=0 || std::get<1>(it->second)!=0 || - std::get<2>(it->second)!=0) - && arg+1(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(t), - std::istreambuf_iterator()); - } - } else { // argument type 0 or wrong parameter displays help - std::cerr<<"SYNOPSIS"<(v.second) - <slotinfo()); + if (slot.size()&&slot!=si.slotDescription) continue; + std::cout<<"Found Slot: "<tokeninfo()); + std::cout<<"Found token: "<(CKO_CERTIFICATE))); + 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(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)); - cryptoki::ObjectList keys - (session.find(cryptoki::Attribute(CKA_CLASS) - .from(CKO_PUBLIC_KEY), - id)); - if (!keys.size()) return; - std::cout<<"Found Certificate: " - <>pin; - cryptoki::Session::Login l(session, pin); - keys = session.find(cryptoki::Attribute(CKA_CLASS) - .from(CKO_PRIVATE_KEY), - id); - if (keys.size()!=1) { - std::cerr<<"**** ERROR: No private key: "<(end - start); - std::cout<<"Done in "<(CKO_PUBLIC_KEY), + id)); + if (!keys.size()) continue; + std::cout<<"Found Certificate: " + <attribute(CKA_LABEL).value<>pin; + cryptoki::Session::Login l(session, pin); + keys = session.find(cryptoki::Attribute(CKA_CLASS) + .from(CKO_PRIVATE_KEY), + id); + if (keys.size()!=1) { + std::cerr<<"**** ERROR: No private key: "< + (end-start)); + std::cout<<"Done in "<