blob: 58bcc26c7c489e2640a26afcb89dba17fba6466e [file] [log] [blame]
Yingdi Yu3715f8d2014-01-30 00:32:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu3715f8d2014-01-30 00:32:20 -080013 */
14
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080015#include "common.hpp"
16
Yingdi Yu3715f8d2014-01-30 00:32:20 -080017#include "sec-rule-specific.hpp"
18#include "signature-sha256-with-rsa.hpp"
19
Yingdi Yu3715f8d2014-01-30 00:32:20 -080020using namespace std;
21
Yingdi Yufc40d872014-02-18 12:56:04 -080022namespace ndn {
Yingdi Yu3715f8d2014-01-30 00:32:20 -080023
24SecRuleSpecific::SecRuleSpecific(shared_ptr<Regex> dataRegex,
25 shared_ptr<Regex> signerRegex)
26 : SecRule(true)
27 , m_dataRegex(dataRegex)
28 , m_signerRegex(signerRegex)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070029{
30}
Yingdi Yu3715f8d2014-01-30 00:32:20 -080031
32SecRuleSpecific::SecRuleSpecific(const SecRuleSpecific& rule)
33 : SecRule(true)
34 , m_dataRegex(rule.m_dataRegex)
35 , m_signerRegex(rule.m_signerRegex)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070036{
37}
Yingdi Yu3715f8d2014-01-30 00:32:20 -080038
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070039bool
Yingdi Yu3715f8d2014-01-30 00:32:20 -080040SecRuleSpecific::matchDataName(const Data& data)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070041{
42 return m_dataRegex->match(data.getName());
43}
Yingdi Yu3715f8d2014-01-30 00:32:20 -080044
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045bool
Yingdi Yu3715f8d2014-01-30 00:32:20 -080046SecRuleSpecific::matchSignerName(const Data& data)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070047{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070048 try
49 {
50 SignatureSha256WithRsa sig(data.getSignature());
51 Name signerName = sig.getKeyLocator().getName();
52 return m_signerRegex->match(signerName);
53 }
54 catch (std::runtime_error& e)
55 {
56 return false;
57 }
Yingdi Yu3715f8d2014-01-30 00:32:20 -080058}
59
60bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061SecRuleSpecific::satisfy(const Data& data)
62{
63 return (matchDataName(data) && matchSignerName(data)) ? true : false;
Yingdi Yufc40d872014-02-18 12:56:04 -080064}
Yingdi Yu3715f8d2014-01-30 00:32:20 -080065
66bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067SecRuleSpecific::satisfy(const Name& dataName, const Name& signerName)
68{
69 return (m_dataRegex->match(dataName) && m_signerRegex->match(signerName));
Yingdi Yu3715f8d2014-01-30 00:32:20 -080070}
Yingdi Yufc40d872014-02-18 12:56:04 -080071
72} // namespace ndn