rllib  1
rlssl.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlssl.cpp - description
3  -------------------
4  begin : Thu Oct 16 2014
5  copyright : (C) 2014 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include "rlsocket.h"
17 
18 typedef struct
19 {
20  SSL *sslStream;
21  SSL_CTX *sslContext;
23 
24 // inspired by inkscape socket.cpp
25 static void cryptoLockCallback(int mode, int type, const char *file, int line)
26 {
27  static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
28  if(type < 0 || type >= CRYPTO_NUM_LOCKS)
29  {
30  printf("cryptoLockCallback::type out of bounds\n");
31  return;
32  }
33 
34  int rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
35  if(!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
36  {
37  printf("cryptoLockCallback::invalid mode\n");
38  return;
39  }
40 
41  if(mode & CRYPTO_LOCK)
42  {
43  if(modes[type])
44  {
45  errstr = "already locked";
46  /* must not happen in a single-threaded program
47  * (would deadlock)
48  */
49  goto err;
50  }
51  modes[type] = rw;
52  }
53  else if (mode & CRYPTO_UNLOCK)
54  {
55  if(!modes[type])
56  {
57  errstr = "not locked";
58  goto err;
59  }
60  if(modes[type] != rw)
61  {
62  errstr = (rw == CRYPTO_READ) ?
63  "CRYPTO_r_unlock on write lock" :
64  "CRYPTO_w_unlock on read lock";
65  }
66  modes[type] = 0;
67  }
68 }
69 
71 {
72  rlssl->sslStream = NULL;
73  rlssl->sslContext = NULL;
74  CRYPTO_set_locking_callback(cryptoLockCallback);
75  CRYPTO_set_id_callback(cryptoIdCallback);
76  SSL_library_init();
77  SSL_load_error_strings();
78  return 0;
79 }
80 
82 {
83  rlssl->sslStream = NULL;
84  rlssl->sslContext = NULL;
85  //SSL_METHOD *meth = SSLv23_method();
86  //SSL_METHOD *meth = SSLv3_client_method();
87  SSL_METHOD *meth = TLSv1_client_method();
88  rlssl->sslContext = SSL_CTX_new(meth);
89  //SSL_CTX_set_info_callback(rlssl->sslContext, infoCallback);
90 
91  /* Connect the SSL socket */
92  rlssl->sslStream = SSL_new(rlssl->sslContext);
93  SSL_set_fd(rlssl->sslStream, sock);
94  if(SSL_connect(rlssl->sslStream)<=0)
95  {
96  //fprintf(stderr, "SSL connect error\n");
97  //disconnect();
98  return -1;
99  }
100  return 0;
101 }
102 
104 {
105  int ret = SSL_shutdown(rlssl->sslStream);
106  if(ret <= 0) ret = -1; // error
107  SSL_free(rlssl->sslStream);
108  if(rlssl->sslContext)
109  {
110  SSL_CTX_free(rlssl->sslContext);
111  }
112  rlssl->sslStream = NULL;
113  rlssl->sslContext = NULL;
114  return ret;
115 }
116 
118 {
119  int count = SSL_pending(rlssl->sslStream);
120  return count;
121 }
122 
123 int rlssl_write(rlssl_struct *rlssl, int ch)
124 {
125  unsigned char c = (unsigned char) ch;
126  return SSL_write(rlssl->sslStream, &c, 1);
127 }
128 
129 int rlssl_write(rlssl_struct *rlssl, const char *text)
130 {
131  return SSL_write(rlssl->sslStream, text, strlen(text));
132 }
133 
135 {
136  unsigned char ch;
137  int ret = SSL_read(rlssl->sslStream, &ch, 1);
138  if(ret <= 0)
139  {
140  printf("rlssl_read: ERROR SSL_read() ret=%d SSL_get_error=%d\n", ret, SSL_get_error(rlssl->sslStream,ret));
141  return ret - 1;
142  }
143  return (int) ch;
144 }
int rlssl_starttls(rlssl_struct *rlssl)
Definition: rlssl.cpp:81
int rlssl_init(rlssl_struct *rlssl)
Definition: rlssl.cpp:70
int rlssl_available(rlssl_struct *rlssl)
Definition: rlssl.cpp:117
SSL_CTX * sslContext
Definition: rlssl.cpp:21
static void cryptoLockCallback(int mode, int type, const char *file, int line)
Definition: rlssl.cpp:25
int rlssl_disconnect(rlssl_struct *rlssl)
Definition: rlssl.cpp:103
SSL * sslStream
Definition: rlssl.cpp:20
int rlssl_write(rlssl_struct *rlssl, int ch)
Definition: rlssl.cpp:123
int rlssl_read(rlssl_struct *rlssl)
Definition: rlssl.cpp:134