blob: 935085021cb14edf0b364ca1c8198e171f25eeb9 [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.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47c93cf2013-08-09 00:38:48 -07005 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompson7a67cb62013-08-26 11:43:18 -07008#include "../c/util/crypto.h"
9#include "../c/encoding/binary-xml-data.h"
10#include "../encoding/binary-xml-encoder.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070011#include <ndn-cpp/sha256-with-rsa-signature.hpp>
Jeff Thompson9296f0c2013-09-23 18:10:27 -070012#include "../util/logging.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070013#include <ndn-cpp/security/security-exception.hpp>
14#include <ndn-cpp/security/policy/policy-manager.hpp>
Jeff Thompsonf309aa62013-10-31 17:03:54 -070015#include "policy/validation-request.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070016#include <ndn-cpp/security/key-chain.hpp>
Jeff Thompson47c93cf2013-08-09 00:38:48 -070017
18using namespace std;
Jeff Thompsond4144fe2013-09-18 15:59:57 -070019using namespace ndn::ptr_lib;
Jeff Thompson47c93cf2013-08-09 00:38:48 -070020
21namespace ndn {
22
Jeff Thompson2ce8f492013-09-17 18:01:25 -070023#if 1
Jeff Thompson10ad12a2013-09-24 16:19:11 -070024static uint8_t DEFAULT_PUBLIC_KEY_DER[] = {
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700250x30, 0x81, 0x9F, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81,
260x8D, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xE1, 0x7D, 0x30, 0xA7, 0xD8, 0x28, 0xAB, 0x1B, 0x84, 0x0B, 0x17,
270x54, 0x2D, 0xCA, 0xF6, 0x20, 0x7A, 0xFD, 0x22, 0x1E, 0x08, 0x6B, 0x2A, 0x60, 0xD1, 0x6C, 0xB7, 0xF5, 0x44, 0x48, 0xBA,
280x9F, 0x3F, 0x08, 0xBC, 0xD0, 0x99, 0xDB, 0x21, 0xDD, 0x16, 0x2A, 0x77, 0x9E, 0x61, 0xAA, 0x89, 0xEE, 0xE5, 0x54, 0xD3,
290xA4, 0x7D, 0xE2, 0x30, 0xBC, 0x7A, 0xC5, 0x90, 0xD5, 0x24, 0x06, 0x7C, 0x38, 0x98, 0xBB, 0xA6, 0xF5, 0xDC, 0x43, 0x60,
300xB8, 0x45, 0xED, 0xA4, 0x8C, 0xBD, 0x9C, 0xF1, 0x26, 0xA7, 0x23, 0x44, 0x5F, 0x0E, 0x19, 0x52, 0xD7, 0x32, 0x5A, 0x75,
310xFA, 0xF5, 0x56, 0x14, 0x4F, 0x9A, 0x98, 0xAF, 0x71, 0x86, 0xB0, 0x27, 0x86, 0x85, 0xB8, 0xE2, 0xC0, 0x8B, 0xEA, 0x87,
320x17, 0x1B, 0x4D, 0xEE, 0x58, 0x5C, 0x18, 0x28, 0x29, 0x5B, 0x53, 0x95, 0xEB, 0x4A, 0x17, 0x77, 0x9F, 0x02, 0x03, 0x01,
330x00, 01
34};
Jeff Thompson2ce8f492013-09-17 18:01:25 -070035#endif
Jeff Thompson47c93cf2013-08-09 00:38:48 -070036
Jeff Thompson29ce3102013-09-27 11:47:48 -070037KeyChain::KeyChain(const shared_ptr<IdentityManager>& identityManager, const shared_ptr<PolicyManager>& policyManager)
38: identityManager_(identityManager), policyManager_(policyManager), face_(0), maxSteps_(100)
Jeff Thompson9296f0c2013-09-23 18:10:27 -070039{
40}
41
Jeff Thompson2ce8f492013-09-17 18:01:25 -070042void
Jeff Thompson29ce3102013-09-27 11:47:48 -070043KeyChain::sign(Data& data, const Name& certificateName, WireFormat& wireFormat)
Jeff Thompson2ce8f492013-09-17 18:01:25 -070044{
Jeff Thompson29ce3102013-09-27 11:47:48 -070045 identityManager_->signByCertificate(data, certificateName, wireFormat);
46}
47
Jeff Thompsonc01e1782013-10-21 14:08:42 -070048shared_ptr<Signature>
49KeyChain::sign(const uint8_t* buffer, size_t bufferLength, const Name& certificateName)
50{
51 return identityManager_->signByCertificate(buffer, bufferLength, certificateName);
52}
53
Jeff Thompson29ce3102013-09-27 11:47:48 -070054void
55KeyChain::signByIdentity(Data& data, const Name& identityName, WireFormat& wireFormat)
56{
57 Name signingCertificateName;
Jeff Thompson9296f0c2013-09-23 18:10:27 -070058
Jeff Thompson29ce3102013-09-27 11:47:48 -070059 if (identityName.getComponentCount() == 0) {
60 Name inferredIdentity = policyManager_->inferSigningIdentity(data.getName());
61 if (inferredIdentity.getComponentCount() == 0)
62 signingCertificateName = identityManager_->getDefaultCertificateName();
63 else
64 signingCertificateName = identityManager_->getDefaultCertificateNameForIdentity(inferredIdentity);
Jeff Thompson2ce8f492013-09-17 18:01:25 -070065 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -070066 else
Jeff Thompson29ce3102013-09-27 11:47:48 -070067 signingCertificateName = identityManager_->getDefaultCertificateNameForIdentity(identityName);
68
69 if (signingCertificateName.getComponentCount() == 0)
70 throw SecurityException("No qualified certificate name found!");
71
72 if (!policyManager_->checkSigningPolicy(data.getName(), signingCertificateName))
Jeff Thompson9296f0c2013-09-23 18:10:27 -070073 throw SecurityException("Signing Cert name does not comply with signing policy");
Jeff Thompson29ce3102013-09-27 11:47:48 -070074
Jeff Thompson56e62652013-10-31 16:13:25 -070075 identityManager_->signByCertificate(data, signingCertificateName, wireFormat);
Jeff Thompson2ce8f492013-09-17 18:01:25 -070076}
77
Jeff Thompsone9ffe792013-10-22 10:58:48 -070078shared_ptr<Signature>
79KeyChain::signByIdentity(const uint8_t* buffer, size_t bufferLength, const Name& identityName)
80{
81 Name signingCertificateName = identityManager_->getDefaultCertificateNameForIdentity(identityName);
Jeff Thompsonc01e1782013-10-21 14:08:42 -070082
Jeff Thompsone9ffe792013-10-22 10:58:48 -070083 if (signingCertificateName.size() == 0)
84 throw SecurityException("No qualified certificate name found!");
Jeff Thompsonc01e1782013-10-21 14:08:42 -070085
Jeff Thompsone9ffe792013-10-22 10:58:48 -070086 return identityManager_->signByCertificate(buffer, bufferLength, signingCertificateName);
87}
Jeff Thompsonc01e1782013-10-21 14:08:42 -070088
Jeff Thompson2ce8f492013-09-17 18:01:25 -070089void
Jeff Thompson7c5d2312013-09-25 16:07:15 -070090KeyChain::verifyData
91 (const shared_ptr<Data>& data, const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed, int stepCount)
Jeff Thompson2ce8f492013-09-17 18:01:25 -070092{
Jeff Thompson2ce8f492013-09-17 18:01:25 -070093 _LOG_TRACE("Enter Verify");
Jeff Thompson2ce8f492013-09-17 18:01:25 -070094
Jeff Thompsonf309aa62013-10-31 17:03:54 -070095 if (policyManager_->requireVerify(*data)) {
96 shared_ptr<ValidationRequest> nextStep = policyManager_->checkVerificationPolicy
97 (data, stepCount, onVerified, onVerifyFailed);
98 if (nextStep) {
99#if 0 // TODO: implement
100 Ptr<Closure> closure = Ptr<Closure> (new Closure(nextStep->m_verifiedCallback,
101 boost::bind(&Keychain::onCertificateInterestTimeout,
102 this,
103 _1,
104 _2,
105 nextStep->m_retry,
106 unverifiedCallback,
107 data),
108 nextStep->m_unverifiedCallback,
109 nextStep->m_stepCount)
110 );
111
112 face_->expressInterest(nextStep->m_interest, closure);
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700113#else
Jeff Thompsonf309aa62013-10-31 17:03:54 -0700114 throw SecurityException("KeyChain::verifyData: Use of ValidationRequest not implemented.");
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700115#endif
Jeff Thompsonf309aa62013-10-31 17:03:54 -0700116 }
117 }
118 else if (policyManager_->skipVerifyAndTrust(*data))
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700119 onVerified(data);
120 else
Jeff Thompson29ce3102013-09-27 11:47:48 -0700121 onVerifyFailed(data);
Jeff Thompson1e90d8c2013-08-12 16:09:25 -0700122}
123
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700124}