From: Aki Tuomi Date: Mon, 19 Oct 2015 11:44:42 +0000 (+0300) Subject: Allow token name instead of slot ID for PKCS#11 X-Git-Tag: dnsdist-1.0.0-alpha1~252^2~5^2~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=248d701fadaf247c90fd0e249a725787e07fc4e1;p=pdns Allow token name instead of slot ID for PKCS#11 --- diff --git a/pdns/dnssecinfra.cc b/pdns/dnssecinfra.cc index 566bb604e..f3403f294 100644 --- a/pdns/dnssecinfra.cc +++ b/pdns/dnssecinfra.cc @@ -72,8 +72,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/dynhandler.cc b/pdns/dynhandler.cc index 5e590f359..e581ac79d 100644 --- a/pdns/dynhandler.cc +++ b/pdns/dynhandler.cc @@ -372,7 +372,7 @@ string DLPolicy(const vector&parts, Utility::pid_t ppid) } #ifdef HAVE_P11KIT1 -extern bool PKCS11ModuleSlotLogin(const std::string& module, int slot, const std::string& pin); +extern bool PKCS11ModuleSlotLogin(const std::string& module, const string& tokenId, const std::string& pin); #endif string DLTokenLogin(const vector&parts, Utility::pid_t ppid) @@ -384,7 +384,7 @@ string DLTokenLogin(const vector&parts, Utility::pid_t ppid) return "invalid number of parameters, needs 4, got " + boost::lexical_cast(parts.size()); } - if (PKCS11ModuleSlotLogin(parts[1], boost::lexical_cast(parts[2]), parts[3])) { + if (PKCS11ModuleSlotLogin(parts[1], parts[2], parts[3])) { return "logged in"; } else { return "could not log in, check logs"; diff --git a/pdns/pdnssec.cc b/pdns/pdnssec.cc index 5d07969fd..e1585e001 100644 --- a/pdns/pdnssec.cc +++ b/pdns/pdnssec.cc @@ -2228,7 +2228,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 1b2e853bf..74622c608 100644 --- a/pdns/pkcs11signers.cc +++ b/pdns/pkcs11signers.cc @@ -211,6 +211,7 @@ class Pkcs11Slot { L< GetSlot(const std::string& module, const CK_SLOT_ID& slotId); + static std::shared_ptr 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 { @@ -615,17 +617,65 @@ class Pkcs11Token { return d_bits; } - static std::shared_ptr GetToken(const std::string& module, const CK_SLOT_ID& slotId, const std::string& label); + static std::shared_ptr GetToken(const std::string& module, const string& tokenId, const std::string& label); }; static std::map > pkcs11_slots; static std::map > pkcs11_tokens; -std::shared_ptr Pkcs11Slot::GetSlot(const std::string& module, const CK_SLOT_ID& slotId) { +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 sidx = module; sidx.append("|"); - sidx.append(boost::lexical_cast(slotId)); + sidx.append(tokenId); std::map >::iterator slotIter; CK_RV err; CK_FUNCTION_LIST* functions; @@ -645,15 +695,10 @@ std::shared_ptr Pkcs11Slot::GetSlot(const std::string& module, const // try to locate a slot _CK_SLOT_INFO info; - unsigned long 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<(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 @@ -662,17 +707,17 @@ std::shared_ptr Pkcs11Slot::GetSlot(const std::string& module, const return pkcs11_slots[sidx]; } -std::shared_ptr Pkcs11Token::GetToken(const std::string& module, const CK_SLOT_ID& slotId, const std::string& label) { +std::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(slotId)); + 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; - std::shared_ptr slot = Pkcs11Slot::GetSlot(module, slotId); + std::shared_ptr slot = Pkcs11Slot::GetSlot(module, tokenId); pkcs11_tokens[tidx] = std::make_shared(slot, label); return pkcs11_tokens[tidx]; } @@ -690,10 +735,10 @@ Pkcs11Token::Pkcs11Token(const std::shared_ptr& slot, const std::str Pkcs11Token::~Pkcs11Token() { } -bool PKCS11ModuleSlotLogin(const std::string& module, int slotId, const std::string& pin) +bool PKCS11ModuleSlotLogin(const std::string& module, const string& tokenId, const std::string& pin) { std::shared_ptr slot; - slot = Pkcs11Slot::GetSlot(module, slotId); + slot = Pkcs11Slot::GetSlot(module, tokenId); if (slot->LoggedIn()) return true; // no point failing return slot->Login(pin); } @@ -896,7 +941,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 148d5ef72..c08a6fa38 100644 --- a/pdns/pkcs11signers.hh +++ b/pdns/pkcs11signers.hh @@ -5,7 +5,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; @@ -44,6 +44,6 @@ class PKCS11DNSCryptoKeyEngine : public DNSCryptoKeyEngine static DNSCryptoKeyEngine* maker(unsigned int algorithm); }; -bool PKCS11ModuleSlotLogin(const std::string& module, int slot, const std::string& pin); +bool PKCS11ModuleSlotLogin(const std::string& module, const string& tokenId, const std::string& pin); #endif /* PDNS_PKCS11SIGNERS_HH */