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