blob: ab618a51fa93c68c71c0175afcce5b38f8b76094 [file] [log] [blame]
Yingdi Yu5edf97d2014-06-15 11:35:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
22 */
23
24#ifndef NDNSEC_CERT_REVOKE_HPP
25#define NDNSEC_CERT_REVOKE_HPP
26
27#include "ndnsec-util.hpp"
28
29int
30ndnsec_cert_revoke(int argc, char** argv)
31{
32 using namespace ndn;
33 namespace po = boost::program_options;
34
35 std::string requestFile("-");
36
37 po::options_description description("General Usage\n ndnsec cert-revoke [-h] request\nGeneral options");
38 description.add_options()
39 ("help,h", "produce help message")
40 ("request,r", po::value<std::string>(&requestFile), "file name of the certificate to revoke, - for stdin")
41 ;
42
43 po::positional_options_description p;
44 p.add("request", 1);
45
46 po::variables_map vm;
47 try
48 {
49 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
50 vm);
51 po::notify(vm);
52 }
53 catch (const std::exception& e)
54 {
55 std::cerr << "ERROR: " << e.what() << std::endl;
56 return 1;
57 }
58
59 if (vm.count("help") != 0)
60 {
61 std::cerr << description << std::endl;
62 return 0;
63 }
64
65 if (vm.count("request") == 0)
66 {
67 std::cerr << "request file must be specified" << std::endl;
68 return 1;
69 }
70
71 shared_ptr<IdentityCertificate> revokedCertificate
72 = getIdentityCertificate(requestFile);
73
74 if (!static_cast<bool>(revokedCertificate))
75 {
76 std::cerr << "ERROR: input error" << std::endl;
77 return 1;
78 }
79
80 KeyChain keyChain;
81 Block wire;
82
83 try
84 {
85 Name certName = revokedCertificate->getName();
86 certName.append("REVOKED");
87
88 Data revocationCert;
89 revocationCert.setName(certName);
90
91 Name keyName;
92
93 const Signature& signature = revokedCertificate->getSignature();
94 if (signature.getType() == Signature::Sha256WithRsa)
95 {
96 SignatureSha256WithRsa sigSha256Rsa(signature);
97 Name keyLocatorName = sigSha256Rsa.getKeyLocator().getName();
98
99 keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName);
100 }
101 else
102 {
103 std::cerr << "ERROR: Unsupported Signature Type!" << std::endl;
104 return 1;
105 }
106
107 if (keyChain.doesPublicKeyExist(keyName))
108 {
109 Name signingCertificateName = keyChain.getDefaultCertificateNameForKey(keyName);
110 keyChain.sign(revocationCert, signingCertificateName);
111 }
112 else
113 {
114 std::cerr << "ERROR: Cannot find the signing key!" << std::endl;
115 return 1;
116 }
117
118 wire = revocationCert.wireEncode();
119 }
120 catch (Signature::Error& e)
121 {
122 std::cerr << "ERROR: No valid signature!" << std::endl;
123 return 1;
124 }
125 catch (KeyLocator::Error& e)
126 {
127 std::cerr << "ERROR: No valid KeyLocator!" << std::endl;
128 return 1;
129 }
130 catch (IdentityCertificate::Error& e)
131 {
132 std::cerr << "ERROR: Cannot determine the signing key!" << std::endl;
133 return 1;
134 }
135
136 try
137 {
138 using namespace CryptoPP;
139 // StringSource ss(wire.wire(), wire.size(), true,
140 // new Base64Encoder(new FileSink(std::cout), true, 64));
141 StringSource ss(wire.wire(), wire.size(), true,
142 new FileSink(std::cout));
143 }
144 catch (const CryptoPP::Exception& e)
145 {
146 std::cerr << "ERROR: " << e.what() << std::endl;
147 return 1;
148 }
149
150 return 0;
151}
152
153#endif //NDNSEC_CERT_REVOKE_HPP