blob: 5c189c5e9eddcd6b4e924b6cfe40b47b2f5bfca6 [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
Alexander Afanasyev44402902014-01-06 00:08:54 -08009#ifdef TEMPRORARILY_DISABLED
10
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -080011#include "../../c/util/crypto.h"
12#include <ndn-cpp/sha256-with-rsa-signature.hpp>
13#include <ndn-cpp/security/security-exception.hpp>
14#include <ndn-cpp/security/identity/identity-storage.hpp>
15#include <ndn-cpp/security/policy/self-verify-policy-manager.hpp>
16
17using namespace std;
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -080018
19namespace ndn {
20
21/**
22 * Verify the signature on the data packet using the given public key. If there is no data.getDefaultWireEncoding(),
23 * this calls data.wireEncode() to set it.
24 * TODO: Move this general verification code to a more central location.
25 * @param data The data packet with the signed portion and the signature to verify. The data packet must have a
26 * Sha256WithRsaSignature.
27 * @param publicKeyDer The DER-encoded public key used to verify the signature.
28 * @return true if the signature verifies, false if not.
29 * @throw SecurityException if data does not have a Sha256WithRsaSignature.
30 */
31static bool
32verifySha256WithRsaSignature(const Data& data, const Blob& publicKeyDer)
33{
34 const Sha256WithRsaSignature *signature = dynamic_cast<const Sha256WithRsaSignature*>(data.getSignature());
35 if (!signature)
36 throw SecurityException("signature is not Sha256WithRsaSignature.");
37
38 // Set the data packet's default wire encoding if it is not already there.
39 if (signature->getDigestAlgorithm().size() != 0)
40 // TODO: Allow a non-default digest algorithm.
41 throw UnrecognizedDigestAlgorithmException("Cannot verify a data packet with a non-default digest algorithm.");
42 if (!data.getDefaultWireEncoding())
43 data.wireEncode();
44
45 // Set signedPortionDigest to the digest of the signed portion of the wire encoding.
46 uint8_t signedPortionDigest[SHA256_DIGEST_LENGTH];
47 ndn_digestSha256(data.getDefaultWireEncoding().signedBuf(), data.getDefaultWireEncoding().signedSize(), signedPortionDigest);
48
49 // Verify the signedPortionDigest.
50 // Use a temporary pointer since d2i updates it.
51 const uint8_t *derPointer = publicKeyDer.buf();
52 RSA *rsaPublicKey = d2i_RSA_PUBKEY(NULL, &derPointer, publicKeyDer.size());
53 if (!rsaPublicKey)
54 throw UnrecognizedKeyFormatException("Error decoding public key in d2i_RSAPublicKey");
55 int success = RSA_verify
56 (NID_sha256, signedPortionDigest, sizeof(signedPortionDigest), (uint8_t *)signature->getSignature().buf(),
57 signature->getSignature().size(), rsaPublicKey);
58 // Free the public key before checking for success.
59 RSA_free(rsaPublicKey);
60
61 // RSA_verify returns 1 for a valid signature.
62 return (success == 1);
63}
64
65SelfVerifyPolicyManager::~SelfVerifyPolicyManager()
66{
67}
68
69bool
70SelfVerifyPolicyManager::skipVerifyAndTrust(const Data& data)
71{
72 return false;
73}
74
75bool
76SelfVerifyPolicyManager::requireVerify(const Data& data)
77{
78 return true;
79}
80
Jeff Thompsonce115762013-12-18 14:59:56 -080081ptr_lib::shared_ptr<ValidationRequest>
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -080082SelfVerifyPolicyManager::checkVerificationPolicy
Jeff Thompsonce115762013-12-18 14:59:56 -080083 (const ptr_lib::shared_ptr<Data>& data, int stepCount, const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed)
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -080084{
Jeff Thompson36fe1bc2013-12-13 16:03:59 -080085 // 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 -080086 const Sha256WithRsaSignature *signature = dynamic_cast<const Sha256WithRsaSignature*>(((const Data*)data.get())->getSignature());
87 if (!signature)
88 throw SecurityException("SelfVerifyPolicyManager: Signature is not Sha256WithRsaSignature.");
89
90 if (signature->getKeyLocator().getType() == ndn_KeyLocatorType_KEY) {
91 // Use the public key DER directly.
92 if (verifySha256WithRsaSignature(*data, signature->getKeyLocator().getKeyData()))
93 onVerified(data);
94 else
95 onVerifyFailed(data);
96 }
97 else if (signature->getKeyLocator().getType() == ndn_KeyLocatorType_KEYNAME && identityStorage_) {
98 // Assume the key name is a certificate name.
99 Blob publicKeyDer = identityStorage_->getKey
100 (IdentityCertificate::certificateNameToPublicKeyName(signature->getKeyLocator().getKeyName()));
101 if (!publicKeyDer)
102 // Can't find the public key with the name.
103 onVerifyFailed(data);
104
105 if (verifySha256WithRsaSignature(*data, publicKeyDer))
106 onVerified(data);
107 else
108 onVerifyFailed(data);
109 }
110 else
111 // Can't find a key to verify.
112 onVerifyFailed(data);
113
114 // No more steps, so return a null ValidationRequest.
Jeff Thompsonce115762013-12-18 14:59:56 -0800115 return ptr_lib::shared_ptr<ValidationRequest>();
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -0800116}
117
118bool
119SelfVerifyPolicyManager::checkSigningPolicy(const Name& dataName, const Name& certificateName)
120{
121 return true;
122}
123
124Name
125SelfVerifyPolicyManager::inferSigningIdentity(const Name& dataName)
126{
127 return Name();
128}
129
130}
Alexander Afanasyev44402902014-01-06 00:08:54 -0800131
132#endif // TEMPORARILY_DISABLED