bool TestExtOpenssl::test_openssl_pkcs7_sign() {
  Variant privkey = f_openssl_pkey_new();
  VERIFY(!privkey.isNull());
  Variant csr = f_openssl_csr_new(null, privkey);
  VERIFY(!csr.isNull());
  Variant scert = f_openssl_csr_sign(csr, null, privkey, 365);
  Variant pubkey = f_openssl_csr_get_public_key(csr);
  VERIFY(!pubkey.isNull());

  String data = "some secret data";
  const char *infile = "test/test_pkcs7.in";
  const char *outfile = "test/test_pkcs7.out";
  f_unlink(infile);
  f_unlink(outfile);
  f_file_put_contents(infile, data);

  VERIFY(f_openssl_pkcs7_sign
         (infile, outfile, scert, privkey,
          CREATE_MAP2("To", "*****@*****.**","From", "*****@*****.**")));

  const char *tmp = "test/test_x509.tmp";
  f_unlink(tmp);
  VS(f_file_get_contents(tmp), false);
  VERIFY(f_openssl_x509_export_to_file(scert, tmp));

  VS(f_openssl_pkcs7_verify(outfile, 0, infile, Variant(tmp).toArray()), true);
  f_unlink(infile);
  f_unlink(outfile);
  f_unlink(tmp);
  return Count(true);
}
bool TestExtOpenssl::test_openssl_csr_sign() {
  Array dn(ArrayInit(7, false).
           set(0, "countryName", "XX").
           set(1, "stateOrProvinceName", "State").
           set(2, "localityName", "SomewhereCity").
           set(3, "organizationName", "MySelf").
           set(4, "organizationalUnitName", "Whatever").
           set(5, "commonName", "mySelf").
           set(6, "emailAddress", "*****@*****.**").
           create());
  String privkeypass = "******";
  int numberofdays = 365;

  Variant privkey = f_openssl_pkey_new();
  VERIFY(!privkey.isNull());
  Variant csr = f_openssl_csr_new(dn, privkey);
  VERIFY(!csr.isNull());
  Variant scert = f_openssl_csr_sign(csr, null, privkey, numberofdays);
  Variant publickey, privatekey, csrStr;
  f_openssl_x509_export(scert, ref(publickey));
  f_openssl_pkey_export(privkey, ref(privatekey), privkeypass);
  f_openssl_csr_export(csr, ref(csrStr));

  //f_var_dump(privatekey); f_var_dump(publickey); f_var_dump(csrStr);
  VERIFY(privatekey.toString().size() > 500);
  VERIFY(publickey.toString().size() > 800);
  VERIFY(csrStr.toString().size() > 500);

  return Count(true);
}
bool TestExtOpenssl::test_openssl_csr_get_subject() {
  Variant csr = f_openssl_csr_new(uninit_null(), uninit_null());
  VERIFY(!csr.isNull());
  VERIFY(equal(f_openssl_csr_get_subject(csr)[s_O],
               String("Internet Widgits Pty Ltd")));
  return Count(true);
}
bool TestExtOpenssl::test_openssl_csr_get_public_key() {
  Variant csr = f_openssl_csr_new(null, null);
  VERIFY(!csr.isNull());
  Variant publickey = f_openssl_csr_get_public_key(csr);
  VERIFY(!same(publickey, false));
  VERIFY(!publickey.isNull());
  return Count(true);
}
bool TestExtOpenssl::test_openssl_x509_check_private_key() {
  Variant privkey = f_openssl_pkey_new();
  VERIFY(!privkey.isNull());
  Variant csr = f_openssl_csr_new(null, privkey);
  VERIFY(!csr.isNull());
  Variant scert = f_openssl_csr_sign(csr, null, privkey, 365);
  VERIFY(f_openssl_x509_check_private_key(scert, privkey));
  return Count(true);
}
bool TestExtOpenssl::test_openssl_csr_export_to_file() {
  Variant csr = f_openssl_csr_new(null, null);
  VERIFY(!csr.isNull());

  const char *tmp = "test/test_csr.tmp";
  f_unlink(tmp);
  VS(f_file_get_contents(tmp), false);
  f_openssl_csr_export_to_file(csr, tmp);
  VERIFY(f_file_get_contents(tmp).toString().size() > 400);
  f_unlink(tmp);

  return Count(true);
}
bool TestExtOpenssl::test_openssl_sign() {
  Variant privkey = f_openssl_pkey_new();
  VERIFY(!privkey.isNull());
  Variant csr = f_openssl_csr_new(null, privkey);
  VERIFY(!csr.isNull());
  Variant pubkey = f_openssl_csr_get_public_key(csr);
  VERIFY(!pubkey.isNull());

  String data = "some secret messages";
  Variant signature;
  VERIFY(f_openssl_sign(data, ref(signature), privkey));
  VS(f_openssl_verify(data, signature, pubkey), 1);

  return Count(true);
}
bool TestExtOpenssl::test_openssl_pkcs12_export_to_file() {
  Variant privkey = f_openssl_pkey_new();
  VERIFY(!privkey.isNull());
  Variant csr = f_openssl_csr_new(null, privkey);
  VERIFY(!csr.isNull());
  Variant scert = f_openssl_csr_sign(csr, null, privkey, 365);

  const char *tmp = "test/test_pkcs12.tmp";
  f_unlink(tmp);
  VS(f_file_get_contents(tmp), false);
  f_openssl_pkcs12_export_to_file(scert, tmp, privkey, "1234");
  VERIFY(f_file_get_contents(tmp).toString().size() > 400);
  f_unlink(tmp);
  return Count(true);
}
bool TestExtOpenssl::test_openssl_pkcs7_encrypt() {
  Variant privkey = f_openssl_pkey_new();
  VERIFY(!privkey.isNull());
  Variant csr = f_openssl_csr_new(null, privkey);
  VERIFY(!csr.isNull());
  Variant scert = f_openssl_csr_sign(csr, null, privkey, 365);
  Variant pubkey = f_openssl_csr_get_public_key(csr);
  VERIFY(!pubkey.isNull());

  String data = "some secret data";
  const char *infile = "test/test_pkcs7.in";
  const char *outfile = "test/test_pkcs7.out";
  f_unlink(infile);
  f_unlink(outfile);
  f_file_put_contents(infile, data);

  VERIFY(f_openssl_pkcs7_encrypt
         (infile, outfile, scert,
          CREATE_MAP2("To", "*****@*****.**","From", "*****@*****.**")));

  f_unlink(infile);
  VERIFY(f_openssl_pkcs7_decrypt(outfile, infile, scert, privkey));
  Variant decrypted = f_file_get_contents(infile);
  f_var_dump(decrypted);
  f_unlink(infile);
  f_unlink(outfile);
  /*
   * PHP didn't work either:

    $privkey = openssl_pkey_new();
    $csr = openssl_csr_new(array(), $privkey);
    $scert = openssl_csr_sign($csr, null, $privkey, 365);

    $data = "some secret data";
    $infile = "test_pkcs7.in";
    $outfile = "test_pkcs7.out";
    file_put_contents($infile, $data);

    openssl_pkcs7_encrypt($infile, $outfile, $scert,
                          array("To" => "*****@*****.**",
                                "From" => "*****@*****.**"));

    var_dump(openssl_pkcs7_decrypt($outfile, $infile, $scert, $privkey));
    $decrypted = file_get_contents($infile);var_dump($decrypted);

   */
  return Count(true);
}
bool TestExtOpenssl::test_openssl_public_encrypt() {
  Variant privkey = f_openssl_pkey_new();
  VERIFY(!privkey.isNull());
  Variant csr = f_openssl_csr_new(null, privkey);
  VERIFY(!csr.isNull());
  Variant pubkey = f_openssl_csr_get_public_key(csr);
  VERIFY(!pubkey.isNull());

  String data = "some secret data";
  Variant out;
  VERIFY(f_openssl_public_encrypt(data, ref(out), pubkey));
  Variant out2;
  VERIFY(f_openssl_private_decrypt(out, ref(out2), privkey));
  VS(out2, data);
  return Count(true);
}
bool TestExtOpenssl::test_openssl_pkcs12_read() {
  Variant privkey = f_openssl_pkey_new();
  VERIFY(!privkey.isNull());
  Variant csr = f_openssl_csr_new(null, privkey);
  VERIFY(!csr.isNull());
  Variant scert = f_openssl_csr_sign(csr, null, privkey, 365);

  Variant pkcs12;
  f_openssl_pkcs12_export(scert, ref(pkcs12), privkey, "1234");

  Variant certs;
  VERIFY(f_openssl_pkcs12_read(pkcs12, ref(certs), "1234"));
  VERIFY(certs["cert"].toString().size() > 500);
  VERIFY(certs["pkey"].toString().size() > 500);

  return Count(true);
}
bool TestExtOpenssl::test_openssl_seal() {
  Variant privkey = f_openssl_pkey_new();
  VERIFY(!privkey.isNull());
  Variant csr = f_openssl_csr_new(null, privkey);
  VERIFY(!csr.isNull());
  Variant pubkey = f_openssl_csr_get_public_key(csr);
  VERIFY(!pubkey.isNull());

  String data = "some secret messages";
  Variant sealed;
  Variant ekeys;
  VERIFY(f_openssl_seal(data, ref(sealed), ref(ekeys),
                        CREATE_VECTOR1(pubkey)));
  VERIFY(!sealed.toString().empty());
  VS(ekeys.toArray().size(), 1);
  VERIFY(!ekeys[0].toString().empty());

  Variant open_data;
  VERIFY(f_openssl_open(sealed, ref(open_data), ekeys[0], privkey));
  VS(open_data, data);

  return Count(true);
}
bool TestExtOpenssl::test_openssl_csr_get_subject() {
  Variant csr = f_openssl_csr_new(null, null);
  VERIFY(!csr.isNull());
  VS(f_openssl_csr_get_subject(csr)["O"], "My Company Ltd");
  return Count(true);
}