Ejemplo n.º 1
0
/* Generates a signature of all the random data and the parameters.
 * Used in DHE_* ciphersuites.
 */
int
MHD_gtls_verify_sig_params (MHD_gtls_session_t session,
                            MHD_gnutls_cert * cert,
                            const MHD_gnutls_datum_t * params,
                            MHD_gnutls_datum_t * signature)
{
  MHD_gnutls_datum_t dconcat;
  int ret;
  mac_hd_t td_md5 = NULL;
  mac_hd_t td_sha;
  opaque concat[36];
  enum MHD_GNUTLS_Protocol ver = MHD__gnutls_protocol_get_version (session);

  if (ver < MHD_GNUTLS_PROTOCOL_TLS1_2)
    {
      td_md5 = MHD_gtls_hash_init (MHD_GNUTLS_MAC_MD5);
      if (td_md5 == NULL)
        {
          MHD_gnutls_assert ();
          return GNUTLS_E_HASH_FAILED;
        }

      MHD_gnutls_hash (td_md5, session->security_parameters.client_random,
                       TLS_RANDOM_SIZE);
      MHD_gnutls_hash (td_md5, session->security_parameters.server_random,
                       TLS_RANDOM_SIZE);
      MHD_gnutls_hash (td_md5, params->data, params->size);
    }

  td_sha = MHD_gtls_hash_init (MHD_GNUTLS_MAC_SHA1);
  if (td_sha == NULL)
    {
      MHD_gnutls_assert ();
      if (td_md5)
        MHD_gnutls_hash_deinit (td_md5, NULL);
      return GNUTLS_E_HASH_FAILED;
    }

  MHD_gnutls_hash (td_sha, session->security_parameters.client_random,
                   TLS_RANDOM_SIZE);
  MHD_gnutls_hash (td_sha, session->security_parameters.server_random,
                   TLS_RANDOM_SIZE);
  MHD_gnutls_hash (td_sha, params->data, params->size);

  if (ver < MHD_GNUTLS_PROTOCOL_TLS1_2)
    {
      MHD_gnutls_hash_deinit (td_md5, concat);
      MHD_gnutls_hash_deinit (td_sha, &concat[16]);
      dconcat.size = 36;
    }
  else
    {
#if 1
      /* Use NULL parameters. */
      memcpy (concat,
              "\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14",
              15);
      MHD_gnutls_hash_deinit (td_sha, &concat[15]);
      dconcat.size = 35;
#else
      /* No parameters field. */
      memcpy (concat,
              "\x30\x1f\x30\x07\x06\x05\x2b\x0e\x03\x02\x1a\x04\x14", 13);
      MHD_gnutls_hash_deinit (td_sha, &concat[13]);
      dconcat.size = 33;
#endif
    }

  dconcat.data = concat;

  ret = MHD__gnutls_verify_sig (cert, &dconcat, signature, dconcat.size - 20);
  if (ret < 0)
    {
      MHD_gnutls_assert ();
      return ret;
    }

  return ret;

}
Ejemplo n.º 2
0
/**
 * Test daemon response to TLS client hello requests containing extensions
 *
 * @param session
 * @param exten_t - the type of extension being appended to client hello request
 * @param ext_count - the number of consecutive extension replicas inserted into request
 * @param ext_length - the length of each appended extension
 * @return 0 on successful test completion, -1 otherwise
 */
