security: provide getKeyLocatorName helper function

refs #3920

Change-Id: Iba8fa7776ca63445608f4eb6fa2e4c54307cc99f
diff --git a/src/security/v2/validation-policy-simple-hierarchy.cpp b/src/security/v2/validation-policy-simple-hierarchy.cpp
index ee1ad94..0ad6128 100644
--- a/src/security/v2/validation-policy-simple-hierarchy.cpp
+++ b/src/security/v2/validation-policy-simple-hierarchy.cpp
@@ -29,19 +29,17 @@
 ValidationPolicySimpleHierarchy::checkPolicy(const Data& data, const shared_ptr<ValidationState>& state,
                                              const ValidationContinuation& continueValidation)
 {
-  if (!data.getSignature().hasKeyLocator()) {
-    return state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Required key locator is missing"});
+  Name klName = getKeyLocatorName(data, *state);
+  if (!state->getOutcome()) { // already failed
+    return;
   }
-  const KeyLocator& locator = data.getSignature().getKeyLocator();
-  if (locator.getType() != KeyLocator::KeyLocator_Name) {
-    return state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Key locator not Name"});
-  }
-  if (locator.getName().getPrefix(-2).isPrefixOf(data.getName())) {
-    continueValidation(make_shared<CertificateRequest>(Interest(locator.getName())), state);
+
+  if (klName.getPrefix(-2).isPrefixOf(data.getName())) {
+    continueValidation(make_shared<CertificateRequest>(Interest(klName)), state);
   }
   else {
     state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Data signing policy violation for " +
-                 data.getName().toUri() + " by " + locator.getName().toUri()});
+                 data.getName().toUri() + " by " + klName.toUri()});
   }
 }
 
@@ -49,27 +47,17 @@
 ValidationPolicySimpleHierarchy::checkPolicy(const Interest& interest, const shared_ptr<ValidationState>& state,
                                              const ValidationContinuation& continueValidation)
 {
-  SignatureInfo info;
-  try {
-    info.wireDecode(interest.getName().at(signed_interest::POS_SIG_INFO).blockFromValue());
+  Name klName = getKeyLocatorName(interest, *state);
+  if (!state->getOutcome()) { // already failed
+    return;
   }
-  catch (const tlv::Error& e) {
-    return state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Invalid signed interest (" +
-                        std::string(e.what()) + ")"});
-  }
-  if (!info.hasKeyLocator()) {
-    return state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Required key locator is missing"});
-  }
-  const KeyLocator& locator = info.getKeyLocator();
-  if (locator.getType() != KeyLocator::KeyLocator_Name) {
-    return state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Key locator not Name"});
-  }
-  if (locator.getName().getPrefix(-2).isPrefixOf(interest.getName())) {
-    continueValidation(make_shared<CertificateRequest>(Interest(locator.getName())), state);
+
+  if (klName.getPrefix(-2).isPrefixOf(interest.getName())) {
+    continueValidation(make_shared<CertificateRequest>(Interest(klName)), state);
   }
   else {
     state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Interest signing policy violation for " +
-                 interest.getName().toUri() + " by " + locator.getName().toUri()});
+                 interest.getName().toUri() + " by " + klName.toUri()});
   }
 }