<commit_before>/*
* (C) 2009 <NAME>
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_X509_CERTIFICATES)

#if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_DSA)

#include <botan/filters.h>
#include <botan/x509self.h>
#include <botan/x509path.h>
#include <botan/x509_ca.h>
#include <botan/pkcs10.h>


#if defined(BOTAN_HAS_RSA)
  #include <botan/rsa.h>
#endif

#if defined(BOTAN_HAS_DSA)
  #include <botan/dsa.h>
#endif

#if defined(BOTAN_HAS_ECDSA)
  #include <botan/ecdsa.h>
#endif

using namespace Botan;

#include <iostream>
#include <memory>

namespace {

u64bit key_id(const Public_Key* key)
   {
   Pipe pipe(new Hash_Filter("SHA-1", 8));
   pipe.start_msg();
   pipe.write(key->algo_name());
   pipe.write(key->algorithm_identifier().parameters);
   pipe.write(key->x509_subject_public_key());
   pipe.end_msg();

   secure_vector<byte> output = pipe.read_all();

   if(output.size() != 8)
      throw Internal_Error("Public_Key::key_id: Incorrect output size");

   u64bit id = 0;
   for(u32bit j = 0; j != 8; ++j)
      id = (id << 8) | output[j];
   return id;
   }


/* Return some option sets */
X509_Cert_Options ca_opts()
   {
   X509_Cert_Options opts("Test CA/US/Botan Project/Testing");

   opts.uri = "http://bot<EMAIL>";
   opts.dns = "botan.randombit.net";
   opts.email = "<EMAIL>";

   opts.CA_key(1);

   return opts;
   }

X509_Cert_Options req_opts1()
   {
   X509_Cert_Options opts("Test User 1/US/Botan Project/Testing");

   opts.uri = "http://botan.randombit.net";
   opts.dns = "botan.randombit.net";
   opts.email = "<EMAIL>";

   return opts;
   }

X509_Cert_Options req_opts2()
   {
   X509_Cert_Options opts("Test User 2/US/Botan Project/Testing");

   opts.uri = "http://botan.randombit.net";
   opts.dns = "botan.randombit.net";
   opts.email = "<EMAIL>";

   opts.add_ex_constraint("PKIX.EmailProtection");

   return opts;
   }

u32bit check_against_copy(const Private_Key& orig,
                          RandomNumberGenerator& rng)
   {
   Private_Key* copy_priv = PKCS8::copy_key(orig, rng);
   Public_Key* copy_pub = X509::copy_key(orig);

   const std::string passphrase= "<PASSWORD>";
   DataSource_Memory enc_source(PKCS8::PEM_encode(orig, rng, passphrase));
   Private_Key* copy_priv_enc = PKCS8::load_key(enc_source, rng,
                                                passphrase);

   u64bit orig_id = key_id(&orig);
   u64bit pub_id = key_id(copy_pub);
   u64bit priv_id = key_id(copy_priv);
   u64bit priv_enc_id = key_id(copy_priv_enc);

   delete copy_pub;
   delete copy_priv;
   delete copy_priv_enc;

   if(orig_id != pub_id || orig_id != priv_id || orig_id != priv_enc_id)
      {
      std::cout << "Failed copy check for " << orig.algo_name() << std::endl;
      return 1;
      }
   return 0;
   }

}

