blob: 09861d3103ad924de25a6b9219046d5791a4cf08 [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>
Yingdi Yu348f5ea2014-03-01 14:47:25 -08009
10using namespace ndn;
11
Yingdi Yu0b0a7362014-08-05 16:31:30 -070012int
Yingdi Yu348f5ea2014-03-01 14:47:25 -080013main()
14{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070015 KeyChain keyChain("sqlite3", "file");
Yingdi Yu348f5ea2014-03-01 14:47:25 -080016 std::vector<CertificateSubjectDescription> subjectDescription;
17
18 Name root("/ndn");
19 Name rootCertName = keyChain.createIdentity(root);
20
21 Name test("/ndn/test");
Yingdi Yu0b0a7362014-08-05 16:31:30 -070022 Name testKeyName = keyChain.generateRsaKeyPairAsDefault(test, true);
23 shared_ptr<IdentityCertificate> testCert =
24 keyChain.prepareUnsignedIdentityCertificate(testKeyName, root,
Yingdi Yua7876722014-03-25 14:46:55 -070025 time::system_clock::now(),
Yingdi Yu0b0a7362014-08-05 16:31:30 -070026 time::system_clock::now() + time::days(7300),
Yingdi Yua7876722014-03-25 14:46:55 -070027 subjectDescription);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080028 keyChain.signByIdentity(*testCert, root);
29 keyChain.addCertificateAsIdentityDefault(*testCert);
30
31 Name alice("/ndn/test/alice");
32 if(!keyChain.doesIdentityExist(alice))
33 {
Yingdi Yu0b0a7362014-08-05 16:31:30 -070034 Name aliceKeyName = keyChain.generateRsaKeyPairAsDefault(alice, true);
35 shared_ptr<IdentityCertificate> aliceCert =
36 keyChain.prepareUnsignedIdentityCertificate(aliceKeyName, test,
Yingdi Yua7876722014-03-25 14:46:55 -070037 time::system_clock::now(),
Yingdi Yu0b0a7362014-08-05 16:31:30 -070038 time::system_clock::now() + time::days(7300),
Yingdi Yua7876722014-03-25 14:46:55 -070039 subjectDescription);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080040 keyChain.signByIdentity(*aliceCert, test);
41 keyChain.addCertificateAsIdentityDefault(*aliceCert);
42 }
43
44 Name bob("/ndn/test/bob");
45 if(!keyChain.doesIdentityExist(bob))
46 {
Yingdi Yu0b0a7362014-08-05 16:31:30 -070047 Name bobKeyName = keyChain.generateRsaKeyPairAsDefault(bob, true);
48 shared_ptr<IdentityCertificate> bobCert =
49 keyChain.prepareUnsignedIdentityCertificate(bobKeyName, test,
50 time::system_clock::now(),
51 time::system_clock::now() + time::days(7300),
52 subjectDescription);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080053 keyChain.signByIdentity(*bobCert, test);
54 keyChain.addCertificateAsIdentityDefault(*bobCert);
55 }
56
57 Name cathy("/ndn/test/cathy");
58 if(!keyChain.doesIdentityExist(cathy))
59 {
Yingdi Yu0b0a7362014-08-05 16:31:30 -070060 Name cathyKeyName = keyChain.generateRsaKeyPairAsDefault(cathy, true);
61 shared_ptr<IdentityCertificate> cathyCert =
62 keyChain.prepareUnsignedIdentityCertificate(cathyKeyName, test,
63 time::system_clock::now(),
64 time::system_clock::now() + time::days(7300),
65 subjectDescription);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080066 keyChain.signByIdentity(*cathyCert, test);
67 keyChain.addCertificateAsIdentityDefault(*cathyCert);
68 }
69}