~ubuntu-branches/ubuntu/saucy/gnutls28/saucy

« back to all changes in this revision

Viewing changes to doc/examples/ex-cert-select.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-07-30 21:40:07 UTC
  • mfrom: (14.1.9 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130730214007-9mrd08xo61kla008
Tags: 3.2.3-1ubuntu1
* Sync with Debian (LP: #1068029). Remaining change:
  - Drop gnutls-bin and -doc since we want to use the versions
    in gnutls26 as the defaults instead

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
               int sign_algos_length, gnutls_pcert_st ** pcert,
39
39
               unsigned int *pcert_length, gnutls_privkey_t * pkey);
40
40
 
41
 
gnutls_pcert_st crt;
 
41
gnutls_pcert_st pcrt;
42
42
gnutls_privkey_t key;
43
43
 
44
 
/* Helper functions to load a certificate and key
45
 
 * files into memory.
46
 
 */
47
 
static gnutls_datum_t
48
 
load_file (const char *file)
49
 
{
50
 
  FILE *f;
51
 
  gnutls_datum_t loaded_file = { NULL, 0 };
52
 
  long filelen;
53
 
  void *ptr;
54
 
 
55
 
  if (!(f = fopen (file, "r"))
56
 
      || fseek (f, 0, SEEK_END) != 0
57
 
      || (filelen = ftell (f)) < 0
58
 
      || fseek (f, 0, SEEK_SET) != 0
59
 
      || !(ptr = malloc ((size_t) filelen))
60
 
      || fread (ptr, 1, (size_t) filelen, f) < (size_t) filelen)
61
 
    {
62
 
      if (f)
63
 
        fclose (f);
64
 
      return loaded_file;
65
 
    }
66
 
 
67
 
  loaded_file.data = ptr;
68
 
  loaded_file.size = (unsigned int) filelen;
69
 
  return loaded_file;
70
 
}
71
 
 
72
 
static void
73
 
unload_file (gnutls_datum_t data)
74
 
{
75
 
  free (data.data);
76
 
}
77
 
 
78
44
/* Load the certificate and the private key.
79
45
 */
80
46
static void
82
48
{
83
49
  int ret;
84
50
  gnutls_datum_t data;
85
 
  gnutls_x509_privkey_t x509_key;
86
51
 
87
 
  data = load_file (CERT_FILE);
88
 
  if (data.data == NULL)
 
52
  ret = gnutls_load_file (CERT_FILE, &data);
 
53
  if (ret < 0)
89
54
    {
90
55
      fprintf (stderr, "*** Error loading certificate file.\n");
91
56
      exit (1);
92
57
    }
93
58
 
94
 
  ret = gnutls_pcert_import_x509_raw (&crt, &data, GNUTLS_X509_FMT_PEM, 0);
 
59
  ret = gnutls_pcert_import_x509_raw (&pcrt, &data, GNUTLS_X509_FMT_PEM, 0);
95
60
  if (ret < 0)
96
61
    {
97
62
      fprintf (stderr, "*** Error loading certificate file: %s\n",
99
64
      exit (1);
100
65
    }
101
66
 
102
 
  unload_file (data);
 
67
  gnutls_free(data.data);
103
68
 
104
 
  data = load_file (KEY_FILE);
105
 
  if (data.data == NULL)
 
69
  ret = gnutls_load_file (KEY_FILE, &data);
 
70
  if (ret < 0)
106
71
    {
107
72
      fprintf (stderr, "*** Error loading key file.\n");
108
73
      exit (1);
109
74
    }
110
75
 
111
 
  gnutls_x509_privkey_init (&x509_key);
 
76
  gnutls_privkey_init (&key);
112
77
 
113
 
  ret = gnutls_x509_privkey_import (x509_key, &data, GNUTLS_X509_FMT_PEM);
 
78
  ret = gnutls_privkey_import_x509_raw (key, &data, GNUTLS_X509_FMT_PEM, NULL, 0);
114
79
  if (ret < 0)
115
80
    {
116
81
      fprintf (stderr, "*** Error loading key file: %s\n",
117
82
               gnutls_strerror (ret));
118
83
      exit (1);
119
84
    }
120
 
 
121
 
  gnutls_privkey_init (&key);
122
 
 
123
 
  ret =
124
 
    gnutls_privkey_import_x509 (key, x509_key,
125
 
                                GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
126
 
  if (ret < 0)
127
 
    {
128
 
      fprintf (stderr, "*** Error importing key: %s\n",
129
 
               gnutls_strerror (ret));
130
 
      exit (1);
131
 
    }
132
 
 
133
 
  unload_file (data);
 
85
    
 
86
  gnutls_free(data.data);
134
87
}
135
88
 
136
89
int
176
129
   */
177
130
  sd = tcp_connect ();
178
131
 
179
 
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
 
132
  gnutls_transport_set_int (session, sd);
180
133
 
181
134
  /* Perform the TLS handshake
182
135
   */
190
143
    }
191
144
  else
192
145
    {
193
 
      printf ("- Handshake was completed\n");
 
146
      char* desc;
 
147
      
 
148
      desc = gnutls_session_get_desc(session);
 
149
      printf ("- Session info: %s\n", desc);
 
150
      gnutls_free(desc);
194
151
    }
195
152
 
196
153
  gnutls_record_send (session, MSG, strlen (MSG));
276
233
  if (type == GNUTLS_CRT_X509)
277
234
    {
278
235
      *pcert_length = 1;
279
 
      *pcert = &crt;
 
236
      *pcert = &pcrt;
280
237
      *pkey = key;
281
238
    }
282
239
  else