blob: fc83c66396bf2773299561b4405e1e00935c82ac [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 Zhud5d99be2013-03-13 19:15:56 -070033 : m_pkey(0)
34 , m_meta("", "", 0, 0)
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070035{
36}
37
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -070038Cert::Cert(const PcoPtr &keyObject, const PcoPtr &metaObject = PcoPtr())
Zhenkai Zhud5d99be2013-03-13 19:15:56 -070039 : m_pkey(0)
40 , m_meta("", "", 0, 0)
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070041{
42 m_name = keyObject->name();
Zhenkai Zhud5d99be2013-03-13 19:15:56 -070043 m_rawKeyBytes = keyObject->content();
44 m_keyHash = *(Hash::FromBytes(m_rawKeyBytes));
45 m_pkey = ccn_d2i_pubkey(head(m_rawKeyBytes), m_rawKeyBytes.size());
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070046 updateMeta(metaObject);
47}
48
Zhenkai Zhud5d99be2013-03-13 19:15:56 -070049Cert::~Cert()
50{
51 if (m_pkey != 0)
52 {
53 ccn_pubkey_free(m_pkey);
54 m_pkey = 0;
55 }
56}
57
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070058void
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -070059Cert::updateMeta(const PcoPtr &metaObject)
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070060{
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 Zhudd1f14d2013-03-13 12:04:28 -070073 string elemName = child->Value();
74 string text = child->GetText();
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070075 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 Zhudd1f14d2013-03-13 12:04:28 -070085 m_meta.validTo = boost::lexical_cast<time_t>(text);
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070086 }
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 Zhud5d99be2013-03-13 19:15:56 -0700100 _LOG_ERROR("Cannot parse meta info:" << std::string((const char *)head(xml), xml.size()));
Zhenkai Zhu3457ed42013-03-12 15:15:21 -0700101 }
102 }
103}
104
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700105Cert::VALIDITY
106Cert::validity()
Zhenkai Zhu3457ed42013-03-12 15:15:21 -0700107{
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 Zhudd1f14d2013-03-13 12:04:28 -0700124 return WITHIN_VALID_TIME_SPAN;
Zhenkai Zhu3457ed42013-03-12 15:15:21 -0700125}
126
127} // Ccnx