blob: 594178342e36f40a0726d9e347734737a620422b [file] [log] [blame]
Yingdi Yu348f5ea2014-03-01 14:47:25 -08001/* -*- 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 Yu0b0a7362014-08-05 16:31:30 -07008#include <ndn-cxx/security/key-chain.hpp>
Varun Patil3d850902020-11-23 12:19:14 +05309#include <ndn-cxx/security/signing-helpers.hpp>
10#include <ndn-cxx/util/io.hpp>
11#include <iostream>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080012
13using namespace ndn;
Varun Patil3d850902020-11-23 12:19:14 +053014using ndn::security::Certificate;
15using ndn::security::pib::Identity;
16using ndn::security::pib::Key;
17
18void
19generateCertificate(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 Yu348f5ea2014-03-01 14:47:25 -080037
Yingdi Yu0b0a7362014-08-05 16:31:30 -070038int
Yingdi Yu348f5ea2014-03-01 14:47:25 -080039main()
40{
Varun Patil3d850902020-11-23 12:19:14 +053041 KeyChain keyChain;
Yingdi Yu348f5ea2014-03-01 14:47:25 -080042
Varun Patil3d850902020-11-23 12:19:14 +053043 // Root certificate
44 generateCertificate(keyChain, "/ndn/test");
Yingdi Yu348f5ea2014-03-01 14:47:25 -080045
Varun Patil3d850902020-11-23 12:19:14 +053046 // Test certificates
47 generateCertificate(keyChain, "/ndn/test/alice");
48 generateCertificate(keyChain, "/ndn/test/bob");
49 generateCertificate(keyChain, "/ndn/test/cathy");
Yingdi Yu348f5ea2014-03-01 14:47:25 -080050
Varun Patil3d850902020-11-23 12:19:14 +053051 std::ofstream file("security/test-anchor.cert");
52 ndn::io::saveBlock(keyChain.getPib().getIdentity("/ndn/test")
53 .getDefaultKey().getDefaultCertificate().wireEncode(),
54 file);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080055}