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