blob: 7edf794e2b9334463e6c11b3d941a50423b71c5b [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;
Jeff Thompson415da1e2013-10-17 16:52:59 -070021
22INIT_LOGGER("ndn.der.CertificateDataVisitor");
23
24namespace ndn {
25
26namespace der {
27
28/*
29 * CertificateDataVisitor
30 */
31void
32CertificateDataVisitor::visit(DerSequence& derSeq, ndnboost::any param)
33{
34 // _LOG_DEBUG("CertificateDataVisitor::visit");
35
Jeff Thompson3d5096c2013-12-11 16:40:05 -080036 DerNodePtrList& children = derSeq.getChildren();
Jeff Thompson67598332013-10-17 17:57:22 -070037 CertificateValidityVisitor validityVisitor;
Jeff Thompson415da1e2013-10-17 16:52:59 -070038 children[0]->accept(validityVisitor, param);
Jeff Thompson67598332013-10-17 17:57:22 -070039 CertificateSubjectVisitor subjectVisitor;
Jeff Thompson415da1e2013-10-17 16:52:59 -070040 children[1]->accept(subjectVisitor, param);
41 PublicKeyVisitor pubkeyVisitor;
42 Certificate* certData = ndnboost::any_cast<Certificate*>(param);
Jeff Thompsonce115762013-12-18 14:59:56 -080043 certData->setPublicKeyInfo(*ndnboost::any_cast<ptr_lib::shared_ptr<PublicKey> >(children[2]->accept(pubkeyVisitor)));
Jeff Thompson415da1e2013-10-17 16:52:59 -070044
45 if(children.size() > 3)
46 {
Jeff Thompson67598332013-10-17 17:57:22 -070047 CertificateExtensionVisitor extnVisitor;
Jeff Thompson415da1e2013-10-17 16:52:59 -070048 children[3]->accept(extnVisitor, param);
49 }
50}
51
52/*
53 * CertValidityVisitor
54 */
55void
Jeff Thompson67598332013-10-17 17:57:22 -070056CertificateValidityVisitor::visit(DerSequence& derSeq, ndnboost::any param)
Jeff Thompson415da1e2013-10-17 16:52:59 -070057{
58 // _LOG_DEBUG("CertValidityVisitor::visit");
59
60 Certificate* certData = ndnboost::any_cast<Certificate*>(param);
61
Jeff Thompson3d5096c2013-12-11 16:40:05 -080062 DerNodePtrList& children = derSeq.getChildren();
Jeff Thompson415da1e2013-10-17 16:52:59 -070063
64 SimpleVisitor simpleVisitor;
65
66 MillisecondsSince1970 notBefore = ndnboost::any_cast<MillisecondsSince1970>(children[0]->accept(simpleVisitor));
67 MillisecondsSince1970 notAfter = ndnboost::any_cast<MillisecondsSince1970>(children[1]->accept(simpleVisitor));
68
69 // _LOG_DEBUG("parsed notBefore: " << notBefore);
70 // _LOG_DEBUG("parsed notAfter: " << notAfter);
71
72 certData->setNotBefore(notBefore);
73 certData->setNotAfter(notAfter);
74}
75
76/*
77 * CertSubDescryptVisitor
78 */
79void
Jeff Thompson67598332013-10-17 17:57:22 -070080CertificateSubjectDescriptionVisitor::visit(DerSequence& derSeq, ndnboost::any param)
Jeff Thompson415da1e2013-10-17 16:52:59 -070081{
82 Certificate* certData = ndnboost::any_cast<Certificate*>(param);
83
Jeff Thompson3d5096c2013-12-11 16:40:05 -080084 DerNodePtrList& children = derSeq.getChildren();
Jeff Thompson415da1e2013-10-17 16:52:59 -070085
86 SimpleVisitor simpleVisitor;
87
88 OID oid = ndnboost::any_cast<OID>(children[0]->accept(simpleVisitor));
89 string value = ndnboost::any_cast<string>(children[1]->accept(simpleVisitor));
90
91 CertificateSubjectDescription subDescrypt(oid, value);
92
93 certData->addSubjectDescription(subDescrypt);
94}
95
96/*
97 * CertSubjectVisitor
98 */
99void
Jeff Thompson67598332013-10-17 17:57:22 -0700100CertificateSubjectVisitor::visit(DerSequence& derSeq, ndnboost::any param)
Jeff Thompson415da1e2013-10-17 16:52:59 -0700101{
102 // _LOG_DEBUG("CertSubjectVisitor::visit");
103
Jeff Thompson3d5096c2013-12-11 16:40:05 -0800104 DerNodePtrList& children = derSeq.getChildren();
Jeff Thompson415da1e2013-10-17 16:52:59 -0700105
Jeff Thompson67598332013-10-17 17:57:22 -0700106 CertificateSubjectDescriptionVisitor descryptVisitor;
Jeff Thompson415da1e2013-10-17 16:52:59 -0700107
Jeff Thompson3d5096c2013-12-11 16:40:05 -0800108 DerNodePtrList::iterator it = children.begin();
Jeff Thompson415da1e2013-10-17 16:52:59 -0700109
110 while(it != children.end()) {
111 (*it)->accept(descryptVisitor, param);
112 it++;
113 }
114}
115
116/*
117 * CertExtnEntryVisitor
118 */
119void
Jeff Thompson67598332013-10-17 17:57:22 -0700120CertificateExtensionEntryVisitor::visit(DerSequence& derSeq, ndnboost::any param)
Jeff Thompson415da1e2013-10-17 16:52:59 -0700121{
122 Certificate* certData = ndnboost::any_cast<Certificate*>(param);
123
Jeff Thompson3d5096c2013-12-11 16:40:05 -0800124 DerNodePtrList& children = derSeq.getChildren();
Jeff Thompson415da1e2013-10-17 16:52:59 -0700125
126 SimpleVisitor simpleVisitor;
127
128 OID oid = ndnboost::any_cast<OID>(children[0]->accept(simpleVisitor));
129 bool critical = ndnboost::any_cast<bool>(children[1]->accept(simpleVisitor));
Jeff Thompsonb8b67582013-11-20 18:37:36 -0800130 const vector<uint8_t>& value = ndnboost::any_cast<const vector<uint8_t>&>(children[2]->accept(simpleVisitor));
Jeff Thompson415da1e2013-10-17 16:52:59 -0700131
132 CertificateExtension extension(oid, critical, value);
133
134 certData->addExtension(extension);
135}
136
137/*
138 * CertExtensionVisitor
139 */
140void
Jeff Thompson67598332013-10-17 17:57:22 -0700141CertificateExtensionVisitor::visit(DerSequence& derSeq, ndnboost::any param)
Jeff Thompson415da1e2013-10-17 16:52:59 -0700142{
Jeff Thompson3d5096c2013-12-11 16:40:05 -0800143 DerNodePtrList& children = derSeq.getChildren();
Jeff Thompson415da1e2013-10-17 16:52:59 -0700144
Jeff Thompson67598332013-10-17 17:57:22 -0700145 CertificateExtensionEntryVisitor extnEntryVisitor;
Jeff Thompson415da1e2013-10-17 16:52:59 -0700146
Jeff Thompson3d5096c2013-12-11 16:40:05 -0800147 DerNodePtrList::iterator it = children.begin();
Jeff Thompson415da1e2013-10-17 16:52:59 -0700148
149 while(it != children.end()) {
150 (*it)->accept(extnEntryVisitor, param);
151 it++;
152 }
153}
154
155} // der
156
157}