blob: 6d2081c234b999ba5a653a9e6c7c2c8ee1301a71 [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
Yingdi Yu4f324632014-01-15 18:10:03 -080011#include "../c/util/crypto.h"
12#include <ndn-cpp/security/identity-storage.hpp>
13#include <ndn-cpp/security/sec-policy-self-verify.hpp>
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -080014
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
Yingdi Yu4f324632014-01-15 18:10:03 -080063SecPolicySelfVerify::~SecPolicySelfVerify()
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -080064{
65}
66
67bool
Yingdi Yu4f324632014-01-15 18:10:03 -080068SecPolicySelfVerify::skipVerifyAndTrust(const Data& data)
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -080069{
70 return false;
71}
72
73bool
Yingdi Yu4f324632014-01-15 18:10:03 -080074SecPolicySelfVerify::requireVerify(const Data& data)
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -080075{
76 return true;
77}
78
Jeff Thompsonce115762013-12-18 14:59:56 -080079ptr_lib::shared_ptr<ValidationRequest>
Yingdi Yu4f324632014-01-15 18:10:03 -080080SecPolicySelfVerify::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)
Yingdi Yu4f324632014-01-15 18:10:03 -080086 throw SecurityException("SecPolicySelfVerify: Signature is not Sha256WithRsaSignature.");
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -080087
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
Yingdi Yu4f324632014-01-15 18:10:03 -0800117SecPolicySelfVerify::checkSigningPolicy(const Name& dataName, const Name& certificateName)
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -0800118{
119 return true;
120}
121
122Name
Yingdi Yu4f324632014-01-15 18:10:03 -0800123SecPolicySelfVerify::inferSigningIdentity(const Name& dataName)
Jeff Thompson3a2eb2f2013-12-11 11:00:27 -0800124{
125 return Name();
126}
127
128}
Alexander Afanasyev44402902014-01-06 00:08:54 -0800129
130#endif // TEMPORARILY_DISABLED