Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 4 | * BSD license, See the LICENSE file for more information |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 5 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 6 | */ |
| 7 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 8 | #ifndef NDNSEC_CERT_GEN_HPP |
| 9 | #define NDNSEC_CERT_GEN_HPP |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 10 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 11 | #include "ndnsec-util.hpp" |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 12 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 13 | int |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 14 | ndnsec_cert_gen(int argc, char** argv) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 15 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 16 | using boost::tokenizer; |
| 17 | using boost::escaped_list_separator; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 18 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 19 | using namespace ndn; |
| 20 | namespace po = boost::program_options; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 21 | |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 22 | std::string notBeforeStr; |
| 23 | std::string notAfterStr; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 24 | std::string subjectName; |
| 25 | std::string requestFile("-"); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 26 | std::string signId; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 27 | std::string subjectInfo; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 28 | bool isSelfSigned = false; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 29 | bool isNack = false; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 30 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 31 | po::options_description description("General Usage\n ndnsec cert-gen [-h] [-S date] [-E date] [-N subject-name] [-I subject-info] [-s sign-id] request\nGeneral options"); |
| 32 | description.add_options() |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 33 | ("help,h", "produce help message") |
| 34 | ("not-before,S", po::value<std::string>(¬BeforeStr), "certificate starting date, YYYYMMDDhhmmss") |
| 35 | ("not-after,E", po::value<std::string>(¬AfterStr), "certificate ending date, YYYYMMDDhhmmss") |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 36 | ("subject-name,N", po::value<std::string>(&subjectName), "subject name") |
| 37 | ("subject-info,I", po::value<std::string>(&subjectInfo), "subject info, pairs of OID and string description: \"2.5.4.10 'University of California, Los Angeles'\"") |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 38 | ("nack", "Generate revocation certificate (NACK)") |
| 39 | ("sign-id,s", po::value<std::string>(&signId), "signing Identity, self-signed if not specified") |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 40 | ("request,r", po::value<std::string>(&requestFile), "request file name, - for stdin") |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 41 | ; |
| 42 | |
| 43 | po::positional_options_description p; |
| 44 | p.add("request", 1); |
| 45 | |
| 46 | po::variables_map vm; |
| 47 | try |
| 48 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 49 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 50 | vm); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 51 | po::notify(vm); |
| 52 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 53 | catch (const std::exception& e) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 54 | { |
| 55 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 56 | return 1; |
| 57 | } |
| 58 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 59 | if (vm.count("help") != 0) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 60 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 61 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 62 | return 0; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 65 | if (vm.count("sign-id") == 0) |
| 66 | { |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 67 | isSelfSigned = true; |
| 68 | } |
| 69 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 70 | if (vm.count("nack") != 0) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 71 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 72 | isNack = true; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | std::vector<CertificateSubjectDescription> otherSubDescrypt; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 76 | tokenizer<escaped_list_separator<char> > subjectInfoItems |
| 77 | (subjectInfo, escaped_list_separator<char> ("\\", " \t", "'\"")); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 78 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 79 | tokenizer<escaped_list_separator<char> >::iterator it = |
| 80 | subjectInfoItems.begin(); |
| 81 | |
| 82 | while (it != subjectInfoItems.end()) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 83 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 84 | std::string oid = *it; |
| 85 | |
| 86 | it++; |
| 87 | if (it == subjectInfoItems.end()) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 88 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 89 | std::cerr << "ERROR: unmatched info for oid [" << oid << "]" << std::endl; |
| 90 | return 1; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 91 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 92 | |
| 93 | std::string value = *it; |
| 94 | |
| 95 | otherSubDescrypt.push_back(CertificateSubjectDescription(oid, value)); |
| 96 | |
| 97 | it++; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 100 | time::system_clock::TimePoint notBefore; |
| 101 | time::system_clock::TimePoint notAfter; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 102 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 103 | if (vm.count("not-before") == 0) |
| 104 | { |
| 105 | notBefore = time::system_clock::now(); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | notBefore = time::fromIsoString(notBeforeStr.substr(0, 8) + "T" + |
| 110 | notBeforeStr.substr(8, 6)); |
| 111 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 112 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 113 | if (vm.count("not-after") == 0) |
| 114 | { |
| 115 | notAfter = notBefore + time::days(365); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + |
| 120 | notAfterStr.substr(8, 6)); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 121 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 122 | if (notAfter < notBefore) |
| 123 | { |
| 124 | std::cerr << "not-before is later than not-after" << std::endl; |
| 125 | return 1; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (vm.count("request") == 0) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 130 | { |
| 131 | std::cerr << "request file must be specified" << std::endl; |
| 132 | return 1; |
| 133 | } |
| 134 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 135 | shared_ptr<IdentityCertificate> selfSignedCertificate |
| 136 | = getIdentityCertificate(requestFile); |
| 137 | |
| 138 | if (!static_cast<bool>(selfSignedCertificate)) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 139 | { |
| 140 | std::cerr << "ERROR: input error" << std::endl; |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | Name keyName = selfSignedCertificate->getPublicKeyName(); |
| 145 | Name signIdName; |
| 146 | Name certName; |
| 147 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 148 | if (isSelfSigned) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 149 | { |
| 150 | certName = keyName.getPrefix(keyName.size()-1); |
| 151 | certName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion(); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | signIdName = Name(signId); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 156 | |
| 157 | int count = 0; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 158 | Name::const_iterator i = keyName.begin(); |
| 159 | Name::const_iterator j = signIdName.begin(); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 160 | for (; i != keyName.end() && j != signIdName.end(); i++, j++) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 161 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 162 | if (*i != *j) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 163 | break; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 164 | |
| 165 | count++; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 166 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 167 | |
| 168 | if (j != signIdName.end() || i == keyName.end()) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 169 | { |
| 170 | std::cerr << "wrong signing identity!" << std::endl; |
| 171 | return 1; |
| 172 | } |
| 173 | |
| 174 | certName = keyName.getSubName(0, count); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 175 | certName.append("KEY") |
| 176 | .append(keyName.getSubName(count, keyName.size() - count)) |
| 177 | .append("ID-CERT") |
| 178 | .appendVersion (); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | Block wire; |
| 182 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 183 | if (!isNack) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 184 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 185 | if (vm.count("subject-name") == 0) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 186 | { |
| 187 | std::cerr << "subject_name must be specified" << std::endl; |
| 188 | return 1; |
| 189 | } |
| 190 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 191 | CertificateSubjectDescription subDescryptName("2.5.4.41", subjectName); |
| 192 | IdentityCertificate certificate; |
| 193 | certificate.setName(certName); |
| 194 | certificate.setNotBefore(notBefore); |
| 195 | certificate.setNotAfter(notAfter); |
| 196 | certificate.setPublicKeyInfo(selfSignedCertificate->getPublicKeyInfo()); |
| 197 | certificate.addSubjectDescription(subDescryptName); |
| 198 | for (size_t i = 0; i < otherSubDescrypt.size(); i++) |
| 199 | certificate.addSubjectDescription(otherSubDescrypt[i]); |
| 200 | certificate.encode(); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 201 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 202 | KeyChain keyChain; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 203 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 204 | if (isSelfSigned) |
| 205 | keyChain.selfSign(certificate); |
| 206 | else |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 207 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 208 | Name signingCertificateName = |
| 209 | keyChain.getDefaultCertificateNameForIdentity(Name(signId)); |
| 210 | |
| 211 | keyChain.sign(certificate, signingCertificateName); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 212 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 213 | wire = certificate.wireEncode(); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 214 | } |
| 215 | else |
| 216 | { |
| 217 | Data revocationCert; |
| 218 | // revocationCert.setContent(void*, 0); // empty content |
| 219 | revocationCert.setName(certName); |
| 220 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 221 | KeyChain keyChain; |
| 222 | |
| 223 | Name signingCertificateName = |
| 224 | keyChain.getDefaultCertificateNameForIdentity(signId); |
| 225 | |
| 226 | keyChain.sign(revocationCert, signingCertificateName); |
| 227 | wire = revocationCert.wireEncode(); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 230 | try |
| 231 | { |
| 232 | using namespace CryptoPP; |
| 233 | StringSource ss(wire.wire(), wire.size(), true, |
| 234 | new Base64Encoder(new FileSink(std::cout), true, 64)); |
| 235 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 236 | catch (const CryptoPP::Exception& e) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 237 | { |
| 238 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 239 | return 1; |
| 240 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 241 | |
| 242 | return 0; |
| 243 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 244 | |
| 245 | #endif //NDNSEC_CERT_GEN_HPP |