security: CryptoPP functions are used directly to encode/decode DER/BER

This change eliminates the need for custom der decoder/encoder.

Change-Id: I5be2e55cec2b63157927a4ad87fffe8e8651ed3c
diff --git a/src/security/certificate/certificate-subject-description.cpp b/src/security/certificate/certificate-subject-description.cpp
index 3571f7f..472b30e 100644
--- a/src/security/certificate/certificate-subject-description.cpp
+++ b/src/security/certificate/certificate-subject-description.cpp
@@ -6,25 +6,58 @@
  * See COPYING for copyright and distribution information.
  */
 
-#include "../../encoding/der/der.hpp"
 #include <ndn-cpp/security/certificate/certificate-subject-description.hpp>
+#include <cryptopp/asn.h>
 
 using namespace std;
+using namespace CryptoPP;
 
 namespace ndn {
 
-ptr_lib::shared_ptr<der::DerNode> 
-CertificateSubjectDescription::toDer() const
+void
+CertificateSubjectDescription::encode(CryptoPP::BufferedTransformation &out) const
 {
-  ptr_lib::shared_ptr<der::DerSequence> root(new der::DerSequence());
+  // RelativeDistinguishedName ::=
+  //     SET OF AttributeTypeAndValue
+  // 
+  // AttributeTypeAndValue ::= SEQUENCE {
+  //     type     AttributeType,
+  //     value    AttributeValue   }
+  // 
+  // AttributeType ::= OBJECT IDENTIFIER
+  // 
+  // AttributeValue ::= ANY DEFINED BY AttributeType
+  DERSequenceEncoder attributeTypeAndValue(out);
+  {
+    oid_.encode(attributeTypeAndValue);
+    DEREncodeTextString(attributeTypeAndValue, value_, PRINTABLE_STRING);
+  }
+  attributeTypeAndValue.MessageEnd();
+}
 
-  ptr_lib::shared_ptr<der::DerOid> oid(new der::DerOid(oid_));
-  ptr_lib::shared_ptr<der::DerPrintableString> value(new der::DerPrintableString(value_));
+void
+CertificateSubjectDescription::decode(CryptoPP::BufferedTransformation &in)
+{
+  // RelativeDistinguishedName ::=
+  //     SET OF AttributeTypeAndValue
+  // 
+  // AttributeTypeAndValue ::= SEQUENCE {
+  //     type     AttributeType,
+  //     value    AttributeValue   }
+  // 
+  // AttributeType ::= OBJECT IDENTIFIER
+  // 
+  // AttributeValue ::= ANY DEFINED BY AttributeType
 
-  root->addChild(oid);
-  root->addChild(value);
+  BERSequenceDecoder attributeTypeAndValue(in);
+  {
+    oid_.decode(attributeTypeAndValue);
 
-  return root;
+    /// @todo May be add more intelligent processing, since the following
+    ///       may fail if somebody encoded attribute that uses non PRINTABLE_STRING as value
+    BERDecodeTextString(attributeTypeAndValue, value_, PRINTABLE_STRING);
+  }
+  attributeTypeAndValue.MessageEnd();
 }
 
 }