1fab2e2f5ab24b329313d2973e84501e60d8a252
[sdk] / extras / SSLSocket.ec
1 #define byte _byte
2 #define int64 _int64
3 #define uint _uint
4 #define set _set
5 #include <openssl/ssl.h>
6 #if defined(__WIN32__)
7 #include <openssl/applink.c>
8 #endif
9 #undef byte
10 #undef int64
11 #undef uint
12 #undef set
13
14 import "ecere"
15
16 /*
17 static char * pass = "password";
18
19 static int password_cb(char *buf) //,int num, int rwflag,void *userdata)
20 {
21    // if(num<strlen(pass)+1) return(0);
22    strcpy(buf,pass);
23    return(strlen(pass));
24 }
25 */
26
27 class SSLSocket : Socket
28 {
29    SSL_CTX *ctx;
30    SSL *ssl;
31    BIO *sbio;
32    SSL_METHOD *meth;
33    int s;
34    bool autoEstablish;
35    autoEstablish = true;
36
37    SSLSocket()
38    {
39       static bool initialized = false;
40       if(!initialized)
41       {
42          SSL_library_init();
43          initialized = true;
44       }
45    }
46
47    void OnDisconnect(int code)
48    {
49       if(sbio)
50       {
51          //BIO_free(sbio);
52          sbio = null;
53       }
54       if(ssl)
55       { 
56          //SSL_free(ssl);
57          ssl = null;
58       }
59       if(ctx)
60       {
61          SSL_CTX_free(ctx);
62          ctx = null;
63       } 
64    }
65
66    int ReceiveData(unsigned char * buffer, int count, unsigned int flags)
67    {
68       int n = ssl ? SSL_read(ssl, buffer, count) : Socket::ReceiveData(buffer, count, flags);
69       return n;
70    }
71
72    int SendData(unsigned char * buffer, int count, unsigned int flags)
73    {
74       int n = ssl ? SSL_write(ssl, buffer, count) : Socket::SendData(buffer, count, flags);
75       return n;
76    }
77
78    bool EstablishConnection()
79    {
80       bool result;
81       X509 *peer;
82       char peer_CN[256];
83       int cipherResult;
84
85       meth = SSLv23_method();
86       //meth = TLSv1_method();
87       ctx = SSL_CTX_new(meth);
88       // cipherResult = SSL_CTX_set_cipher_list(ctx, "ALL:!DH:!EXP:!RC4:@STRENGTH"); // TLS_RSA_WITH_3DES_EDE_CBC_SHA ?
89       SSL_CTX_set_session_id_context(ctx, (void *)this, sizeof(SSLSocket));
90       ssl = SSL_new(ctx);
91       sbio = BIO_new_socket(s, BIO_NOCLOSE);
92       SSL_set_bio(ssl,sbio,sbio);
93       
94       result = SSL_connect(ssl) > 0;
95
96       /*
97       if(result)
98       {
99          if(!(SSL_CTX_use_certificate_chain_file(ctx, "client.pem")))
100             printf("Can't read certificate file");
101          
102          SSL_CTX_set_default_passwd_cb(ctx, password_cb);
103          
104          if(!(SSL_CTX_use_PrivateKey_file(ctx, "client.pem", SSL_FILETYPE_PEM)))
105             printf("Can't read key file");
106
107          if(!(SSL_CTX_load_verify_locations(ctx, "root.pem", 0)))
108             printf("Can't read CA list");
109
110          if(SSL_get_verify_result(ssl)!=X509_V_OK)
111             printf("Certificate doesn't verify");
112
113          peer=SSL_get_peer_certificate(ssl);
114          X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
115          NID_commonName, peer_CN, 256);
116
117          if(strcasecmp(peer_CN, host))
118             printf("Common name doesn't match host name");
119       }
120       */
121       return result;
122    }
123
124    bool OnEstablishConnection(int s)
125    {
126       this.s = s;
127       if(autoEstablish)
128          return EstablishConnection();
129       else
130          return true;
131    }
132
133    ~SSLSocket()
134    {
135       /*if(sbio)
136          BIO_free(sbio);
137       if(ssl)
138          SSL_free(ssl);*/
139       if(ctx)
140          SSL_CTX_free(ctx);
141    }
142 }