aboutsummaryrefslogtreecommitdiffstats
path: root/src/cmd/self_sig.cpp
blob: 0a6c59d87fb4d82551fa12c3e00fc4f5c9be3e63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "apps.h"

#if defined(BOTAN_HAS_X509_CERTIFICATES)
#include <botan/x509self.h>
#include <botan/rsa.h>
#include <botan/dsa.h>
using namespace Botan;

#include <fstream>
#include <memory>

namespace {

int self_sig(int argc, char* argv[])
   {
   if(argc != 7)
      {
      std::cout << "Usage: " << argv[0]
                << " passphrase [CA|user] name country_code organization email"
                << std::endl;
      return 1;
      }

   std::string CA_flag = argv[2];
   bool do_CA = false;

   if(CA_flag == "CA") do_CA = true;
   else if(CA_flag == "user") do_CA = false;
   else
      {
      std::cout << "Bad flag for CA/user switch: " << CA_flag << std::endl;
      return 1;
      }

   try
      {
      AutoSeeded_RNG rng;

      RSA_PrivateKey key(rng, 2048);
      //DL_Group group(rng, DL_Group::DSA_Kosherizer, 2048, 256);

      //DSA_PrivateKey key(rng, group);

      std::ofstream priv_key("private.pem");
      priv_key << PKCS8::PEM_encode(key, rng, argv[1]);

      X509_Cert_Options opts;

      opts.common_name = argv[3];
      opts.country = argv[4];
      opts.organization = argv[5];
      opts.email = argv[6];
      /* Fill in other values of opts here */

      //opts.xmpp = "lloyd@randombit.net";

      if(do_CA)
         opts.CA_key();

      X509_Certificate cert =
         X509::create_self_signed_cert(opts, key, "SHA-256", rng);

      std::ofstream cert_file("cert.pem");
      cert_file << cert.PEM_encode();
   }
   catch(std::exception& e)
      {
      std::cout << "Exception: " << e.what() << std::endl;
      return 1;
      }

   return 0;
   }

REGISTER_APP(self_sig);

}

#endif