Jeff Thompson | 3a2eb2f | 2013-12-11 11:00:27 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 4440290 | 2014-01-06 00:08:54 -0800 | [diff] [blame] | 9 | #ifdef TEMPRORARILY_DISABLED |
| 10 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 11 | #include "../c/util/crypto.h" |
| 12 | #include <ndn-cpp/security/identity-storage.hpp> |
| 13 | #include <ndn-cpp/security/sec-policy-self-verify.hpp> |
Jeff Thompson | 3a2eb2f | 2013-12-11 11:00:27 -0800 | [diff] [blame] | 14 | |
| 15 | using namespace std; |
Jeff Thompson | 3a2eb2f | 2013-12-11 11:00:27 -0800 | [diff] [blame] | 16 | |
| 17 | namespace 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 | */ |
| 29 | static bool |
| 30 | verifySha256WithRsaSignature(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 Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 63 | SecPolicySelfVerify::~SecPolicySelfVerify() |
Jeff Thompson | 3a2eb2f | 2013-12-11 11:00:27 -0800 | [diff] [blame] | 64 | { |
| 65 | } |
| 66 | |
| 67 | bool |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 68 | SecPolicySelfVerify::skipVerifyAndTrust(const Data& data) |
Jeff Thompson | 3a2eb2f | 2013-12-11 11:00:27 -0800 | [diff] [blame] | 69 | { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | bool |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 74 | SecPolicySelfVerify::requireVerify(const Data& data) |
Jeff Thompson | 3a2eb2f | 2013-12-11 11:00:27 -0800 | [diff] [blame] | 75 | { |
| 76 | return true; |
| 77 | } |
| 78 | |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 79 | ptr_lib::shared_ptr<ValidationRequest> |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 80 | SecPolicySelfVerify::checkVerificationPolicy |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 81 | (const ptr_lib::shared_ptr<Data>& data, int stepCount, const OnVerified& onVerified, const OnVerifyFailed& onVerifyFailed) |
Jeff Thompson | 3a2eb2f | 2013-12-11 11:00:27 -0800 | [diff] [blame] | 82 | { |
Jeff Thompson | 36fe1bc | 2013-12-13 16:03:59 -0800 | [diff] [blame] | 83 | // Cast to const Data* so that we use the const version of getSignature() and don't reset the default encoding. |
Jeff Thompson | 3a2eb2f | 2013-12-11 11:00:27 -0800 | [diff] [blame] | 84 | const Sha256WithRsaSignature *signature = dynamic_cast<const Sha256WithRsaSignature*>(((const Data*)data.get())->getSignature()); |
| 85 | if (!signature) |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 86 | throw SecurityException("SecPolicySelfVerify: Signature is not Sha256WithRsaSignature."); |
Jeff Thompson | 3a2eb2f | 2013-12-11 11:00:27 -0800 | [diff] [blame] | 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 Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 113 | return ptr_lib::shared_ptr<ValidationRequest>(); |
Jeff Thompson | 3a2eb2f | 2013-12-11 11:00:27 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 117 | SecPolicySelfVerify::checkSigningPolicy(const Name& dataName, const Name& certificateName) |
Jeff Thompson | 3a2eb2f | 2013-12-11 11:00:27 -0800 | [diff] [blame] | 118 | { |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | Name |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame^] | 123 | SecPolicySelfVerify::inferSigningIdentity(const Name& dataName) |
Jeff Thompson | 3a2eb2f | 2013-12-11 11:00:27 -0800 | [diff] [blame] | 124 | { |
| 125 | return Name(); |
| 126 | } |
| 127 | |
| 128 | } |
Alexander Afanasyev | 4440290 | 2014-01-06 00:08:54 -0800 | [diff] [blame] | 129 | |
| 130 | #endif // TEMPORARILY_DISABLED |