size_t test_x509()
   {
   auto& rng = test_rng();
   const std::string hash_fn = "SHA-256";

   size_t fails = 0;

   /* Create the CA's key and self-signed cert */
   RSA_PrivateKey ca_key(rng, 2048);

   X509_Certificate ca_cert = X509::create_self_signed_cert(ca_opts(),
                                                            ca_key,
                                                            hash_fn,
                                                            rng);
   /* Create user #1's key and cert request */
   DSA_PrivateKey user1_key(rng, DL_Group("dsa/botan/2048"));

   PKCS10_Request user1_req = X509::create_cert_req(req_opts1(),
                                                    user1_key,
                                                    "SHA-1",
                                                    rng);

   /* Create user #2's key and cert request */
#if defined(BOTAN_HAS_ECDSA)
   EC_Group ecc_domain(OID("1.2.840.10045.3.1.7"));
   ECDSA_PrivateKey user2_key(rng, ecc_domain);
#else
   RSA_PrivateKey user2_key(rng, 1536);
#endif

   PKCS10_Request user2_req = X509::create_cert_req(req_opts2(),
                                                    user2_key,
                                                    hash_fn,
                                                    rng);

   /* Create the CA object */
   X509_CA ca(ca_cert, ca_key, hash_fn);

   /* Sign the requests to create the certs */
   X509_Certificate user1_cert =
      ca.sign_request(user1_req, rng,
                      X509_Time("2008-01-01"), X509_Time("2100-01-01"));

   X509_Certificate user2_cert = ca.sign_request(user2_req, rng,
                                                 X509_Time("2008-01-01"),
                                                 X509_Time("2100-01-01"));
   X509_CRL crl1 = ca.new_crl(rng);

   /* Verify the certs */
   Certificate_Store_In_Memory store;

   store.add_certificate(ca_cert);

   Path_Validation_Restrictions restrictions(false);

   Path_Validation_Result result_u1 = x509_path_validate(user1_cert, restrictions, store);
   if(!result_u1.successful_validation())
      {
      std::cout << "FAILED: User cert #1 did not validate - "
                << result_u1.result_string() << std::endl;<commit_msg>Construct X509_Time from date integers in test x509<commit_after>/*
* (C) 2009 <NAME>
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_X509_CERTIFICATES)

#if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_DSA)

#include <botan/calendar.h>
#include <botan/filters.h>
#include <botan/x509self.h>
#include <botan/x509path.h>
#include <botan/x509_ca.h>
#include <botan/pkcs10.h>

#if defined(BOTAN_HAS_RSA)
  #include <botan/rsa.h>
#endif

#if defined(BOTAN_HAS_DSA)
  #include <botan/dsa.h>
#endif

#if defined(BOTAN_HAS_ECDSA)
  #include <botan/ecdsa.h>
#endif

#include <iostream>
#include <memory>

using namespace Botan;

namespace {

X509_Time from_date(const int y, const int m, const int d)
   {
   auto t = calendar_point(y, m, d, 0, 0, 0);
   return X509_Time(t.to_std_timepoint());
   }

u64bit key_id(const Public_Key* key)
   {
   Pipe pipe(new Hash_Filter("SHA-1", 8));
   pipe.start_msg();
   pipe.write(key->algo_name());
   pipe.write(key->algorithm_identifier().parameters);
   pipe.write(key->x509_subject_public_key());
   pipe.end_msg();

   secure_vector<byte> output = pipe.read_all();

   if(output.size() != 8)
      throw Internal_Error("Public_Key::key_id: Incorrect output size");

   u64bit id = 0;
   for(u32bit j = 0; j != 8; ++j)
      id = (id << 8) | output[j];
   return id;
   }


/* Return some option sets */
X509_Cert_Options ca_opts()
   {
   X509_Cert_Options opts("Test CA/US/Botan Project/Testing");

   opts.uri = "http://botan.randombit.net";
   opts.dns = "botan.randombit.net";
   opts.email = "<EMAIL>";

   opts.CA_key(1);

   return opts;
   }

X509_Cert_Options req_opts1()
   {
   X509_Cert_Options opts("Test User 1/US/Botan Project/Testing");

   opts.uri = "http://botan.randombit.net";
   opts.dns = "botan.randombit.net";
   opts.email = "<EMAIL>";

   return opts;
   }

X509_Cert_Options req_opts2()
   {
   X509_Cert_Options opts("Test User 2/US/Botan Project/Testing");

   opts.uri = "http://botan.randombit.net";
   opts.dns = "botan.randombit.net";
   opts.email = "<EMAIL>";

   opts.add_ex_constraint("PKIX.EmailProtection");

   return opts;
   }

u32bit check_against_copy(const Private_Key& orig,
                          RandomNumberGenerator& rng)
   {
   Private_Key* copy_priv = PKCS8::copy_key(orig, rng);
   Public_Key* copy_pub = X509::copy_key(orig);

   const std::string passphrase= "<PASSWORD>! -<PASSWORD>";
   DataSource_Memory enc_source(PKCS8::PEM_encode(orig, rng, passphrase));
   Private_Key* copy_priv_enc = PKCS8::load_key(enc_source, rng,
                                                passphrase);

   u64bit orig_id = key_id(&orig);
   u64bit pub_id = key_id(copy_pub);
   u64bit priv_id = key_id(copy_priv);
   u64bit priv_enc_id = key_id(copy_priv_enc);

   delete copy_pub;
   delete copy_priv;
   delete copy_priv_enc;

   if(orig_id != pub_id || orig_id != priv_id || orig_id != priv_enc_id)
      {
      std::cout << "Failed copy check for " << orig.algo_name() << std::endl;
      return 1;
      }
   return 0;
   }

}

size_t test_x509()
   {
   auto& rng = test_rng();
   const std::string hash_fn = "SHA-256";

   size_t fails = 0;

   /* Create the CA's key and self-signed cert */
   RSA_PrivateKey ca_key(rng, 2048);

   X509_Certificate ca_cert = X509::create_self_signed_cert(ca_opts(),
                                                            ca_key,
                                                            hash_fn,
                                                            rng);
   /* Create user #1's key and cert request */
   DSA_PrivateKey user1_key(rng, DL_Group("dsa/botan/2048"));

   PKCS10_Request user1_req = X509::create_cert_req(req_opts1(),
                                                    user1_key,
                                                    "SHA-1",
                                                    rng);

   /* Create user #2's key and cert request */
#if defined(BOTAN_HAS_ECDSA)
   EC_Group ecc_domain(OID("1.2.840.10045.3.1.7"));
   ECDSA_PrivateKey user2_key(rng, ecc_domain);
#else
   RSA_PrivateKey user2_key(rng, 1536);
#endif

   PKCS10_Request user2_req = X509::create_cert_req(req_opts2(),
                                                    user2_key,
                                                    hash_fn,
                                                    rng);

   /* Create the CA object */
   X509_CA ca(ca_cert, ca_key, hash_fn);

   /* Sign the requests to create the certs */
   X509_Certificate user1_cert =
      ca.sign_request(user1_req, rng,
                      from_date(2008, 01, 01), from_date(2100, 01, 01));

   X509_Certificate user2_cert = ca.sign_request(user2_req, rng,
                                                 from_date(2008, 01, 01),
                                                 from_date(2100, 01, 01));
   X509_CRL crl1 = ca.new_crl(rng);

   /* Verify the certs */
   Certificate_Store_In_Memory store;

   store.add_certificate(ca_cert);

   Path_Validation_Restrictions restrictions(false);

   Path_Validation_Result result_u1 = x509_path_validate(user1_cert, restrictions, store);
   if(!result_u1.successful_validation())
      {
      std::cout << "FAILED: User cert #1 did not validate - "
                << result_u1.result_string() << std::endl;
{'fraction_non_alphanumeric': 0.12132846995584565, 'fraction_numerical': 0.03945095027836437, 'mean_word_length': 2.276415094339623, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5181935', 'n_tokens_mistral': 3833, 'n_tokens_neox': 3504, 'n_words': 689}