blob: 26dcec8fbaf28d13eed3b744ce69d01021d74879 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47c93cf2013-08-09 00:38:48 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompsonba16b8f2013-12-16 13:11:47 -08004 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
Jeff Thompson7687dc02013-09-13 11:54:07 -07005 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47c93cf2013-08-09 00:38:48 -07006 * See COPYING for copyright and distribution information.
7 */
8
Jeff Thompson9296f0c2013-09-23 18:10:27 -07009#include "../util/logging.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070010#include <ndn-cpp/security/security-exception.hpp>
11#include <ndn-cpp/security/policy/policy-manager.hpp>
12#include <ndn-cpp/security/key-chain.hpp>
Jeff Thompson47c93cf2013-08-09 00:38:48 -070013
14using namespace std;
Jeff Thompson2381da82013-11-06 14:34:09 -080015using namespace ndn::func_lib;
Jeff Thompson9bdeb6d2013-11-06 14:30:07 -080016#if NDN_CPP_HAVE_STD_FUNCTION
Jeff Thompson09324ed2013-11-06 14:40:35 -080017// In the std library, the placeholders are in a different namespace than boost.
18using namespace ndn::func_lib::placeholders;
Jeff Thompson9bdeb6d2013-11-06 14:30:07 -080019#endif
Jeff Thompson47c93cf2013-08-09 00:38:48 -070020
21namespace ndn {
Jeff Thompson15966392013-12-04 17:37:17 -080022
Jeff Thompsonce115762013-12-18 14:59:56 -080023KeyChain::KeyChain(const ptr_lib::shared_ptr<IdentityManager>& identityManager, const ptr_lib::shared_ptr<PolicyManager>& policyManager)
Jeff Thompson29ce3102013-09-27 11:47:48 -070024: identityManager_(identityManager), policyManager_(policyManager), face_(0), maxSteps_(100)
Jeff Thompson9296f0c2013-09-23 18:10:27 -070025{
26}
27
Jeff Thompson2ce8f492013-09-17 18:01:25 -070028void
Jeff Thompson29ce3102013-09-27 11:47:48 -070029KeyChain::sign(Data& data, const Name& certificateName, WireFormat& wireFormat)
Jeff Thompson2ce8f492013-09-17 18:01:25 -070030{
Jeff Thompson29ce3102013-09-27 11:47:48 -070031 identityManager_->signByCertificate(data, certificateName, wireFormat);
32}
33
Jeff Thompsonce115762013-12-18 14:59:56 -080034ptr_lib::shared_ptr<Signature>
Jeff Thompsonc01e1782013-10-21 14:08:42 -070035KeyChain::sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName)
36{
37 return identityManager_->signByCertificate(buffer, bufferLength, certificateName);
38}
39
Jeff Thompson29ce3102013-09-27 11:47:48 -070040void
41KeyChain::signByIdentity(Data& data, const Name& identityName, WireFormat& wireFormat)
42{
43 Name signingCertificateName;
Jeff Thompson9296f0c2013-09-23 18:10:27 -070044
Jeff Thompson29ce3102013-09-27 11:47:48 -070045 if (identityName.getComponentCount() == 0) {
46 Name inferredIdentity = policyManager_->inferSigningIdentity(data.getName());
47 if (inferredIdentity.getComponentCount() == 0)
48 signingCertificateName = identityManager_->getDefaultCertificateName();
49 else
50 signingCertificateName = identityManager_->getDefaultCertificateNameForIdentity(inferredIdentity);
Jeff Thompson2ce8f492013-09-17 18:01:25 -070051 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -070052 else
Jeff Thompson29ce3102013-09-27 11:47:48 -070053 signingCertificateName = identityManager_->getDefaultCertificateNameForIdentity(identityName);
54
55 if (signingCertificateName.getComponentCount() == 0)
56 throw SecurityException("No qualified certificate name found!");
57
58 if (!policyManager_->checkSigningPolicy(data.getName(), signingCertificateName))
Jeff Thompson9296f0c2013-09-23 18:10:27 -070059 throw SecurityException("Signing Cert name does not comply with signing policy");
Jeff Thompson29ce3102013-09-27 11:47:48 -070060
Jeff Thompson56e62652013-10-31 16:13:25 -070061 identityManager_->signByCertificate(data, signingCertificateName, wireFormat);
Jeff Thompson2ce8f492013-09-17 18:01:25 -070062}
63
Jeff Thompsonce115762013-12-18 14:59:56 -080064ptr_lib::shared_ptr<Signature>
Jeff Thompsone9ffe792013-10-22 10:58:48 -070065KeyChain::signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName)
66{
67 Name signingCertificateName = identityManager_->getDefaultCertificateNameForIdentity(identityName);
Jeff Thompsonc01e1782013-10-21 14:08:42 -070068
Jeff Thompsone9ffe792013-10-22 10:58:48 -070069 if (signingCertificateName.size() == 0)
70 throw SecurityException("No qualified certificate name found!");
Jeff Thompsonc01e1782013-10-21 14:08:42 -070071
Jeff Thompsone9ffe792013-10-22 10:58:48 -070072 return identityManager_->signByCertificate(buffer, bufferLength, signingCertificateName);
73}
Jeff Thompsonc01e1782013-10-21 14:08:42 -070074
Jeff Thompson2ce8f492013-09-17 18:01:25 -070075void
Jeff Thompson7c5d2312013-09-25 16:07:15 -070076KeyChain::verifyData
Jeff Thompsonce115762013-12-18 14:59:56 -080077 (const ptr_lib::shared_ptr<Data>& data, const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed, int stepCount)
Jeff Thompson2ce8f492013-09-17 18:01:25 -070078{
Jeff Thompson2ce8f492013-09-17 18:01:25 -070079 _LOG_TRACE("Enter Verify");
Jeff Thompson2ce8f492013-09-17 18:01:25 -070080
Jeff Thompsonf309aa62013-10-31 17:03:54 -070081 if (policyManager_->requireVerify(*data)) {
Jeff Thompsonce115762013-12-18 14:59:56 -080082 ptr_lib::shared_ptr<ValidationRequest> nextStep = policyManager_->checkVerificationPolicy
Jeff Thompsonf309aa62013-10-31 17:03:54 -070083 (data, stepCount, onVerified, onVerifyFailed);
Jeff Thompsoncda349e2013-11-05 17:37:39 -080084 if (nextStep)
85 face_->expressInterest
86 (*nextStep->interest_,
87 bind(&KeyChain::onCertificateData, this, _1, _2, nextStep),
88 bind(&KeyChain::onCertificateInterestTimeout, this, _1, nextStep->retry_, onVerifyFailed, data, nextStep));
Jeff Thompsonf309aa62013-10-31 17:03:54 -070089 }
90 else if (policyManager_->skipVerifyAndTrust(*data))
Jeff Thompson2ce8f492013-09-17 18:01:25 -070091 onVerified(data);
92 else
Jeff Thompson29ce3102013-09-27 11:47:48 -070093 onVerifyFailed(data);
Jeff Thompson1e90d8c2013-08-12 16:09:25 -070094}
95
Jeff Thompsoncda349e2013-11-05 17:37:39 -080096void
Jeff Thompsonce115762013-12-18 14:59:56 -080097KeyChain::onCertificateData(const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &data, ptr_lib::shared_ptr<ValidationRequest> nextStep)
Jeff Thompsoncda349e2013-11-05 17:37:39 -080098{
99 // Try to verify the certificate (data) according to the parameters in nextStep.
100 verifyData(data, nextStep->onVerified_, nextStep->onVerifyFailed_, nextStep->stepCount_);
101}
102
103void
104KeyChain::onCertificateInterestTimeout
Jeff Thompsonce115762013-12-18 14:59:56 -0800105 (const ptr_lib::shared_ptr<const Interest> &interest, int retry, const OnVerifyFailed& onVerifyFailed, const ptr_lib::shared_ptr<Data> &data,
106 ptr_lib::shared_ptr<ValidationRequest> nextStep)
Jeff Thompsoncda349e2013-11-05 17:37:39 -0800107{
108 if (retry > 0)
109 // Issue the same expressInterest as in verifyData except decrement retry.
110 face_->expressInterest
111 (*interest,
112 bind(&KeyChain::onCertificateData, this, _1, _2, nextStep),
113 bind(&KeyChain::onCertificateInterestTimeout, this, _1, retry - 1, onVerifyFailed, data, nextStep));
114 else
115 onVerifyFailed(data);
116}
117
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700118}