blob: 8acda6efb916665c3559f6a607ffc52de5525f0c [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080020 */
21
Yingdi Yufc40d872014-02-18 12:56:04 -080022#ifndef NDN_SECURITY_SIGNATURE_SHA256_WITH_RSA_HPP
23#define NDN_SECURITY_SIGNATURE_SHA256_WITH_RSA_HPP
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080024
Yingdi Yu4f324632014-01-15 18:10:03 -080025#include "../data.hpp"
Alexander Afanasyevc348f832014-02-17 16:35:17 -080026#include "../encoding/tlv.hpp"
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080027
28namespace ndn {
29
30/**
31 * Representing of SHA256-with-RSA signature in a data packet.
32 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070033class SignatureSha256WithRsa : public Signature
34{
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080035public:
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070036 class Error : public Signature::Error
37 {
38 public:
39 explicit
40 Error(const std::string& what)
41 : Signature::Error(what)
42 {
43 }
44 };
45
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080046 SignatureSha256WithRsa()
47 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070048 m_info = Block(Tlv::SignatureInfo);
49
50 m_type = Signature::Sha256WithRsa;
51 m_info.push_back(nonNegativeIntegerBlock(Tlv::SignatureType, Tlv::SignatureSha256WithRsa));
52 m_info.push_back(m_keyLocator.wireEncode());
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080053 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070054
55 SignatureSha256WithRsa(const Signature& signature)
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080056 : Signature(signature)
57 {
58 if (getType() != Signature::Sha256WithRsa)
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070059 throw Error("Incorrect signature type");
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080060
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070061 m_info.parse();
62 Block::element_const_iterator i = m_info.find(Tlv::KeyLocator);
63 if (i != m_info.elements_end())
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080064 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070065 m_keyLocator.wireDecode(*i);
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080066 }
67 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070068
69 const KeyLocator&
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080070 getKeyLocator() const
71 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070072 return m_keyLocator;
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080073 }
74
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070075 void
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080076 setKeyLocator(const KeyLocator& keyLocator)
77 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070078 m_keyLocator = keyLocator;
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080079
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070080 m_info.remove(ndn::Tlv::KeyLocator);
81 m_info.push_back(m_keyLocator.wireEncode());
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080082 }
83
84private:
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070085 KeyLocator m_keyLocator;
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080086};
87
88} // namespace ndn
89
Yingdi Yufc40d872014-02-18 12:56:04 -080090#endif //NDN_SECURITY_SIGNATURE_SHA256_WITH_RSA_HPP