| 244 | | static GIOChannel *(GIOChannel *handle, const char *mycert, const char *mypkey, const char *cafile, const char *capath, gboolean verify) |
| 245 | | { |
| 246 | | GIOSSLChannel *chan; |
| 247 | | GIOChannel *gchan; |
| 248 | | int fd; |
| 249 | | SSL *ssl; |
| 250 | | SSL_CTX *ctx = NULL;
|
| 251 | | |
| 252 | | g_return_val_if_fail(handle != NULL, NULL);
|
| 253 | | |
| 254 | | if(!ssl_ctx && !()) |
| 255 | | return NULL;
|
| 256 | | |
| 257 | | if(!(fd = g_io_channel_unix_get_fd(handle))) |
| 258 | | return NULL;
|
| 259 | | |
| 260 | | if (mycert && *mycert) { |
| 261 | | char *scert = NULL, *spkey = NULL;
|
| 262 | | if ((ctx = SSL_CTX_new(SSLv23_client_method())) == NULL) {
|
| 263 | | g_error("Could not allocate memory for SSL context");
|
| 264 | | return NULL;
|
| 265 | | } |
| 266 | | scert = convert_home(mycert); |
| 267 | | if (mypkey && *mypkey) |
| 268 | | spkey = convert_home(mypkey); |
| 269 | | if (! SSL_CTX_use_certificate_file(ctx, scert, SSL_FILETYPE_PEM))
|
| 270 | | g_warning("Loading of client certificate '%s' failed", mycert);
|
| 271 | | else if (! SSL_CTX_use_PrivateKey_file(ctx, spkey ? spkey : scert, SSL_FILETYPE_PEM))
|
| 272 | | g_warning("Loading of private key '%s' failed", mypkey ? mypkey : mycert);
|
| 273 | | else if (! SSL_CTX_check_private_key(ctx)) |
| 274 | | g_warning("Private key does not match the certificate");
|
| 275 | | g_free(scert); |
| 276 | | g_free(spkey); |
| 277 | | } |
| 278 | | |
| 279 | | if ((cafile && *cafile) || (capath && *capath)) { |
| 280 | | char *scafile = NULL;
|
| 281 | | char *scapath = NULL;
|
| 282 | | if (! ctx && (ctx = SSL_CTX_new(SSLv23_client_method())) == NULL) {
|
| 283 | | g_error("Could not allocate memory for SSL context");
|
| 284 | | return NULL;
|
| 285 | | } |
| 286 | | if (cafile && *cafile) |
| 287 | | scafile = convert_home(cafile); |
| 288 | | if (capath && *capath) |
| 289 | | scapath = convert_home(capath); |
| 290 | | if (! SSL_CTX_load_verify_locations(ctx, scafile, scapath)) { |
| 291 | | g_warning("Could not load CA list for verifying SSL server certificate");
|
| 292 | | g_free(scafile); |
| 293 | | g_free(scapath); |
| 294 | | SSL_CTX_free(ctx); |
| 295 | | return NULL;
|
| 296 | | } |
| 297 | | g_free(scafile); |
| 298 | | g_free(scapath); |
| 299 | | verify = TRUE;
|
| 300 | | } |
| 301 | | |
| 302 | | if (ctx == NULL)
|
| 303 | | ctx = ssl_ctx; |
| 304 | | |
| 305 | | if(!(ssl = SSL_new(ctx))) |
| 306 | | { |
| 307 | | g_warning("Failed to allocate SSL structure");
|
| 308 | | return NULL;
|
| 309 | | } |
| 310 | | |
| 311 | | if(!SSL_set_fd(ssl, fd)) |
| 312 | | { |
| 313 | | g_warning("Failed to associate socket to SSL stream");
|
| 314 | | SSL_free(ssl); |
| 315 | | if (ctx != ssl_ctx) |
| 316 | | SSL_CTX_free(ctx); |
| 317 | | return NULL;
|
| 318 | | } |
| 319 | | |
| 320 | | chan = g_new0(GIOSSLChannel, 1);
|
| 321 | | chan->fd = fd; |
| 322 | | chan->giochan = handle; |
| 323 | | chan->ssl = ssl; |
| 324 | | chan->ctx = ctx; |
| 325 | | chan->verify = verify; |
| 326 | | |
| 327 | | gchan = (GIOChannel *)chan; |
| 328 | | gchan->funcs = &; |
| 329 | | g_io_channel_init(gchan); |
| 330 | | gchan->is_readable = gchan->is_writeable = TRUE;
|
| 331 | | gchan->use_buffer = FALSE;
|
| 332 | | |
| 333 | | return gchan; |
| 334 | | } |