~ubuntu-branches/ubuntu/intrepid/gnutls26/intrepid-security

« back to all changes in this revision

Viewing changes to doc/examples/ex-serv-pgp.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2008-05-01 13:09:49 UTC
  • Revision ID: james.westby@ubuntu.com-20080501130949-qsbsi06stso6a0ij
Tags: upstream-2.2.3~rc
ImportĀ upstreamĀ versionĀ 2.2.3~rc

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2007 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 <errno.h>
 
15
#include <sys/types.h>
 
16
#include <sys/socket.h>
 
17
#include <arpa/inet.h>
 
18
#include <netinet/in.h>
 
19
#include <string.h>
 
20
#include <unistd.h>
 
21
#include <gnutls/gnutls.h>
 
22
/* Must be linked against gnutls-extra.
 
23
 */
 
24
#include <gnutls/extra.h>
 
25
 
 
26
#define KEYFILE "secret.asc"
 
27
#define CERTFILE "public.asc"
 
28
#define RINGFILE "ring.gpg"
 
29
 
 
30
/* This is a sample TLS 1.0-OpenPGP echo server.
 
31
 */
 
32
 
 
33
 
 
34
#define SA struct sockaddr
 
35
#define SOCKET_ERR(err,s) if(err==-1) {perror(s);return(1);}
 
36
#define MAX_BUF 1024
 
37
#define PORT 5556               /* listen to 5556 port */
 
38
#define DH_BITS 1024
 
39
 
 
40
/* These are global */
 
41
gnutls_certificate_credentials_t cred;
 
42
gnutls_dh_params_t dh_params;
 
43
 
 
44
static int
 
45
generate_dh_params (void)
 
46
{
 
47
 
 
48
  /* Generate Diffie Hellman parameters - for use with DHE
 
49
   * kx algorithms. These should be discarded and regenerated
 
50
   * once a day, once a week or once a month. Depending on the
 
51
   * security requirements.
 
52
   */
 
53
  gnutls_dh_params_init (&dh_params);
 
54
  gnutls_dh_params_generate2 (dh_params, DH_BITS);
 
55
 
 
56
  return 0;
 
57
}
 
58
 
 
59
gnutls_session_t
 
60
initialize_tls_session (void)
 
61
{
 
62
  gnutls_session_t session;
 
63
 
 
64
  gnutls_init (&session, GNUTLS_SERVER);
 
65
 
 
66
  gnutls_priority_set_direct(session, "NORMAL", NULL);
 
67
 
 
68
  /* request client certificate if any.
 
69
   */
 
70
  gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
 
71
 
 
72
  gnutls_dh_set_prime_bits (session, DH_BITS);
 
73
 
 
74
  return session;
 
75
}
 
76
 
 
77
int
 
78
main (void)
 
79
{
 
80
  int err, listen_sd, i;
 
81
  int sd, ret;
 
82
  struct sockaddr_in sa_serv;
 
83
  struct sockaddr_in sa_cli;
 
84
  int client_len;
 
85
  char topbuf[512];
 
86
  gnutls_session_t session;
 
87
  char buffer[MAX_BUF + 1];
 
88
  int optval = 1;
 
89
  char name[256];
 
90
 
 
91
  strcpy (name, "Echo Server");
 
92
 
 
93
  /* this must be called once in the program
 
94
   */
 
95
  gnutls_global_init ();
 
96
 
 
97
  gnutls_certificate_allocate_credentials (&cred);
 
98
  gnutls_certificate_set_openpgp_keyring_file (cred, RINGFILE, GNUTLS_OPENPGP_FMT_BASE64);
 
99
 
 
100
  gnutls_certificate_set_openpgp_key_file (cred, CERTFILE, KEYFILE, GNUTLS_OPENPGP_FMT_BASE64);
 
101
 
 
102
  generate_dh_params ();
 
103
 
 
104
  gnutls_certificate_set_dh_params (cred, dh_params);
 
105
 
 
106
  /* Socket operations
 
107
   */
 
108
  listen_sd = socket (AF_INET, SOCK_STREAM, 0);
 
109
  SOCKET_ERR (listen_sd, "socket");
 
110
 
 
111
  memset (&sa_serv, '\0', sizeof (sa_serv));
 
112
  sa_serv.sin_family = AF_INET;
 
113
  sa_serv.sin_addr.s_addr = INADDR_ANY;
 
114
  sa_serv.sin_port = htons (PORT);      /* Server Port number */
 
115
 
 
116
  setsockopt (listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
 
117
 
 
118
  err = bind (listen_sd, (SA *) & sa_serv, sizeof (sa_serv));
 
119
  SOCKET_ERR (err, "bind");
 
120
  err = listen (listen_sd, 1024);
 
121
  SOCKET_ERR (err, "listen");
 
122
 
 
123
  printf ("%s ready. Listening to port '%d'.\n\n", name, PORT);
 
124
 
 
125
  client_len = sizeof (sa_cli);
 
126
  for (;;)
 
127
    {
 
128
      session = initialize_tls_session ();
 
129
 
 
130
      sd = accept (listen_sd, (SA *) & sa_cli, &client_len);
 
131
 
 
132
      printf ("- connection from %s, port %d\n",
 
133
              inet_ntop (AF_INET, &sa_cli.sin_addr, topbuf,
 
134
                         sizeof (topbuf)), ntohs (sa_cli.sin_port));
 
135
 
 
136
      gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
 
137
      ret = gnutls_handshake (session);
 
138
      if (ret < 0)
 
139
        {
 
140
          close (sd);
 
141
          gnutls_deinit (session);
 
142
          fprintf (stderr, "*** Handshake has failed (%s)\n\n",
 
143
                   gnutls_strerror (ret));
 
144
          continue;
 
145
        }
 
146
      printf ("- Handshake was completed\n");
 
147
 
 
148
      /* see the Getting peer's information example */
 
149
      /* print_info(session); */
 
150
 
 
151
      i = 0;
 
152
      for (;;)
 
153
        {
 
154
          memset (buffer, 0, MAX_BUF + 1);
 
155
          ret = gnutls_record_recv (session, buffer, MAX_BUF);
 
156
 
 
157
          if (ret == 0)
 
158
            {
 
159
              printf ("\n- Peer has closed the GNUTLS connection\n");
 
160
              break;
 
161
            }
 
162
          else if (ret < 0)
 
163
            {
 
164
              fprintf (stderr, "\n*** Received corrupted "
 
165
                       "data(%d). Closing the connection.\n\n", ret);
 
166
              break;
 
167
            }
 
168
          else if (ret > 0)
 
169
            {
 
170
              /* echo data back to the client
 
171
               */
 
172
              gnutls_record_send (session, buffer, strlen (buffer));
 
173
            }
 
174
        }
 
175
      printf ("\n");
 
176
      /* do not wait for the peer to close the connection.
 
177
       */
 
178
      gnutls_bye (session, GNUTLS_SHUT_WR);
 
179
 
 
180
      close (sd);
 
181
      gnutls_deinit (session);
 
182
 
 
183
    }
 
184
  close (listen_sd);
 
185
 
 
186
  gnutls_certificate_free_credentials (cred);
 
187
 
 
188
  gnutls_global_deinit ();
 
189
 
 
190
  return 0;
 
191
 
 
192
}