blob: a39c4be5cfd2ac5870f7fe3de952109bd7267c08 [file] [log] [blame]
Jeff Thompson47c93cf2013-08-09 00:38:48 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47c93cf2013-08-09 00:38:48 -07004 * See COPYING for copyright and distribution information.
5 */
6
Jeff Thompson7a67cb62013-08-26 11:43:18 -07007#include "../c/util/crypto.h"
8#include "../c/encoding/binary-xml-data.h"
9#include "../encoding/binary-xml-encoder.hpp"
Jeff Thompson20af0732013-09-12 17:01:45 -070010#include "../sha256-with-rsa-signature.hpp"
Jeff Thompson9296f0c2013-09-23 18:10:27 -070011#include "../util/logging.hpp"
12#include "security-exception.hpp"
Jeff Thompson47c93cf2013-08-09 00:38:48 -070013#include "key-chain.hpp"
14
15using namespace std;
Jeff Thompsond4144fe2013-09-18 15:59:57 -070016using namespace ndn::ptr_lib;
Jeff Thompson47c93cf2013-08-09 00:38:48 -070017
18namespace ndn {
19
Jeff Thompson2ce8f492013-09-17 18:01:25 -070020#if 1
Jeff Thompson10ad12a2013-09-24 16:19:11 -070021static uint8_t DEFAULT_PUBLIC_KEY_DER[] = {
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700220x30, 0x81, 0x9F, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81,
230x8D, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xE1, 0x7D, 0x30, 0xA7, 0xD8, 0x28, 0xAB, 0x1B, 0x84, 0x0B, 0x17,
240x54, 0x2D, 0xCA, 0xF6, 0x20, 0x7A, 0xFD, 0x22, 0x1E, 0x08, 0x6B, 0x2A, 0x60, 0xD1, 0x6C, 0xB7, 0xF5, 0x44, 0x48, 0xBA,
250x9F, 0x3F, 0x08, 0xBC, 0xD0, 0x99, 0xDB, 0x21, 0xDD, 0x16, 0x2A, 0x77, 0x9E, 0x61, 0xAA, 0x89, 0xEE, 0xE5, 0x54, 0xD3,
260xA4, 0x7D, 0xE2, 0x30, 0xBC, 0x7A, 0xC5, 0x90, 0xD5, 0x24, 0x06, 0x7C, 0x38, 0x98, 0xBB, 0xA6, 0xF5, 0xDC, 0x43, 0x60,
270xB8, 0x45, 0xED, 0xA4, 0x8C, 0xBD, 0x9C, 0xF1, 0x26, 0xA7, 0x23, 0x44, 0x5F, 0x0E, 0x19, 0x52, 0xD7, 0x32, 0x5A, 0x75,
280xFA, 0xF5, 0x56, 0x14, 0x4F, 0x9A, 0x98, 0xAF, 0x71, 0x86, 0xB0, 0x27, 0x86, 0x85, 0xB8, 0xE2, 0xC0, 0x8B, 0xEA, 0x87,
290x17, 0x1B, 0x4D, 0xEE, 0x58, 0x5C, 0x18, 0x28, 0x29, 0x5B, 0x53, 0x95, 0xEB, 0x4A, 0x17, 0x77, 0x9F, 0x02, 0x03, 0x01,
300x00, 01
31};
Jeff Thompson2ce8f492013-09-17 18:01:25 -070032#endif
Jeff Thompson47c93cf2013-08-09 00:38:48 -070033
Jeff Thompson40f361a2013-09-25 13:12:48 -070034KeyChain::KeyChain(const shared_ptr<IdentityManager>& identityManager)
35: identityManager_(identityManager), face_(0), maxSteps_(100)
Jeff Thompson9296f0c2013-09-23 18:10:27 -070036{
37}
38
Jeff Thompson2ce8f492013-09-17 18:01:25 -070039static bool
40verifySignature(const Data& data /*, const Publickey& publickey */)
Jeff Thompson47c93cf2013-08-09 00:38:48 -070041{
Jeff Thompson2ce8f492013-09-17 18:01:25 -070042#if 0
43 using namespace CryptoPP;
Jeff Thompson47c93cf2013-08-09 00:38:48 -070044
Jeff Thompson2ce8f492013-09-17 18:01:25 -070045 Blob unsignedData(data.getSignedBlob()->signed_buf(), data.getSignedBlob()->signed_size());
46 bool result = false;
47
48 // Temporarily hardwire. It should be assigned by Signature.getAlgorithm().
49 DigestAlgorithm digestAlg = DIGEST_SHA256;
50 // Temporarily hardwire. It should be assigned by Publickey.getKeyType().
51 KeyType keyType = KEY_TYPE_RSA;
52 if (keyType == KEY_TYPE_RSA) {
53 RSA::PublicKey pubKey;
54 ByteQueue queue;
Jeff Thompson3df81372013-08-09 12:02:37 -070055
Jeff Thompson2ce8f492013-09-17 18:01:25 -070056 queue.Put((const byte*)publickey.getKeyBlob ().buf (), publickey.getKeyBlob ().size ());
57 pubKey.Load(queue);
Jeff Thompson47c93cf2013-08-09 00:38:48 -070058
Jeff Thompson2ce8f492013-09-17 18:01:25 -070059 if (DIGEST_SHA256 == digestAlg) {
60 Ptr<const signature::Sha256WithRsa> sigPtr = boost::dynamic_pointer_cast<const signature::Sha256WithRsa> (data.getSignature());
61 const Blob & sigBits = sigPtr->getSignatureBits();
Jeff Thompson3c73da42013-08-12 11:19:05 -070062
Jeff Thompson2ce8f492013-09-17 18:01:25 -070063 RSASS<PKCS1v15, SHA256>::Verifier verifier (pubKey);
64 result = verifier.VerifyMessage((const byte*) unsignedData.buf(), unsignedData.size(), (const byte*)sigBits.buf(), sigBits.size());
65 _LOG_DEBUG("Signature verified? " << data.getName() << " " << boolalpha << result);
66 }
67 }
68
69 return result;
70#else
71 const Sha256WithRsaSignature *signature = dynamic_cast<const Sha256WithRsaSignature*>(data.getSignature());
Jeff Thompson20af0732013-09-12 17:01:45 -070072 if (!signature)
Jeff Thompson9296f0c2013-09-23 18:10:27 -070073 throw SecurityException("signature is not Sha256WithRsaSignature.");
Jeff Thompson20af0732013-09-12 17:01:45 -070074
75 if (signature->getDigestAlgorithm().size() != 0)
Jeff Thompson1e90d8c2013-08-12 16:09:25 -070076 // TODO: Allow a non-default digest algorithm.
Jeff Thompson9296f0c2013-09-23 18:10:27 -070077 throw UnrecognizedDigestAlgorithmException("Cannot verify a data packet with a non-default digest algorithm.");
Jeff Thompson2ce8f492013-09-17 18:01:25 -070078 if (!data.getWireEncoding())
Jeff Thompson9296f0c2013-09-23 18:10:27 -070079 // Don't expect this to happen
80 throw SecurityException("The Data wireEncoding is null.");
Jeff Thompson10ad12a2013-09-24 16:19:11 -070081 uint8_t signedPortionDigest[SHA256_DIGEST_LENGTH];
Jeff Thompson2ce8f492013-09-17 18:01:25 -070082 ndn_digestSha256(data.getWireEncoding().signedBuf(), data.getWireEncoding().signedSize(), signedPortionDigest);
Jeff Thompson1e90d8c2013-08-12 16:09:25 -070083
Jeff Thompson9c661702013-09-13 14:35:44 -070084 // Verify the signedPortionDigest.
Jeff Thompson192e86b2013-08-13 11:34:24 -070085 // Use a temporary pointer since d2i updates it.
Jeff Thompson10ad12a2013-09-24 16:19:11 -070086 const uint8_t *derPointer = DEFAULT_PUBLIC_KEY_DER;
Jeff Thompson2ce8f492013-09-17 18:01:25 -070087 RSA *publicKey = d2i_RSA_PUBKEY(NULL, &derPointer, sizeof(DEFAULT_PUBLIC_KEY_DER));
Jeff Thompson1e90d8c2013-08-12 16:09:25 -070088 if (!publicKey)
Jeff Thompson9296f0c2013-09-23 18:10:27 -070089 throw UnrecognizedKeyFormatException("Error decoding public key in d2i_RSAPublicKey");
Jeff Thompson1e90d8c2013-08-12 16:09:25 -070090 int success = RSA_verify
Jeff Thompson10ad12a2013-09-24 16:19:11 -070091 (NID_sha256, signedPortionDigest, sizeof(signedPortionDigest), (uint8_t *)signature->getSignature().buf(),
Jeff Thompson20af0732013-09-12 17:01:45 -070092 signature->getSignature().size(), publicKey);
Jeff Thompson1e90d8c2013-08-12 16:09:25 -070093 // Free the public key before checking for success.
94 RSA_free(publicKey);
95
96 return (success == 1);
Jeff Thompson2ce8f492013-09-17 18:01:25 -070097#endif
98}
99
100void
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700101KeyChain::signData(Data& data, const Name& certificateNameIn, WireFormat& wireFormat)
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700102{
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700103 Name inferredCertificateName;
104 const Name* certificateName;
105
106 if (certificateNameIn.getComponentCount() == 0) {
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700107#if 0
Jeff Thompson40f361a2013-09-25 13:12:48 -0700108 inferredCertificateName = identityManager_->getDefaultCertificateNameForIdentity(policyManager_->inferSigningIdentity(data.getName ()));
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700109#else
110 inferredCertificateName = Name();
111#endif
112 if (inferredCertificateName.getComponentCount() == 0)
113 throw SecurityException("No qualified certificate name can be inferred");
114
115 certificateName = &inferredCertificateName;
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700116 }
Jeff Thompson9296f0c2013-09-23 18:10:27 -0700117 else
118 certificateName = &certificateNameIn;
119
120#if 0
121 if (!policyManager_->checkSigningPolicy (data.getName (), certificateName))
122 throw SecurityException("Signing Cert name does not comply with signing policy");
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700123#endif
124
Jeff Thompson40f361a2013-09-25 13:12:48 -0700125 identityManager_->signByCertificate(data, *certificateName, wireFormat);
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700126}
127
128void
Jeff Thompson7c5d2312013-09-25 16:07:15 -0700129KeyChain::verifyData
130 (const shared_ptr<Data>& data, const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed, int stepCount)
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700131{
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700132 _LOG_TRACE("Enter Verify");
Jeff Thompson2ce8f492013-09-17 18:01:25 -0700133
134#if 0
135 if (m_policyManager->requireVerify(*dataPtr))
136 stepVerify(dataPtr, true, m_maxStep, onVerified, onVerifyFailed);
137 else if(m_policyManager->skipVerify(*dataPtr))
138#else
139 if (verifySignature(*data))
140#endif
141 onVerified(data);
142 else
143 onVerifyFailed();
Jeff Thompson1e90d8c2013-08-12 16:09:25 -0700144}
145
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700146}