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