Exemple #1
0
void create_server_keys(SshPrivateKey *host, SshPrivateKey *server,
                        unsigned char **blob, unsigned int *len)
{
  SshPublicKey public_host_key;

#ifdef DEBUG
  ssh_debug("generating host key");
#endif
  if (ssh_private_key_generate(random_state, host,
                               SSH_CRYPTO_RSA, SSH_PKF_SIZE, 768, 
                               SSH_PKF_END) != SSH_CRYPTO_OK)
    ssh_fatal("Generating host key failed");
#ifdef DEBUG
  ssh_debug("generating server key");
#endif

  if (ssh_private_key_generate(random_state, server,
                               SSH_CRYPTO_RSA, SSH_PKF_SIZE, 512, 
                               SSH_PKF_END) != SSH_CRYPTO_OK)
    ssh_fatal("Generating server key failed");

#ifdef DEBUG  
  ssh_debug("deriving public host key blob");
#endif
  public_host_key = ssh_private_key_derive_public_key(*host);
  if (ssh_public_key_export(public_host_key, blob, len) != SSH_CRYPTO_OK)
    ssh_fatal("deriving public key blob failed");
  ssh_public_key_free(public_host_key);
}
int main(int argc, char **argv)
{
  SshExternalKeyTestCtx test_ctx;
  int i;
  SshPrivateKey prv_key;
  SshPublicKey pub_key;
  SshMPInteger n;

  parse_arguments(argc, argv);

  ssh_pk_provider_register(&ssh_pk_if_modn_generator);
  /* Initialize the event loop and the test context. */
  ssh_event_loop_initialize();
  ssh_debug_set_level_string(debug_level_string);

  ssh_global_init();
  /* Initialize the crypto library. */
  if (ssh_crypto_library_initialize() != SSH_CRYPTO_OK)
    ssh_fatal("Cannot initialize the crypto library");

  test_ctx = ssh_xcalloc(1, sizeof(*test_ctx));
  test_ctx->accelerated_encrypts_left = default_accelerated_encrypts;
  test_ctx->timer = ssh_time_measure_allocate();

  SSH_DEBUG(3, ("Reading the test key. Please wait...."));

  prv_key = get_prv_key("accelerator-test.prv");

  if (ssh_private_key_select_scheme(prv_key,
                                    SSH_PKF_ENCRYPT, "rsa-none-none",
                                    SSH_PKF_END) != SSH_CRYPTO_OK)
    ssh_fatal("Could not select the scheme for private key");



  if (ssh_private_key_derive_public_key(prv_key, &pub_key)
      != SSH_CRYPTO_OK)
    {
      ssh_fatal("Can not derive a public key from a "
                "stored private key");
    }

  if (ssh_public_key_select_scheme(pub_key,
                                   SSH_PKF_ENCRYPT, "rsa-none-none",
                                   SSH_PKF_END) != SSH_CRYPTO_OK)
    ssh_fatal("Could not select the scheme for public key");



  n = ssh_mprz_malloc();

  /* Get information about the RSA key. E and N are needed for nFast. */
  if (ssh_public_key_get_info(pub_key,
                              SSH_PKF_MODULO_N, n,
                              SSH_PKF_END)
      != SSH_CRYPTO_OK)
    {
      return FALSE;
    }


#if 0
  n_bytes = (ssh_mprz_get_size(n, 2) + 7) / 8;
  if (n_bytes == 0 || (n_bytes & 3) != 0)
    n_bytes += (4 - (n_bytes & 3));

  test_ctx->big_buf = ssh_xmalloc(n_bytes);
  test_ctx->big_buf_len = n_bytes;

  ssh_mprz_get_buf(test_ctx->big_buf, test_ctx->big_buf_len, n);
  ssh_mprz_free(n);
  test_ctx->big_buf_len = 128;
  test_ctx->big_buf[0] = 1;
#else
#if 0
  n_bytes = ssh_mprz_get_size(n, 8);
  test_ctx->big_buf = ssh_xmalloc(n_bytes);
  test_ctx->big_buf_len = n_bytes;
  ssh_mprz_init(&r);
  ssh_mprz_rand(&r, n_bytes * 8);
  ssh_mprz_mod(&r, &r, n);
  ssh_mprz_get_buf(test_ctx->big_buf, test_ctx->big_buf_len, &r);
  ssh_mprz_free(n);
  ssh_mprz_clear(&r);
#else
  test_ctx->big_buf = ssh_xmalloc(129);
  test_ctx->big_buf_len = 129;
  memcpy(test_ctx->big_buf,
         "\x00\x50\xe7\x85\x86\x40\xf8\x9b"
         "\xb8\xeb\x19\x64\xd8\x51\x33\xd7"
         "\x4f\xac\x32\x5d\x03\x66\x3d\x0c"
         "\xbe\xfd\x40\x29\x82\xb7\x61\x09"
         "\x15\x37\x4f\xe1\xd0\x57\xb0\x6d"
         "\x16\x49\x73\x25\x20\x3d\xa8\xfa"
         "\xf6\xb4\x72\xec\x75\xc8\x42\xc7"
         "\x99\x64\x63\x23\x29\xe0\x65\xa1"
         "\x2a\xc2\xb7\xf1\x5b\xb4\x9b\x30"
         "\xdb\xc7\x22\xb9\xf9\xde\xb5\x09"
         "\xb5\xe0\x0a\xca\xc5\xf9\xaf\x8f"
         "\x54\xf2\x9a\x06\x2b\xc1\xc2\x65"
         "\x87\xb3\xd5\xec\xd3\x8a\x2f\xa7"
         "\x5f\x69\x34\xe7\x7f\xeb\xaf\x56"
         "\x3c\x3d\x71\x3f\x73\xba\x8b\xa7"
         "\xd3\xe5\x6d\x98\xc8\x01\x6b\x18"
         "\x14",
         129);
#endif
#endif

  test_ctx->pub_key = pub_key;
  test_ctx->prv_key = prv_key;

  test_ek_add(test_ctx);
#ifndef WIN32
  ssh_register_signal(SIGUSR1, test_signal_handler, test_ctx);
#endif
  ssh_event_loop_run();

  /* Uninitialize. */
  for (i = 0; i < test_ctx->num_prv_keys; i++)
    ssh_private_key_free(test_ctx->prv_keys[i]);

  for (i = 0; i < test_ctx->num_pub_keys; i++)
    ssh_public_key_free(test_ctx->pub_keys[i]);


  ssh_xfree(test_ctx->prv_keys);
  ssh_xfree(test_ctx->pub_keys);
  ssh_xfree(test_ctx);
  return 0;
}