static int
test_hello_extension (gnutls_session_t session, extensions_t exten_t,
                      int ext_count, int ext_length)
{
  int i, ret = 0, pos = 0;
  MHD_socket sd;
  int exten_data_len, ciphersuite_len, datalen;
  struct sockaddr_in sa;
  char url[255];
  opaque *data = NULL;
  uint8_t session_id_len = 0;
  opaque rnd[TLS_RANDOM_SIZE];
  opaque extdata[MAX_EXT_DATA_LENGTH];

  /* single, null compression */
  unsigned char comp[] = { 0x01, 0x00 };
  struct CBC cbc;

  sd = -1;
  memset (&cbc, 0, sizeof (struct CBC));
  if (NULL == (cbc.buf = malloc (sizeof (char) * 256)))
    {
      fprintf (stderr, MHD_E_MEM);
      ret = -1;
      goto cleanup;
    }
  cbc.size = 256;

  sd = socket (AF_INET, SOCK_STREAM, 0);
  if (sd == -1)
    {
      fprintf(stderr, "Failed to create socket: %s\n", strerror(errno));
      free (cbc.buf);
      return -1;
    }
  memset (&sa, '\0', sizeof (struct sockaddr_in));
  sa.sin_family = AF_INET;
  sa.sin_port = htons (DEAMON_TEST_PORT);
  sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);

  enum MHD_GNUTLS_Protocol hver;

  /* init hash functions */
  session->internals.handshake_mac_handle_md5 =
    MHD_gtls_hash_init (MHD_GNUTLS_MAC_MD5);
  session->internals.handshake_mac_handle_sha =
    MHD_gtls_hash_init (MHD_GNUTLS_MAC_SHA1);

  /* version = 2 , random = [4 for unix time + 28 for random bytes] */
  datalen = 2 /* version */ + TLS_RANDOM_SIZE + (session_id_len + 1);

  data = MHD_gnutls_malloc (datalen);
  if (data == NULL)
      {
	 free (cbc.buf);
	 return -1;
      }
  hver = MHD_gtls_version_max (session);
  data[pos++] = MHD_gtls_version_get_major (hver);
  data[pos++] = MHD_gtls_version_get_minor (hver);

  /* Set the version we advertise as maximum (RSA uses it). */
  set_adv_version (session, MHD_gtls_version_get_major (hver),
                   MHD_gtls_version_get_minor (hver));

  session->security_parameters.version = hver;
  session->security_parameters.timestamp = time (NULL);

  /* generate session client random */
  memset (session->security_parameters.client_random, 0, TLS_RANDOM_SIZE);
  gnutls_write_uint32 (time (NULL), rnd);
  if (GC_OK != MHD_gc_nonce ((char *) &rnd[4], TLS_RANDOM_SIZE - 4)) abort ();
  memcpy (session->security_parameters.client_random, rnd, TLS_RANDOM_SIZE);
  memcpy (&data[pos], rnd, TLS_RANDOM_SIZE);
  pos += TLS_RANDOM_SIZE;

  /* Copy the Session ID       */
  data[pos++] = session_id_len;

  /*
   * len = ciphersuite data + 2 bytes ciphersuite length \
   *       1 byte compression length + 1 byte compression data + \
   * 2 bytes extension length, extensions data
   */
  ciphersuite_len = MHD__gnutls_copy_ciphersuites (session, extdata,
                                                   sizeof (extdata));
  exten_data_len = ext_count * (2 + 2 + ext_length);
  datalen += ciphersuite_len + 2 + 2 + exten_data_len;
  data = MHD_gtls_realloc_fast (data, datalen);
  memcpy (&data[pos], extdata, sizeof (ciphersuite_len));
  pos += ciphersuite_len;

  /* set compression */
  memcpy (&data[pos], comp, sizeof (comp));
  pos += 2;

  /* set extensions length = 2 type bytes + 2 length bytes + extension length */
  gnutls_write_uint16 (exten_data_len, &data[pos]);
  pos += 2;
  for (i = 0; i < ext_count; ++i)
    {
      /* write extension type */
      gnutls_write_uint16 (exten_t, &data[pos]);
      pos += 2;
      gnutls_write_uint16 (ext_length, &data[pos]);
      pos += 2;
      /* we might want to generate random data here */
      memset (&data[pos], 0, ext_length);
      pos += ext_length;
    }

  if (connect (sd, &sa, sizeof (struct sockaddr_in)) < 0)
    {
      fprintf (stderr, "%s\n", MHD_E_FAILED_TO_CONNECT);
      ret = -1;
      goto cleanup;
    }

  gnutls_transport_set_ptr (session, (MHD_gnutls_transport_ptr_t) (long) sd);

  if (gen_test_file_url (url, DEAMON_TEST_PORT))
    {
      ret = -1;
      goto cleanup;
    }

  /* this should crash the server */
  ret = gnutls_send_handshake (session, data, datalen,
			       GNUTLS_HANDSHAKE_CLIENT_HELLO);

  /* advance to STATE2 */
  session->internals.handshake_state = STATE2;
  ret = gnutls_handshake (session);
  ret = gnutls_bye (session, GNUTLS_SHUT_WR);

  gnutls_free (data);

  /* make sure daemon is still functioning */
  if (CURLE_OK != send_curl_req (url, &cbc, "AES128-SHA",
                                 MHD_GNUTLS_PROTOCOL_TLS1_2))
    {
      ret = -1;
      goto cleanup;
    }

