Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 5 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 9 | #include <ndn-cpp/security/certificate/certificate-extension.hpp> |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 10 | #include <cryptopp/asn.h> |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 11 | |
| 12 | using namespace std; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 13 | using namespace CryptoPP; |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 14 | |
| 15 | namespace ndn { |
| 16 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 17 | void |
| 18 | CertificateExtension::encode(CryptoPP::BufferedTransformation &out) const |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 19 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 20 | // Extension ::= SEQUENCE { |
| 21 | // extnID OBJECT IDENTIFIER, |
| 22 | // critical BOOLEAN DEFAULT FALSE, |
| 23 | // extnValue OCTET STRING } |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 25 | DERSequenceEncoder extension(out); |
| 26 | { |
| 27 | extensionId_.encode(extension); |
| 28 | DEREncodeUnsigned(extension, isCritical_, BOOLEAN); |
| 29 | DEREncodeOctetString(extension, extensionValue_.buf(), extensionValue_.size()); |
| 30 | } |
| 31 | extension.MessageEnd(); |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 34 | void |
| 35 | CertificateExtension::decode(CryptoPP::BufferedTransformation &in) |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 36 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 37 | // Extension ::= SEQUENCE { |
| 38 | // extnID OBJECT IDENTIFIER, |
| 39 | // critical BOOLEAN DEFAULT FALSE, |
| 40 | // extnValue OCTET STRING } |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 42 | BERSequenceDecoder extension(in); |
| 43 | { |
| 44 | extensionId_.decode(extension); |
| 45 | BERDecodeUnsigned(extension, isCritical_, BOOLEAN); |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 46 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 47 | // the extra copy operation can be optimized, but not trivial, |
| 48 | // since the length is not known in advance |
| 49 | SecByteBlock tmpBlock; |
| 50 | BERDecodeOctetString(extension, tmpBlock); |
| 51 | extensionValue_.assign(tmpBlock.begin(), tmpBlock.end()); |
| 52 | } |
| 53 | extension.MessageEnd(); |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 54 | } |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 55 | |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 56 | } |