Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -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 | */ |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 21 | #include "ccnx-cert.h" |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 22 | #include <tinyxml.h> |
| 23 | #include <boost/lexical_cast.hpp> |
| 24 | #include "logging.h" |
| 25 | |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 26 | INIT_LOGGER ("Ccnx.Cert"); |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 27 | |
| 28 | using namespace std; |
| 29 | |
| 30 | namespace Ccnx { |
| 31 | |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 32 | Cert::Cert() |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 33 | : m_pkey(0) |
| 34 | , m_meta("", "", 0, 0) |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 35 | { |
| 36 | } |
| 37 | |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 38 | Cert::Cert(const PcoPtr &keyObject, const PcoPtr &metaObject = PcoPtr()) |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 39 | : m_pkey(0) |
| 40 | , m_meta("", "", 0, 0) |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 41 | { |
| 42 | m_name = keyObject->name(); |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 43 | m_rawKeyBytes = keyObject->content(); |
| 44 | m_keyHash = *(Hash::FromBytes(m_rawKeyBytes)); |
| 45 | m_pkey = ccn_d2i_pubkey(head(m_rawKeyBytes), m_rawKeyBytes.size()); |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 46 | updateMeta(metaObject); |
| 47 | } |
| 48 | |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 49 | Cert::~Cert() |
| 50 | { |
| 51 | if (m_pkey != 0) |
| 52 | { |
| 53 | ccn_pubkey_free(m_pkey); |
| 54 | m_pkey = 0; |
| 55 | } |
| 56 | } |
| 57 | |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 58 | void |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 59 | Cert::updateMeta(const PcoPtr &metaObject) |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 60 | { |
| 61 | if (metaObject) |
| 62 | { |
| 63 | TiXmlDocument doc; |
| 64 | Bytes xml = metaObject->content(); |
| 65 | // just make sure it's null terminated as it's required by TiXmlDocument::parse |
| 66 | xml.push_back('\0'); |
| 67 | doc.Parse((const char *)(head(xml))); |
| 68 | if (!doc.Error()) |
| 69 | { |
| 70 | TiXmlElement *root = doc.RootElement(); |
| 71 | for (TiXmlElement *child = root->FirstChildElement(); child; child = child->NextSiblingElement()) |
| 72 | { |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 73 | string elemName = child->Value(); |
| 74 | string text = child->GetText(); |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 75 | if (elemName == "Name") |
| 76 | { |
| 77 | m_meta.realworldID = text; |
| 78 | } |
| 79 | else if (elemName == "Affiliation") |
| 80 | { |
| 81 | m_meta.affiliation = text; |
| 82 | } |
| 83 | else if (elemName == "Valid_to") |
| 84 | { |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 85 | m_meta.validTo = boost::lexical_cast<time_t>(text); |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 86 | } |
| 87 | else if (elemName == "Valid_from") |
| 88 | { |
| 89 | // this is not included in the key meta yet |
| 90 | // but it should eventually be there |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | // ignore known stuff |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | else |
| 99 | { |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 100 | _LOG_ERROR("Cannot parse meta info:" << std::string((const char *)head(xml), xml.size())); |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 105 | Cert::VALIDITY |
| 106 | Cert::validity() |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 107 | { |
| 108 | if (m_meta.validFrom == 0 && m_meta.validTo == 0) |
| 109 | { |
| 110 | return OTHER; |
| 111 | } |
| 112 | |
| 113 | time_t now = time(NULL); |
| 114 | if (now < m_meta.validFrom) |
| 115 | { |
| 116 | return NOT_YET_VALID; |
| 117 | } |
| 118 | |
| 119 | if (now >= m_meta.validTo) |
| 120 | { |
| 121 | return EXPIRED; |
| 122 | } |
| 123 | |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 124 | return WITHIN_VALID_TIME_SPAN; |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | } // Ccnx |