Ejemplo n.º 1
0
inline shared_ptr<IdentityCertificate>
SecPublicInfo::defaultCertificate()
{
  return getDefaultCertificate();
}
Ejemplo n.º 2
0
int
ndnsec_get_default(int argc, char** argv)
{
  namespace po = boost::program_options;

  bool wantDefaultKey = false;
  bool wantDefaultCert = false;
  bool isQuiet = false;
  Name identityName;
  Name keyName;

  po::options_description description(
    "Usage: ndnsec get-default [-h] [-k|-c] [-i ID|-K KEY] [-q]\n"
    "\n"
    "Options");
  description.add_options()
    ("help,h", "produce help message")
    ("default-key,k",  po::bool_switch(&wantDefaultKey), "show default key, instead of identity")
    ("default-cert,c", po::bool_switch(&wantDefaultCert), "show default certificate, instead of identity")
    ("identity,i",     po::value<Name>(&identityName), "target identity")
    ("key,K",          po::value<Name>(&keyName), "target key")
    ("quiet,q",        po::bool_switch(&isQuiet), "do not print trailing newline")
    ;

  po::variables_map vm;
  try {
    po::store(po::parse_command_line(argc, argv, description), vm);
    po::notify(vm);
  }
  catch (const std::exception& e) {
    std::cerr << "ERROR: " << e.what() << "\n\n"
              << description << std::endl;
    return 2;
  }

  if (vm.count("help") > 0) {
    std::cout << description << std::endl;
    return 0;
  }

  if (wantDefaultKey && wantDefaultCert) {
    std::cerr << "ERROR: cannot specify both '--default-key' and '--default-cert'" << std::endl;
    return 2;
  }

  if (vm.count("identity") && vm.count("key")) {
    std::cerr << "ERROR: cannot specify both '--identity' and '--key'" << std::endl;
    return 2;
  }

  security::v2::KeyChain keyChain;

  if (vm.count("key") > 0) {
    if (wantDefaultCert) {
      auto cert = keyChain.getPib()
                  .getIdentity(security::v2::extractIdentityFromKeyName(keyName))
                  .getKey(keyName)
                  .getDefaultCertificate();
      std::cout << cert.getName();
      if (!isQuiet) {
        std::cout << std::endl;
      }
      return 0;
    }
    return 2;
  }
  else if (vm.count("identity") > 0) {
    auto key = keyChain.getPib()
               .getIdentity(identityName)
               .getDefaultKey();
    if (wantDefaultKey) {
      std::cout << key.getName();
      if (!isQuiet)
        std::cout << std::endl;
      return 0;
    }
    if (wantDefaultCert) {
      std::cout << key.getDefaultCertificate().getName();
      if (!isQuiet)
        std::cout << std::endl;
      return 0;
    }
    return 2;
  }
  else {
    auto identity = keyChain.getPib()
                    .getDefaultIdentity();
    if (wantDefaultKey) {
      std::cout << identity.getDefaultKey().getName();
    }
    else if (wantDefaultCert) {
      std::cout << identity.getDefaultKey().getDefaultCertificate().getName();
    }
    else {
      std::cout << identity.getName();
    }
    if (!isQuiet)
      std::cout << std::endl;
    return 0;
  }
}