rllib  1
Classes | Functions
rlssl.cpp File Reference
#include "rlsocket.h"
Include dependency graph for rlssl.cpp:

Go to the source code of this file.

Classes

struct  rlssl_struct
 

Functions

static void cryptoLockCallback (int mode, int type, const char *file, int line)
 
int rlssl_init (rlssl_struct *rlssl)
 
int rlssl_starttls (rlssl_struct *rlssl)
 
int rlssl_disconnect (rlssl_struct *rlssl)
 
int rlssl_available (rlssl_struct *rlssl)
 
int rlssl_write (rlssl_struct *rlssl, int ch)
 
int rlssl_write (rlssl_struct *rlssl, const char *text)
 
int rlssl_read (rlssl_struct *rlssl)
 

Function Documentation

◆ cryptoLockCallback()

static void cryptoLockCallback ( int  mode,
int  type,
const char *  file,
int  line 
)
static

Definition at line 25 of file rlssl.cpp.

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 }

◆ rlssl_available()

int rlssl_available ( rlssl_struct rlssl)

Definition at line 117 of file rlssl.cpp.

118 {
119  int count = SSL_pending(rlssl->sslStream);
120  return count;
121 }
SSL * sslStream
Definition: rlssl.cpp:20

◆ rlssl_disconnect()

int rlssl_disconnect ( rlssl_struct rlssl)

Definition at line 103 of file rlssl.cpp.

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 }
SSL_CTX * sslContext
Definition: rlssl.cpp:21
SSL * sslStream
Definition: rlssl.cpp:20

◆ rlssl_init()

int rlssl_init ( rlssl_struct rlssl)

Definition at line 70 of file rlssl.cpp.

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 }
SSL_CTX * sslContext
Definition: rlssl.cpp:21
static void cryptoLockCallback(int mode, int type, const char *file, int line)
Definition: rlssl.cpp:25
SSL * sslStream
Definition: rlssl.cpp:20

◆ rlssl_read()

int rlssl_read ( rlssl_struct rlssl)

Definition at line 134 of file rlssl.cpp.

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 }
SSL * sslStream
Definition: rlssl.cpp:20

◆ rlssl_starttls()

int rlssl_starttls ( rlssl_struct rlssl)

Definition at line 81 of file rlssl.cpp.

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 }
SSL_CTX * sslContext
Definition: rlssl.cpp:21
SSL * sslStream
Definition: rlssl.cpp:20

◆ rlssl_write() [1/2]

int rlssl_write ( rlssl_struct rlssl,
int  ch 
)

Definition at line 123 of file rlssl.cpp.

124 {
125  unsigned char c = (unsigned char) ch;
126  return SSL_write(rlssl->sslStream, &c, 1);
127 }
SSL * sslStream
Definition: rlssl.cpp:20

◆ rlssl_write() [2/2]

int rlssl_write ( rlssl_struct rlssl,
const char *  text 
)

Definition at line 129 of file rlssl.cpp.

130 {
131  return SSL_write(rlssl->sslStream, text, strlen(text));
132 }
SSL * sslStream
Definition: rlssl.cpp:20