~ubuntu-branches/ubuntu/lucid/gnutls26/lucid-proposed

« back to all changes in this revision

Viewing changes to doc/examples/ex-client-psk.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2008-06-24 19:13:25 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080624191325-33rewwn0p11jzdvq
Tags: 2.4.0-2
* Standards version 3.8.0. Rename README.source_and_patches to README.source.
* Upload to unstable.
* Point watchfile to stable releases again.
* Merge experimental and unstable changelog.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2007, 2008 Free Software Foundation
 
2
 *
 
3
 * Copying and distribution of this file, with or without modification,
 
4
 * are permitted in any medium without royalty provided the copyright
 
5
 * notice and this notice are preserved.
 
6
 */
 
7
 
 
8
#if HAVE_CONFIG_H
 
9
# include <config.h>
 
10
#endif
 
11
 
 
12
#include <stdio.h>
 
13
#include <stdlib.h>
 
14
#include <string.h>
 
15
#include <sys/types.h>
 
16
#include <sys/socket.h>
 
17
#include <arpa/inet.h>
 
18
#include <unistd.h>
 
19
#include <gnutls/gnutls.h>
 
20
 
 
21
/* A very basic TLS client, with PSK authentication.
 
22
 */
 
23
 
 
24
#define MAX_BUF 1024
 
25
#define CAFILE "ca.pem"
 
26
#define MSG "GET / HTTP/1.0\r\n\r\n"
 
27
 
 
28
extern int tcp_connect (void);
 
29
extern void tcp_close (int sd);
 
30
 
 
31
int
 
32
main (void)
 
33
{
 
34
  int ret, sd, ii;
 
35
  gnutls_session_t session;
 
36
  char buffer[MAX_BUF + 1];
 
37
  const char *err;
 
38
  gnutls_psk_client_credentials_t pskcred;
 
39
  const gnutls_datum_t key = { "DEADBEEF", 8 };
 
40
 
 
41
  gnutls_global_init ();
 
42
 
 
43
  gnutls_psk_allocate_client_credentials (&pskcred);
 
44
  gnutls_psk_set_client_credentials (pskcred, "test", &key,
 
45
                                     GNUTLS_PSK_KEY_HEX);
 
46
 
 
47
  /* Initialize TLS session
 
48
   */
 
49
  gnutls_init (&session, GNUTLS_CLIENT);
 
50
 
 
51
  /* Use default priorities */
 
52
  ret = gnutls_priority_set_direct (session, "PERFORMANCE", &err);
 
53
  if (ret < 0) {
 
54
    if (ret == GNUTLS_E_INVALID_REQUEST) {
 
55
      fprintf(stderr, "Syntax error at: %s\n", err);
 
56
    }
 
57
    exit(1);
 
58
  }
 
59
 
 
60
  /* put the x509 credentials to the current session
 
61
   */
 
62
  gnutls_credentials_set (session, GNUTLS_CRD_PSK, pskcred);
 
63
 
 
64
  /* connect to the peer
 
65
   */
 
66
  sd = tcp_connect ();
 
67
 
 
68
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
 
69
 
 
70
  /* Perform the TLS handshake
 
71
   */
 
72
  ret = gnutls_handshake (session);
 
73
 
 
74
  if (ret < 0)
 
75
    {
 
76
      fprintf (stderr, "*** Handshake failed\n");
 
77
      gnutls_perror (ret);
 
78
      goto end;
 
79
    }
 
80
  else
 
81
    {
 
82
      printf ("- Handshake was completed\n");
 
83
    }
 
84
 
 
85
  gnutls_record_send (session, MSG, strlen (MSG));
 
86
 
 
87
  ret = gnutls_record_recv (session, buffer, MAX_BUF);
 
88
  if (ret == 0)
 
89
    {
 
90
      printf ("- Peer has closed the TLS connection\n");
 
91
      goto end;
 
92
    }
 
93
  else if (ret < 0)
 
94
    {
 
95
      fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
 
96
      goto end;
 
97
    }
 
98
 
 
99
  printf ("- Received %d bytes: ", ret);
 
100
  for (ii = 0; ii < ret; ii++)
 
101
    {
 
102
      fputc (buffer[ii], stdout);
 
103
    }
 
104
  fputs ("\n", stdout);
 
105
 
 
106
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
 
107
 
 
108
end:
 
109
 
 
110
  tcp_close (sd);
 
111
 
 
112
  gnutls_deinit (session);
 
113
 
 
114
  gnutls_psk_free_client_credentials (pskcred);
 
115
 
 
116
  gnutls_global_deinit ();
 
117
 
 
118
  return 0;
 
119
}