blob: dc4058e85c74f3caa6dc54fc74c5cb80bc419a7e [file] [log] [blame]
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
9#include "../../c/util/crypto.h"
10#include <ndn-cpp/sha256-with-rsa-signature.hpp>
11#include <ndn-cpp/security/security-exception.hpp>
12#include <ndn-cpp/security/identity/identity-storage.hpp>
13#include <ndn-cpp/security/policy/self-verify-policy-manager.hpp>
14
15using namespace std;
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -080016
17namespace ndn {
18
19/**
20 * Verify the signature on the data packet using the given public key. If there is no data.getDefaultWireEncoding(),
21 * this calls data.wireEncode() to set it.
22 * TODO: Move this general verification code to a more central location.
23 * @param data The data packet with the signed portion and the signature to verify. The data packet must have a
24 * Sha256WithRsaSignature.
25 * @param publicKeyDer The DER-encoded public key used to verify the signature.
26 * @return true if the signature verifies, false if not.
27 * @throw SecurityException if data does not have a Sha256WithRsaSignature.
28 */
29static bool
30verifySha256WithRsaSignature(const Data& data, const Blob& publicKeyDer)
31{
32 const Sha256WithRsaSignature *signature = dynamic_cast<const Sha256WithRsaSignature*>(data.getSignature());
33 if (!signature)
34 throw SecurityException("signature is not Sha256WithRsaSignature.");
35
36 // Set the data packet's default wire encoding if it is not already there.
37 if (signature->getDigestAlgorithm().size() != 0)
38 // TODO: Allow a non-default digest algorithm.
39 throw UnrecognizedDigestAlgorithmException("Cannot verify a data packet with a non-default digest algorithm.");
40 if (!data.getDefaultWireEncoding())
41 data.wireEncode();
42
43 // Set signedPortionDigest to the digest of the signed portion of the wire encoding.
44 uint8_t signedPortionDigest[SHA256_DIGEST_LENGTH];
45 ndn_digestSha256(data.getDefaultWireEncoding().signedBuf(), data.getDefaultWireEncoding().signedSize(), signedPortionDigest);
46
47 // Verify the signedPortionDigest.
48 // Use a temporary pointer since d2i updates it.
49 const uint8_t *derPointer = publicKeyDer.buf();
50 RSA *rsaPublicKey = d2i_RSA_PUBKEY(NULL, &derPointer, publicKeyDer.size());
51 if (!rsaPublicKey)
52 throw UnrecognizedKeyFormatException("Error decoding public key in d2i_RSAPublicKey");
53 int success = RSA_verify
54 (NID_sha256, signedPortionDigest, sizeof(signedPortionDigest), (uint8_t *)signature->getSignature().buf(),
55 signature->getSignature().size(), rsaPublicKey);
56 // Free the public key before checking for success.
57 RSA_free(rsaPublicKey);
58
59 // RSA_verify returns 1 for a valid signature.
60 return (success == 1);
61}
62
63SelfVerifyPolicyManager::~SelfVerifyPolicyManager()
64{
65}
66
67bool
68SelfVerifyPolicyManager::skipVerifyAndTrust(const Data& data)
69{
70 return false;
71}
72
73bool
74SelfVerifyPolicyManager::requireVerify(const Data& data)
75{
76 return true;
77}
78
Jeff Thompsonce115762013-12-18 14:59:56 -080079ptr_lib::shared_ptr<ValidationRequest>
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -080080SelfVerifyPolicyManager::checkVerificationPolicy
Jeff Thompsonce115762013-12-18 14:59:56 -080081 (const ptr_lib::shared_ptr<Data>& data, int stepCount, const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed)
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -080082{
Jeff Thompson36fe1bc2013-12-13 16:03:59 -080083 // Cast to const Data* so that we use the const version of getSignature() and don't reset the default encoding.
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -080084 const Sha256WithRsaSignature *signature = dynamic_cast<const Sha256WithRsaSignature*>(((const Data*)data.get())->getSignature());
85 if (!signature)
86 throw SecurityException("SelfVerifyPolicyManager: Signature is not Sha256WithRsaSignature.");
87
88 if (signature->getKeyLocator().getType() == ndn_KeyLocatorType_KEY) {
89 // Use the public key DER directly.
90 if (verifySha256WithRsaSignature(*data, signature->getKeyLocator().getKeyData()))
91 onVerified(data);
92 else
93 onVerifyFailed(data);
94 }
95 else if (signature->getKeyLocator().getType() == ndn_KeyLocatorType_KEYNAME && identityStorage_) {
96 // Assume the key name is a certificate name.
97 Blob publicKeyDer = identityStorage_->getKey
98 (IdentityCertificate::certificateNameToPublicKeyName(signature->getKeyLocator().getKeyName()));
99 if (!publicKeyDer)
100 // Can't find the public key with the name.
101 onVerifyFailed(data);
102
103 if (verifySha256WithRsaSignature(*data, publicKeyDer))
104 onVerified(data);
105 else
106 onVerifyFailed(data);
107 }
108 else
109 // Can't find a key to verify.
110 onVerifyFailed(data);
111
112 // No more steps, so return a null ValidationRequest.
Jeff Thompsonce115762013-12-18 14:59:56 -0800113 return ptr_lib::shared_ptr<ValidationRequest>();
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -0800114}
115
116bool
117SelfVerifyPolicyManager::checkSigningPolicy(const Name& dataName, const Name& certificateName)
118{
119 return true;
120}
121
122Name
123SelfVerifyPolicyManager::inferSigningIdentity(const Name& dataName)
124{
125 return Name();
126}
127
128}