From: Andrei Benea Date: Thu, 29 Oct 2015 13:57:56 +0000 (+0200) Subject: Allow token name instead of slot ID for PKCS#11 X-Git-Tag: auth-3.4.7~4^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=991090842478aa42533095e2dfcc2ec3664a1568;p=pdns Allow token name instead of slot ID for PKCS#11 --- diff --git a/pdns/dnssecinfra.cc b/pdns/dnssecinfra.cc index 29ef7dcb9..43842d1b8 100644 --- a/pdns/dnssecinfra.cc +++ b/pdns/dnssecinfra.cc @@ -60,8 +60,7 @@ DNSCryptoKeyEngine* DNSCryptoKeyEngine::makeFromISCString(DNSKEYRecordContent& d pkcs11=true; continue; } else if (pdns_iequals(key,"slot")) { - int slot = atoi(value.c_str()); - stormap["slot"]=lexical_cast(slot); + stormap["slot"]=value; continue; } else if (pdns_iequals(key,"label")) { stormap["label"]=value; diff --git a/pdns/pdnssec.cc b/pdns/pdnssec.cc index 4f4541f68..e5038a409 100644 --- a/pdns/pdnssec.cc +++ b/pdns/pdnssec.cc @@ -2034,7 +2034,7 @@ try std::vector keys; if (cmds.size() < 9) { - std::cout << "Usage: pdnssec hsm assign zone algorithm ksk|zsk module slot pin label" << std::endl; + std::cout << "Usage: pdnssec hsm assign zone algorithm ksk|zsk module token pin label" << std::endl; return 1; } diff --git a/pdns/pkcs11signers.cc b/pdns/pkcs11signers.cc index fb6622e74..d85b0d08e 100644 --- a/pdns/pkcs11signers.cc +++ b/pdns/pkcs11signers.cc @@ -213,6 +213,7 @@ class Pkcs11Slot { L< GetSlot(const std::string& module, const string& tokenId); + static CK_RV HuntSlot(const string& tokenId, CK_SLOT_ID &slotId, _CK_SLOT_INFO* info, CK_FUNCTION_LIST* functions); }; class Pkcs11Token { @@ -607,31 +611,72 @@ class Pkcs11Token { return d_bits; } - static boost::shared_ptr GetToken(const std::string& module, const CK_SLOT_ID& slotId, const std::string& label); + static boost::shared_ptr GetToken(const std::string& module, const string& tokenId, const std::string& label); }; static std::map > pkcs11_slots; static std::map > pkcs11_tokens; -boost::shared_ptr Pkcs11Token::GetToken(const std::string& module, const CK_SLOT_ID& slotId, const std::string& label) { +CK_RV Pkcs11Slot::HuntSlot(const string& tokenId, CK_SLOT_ID &slotId, _CK_SLOT_INFO* info, CK_FUNCTION_LIST* functions) +{ + CK_RV err; + unsigned long slots; + _CK_TOKEN_INFO tinfo; + + // go thru all slots + // this is required by certain tokens, otherwise C_GetSlotInfo will not return a token + err = functions->C_GetSlotList(CK_FALSE, NULL_PTR, &slots); + if (err) { + L< Pkcs11Slot::GetSlot(const std::string& module, const string& tokenId) { // see if we can find module - std::string tidx = module; - tidx.append("|"); - tidx.append(boost::lexical_cast(slotId)); - std::string sidx = tidx; - tidx.append("|"); - tidx.append(label); - std::map >::iterator tokenIter; + std::string sidx = module; + sidx.append("|"); + sidx.append(tokenId); std::map >::iterator slotIter; CK_RV err; CK_FUNCTION_LIST* functions; - if ((tokenIter = pkcs11_tokens.find(tidx)) != pkcs11_tokens.end()) return tokenIter->second; - // see if we have slot if ((slotIter = pkcs11_slots.find(sidx)) != pkcs11_slots.end()) { - pkcs11_tokens[tidx] = boost::make_shared(slotIter->second, label); - return pkcs11_tokens[tidx]; + return slotIter->second; } #ifdef HAVE_P11KIT1_V2 @@ -644,23 +689,30 @@ boost::shared_ptr Pkcs11Token::GetToken(const std::string& module, // try to locate a slot _CK_SLOT_INFO info; - unsigned long slots; + CK_SLOT_ID slotId; - // this is required by certain tokens, otherwise C_GetSlotInfo will not return a token - err = functions->C_GetSlotList(CK_FALSE, NULL_PTR, &slots); - if (err) - L<(slotId) + std::string(" on module ") + module + std::string(": error code ") + boost::lexical_cast(err)); + if ((err = Pkcs11Slot::HuntSlot(tokenId, slotId, &info, functions))) { + throw PDNSException(std::string("Cannot find PKCS#11 token ") + tokenId + std::string(" on module ") + module + std::string(": error code ") + boost::lexical_cast(err)); } // store slot pkcs11_slots[sidx] = boost::make_shared(functions, slotId); - // looks ok to me. - pkcs11_tokens[tidx] = boost::make_shared(pkcs11_slots[sidx], label); + return pkcs11_slots[sidx]; +} + +boost::shared_ptr Pkcs11Token::GetToken(const std::string& module, const string& tokenId, const std::string& label) { + // see if we can find module + std::string tidx = module; + tidx.append("|"); + tidx.append(boost::lexical_cast(tokenId)); + tidx.append("|"); + tidx.append(label); + std::map >::iterator tokenIter; + if ((tokenIter = pkcs11_tokens.find(tidx)) != pkcs11_tokens.end()) return tokenIter->second; + boost::shared_ptr slot = Pkcs11Slot::GetSlot(module, tokenId); + pkcs11_tokens[tidx] = boost::make_shared(slot, label); return pkcs11_tokens[tidx]; } @@ -677,6 +729,14 @@ Pkcs11Token::Pkcs11Token(const boost::shared_ptr& slot, const std::s Pkcs11Token::~Pkcs11Token() { } +bool PKCS11ModuleSlotLogin(const std::string& module, const string& tokenId, const std::string& pin) +{ + boost::shared_ptr slot; + slot = Pkcs11Slot::GetSlot(module, tokenId); + if (slot->LoggedIn()) return true; // no point failing + return slot->Login(pin); +} + PKCS11DNSCryptoKeyEngine::PKCS11DNSCryptoKeyEngine(unsigned int algorithm): DNSCryptoKeyEngine(algorithm) {} PKCS11DNSCryptoKeyEngine::~PKCS11DNSCryptoKeyEngine() {} PKCS11DNSCryptoKeyEngine::PKCS11DNSCryptoKeyEngine(const PKCS11DNSCryptoKeyEngine& orig) : DNSCryptoKeyEngine(orig.d_algorithm) {} @@ -875,7 +935,8 @@ DNSCryptoKeyEngine::storvector_t PKCS11DNSCryptoKeyEngine::convertToISCVector() void PKCS11DNSCryptoKeyEngine::fromISCMap(DNSKEYRecordContent& drc, stormap_t& stormap) { drc.d_algorithm = atoi(stormap["algorithm"].c_str()); d_module = stormap["engine"]; - d_slot_id = atoi(stormap["slot"].c_str()); + d_slot_id = stormap["slot"]; + boost::trim(d_slot_id); d_pin = stormap["pin"]; d_label = stormap["label"]; // validate parameters diff --git a/pdns/pkcs11signers.hh b/pdns/pkcs11signers.hh index be8ff6230..254bb10d9 100644 --- a/pdns/pkcs11signers.hh +++ b/pdns/pkcs11signers.hh @@ -2,7 +2,7 @@ class PKCS11DNSCryptoKeyEngine : public DNSCryptoKeyEngine { protected: std::string d_module; - unsigned long d_slot_id; + std::string d_slot_id; std::string d_pin; std::string d_label; @@ -41,3 +41,4 @@ class PKCS11DNSCryptoKeyEngine : public DNSCryptoKeyEngine static DNSCryptoKeyEngine* maker(unsigned int algorithm); }; +bool PKCS11ModuleSlotLogin(const std::string& module, const string& tokenId, const std::string& pin);