blob: 802ed4f006f985502e82a0d3198877adfd9e8256 [file] [log] [blame]
Yingdi Yu43e71612013-10-30 22:19:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080011#include "sec-rule-sync-specific.h"
12#include <ndn-cpp/security/signature-sha256-with-rsa.hpp>
Yingdi Yu43e71612013-10-30 22:19:31 -070013
14using namespace ndn;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080015using namespace ndn::ptr_lib;
Yingdi Yu43e71612013-10-30 22:19:31 -070016using namespace std;
Yingdi Yu43e71612013-10-30 22:19:31 -070017
18
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080019SecRuleSyncSpecific::SecRuleSyncSpecific(shared_ptr<Regex> dataRegex,
20 shared_ptr<Regex> signerRegex)
21 : SecRule(SecRule::IDENTITY_RULE, true)
Yingdi Yu43e71612013-10-30 22:19:31 -070022 , m_dataRegex(dataRegex)
23 , m_signerRegex(signerRegex)
24{}
25
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080026SecRuleSyncSpecific::SecRuleSyncSpecific(const SecRuleSyncSpecific& rule)
27 : SecRule(SecRule::IDENTITY_RULE, true)
Yingdi Yu43e71612013-10-30 22:19:31 -070028 , m_dataRegex(rule.m_dataRegex)
29 , m_signerRegex(rule.m_signerRegex)
30{}
31
32bool
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080033SecRuleSyncSpecific::matchDataName(const Data& data)
Yingdi Yu43e71612013-10-30 22:19:31 -070034{ return m_dataRegex->match(data.getName()); }
35
36bool
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080037SecRuleSyncSpecific::matchSignerName(const Data& data)
Yingdi Yu43e71612013-10-30 22:19:31 -070038{
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080039 try{
40 SignatureSha256WithRsa sig(data.getSignature());
41 Name signerName = sig.getKeyLocator().getName ();
42 return m_signerRegex->match(signerName);
43 }catch(SignatureSha256WithRsa::Error &e){
44 return false;
45 }catch(KeyLocator::Error &e){
46 return false;
47 }
Yingdi Yu43e71612013-10-30 22:19:31 -070048}
49
50bool
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080051SecRuleSyncSpecific::satisfy(const Data & data)
Yingdi Yu43e71612013-10-30 22:19:31 -070052{ return (matchDataName(data) && matchSignerName(data)) ? true : false ; }
53
54bool
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080055SecRuleSyncSpecific::satisfy(const Name & dataName, const Name & signerName)
Yingdi Yu43e71612013-10-30 22:19:31 -070056{ return (m_dataRegex->match(dataName) && m_signerRegex->match(signerName)); }