cleanup:
  if (-1 != sd)
    MHD_socket_close_ (sd);
  gnutls_free (cbc.buf);
  return ret;
}
Ejemplo n.º 3
0
/* Generates a signature of all the random data and the parameters.
 * Used in DHE_* ciphersuites.
 */
int
MHD_gtls_tls_sign_params (MHD_gtls_session_t session,
                          MHD_gnutls_cert * cert,
                          MHD_gnutls_privkey * pkey,
                          MHD_gnutls_datum_t * params,
                          MHD_gnutls_datum_t * signature)
{
  MHD_gnutls_datum_t dconcat;
  int ret;
  mac_hd_t td_sha;
  opaque concat[36];
  enum MHD_GNUTLS_Protocol ver = MHD__gnutls_protocol_get_version (session);

  td_sha = MHD_gtls_hash_init (MHD_GNUTLS_MAC_SHA1);
  if (td_sha == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_HASH_FAILED;
    }

  MHD_gnutls_hash (td_sha, session->security_parameters.client_random,
                   TLS_RANDOM_SIZE);
  MHD_gnutls_hash (td_sha, session->security_parameters.server_random,
                   TLS_RANDOM_SIZE);
  MHD_gnutls_hash (td_sha, params->data, params->size);

  switch (cert->subject_pk_algorithm)
    {
    case MHD_GNUTLS_PK_RSA:
      if (ver < MHD_GNUTLS_PROTOCOL_TLS1_2)
        {
          mac_hd_t td_md5 = MHD_gtls_hash_init (MHD_GNUTLS_MAC_MD5);
          if (td_md5 == NULL)
            {
              MHD_gnutls_assert ();
              return GNUTLS_E_HASH_FAILED;
            }

          MHD_gnutls_hash (td_md5, session->security_parameters.client_random,
                           TLS_RANDOM_SIZE);
          MHD_gnutls_hash (td_md5, session->security_parameters.server_random,
                           TLS_RANDOM_SIZE);
          MHD_gnutls_hash (td_md5, params->data, params->size);

          MHD_gnutls_hash_deinit (td_md5, concat);
          MHD_gnutls_hash_deinit (td_sha, &concat[16]);

          dconcat.size = 36;
        }
      else
        {
#if 1
          /* Use NULL parameters. */
          memcpy (concat,
                  "\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14",
                  15);
          MHD_gnutls_hash_deinit (td_sha, &concat[15]);
          dconcat.size = 35;
#else
          /* No parameters field. */
          memcpy (concat,
                  "\x30\x1f\x30\x07\x06\x05\x2b\x0e\x03\x02\x1a\x04\x14", 13);
          MHD_gnutls_hash_deinit (td_sha, &concat[13]);
          dconcat.size = 33;
#endif
        }
      dconcat.data = concat;
      break;
    default:
      MHD_gnutls_assert ();
      MHD_gnutls_hash_deinit (td_sha, NULL);
      return GNUTLS_E_INTERNAL_ERROR;
    }
  ret = MHD__gnutls_tls_sign (session, cert, pkey, &dconcat, signature);
  if (ret < 0)
    {
      MHD_gnutls_assert ();
    }

  return ret;

}