blob: db1d91041b9b29918342e09a8224dc82b0cef6c8 [file] [log] [blame]
Yingdi Yu89da3052013-10-26 15:08:37 -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 Yu42f66462013-10-31 17:38:22 -070011#include "chat-policy-rule.h"
Yingdi Yu64206112013-12-24 11:16:32 +080012#include <ndn-cpp/sha256-with-rsa-signature.hpp>
Yingdi Yu89da3052013-10-26 15:08:37 -070013
14using namespace ndn;
15using namespace std;
Yingdi Yu64206112013-12-24 11:16:32 +080016using namespace ndn::ptr_lib;
Yingdi Yu89da3052013-10-26 15:08:37 -070017
18
Yingdi Yu64206112013-12-24 11:16:32 +080019ChatPolicyRule::ChatPolicyRule(shared_ptr<Regex> dataRegex,
20 shared_ptr<Regex> signerRegex)
Yingdi Yu42f66462013-10-31 17:38:22 -070021 : PolicyRule(PolicyRule::IDENTITY_POLICY, true)
Yingdi Yu89da3052013-10-26 15:08:37 -070022 , m_dataRegex(dataRegex)
23 , m_signerRegex(signerRegex)
Yingdi Yu42f66462013-10-31 17:38:22 -070024{}
25
26ChatPolicyRule::ChatPolicyRule(const ChatPolicyRule& rule)
27 : PolicyRule(PolicyRule::IDENTITY_POLICY, true)
28 , m_dataRegex(rule.m_dataRegex)
29 , m_signerRegex(rule.m_signerRegex)
Yingdi Yu89da3052013-10-26 15:08:37 -070030{}
31
32bool
33ChatPolicyRule::matchDataName(const Data & data)
Yingdi Yu42f66462013-10-31 17:38:22 -070034{ return m_dataRegex->match(data.getName()); }
Yingdi Yu89da3052013-10-26 15:08:37 -070035
36bool
37ChatPolicyRule::matchSignerName(const Data & data)
38{
Yingdi Yu64206112013-12-24 11:16:32 +080039 const Sha256WithRsaSignature* sigPtr = dynamic_cast<const Sha256WithRsaSignature*> (data.getSignature());
Yingdi Yub35b8652013-11-07 11:32:40 -080040
Yingdi Yu64206112013-12-24 11:16:32 +080041 if(NULL == sigPtr)
Yingdi Yub35b8652013-11-07 11:32:40 -080042 return false;
43
Yingdi Yu64206112013-12-24 11:16:32 +080044 if(ndn_KeyLocatorType_KEYNAME != sigPtr->getKeyLocator().getType())
Yingdi Yub35b8652013-11-07 11:32:40 -080045 return false;
46
Yingdi Yu89da3052013-10-26 15:08:37 -070047 Name signerName = sigPtr->getKeyLocator ().getKeyName ();
Yingdi Yu42f66462013-10-31 17:38:22 -070048 return m_signerRegex->match(signerName);
Yingdi Yu89da3052013-10-26 15:08:37 -070049}
50
51bool
52ChatPolicyRule::satisfy(const Data & data)
53{ return (matchDataName(data) && matchSignerName(data)) ? true : false ; }
54
55bool
56ChatPolicyRule::satisfy(const Name & dataName, const Name & signerName)
Yingdi Yu42f66462013-10-31 17:38:22 -070057{ return (m_dataRegex->match(dataName) && m_signerRegex->match(signerName)); }