/*
  Implement the functionality of a simple CA: read in a CA certificate,
  the associated private key, and a PKCS #10 certificate request. Sign the
  request and print out the new certificate.

  File names are hardcoded for simplicity.
    cert.pem:    The CA's certificate (perhaps created by self_sig)
    private.pem: The CA's private key
    req.pem:     The user's PKCS #10 certificate request

  Written by Jack Lloyd, May 19, 2003

  This file is in the public domain.
*/

#include <botan/x509_ca.h>
using namespace Botan;

#include <iostream>

int main(int argc, char* argv[])
   {
   try {
      LibraryInitializer init;

      if(argc != 2)
         {
         std::cout << "Usage: " << argv[0] << " passphrase" << std::endl;
         return 1;
         }

      X509_Certificate ca_cert("cert.pem");
      PKCS10_Request req("req.pem");
      PKCS8_PrivateKey* privkey = PKCS8::load_key("private.pem", argv[1]);

      X509_CA ca(ca_cert, *privkey);

      X509_Certificate new_cert = ca.sign_request(req);

      std::cout << new_cert.PEM_encode();

      delete privkey;
   }
   catch(std::exception& e)
      {
      std::cout << e.what() << std::endl;
      return 1;
      }
   return 0;
   }
