key-locator+signature: Extensions and corrections

Change-Id: Ib17804874d7ac69feeac7972fa4e266d5ae61ad0
diff --git a/include/ndn-cpp/key-locator.hpp b/include/ndn-cpp/key-locator.hpp
index 3f53ef4..57b5f4b 100644
--- a/include/ndn-cpp/key-locator.hpp
+++ b/include/ndn-cpp/key-locator.hpp
@@ -23,12 +23,16 @@
     
     KeyLocator_Unknown = 255
   };
-  
+
+  inline
   KeyLocator()
     : type_(KeyLocator_None)
   {
   }
 
+  inline
+  KeyLocator(const Name &name);
+
   inline const Block& 
   wireEncode() const;
 
@@ -62,6 +66,12 @@
   mutable Block wire_;
 };
 
+inline
+KeyLocator::KeyLocator(const Name &name)
+{
+  setName(name);
+}
+
 inline const Block& 
 KeyLocator::wireEncode() const
 {
diff --git a/include/ndn-cpp/security/signature/signature-sha256-with-rsa.hpp b/include/ndn-cpp/security/signature/signature-sha256-with-rsa.hpp
new file mode 100644
index 0000000..a24e94b
--- /dev/null
+++ b/include/ndn-cpp/security/signature/signature-sha256-with-rsa.hpp
@@ -0,0 +1,63 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * @author: Jeff Thompson <jefft0@remap.ucla.edu>
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_SIGNATURE_SHA256_WITH_RSA_HPP
+#define NDN_SIGNATURE_SHA256_WITH_RSA_HPP
+
+#include "../../data.hpp"
+
+namespace ndn {
+
+/**
+ * Representing of SHA256-with-RSA signature in a data packet.
+ */
+class SignatureSha256WithRsa : public Signature {
+public:
+  SignatureSha256WithRsa()
+  {
+    info_ = Block(Tlv::SignatureInfo);
+    
+    type_ = Signature::Sha256WithRsa;
+    info_.push_back(nonNegativeIntegerBlock(Tlv::SignatureType, Tlv::SignatureSha256WithRsa));
+  }
+  
+  SignatureSha256WithRsa(const Signature &signature)
+    : Signature(signature)
+  {
+    if (getType() != Signature::Sha256WithRsa)
+      throw Signature::Error("Incorrect signature type");
+
+    info_.parse();
+    Block::element_iterator i = info_.find(Tlv::KeyLocator);
+    if (i != info_.getAll().end())
+      {
+        keyLocator_.wireDecode(*i);
+      }
+  }
+  
+  const KeyLocator& 
+  getKeyLocator() const
+  {
+    return keyLocator_;
+  }
+
+  void 
+  setKeyLocator(const KeyLocator& keyLocator)
+  {
+    keyLocator_ = keyLocator;
+
+    /// @todo Ensure that keylocator does not exist
+    info_.push_back(keyLocator_.wireEncode());
+  }
+
+private:
+  KeyLocator keyLocator_;
+};
+
+} // namespace ndn
+
+#endif
diff --git a/include/ndn-cpp/signature.hpp b/include/ndn-cpp/signature.hpp
index c6d914c..e45dbd6 100644
--- a/include/ndn-cpp/signature.hpp
+++ b/include/ndn-cpp/signature.hpp
@@ -15,9 +15,11 @@
  */
 class Signature {
 public:
+  struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
+
   enum {
-    DigestSha256 = 0,
-    SignatureSha256WithRsa = 1
+    Sha256 = 0,
+    Sha256WithRsa = 1
   };
   
   Signature()
@@ -49,6 +51,7 @@
   const Block&
   getInfo() const
   {
+    info_.encode(); // will do nothing if wire already exists
     return info_;
   }
 
@@ -73,6 +76,7 @@
   const Block&
   getValue() const
   {
+    value_.encode(); // will do nothing if wire already exists
     return value_;
   }
 
@@ -90,11 +94,11 @@
     value_ = Block();
   }
 
-private:
+protected:
   int32_t type_;
   
-  Block info_;
-  Block value_;
+  mutable Block info_;
+  mutable Block value_;
 };
 
 } // namespace ndn