Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "ccnx-verifier.h" |
Zhenkai Zhu | 746d444 | 2013-03-13 17:06:54 -0700 | [diff] [blame] | 23 | #include "ccnx-wrapper.h" |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 24 | |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 25 | INIT_LOGGER ("Ccnx.Verifier"); |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 26 | namespace Ccnx { |
| 27 | |
| 28 | static const size_t ROOT_KEY_DIGEST_LEN = 32; // SHA-256 |
| 29 | static const unsigned char ROOT_KEY_DIGEST[ROOT_KEY_DIGEST_LEN] = {0xa7, 0xd9, 0x8b, 0x81, 0xde, 0x13, 0xfc, |
| 30 | 0x56, 0xc5, 0xa6, 0x92, 0xb4, 0x44, 0x93, 0x6e, 0x56, 0x70, 0x9d, 0x52, 0x6f, 0x70, |
| 31 | 0xed, 0x39, 0xef, 0xb5, 0xe2, 0x3, 0x29, 0xa5, 0x53, 0x3e, 0x68}; |
| 32 | |
| 33 | Verifier::Verifier(CcnxWrapper *ccnx) |
| 34 | : m_ccnx(ccnx) |
| 35 | , m_rootKeyDigest(ROOT_KEY_DIGEST, ROOT_KEY_DIGEST_LEN) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | Verifier::~Verifier() |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | bool |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 44 | Verifier::verify(const PcoPtr &pco, double maxWait) |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 45 | { |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 46 | _LOG_TRACE("Verifying content [" << pco->name() << "]"); |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 47 | HashPtr publisherPublicKeyDigest = pco->publisherPublicKeyDigest(); |
Zhenkai Zhu | 746d444 | 2013-03-13 17:06:54 -0700 | [diff] [blame] | 48 | |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 49 | { |
Zhenkai Zhu | 746d444 | 2013-03-13 17:06:54 -0700 | [diff] [blame] | 50 | UniqueRecLock lock(m_cacheLock); |
| 51 | CertCache::iterator it = m_certCache.find(*publisherPublicKeyDigest); |
| 52 | if (it != m_certCache.end()) |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 53 | { |
Zhenkai Zhu | 746d444 | 2013-03-13 17:06:54 -0700 | [diff] [blame] | 54 | CertPtr cert = it->second; |
| 55 | if (cert->validity() == Cert::WITHIN_VALID_TIME_SPAN) |
| 56 | { |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 57 | pco->verifySignature(cert); |
| 58 | return pco->verified(); |
Zhenkai Zhu | 746d444 | 2013-03-13 17:06:54 -0700 | [diff] [blame] | 59 | } |
| 60 | else |
| 61 | { |
| 62 | // delete the invalid cert cache |
| 63 | m_certCache.erase(it); |
| 64 | } |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | // keyName is the name specified in key locator, i.e. without version and segment |
| 69 | Name keyName = pco->keyName(); |
| 70 | int keyNameSize = keyName.size(); |
| 71 | |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 72 | if (keyNameSize < 2) |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 73 | { |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 74 | _LOG_ERROR("Key name is empty or has too few components."); |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 78 | // for keys, we have to make sure key name is strictly prefix of the content name |
| 79 | if (pco->type() == ParsedContentObject::KEY) |
| 80 | { |
| 81 | Name contentName = pco->name(); |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 82 | // when checking for prefix, do not include the hash in the key name (which is the last component) |
| 83 | Name keyNamePrefix = keyName.getPartialName(0, keyNameSize - 1); |
| 84 | if (keyNamePrefix.size() >= contentName.size() || contentName.getPartialName(0, keyNamePrefix.size()) != keyNamePrefix) |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 85 | { |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 86 | _LOG_ERROR("Key name prefix [" << keyNamePrefix << "] is not the prefix of content name [" << contentName << "]"); |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | // for now, user can assign any data using his key |
| 93 | } |
| 94 | |
| 95 | Name metaName = keyName.getPartialName(0, keyNameSize - 1) + Name("/info") + keyName.getPartialName(keyNameSize - 1); |
| 96 | |
| 97 | Selectors selectors; |
| 98 | |
| 99 | selectors.childSelector(Selectors::RIGHT) |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 100 | .interestLifetime(maxWait); |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 101 | |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 102 | PcoPtr keyObject = m_ccnx->get(keyName, selectors, maxWait); |
| 103 | PcoPtr metaObject = m_ccnx->get(metaName, selectors, maxWait); |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 104 | if (!keyObject || !metaObject ) |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 105 | { |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 106 | _LOG_ERROR("can not fetch key or meta"); |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 107 | return false; |
| 108 | } |
| 109 | |
| 110 | HashPtr publisherKeyHashInKeyObject = keyObject->publisherPublicKeyDigest(); |
| 111 | HashPtr publisherKeyHashInMetaObject = metaObject->publisherPublicKeyDigest(); |
| 112 | |
| 113 | // make sure key and meta are signed using the same key |
| 114 | if (publisherKeyHashInKeyObject->IsZero() || ! (*publisherKeyHashInKeyObject == *publisherKeyHashInMetaObject)) |
| 115 | { |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 116 | _LOG_ERROR("Key and Meta not signed by the same publisher"); |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 120 | CertPtr cert = boost::make_shared<Cert>(keyObject, metaObject); |
| 121 | if (cert->validity() != Cert::WITHIN_VALID_TIME_SPAN) |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 122 | { |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 123 | _LOG_ERROR("Certificate is not valid, validity status is : " << cert->validity()); |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 127 | // check pco is actually signed by this key (i.e. we don't trust the publisherPublicKeyDigest given by ccnx c lib) |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 128 | if (! (*pco->publisherPublicKeyDigest() == cert->keyDigest())) |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 129 | { |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 130 | _LOG_ERROR("key digest does not match the publisher public key digest of the content object"); |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 134 | // now we only need to make sure the key is trustworthy |
| 135 | if (cert->keyDigest() == m_rootKeyDigest) |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 136 | { |
| 137 | // the key is the root key |
| 138 | // do nothing now |
| 139 | } |
| 140 | else |
| 141 | { |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 142 | // can not verify key or can not verify meta |
| 143 | if (!verify(keyObject, maxWait) || !verify(metaObject, maxWait)) |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 144 | { |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 145 | _LOG_ERROR("Can not verify key or meta"); |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // ok, keyObject verified, because metaObject is signed by the same parent key and integrity checked |
| 151 | // so metaObject is also verified |
Zhenkai Zhu | 746d444 | 2013-03-13 17:06:54 -0700 | [diff] [blame] | 152 | { |
| 153 | UniqueRecLock lock(m_cacheLock); |
| 154 | m_certCache.insert(std::make_pair(cert->keyDigest(), cert)); |
| 155 | } |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 156 | |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 157 | pco->verifySignature(cert); |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 158 | if (pco->verified()) |
| 159 | { |
| 160 | _LOG_TRACE("[" << pco->name() << "] VERIFIED."); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | _LOG_ERROR("[" << pco->name() << "] CANNOT BE VERIFIED."); |
| 165 | } |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 166 | return pco->verified(); |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | } // Ccnx |