blob: 4270d68e55296608be7020e0a6a07d89e15fc367 [file] [log] [blame]
Jeff Thompson415da1e2013-10-17 16:52:59 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
9#include "simple-visitor.hpp"
10#include "public-key-visitor.hpp"
11#include "../der.hpp"
12
13#include <ndn-cpp/security/certificate/certificate.hpp>
14#include <ndn-cpp/security/certificate/certificate-subject-description.hpp>
15#include <ndn-cpp/security/certificate/certificate-extension.hpp>
16
17#include "../../../util/logging.hpp"
18#include "certificate-data-visitor.hpp"
19
20using namespace std;
21using namespace ndn::ptr_lib;
22
23INIT_LOGGER("ndn.der.CertificateDataVisitor");
24
25namespace ndn {
26
27namespace der {
28
29/*
30 * CertificateDataVisitor
31 */
32void
33CertificateDataVisitor::visit(DerSequence& derSeq, ndnboost::any param)
34{
35 // _LOG_DEBUG("CertificateDataVisitor::visit");
36
Jeff Thompson3d5096c2013-12-11 16:40:05 -080037 DerNodePtrList& children = derSeq.getChildren();
Jeff Thompson67598332013-10-17 17:57:22 -070038 CertificateValidityVisitor validityVisitor;
Jeff Thompson415da1e2013-10-17 16:52:59 -070039 children[0]->accept(validityVisitor, param);
Jeff Thompson67598332013-10-17 17:57:22 -070040 CertificateSubjectVisitor subjectVisitor;
Jeff Thompson415da1e2013-10-17 16:52:59 -070041 children[1]->accept(subjectVisitor, param);
42 PublicKeyVisitor pubkeyVisitor;
43 Certificate* certData = ndnboost::any_cast<Certificate*>(param);
44 certData->setPublicKeyInfo(*ndnboost::any_cast<shared_ptr<PublicKey> >(children[2]->accept(pubkeyVisitor)));
45
46 if(children.size() > 3)
47 {
Jeff Thompson67598332013-10-17 17:57:22 -070048 CertificateExtensionVisitor extnVisitor;
Jeff Thompson415da1e2013-10-17 16:52:59 -070049 children[3]->accept(extnVisitor, param);
50 }
51}
52
53/*
54 * CertValidityVisitor
55 */
56void
Jeff Thompson67598332013-10-17 17:57:22 -070057CertificateValidityVisitor::visit(DerSequence& derSeq, ndnboost::any param)
Jeff Thompson415da1e2013-10-17 16:52:59 -070058{
59 // _LOG_DEBUG("CertValidityVisitor::visit");
60
61 Certificate* certData = ndnboost::any_cast<Certificate*>(param);
62
Jeff Thompson3d5096c2013-12-11 16:40:05 -080063 DerNodePtrList& children = derSeq.getChildren();
Jeff Thompson415da1e2013-10-17 16:52:59 -070064
65 SimpleVisitor simpleVisitor;
66
67 MillisecondsSince1970 notBefore = ndnboost::any_cast<MillisecondsSince1970>(children[0]->accept(simpleVisitor));
68 MillisecondsSince1970 notAfter = ndnboost::any_cast<MillisecondsSince1970>(children[1]->accept(simpleVisitor));
69
70 // _LOG_DEBUG("parsed notBefore: " << notBefore);
71 // _LOG_DEBUG("parsed notAfter: " << notAfter);
72
73 certData->setNotBefore(notBefore);
74 certData->setNotAfter(notAfter);
75}
76
77/*
78 * CertSubDescryptVisitor
79 */
80void
Jeff Thompson67598332013-10-17 17:57:22 -070081CertificateSubjectDescriptionVisitor::visit(DerSequence& derSeq, ndnboost::any param)
Jeff Thompson415da1e2013-10-17 16:52:59 -070082{
83 Certificate* certData = ndnboost::any_cast<Certificate*>(param);
84
Jeff Thompson3d5096c2013-12-11 16:40:05 -080085 DerNodePtrList& children = derSeq.getChildren();
Jeff Thompson415da1e2013-10-17 16:52:59 -070086
87 SimpleVisitor simpleVisitor;
88
89 OID oid = ndnboost::any_cast<OID>(children[0]->accept(simpleVisitor));
90 string value = ndnboost::any_cast<string>(children[1]->accept(simpleVisitor));
91
92 CertificateSubjectDescription subDescrypt(oid, value);
93
94 certData->addSubjectDescription(subDescrypt);
95}
96
97/*
98 * CertSubjectVisitor
99 */
100void
Jeff Thompson67598332013-10-17 17:57:22 -0700101CertificateSubjectVisitor::visit(DerSequence& derSeq, ndnboost::any param)
Jeff Thompson415da1e2013-10-17 16:52:59 -0700102{
103 // _LOG_DEBUG("CertSubjectVisitor::visit");
104
Jeff Thompson3d5096c2013-12-11 16:40:05 -0800105 DerNodePtrList& children = derSeq.getChildren();
Jeff Thompson415da1e2013-10-17 16:52:59 -0700106
Jeff Thompson67598332013-10-17 17:57:22 -0700107 CertificateSubjectDescriptionVisitor descryptVisitor;
Jeff Thompson415da1e2013-10-17 16:52:59 -0700108
Jeff Thompson3d5096c2013-12-11 16:40:05 -0800109 DerNodePtrList::iterator it = children.begin();
Jeff Thompson415da1e2013-10-17 16:52:59 -0700110
111 while(it != children.end()) {
112 (*it)->accept(descryptVisitor, param);
113 it++;
114 }
115}
116
117/*
118 * CertExtnEntryVisitor
119 */
120void
Jeff Thompson67598332013-10-17 17:57:22 -0700121CertificateExtensionEntryVisitor::visit(DerSequence& derSeq, ndnboost::any param)
Jeff Thompson415da1e2013-10-17 16:52:59 -0700122{
123 Certificate* certData = ndnboost::any_cast<Certificate*>(param);
124
Jeff Thompson3d5096c2013-12-11 16:40:05 -0800125 DerNodePtrList& children = derSeq.getChildren();
Jeff Thompson415da1e2013-10-17 16:52:59 -0700126
127 SimpleVisitor simpleVisitor;
128
129 OID oid = ndnboost::any_cast<OID>(children[0]->accept(simpleVisitor));
130 bool critical = ndnboost::any_cast<bool>(children[1]->accept(simpleVisitor));
Jeff Thompsonb8b67582013-11-20 18:37:36 -0800131 const vector<uint8_t>& value = ndnboost::any_cast<const vector<uint8_t>&>(children[2]->accept(simpleVisitor));
Jeff Thompson415da1e2013-10-17 16:52:59 -0700132
133 CertificateExtension extension(oid, critical, value);
134
135 certData->addExtension(extension);
136}
137
138/*
139 * CertExtensionVisitor
140 */
141void
Jeff Thompson67598332013-10-17 17:57:22 -0700142CertificateExtensionVisitor::visit(DerSequence& derSeq, ndnboost::any param)
Jeff Thompson415da1e2013-10-17 16:52:59 -0700143{
Jeff Thompson3d5096c2013-12-11 16:40:05 -0800144 DerNodePtrList& children = derSeq.getChildren();
Jeff Thompson415da1e2013-10-17 16:52:59 -0700145
Jeff Thompson67598332013-10-17 17:57:22 -0700146 CertificateExtensionEntryVisitor extnEntryVisitor;
Jeff Thompson415da1e2013-10-17 16:52:59 -0700147
Jeff Thompson3d5096c2013-12-11 16:40:05 -0800148 DerNodePtrList::iterator it = children.begin();
Jeff Thompson415da1e2013-10-17 16:52:59 -0700149
150 while(it != children.end()) {
151 (*it)->accept(extnEntryVisitor, param);
152 it++;
153 }
154}
155
156} // der
157
158}