From c5e45463829c76b16cb832560e7e60917af35878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Tue, 24 Sep 2013 12:05:22 +0000 Subject: [PATCH] add example for signing; refs #27 --- doc/examples/cryptoki-sign-demo.cxx | 132 ++++++++++++++++++++++++++++ doc/examples/makefile.am | 13 ++- 2 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 doc/examples/cryptoki-sign-demo.cxx diff --git a/doc/examples/cryptoki-sign-demo.cxx b/doc/examples/cryptoki-sign-demo.cxx new file mode 100644 index 0000000..4437fe6 --- /dev/null +++ b/doc/examples/cryptoki-sign-demo.cxx @@ -0,0 +1,132 @@ +/*! @file + + @id $Id$ +*/ +// 1 2 3 4 5 6 7 8 +// 45678901234567890123456789012345678901234567890123456789012345678901234567890 + +#include +#include +#include +#include +#include +#include +#include +#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) + <(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 "<