blob: e202c658fcb27971c1fbfe08ca40f241db7016aa [file] [log] [blame]
Zhenkai Zhu3457ed42013-03-12 15:15:21 -07001/* -*- 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 Zhu9dd9adc2013-03-13 16:12:09 -070021#include "ccnx-cert.h"
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070022#include <tinyxml.h>
23#include <boost/lexical_cast.hpp>
24#include "logging.h"
25
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -070026INIT_LOGGER ("Ccnx.Cert");
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070027
28using namespace std;
29
30namespace Ccnx {
31
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -070032Cert::Cert()
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070033 : m_meta("", "", 0, 0)
34{
35}
36
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -070037Cert::Cert(const PcoPtr &keyObject, const PcoPtr &metaObject = PcoPtr())
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070038 : m_meta("", "", 0, 0)
39{
40 m_name = keyObject->name();
41 m_raw = keyObject->content();
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -070042 m_hash = *(Hash::FromBytes(m_raw));
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070043 updateMeta(metaObject);
44}
45
46void
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -070047Cert::updateMeta(const PcoPtr &metaObject)
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070048{
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 Zhudd1f14d2013-03-13 12:04:28 -070061 string elemName = child->Value();
62 string text = child->GetText();
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070063 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 Zhudd1f14d2013-03-13 12:04:28 -070073 m_meta.validTo = boost::lexical_cast<time_t>(text);
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070074 }
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 Zhu9dd9adc2013-03-13 16:12:09 -070093Cert::VALIDITY
94Cert::validity()
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070095{
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 Zhudd1f14d2013-03-13 12:04:28 -0700112 return WITHIN_VALID_TIME_SPAN;
Zhenkai Zhu3457ed42013-03-12 15:15:21 -0700113}
114
115} // Ccnx