Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -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. |
| 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 8 | #include <ndn-cxx/security/key-chain.hpp> |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 9 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 10 | #include <ndn-cxx/util/io.hpp> |
| 11 | #include <iostream> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 12 | |
| 13 | using namespace ndn; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 14 | using ndn::security::Certificate; |
| 15 | using ndn::security::pib::Identity; |
| 16 | using ndn::security::pib::Key; |
| 17 | |
| 18 | void |
| 19 | generateCertificate(KeyChain& keyChain, Name name) { |
| 20 | try { |
| 21 | keyChain.getPib().getIdentity(name); |
| 22 | } catch (ndn::security::pib::Pib::Error&) { |
| 23 | Identity id = keyChain.createIdentity(name); |
| 24 | Key key = id.getDefaultKey(); |
| 25 | Certificate cert = Certificate(key.getDefaultCertificate()); |
| 26 | |
| 27 | ndn::SignatureInfo signatureInfo; |
| 28 | signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod( |
| 29 | time::system_clock::now(), time::system_clock::now() + time::days(7300))); |
| 30 | |
| 31 | keyChain.sign(cert, security::signingByIdentity("/ndn/test").setSignatureInfo(signatureInfo)); |
| 32 | keyChain.setDefaultCertificate(key, cert); |
| 33 | |
| 34 | std::cout << "Generated cert " << cert.getName() << " with KeyLocator " << cert.getKeyLocator().value() << std::endl; |
| 35 | } |
| 36 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 37 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 38 | int |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 39 | main() |
| 40 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 41 | KeyChain keyChain; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 42 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 43 | // Root certificate |
| 44 | generateCertificate(keyChain, "/ndn/test"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 45 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 46 | // Test certificates |
| 47 | generateCertificate(keyChain, "/ndn/test/alice"); |
| 48 | generateCertificate(keyChain, "/ndn/test/bob"); |
| 49 | generateCertificate(keyChain, "/ndn/test/cathy"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 50 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 51 | std::ofstream file("security/test-anchor.cert"); |
| 52 | ndn::io::saveBlock(keyChain.getPib().getIdentity("/ndn/test") |
| 53 | .getDefaultKey().getDefaultCertificate().wireEncode(), |
| 54 | file); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 55 | } |