blob: b280c41a03b842194c5fab0a761174ac18512959 [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
7#include <stdexcept>
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 Thompson20af0732013-09-12 17:01:45 -070011#include "../sha256-with-rsa-signature.hpp"
Jeff Thompson47c93cf2013-08-09 00:38:48 -070012#include "key-chain.hpp"
13
14using namespace std;
15
16namespace ndn {
17
Jeff Thompson2ce8f492013-09-17 18:01:25 -070018#if 1
Jeff Thompson3df81372013-08-09 12:02:37 -070019static unsigned char DEFAULT_PUBLIC_KEY_DER[] = {
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700200x30, 0x81, 0x9F, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81,
210x8D, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xE1, 0x7D, 0x30, 0xA7, 0xD8, 0x28, 0xAB, 0x1B, 0x84, 0x0B, 0x17,
220x54, 0x2D, 0xCA, 0xF6, 0x20, 0x7A, 0xFD, 0x22, 0x1E, 0x08, 0x6B, 0x2A, 0x60, 0xD1, 0x6C, 0xB7, 0xF5, 0x44, 0x48, 0xBA,
230x9F, 0x3F, 0x08, 0xBC, 0xD0, 0x99, 0xDB, 0x21, 0xDD, 0x16, 0x2A, 0x77, 0x9E, 0x61, 0xAA, 0x89, 0xEE, 0xE5, 0x54, 0xD3,
240xA4, 0x7D, 0xE2, 0x30, 0xBC, 0x7A, 0xC5, 0x90, 0xD5, 0x24, 0x06, 0x7C, 0x38, 0x98, 0xBB, 0xA6, 0xF5, 0xDC, 0x43, 0x60,
250xB8, 0x45, 0xED, 0xA4, 0x8C, 0xBD, 0x9C, 0xF1, 0x26, 0xA7, 0x23, 0x44, 0x5F, 0x0E, 0x19, 0x52, 0xD7, 0x32, 0x5A, 0x75,
260xFA, 0xF5, 0x56, 0x14, 0x4F, 0x9A, 0x98, 0xAF, 0x71, 0x86, 0xB0, 0x27, 0x86, 0x85, 0xB8, 0xE2, 0xC0, 0x8B, 0xEA, 0x87,
270x17, 0x1B, 0x4D, 0xEE, 0x58, 0x5C, 0x18, 0x28, 0x29, 0x5B, 0x53, 0x95, 0xEB, 0x4A, 0x17, 0x77, 0x9F, 0x02, 0x03, 0x01,
280x00, 01
29};
Jeff Thompson2ce8f492013-09-17 18:01:25 -070030#endif
Jeff Thompson47c93cf2013-08-09 00:38:48 -070031
Jeff Thompson2ce8f492013-09-17 18:01:25 -070032static bool
33verifySignature(const Data& data /*, const Publickey& publickey */)
Jeff Thompson47c93cf2013-08-09 00:38:48 -070034{
Jeff Thompson2ce8f492013-09-17 18:01:25 -070035#if 0
36 using namespace CryptoPP;
Jeff Thompson47c93cf2013-08-09 00:38:48 -070037
Jeff Thompson2ce8f492013-09-17 18:01:25 -070038 Blob unsignedData(data.getSignedBlob()->signed_buf(), data.getSignedBlob()->signed_size());
39 bool result = false;
40
41 // Temporarily hardwire. It should be assigned by Signature.getAlgorithm().
42 DigestAlgorithm digestAlg = DIGEST_SHA256;
43 // Temporarily hardwire. It should be assigned by Publickey.getKeyType().
44 KeyType keyType = KEY_TYPE_RSA;
45 if (keyType == KEY_TYPE_RSA) {
46 RSA::PublicKey pubKey;
47 ByteQueue queue;
Jeff Thompson3df81372013-08-09 12:02:37 -070048
Jeff Thompson2ce8f492013-09-17 18:01:25 -070049 queue.Put((const byte*)publickey.getKeyBlob ().buf (), publickey.getKeyBlob ().size ());
50 pubKey.Load(queue);
Jeff Thompson47c93cf2013-08-09 00:38:48 -070051
Jeff Thompson2ce8f492013-09-17 18:01:25 -070052 if (DIGEST_SHA256 == digestAlg) {
53 Ptr<const signature::Sha256WithRsa> sigPtr = boost::dynamic_pointer_cast<const signature::Sha256WithRsa> (data.getSignature());
54 const Blob & sigBits = sigPtr->getSignatureBits();
Jeff Thompson3c73da42013-08-12 11:19:05 -070055
Jeff Thompson2ce8f492013-09-17 18:01:25 -070056 RSASS<PKCS1v15, SHA256>::Verifier verifier (pubKey);
57 result = verifier.VerifyMessage((const byte*) unsignedData.buf(), unsignedData.size(), (const byte*)sigBits.buf(), sigBits.size());
58 _LOG_DEBUG("Signature verified? " << data.getName() << " " << boolalpha << result);
59 }
60 }
61
62 return result;
63#else
64 const Sha256WithRsaSignature *signature = dynamic_cast<const Sha256WithRsaSignature*>(data.getSignature());
Jeff Thompson20af0732013-09-12 17:01:45 -070065 if (!signature)
Jeff Thompson2ce8f492013-09-17 18:01:25 -070066 throw std::runtime_error("signature is not Sha256WithRsaSignature.");
Jeff Thompson20af0732013-09-12 17:01:45 -070067
68 if (signature->getDigestAlgorithm().size() != 0)
Jeff Thompson1e90d8c2013-08-12 16:09:25 -070069 // TODO: Allow a non-default digest algorithm.
Jeff Thompson2ce8f492013-09-17 18:01:25 -070070 throw std::runtime_error("Cannot verify a data packet with a non-default digest algorithm.");
71 if (!data.getWireEncoding())
72 throw std::runtime_error("The Data wireEncoding is null.");
Jeff Thompson9c661702013-09-13 14:35:44 -070073 unsigned char signedPortionDigest[SHA256_DIGEST_LENGTH];
Jeff Thompson2ce8f492013-09-17 18:01:25 -070074 ndn_digestSha256(data.getWireEncoding().signedBuf(), data.getWireEncoding().signedSize(), signedPortionDigest);
Jeff Thompson1e90d8c2013-08-12 16:09:25 -070075
Jeff Thompson9c661702013-09-13 14:35:44 -070076 // Verify the signedPortionDigest.
Jeff Thompson192e86b2013-08-13 11:34:24 -070077 // Use a temporary pointer since d2i updates it.
Jeff Thompson2ce8f492013-09-17 18:01:25 -070078 const unsigned char *derPointer = DEFAULT_PUBLIC_KEY_DER;
79 RSA *publicKey = d2i_RSA_PUBKEY(NULL, &derPointer, sizeof(DEFAULT_PUBLIC_KEY_DER));
Jeff Thompson1e90d8c2013-08-12 16:09:25 -070080 if (!publicKey)
81 throw std::runtime_error("Error decoding public key in d2i_RSAPublicKey");
82 int success = RSA_verify
Jeff Thompson9c661702013-09-13 14:35:44 -070083 (NID_sha256, signedPortionDigest, sizeof(signedPortionDigest), (unsigned char *)signature->getSignature().buf(),
Jeff Thompson20af0732013-09-12 17:01:45 -070084 signature->getSignature().size(), publicKey);
Jeff Thompson1e90d8c2013-08-12 16:09:25 -070085 // Free the public key before checking for success.
86 RSA_free(publicKey);
87
88 return (success == 1);
Jeff Thompson2ce8f492013-09-17 18:01:25 -070089#endif
90}
91
92void
93KeyChain::signData(Data& data, const Name& signerName, bool byKeyName, WireFormat& wireFormat)
94{
95 Name certificateName;
96
97#if 0
98 if(signerName.getComponentCount() == 0)
99 certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_policyManager->inferSigningIdentity (data.getName ()));
100 else {
101 if (byKeyName)
102 certificateName = m_identityManager->getDefaultCertificateNameByIdentity(signerName);
103 else
104 certificateName = signerName;
105 }
106
107 if (certificateName.getComponentCount() == 0)
108 throw std::runtime_error("No qualified certificate name found!");
109
110 if (!m_policyManager->checkSigningPolicy (data.getName (), certificateName))
111 throw std::runtime_error("Signing Cert name does not comply with signing policy");
112#endif
113
114 identityManager_->signByCertificate(data, certificateName, wireFormat);
115}
116
117void
118KeyChain::verifyData(const ptr_lib::shared_ptr<Data>& data, const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed)
119{
120#if 0
121 _LOG_TRACE("Enter Verify");
122#endif
123
124#if 0
125 if (m_policyManager->requireVerify(*dataPtr))
126 stepVerify(dataPtr, true, m_maxStep, onVerified, onVerifyFailed);
127 else if(m_policyManager->skipVerify(*dataPtr))
128#else
129 if (verifySignature(*data))
130#endif
131 onVerified(data);
132 else
133 onVerifyFailed();
Jeff Thompson1e90d8c2013-08-12 16:09:25 -0700134}
135
Jeff Thompson47c93cf2013-08-09 00:38:48 -0700136}