f52a9c025bb4f8cc8e6dfd05bfa42a35f825d98e
[openssl.git] / demos / ssl / cli.cpp
1 /* cli.cpp  -  Minimal ssleay client for Unix
2    30.9.1996, Sampo Kellomaki <sampo@iki.fi> */
3
4 #include <stdio.h>
5 #include <memory.h>
6 #include <errno.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <netdb.h>
12
13 #include "rsa.h"       /* SSLeay stuff */
14 #include "crypto.h"
15 #include "x509.h"
16 #include "pem.h"
17 #include "ssl.h"
18 #include "err.h"
19
20 #define CHK_NULL(x) if ((x)==NULL) exit (1)
21 #define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(1); }
22 #define CHK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(2); }
23
24 void main ()
25 {
26   int err;
27   int sd;
28   struct sockaddr_in sa;
29   SSL_CTX* ctx;
30   SSL*     ssl;
31   X509*    server_cert;
32   char*    str;
33   char     buf [4096];
34
35   SSL_load_error_strings();
36   ctx = SSL_CTX_new ();                        CHK_NULL(ctx);
37   
38   /* ----------------------------------------------- */
39   /* Create a socket and connect to server using normal socket calls. */
40   
41   sd = socket (AF_INET, SOCK_STREAM, 0);       CHK_ERR(sd, "socket");
42  
43   memset (&sa, '\0', sizeof(sa));
44   sa.sin_family      = AF_INET;
45   sa.sin_addr.s_addr = inet_addr ("127.0.0.1");   /* Server IP */
46   sa.sin_port        = htons     (1111);          /* Server Port number */
47   
48   err = connect(sd, (struct sockaddr*) &sa,
49                 sizeof(sa));                   CHK_ERR(err, "connect");
50
51   /* ----------------------------------------------- */
52   /* Now we have TCP conncetion. Start SSL negotiation. */
53   
54   ssl = SSL_new (ctx);                         CHK_NULL(ssl);    
55   SSL_set_fd (ssl, sd);
56   err = SSL_connect (ssl);                     CHK_SSL(err);
57     
58   /* Following two steps are optional and not required for
59      data exchange to be successful. */
60   
61   /* Get the cipher - opt */
62
63   printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
64   
65   /* Get server's certificate (note: beware of dynamic allocation) - opt */
66
67   server_cert = SSL_get_peer_certificate (ssl);       CHK_NULL(server_cert);
68   printf ("Server certificate:\n");
69   
70   str = X509_NAME_oneline (X509_get_subject_name (server_cert));
71   CHK_NULL(str);
72   printf ("\t subject: %s\n", str);
73   Free (str);
74
75   str = X509_NAME_oneline (X509_get_issuer_name  (server_cert));
76   CHK_NULL(str);
77   printf ("\t issuer: %s\n", str);
78   Free (str);
79
80   /* We could do all sorts of certificate verification stuff here before
81      deallocating the certificate. */
82
83   X509_free (server_cert);
84   
85   /* --------------------------------------------------- */
86   /* DATA EXCHANGE - Send a message and receive a reply. */
87
88   err = SSL_write (ssl, "Hello World!", strlen("Hello World!"));  CHK_SSL(err);
89   
90   shutdown (sd, 1);  /* Half close, send EOF to server. */
91   
92   err = SSL_read (ssl, buf, sizeof(buf) - 1);                     CHK_SSL(err);
93   buf[err] = '\0';
94   printf ("Got %d chars:'%s'\n", err, buf);
95
96   /* Clean up. */
97
98   close (sd);
99   SSL_free (ssl);
100   SSL_CTX_free (ctx);
101 }
102 /* EOF - cli.cpp */