Skip to content
This repository
tag: v1598
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 570 lines (456 sloc) 13.852 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
/*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*/
/*
@author Frederic Ye
**/

/* Warning: The following code is highly inspired by Ocaml-ssl binding
-> TODO: Check Ocaml-ssl licence */

#include <caml/alloc.h>
#include <caml/callback.h>
#include <caml/custom.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>
#include <caml/signals.h>

#include <openssl/ssl.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/crypto.h>
#include <openssl/bn.h>
#include <openssl/engine.h>

// SSL
/* Some definitions from Ocaml-SSL */
#define Cert_val(v) (*((X509**)Data_custom_val(v)))
#define RSA_val(v) (*((RSA**)Data_custom_val(v)))
#define Ctx_val(v) (*((SSL_CTX**)Data_custom_val(v)))
#define SSL_val(v) (*((SSL**)Data_custom_val(v)))
#define ONELINE_NAME(X) X509_NAME_oneline(X, 0, 0)

#include "../libbase/mlstate_platform.h"

#ifdef MLSTATE_WINDOWS
#include <io.h>
#include <process.h>
#else
#include <unistd.h>
#endif

#include <string.h>
#include <assert.h>


CAMLprim value ocaml_ssl_ext_init(void) {
  /* ERR_load_crypto_strings(); */
  OpenSSL_add_all_digests();
  OpenSSL_add_all_ciphers();
  OpenSSL_add_all_algorithms();
  /* ENGINE_load_builtin_engines(); */
  return Val_unit;
}

static int s_server_session_id_context = 1; /* anything will do */
CAMLprim value ocaml_ssl_ext_ctx_set_session_id_context(value context)
{
  CAMLparam1(context);
  SSL_CTX *ctx = Ctx_val(context);

  caml_enter_blocking_section();
  SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context,
sizeof s_server_session_id_context);
  caml_leave_blocking_section();

  CAMLreturn(Val_unit);
}

CAMLprim value ocaml_ssl_ext_ctx_set_options(value context)
{
  CAMLparam1(context);
  long ans;
  SSL_CTX *ctx = Ctx_val(context);

  caml_enter_blocking_section();
  ans = SSL_CTX_set_options(ctx, SSL_OP_ALL | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
  caml_leave_blocking_section();

  CAMLreturn(Val_long(ans));
}

CAMLprim value ocaml_ssl_ext_ctx_set_accept_renegotiate(value context)
{
  CAMLparam1(context);
  long ans;
  SSL_CTX *ctx = Ctx_val(context);

  caml_enter_blocking_section();
  ans = SSL_CTX_sess_accept_renegotiate(ctx);
  caml_leave_blocking_section();

  CAMLreturn(Val_long(ans));
}

static int no_client_verify_callback(int, X509_STORE_CTX *);

CAMLprim value ocaml_ssl_ext_get_no_client_verify_callback_ptr(value unit)
{
  return (value)no_client_verify_callback;
}

CAMLprim value ocaml_ssl_ext_get_signature(value certificate)
{
  CAMLparam1(certificate);
  X509 *cert = Cert_val(certificate);
  unsigned char *signature ; //= cert->signature->data;

  caml_enter_blocking_section();
  signature = cert->signature->data;

  caml_leave_blocking_section();
  if (signature == NULL) caml_raise_not_found ();

  CAMLreturn(caml_copy_string(String_val(signature)));
}

CAMLprim value ocaml_ssl_ext_get_hash(value certificate)
{
  CAMLparam1(certificate);
  X509 *cert = Cert_val(certificate);
  unsigned char *hash ; //= cert->sha1_hash;

  caml_enter_blocking_section();
  hash = cert->sha1_hash;
  caml_leave_blocking_section();
  if (hash == NULL) caml_raise_not_found ();

  CAMLreturn(caml_copy_string(String_val(hash)));
}

CAMLprim value ocaml_ssl_ext_compute_digest(value certificate, value dname)
{
  CAMLparam2(certificate, dname);
  X509 *cert = Cert_val(certificate);
  char *digest_name = String_val(dname);
  unsigned int n;
  unsigned char digest[64];

  const EVP_MD *fdig = EVP_get_digestbyname(digest_name);

  caml_enter_blocking_section();
  // @see evp.h #define EVP_MAX_MD_SIZE 64 /* longest known is SHA512 */

  X509_digest(cert,fdig,digest,&n);
  caml_leave_blocking_section();
  if (digest == NULL) caml_raise_not_found ();
  
  CAMLreturn(caml_copy_string(String_val(digest)));
}

static int no_client_verify_callback(int ok, X509_STORE_CTX *ctx)
{
  char *subject, *issuer;
  unsigned char *signature;
  char *xs;

  xs = (char *)X509_STORE_CTX_get_current_cert(ctx);

  subject = issuer = NULL;
  signature = NULL;

  /* First thing is to have a meaningful name for the current
* certificate that is being verified ... and if we cannot
* determine that then something is seriously wrong!
*/
   subject=(char*)ONELINE_NAME(X509_get_subject_name((X509*)xs));
   if (subject == NULL)
   {
     ERR_print_errors_fp(stderr);
     ok = 0;
     goto return_time;
   }
   issuer = (char*)ONELINE_NAME(X509_get_issuer_name((X509*)xs));
   if (issuer == NULL)
   {
     ERR_print_errors_fp(stderr);
     ok = 0;
     goto return_time;
   }
  signature = (((X509*)xs)->signature)->data;
  if (signature == NULL) {
    ERR_print_errors_fp(stderr);
    ok = 0;
    goto return_time;
  }

  ok = 1;

return_time:

  /* Clean up things. */
  if (subject)
    free(subject);
  if (issuer)
    free(issuer);

  return ok;
}

void print_ssl_error(err) {
  switch (err) {
  case SSL_ERROR_NONE: printf("SSL_ERROR_NONE\n"); break;
  case SSL_ERROR_ZERO_RETURN: printf("SSL_ERROR_ZERO_RETURN\n"); break;
  case SSL_ERROR_WANT_READ: printf("SSL_ERROR_WANT_READ\n"); break;
  case SSL_ERROR_WANT_WRITE: printf("SSL_ERROR_WANT_WRITE\n"); break;
  case SSL_ERROR_WANT_CONNECT: printf("SSL_ERROR_WANT_CONNECT\n"); break;
  case SSL_ERROR_WANT_ACCEPT: printf("SSL_ERROR_WANT_ACCEPT\n"); break;
  case SSL_ERROR_WANT_X509_LOOKUP: printf("SSL_ERROR_WANT_X509_LOOKUP\n"); break;
  case SSL_ERROR_SYSCALL: printf("SSL_ERROR_SYSCALL\n"); break;
  case SSL_ERROR_SSL: printf("SSL_ERROR_SSL\n"); break;
  default: printf("...\n"); break;
  }
}

CAMLprim value ocaml_ssl_ext_renegotiate(value socket)
{
  CAMLparam1(socket);
  SSL *ssl = SSL_val(socket);
  int ret;
  caml_enter_blocking_section();
  ret = SSL_renegotiate(ssl);
  caml_leave_blocking_section();
  if(ret <= 0)
    caml_raise_constant(*caml_named_value("ssl_ext_exn_renegotiation_error"));
  CAMLreturn(Val_unit);
}


/* Very basic implementation of a certificate chain check:
Must be greatly improved!!! */
CAMLprim value ocaml_ssl_ext_verify_chain(value certificate, value caf)
{
  CAMLparam2(certificate, caf);
  char *cafile = String_val(caf);
  X509 *cert = Cert_val(certificate);
  int i = 0;
  X509_STORE *ctx=X509_STORE_new();
  X509_LOOKUP *lookup=X509_STORE_add_lookup(ctx,X509_LOOKUP_file());
  X509_STORE_CTX *csc=X509_STORE_CTX_new();
  
/* printf("CAfile %s\n", cafile); */
/* printf("Certificate: "); */
/* X509_NAME_print_ex_fp(stdout, */
/* X509_get_subject_name(cert), */
/* 0, XN_FLAG_ONELINE); */
/* printf("\n"); */

 
  if (ctx == NULL) printf("ctx error\n");

  OpenSSL_add_all_algorithms();

  if (lookup == NULL) printf("lookup error\n");
  if (!X509_LOOKUP_load_file(lookup,cafile,X509_FILETYPE_PEM)) {
    printf("error load file\n");
  }

  if (csc == NULL) printf("csc error\n");

  X509_STORE_set_flags(ctx, 0);
  if (!X509_STORE_CTX_init(csc,ctx,cert,NULL)) {
    printf("store init error\n");
  }

  i = X509_verify_cert(csc);

  X509_STORE_CTX_free(csc);
  X509_STORE_free(ctx);

  CAMLreturn(Val_int(i));
}

CAMLprim value ocaml_ssl_ext_do_handshake(value socket)
{
  CAMLparam1(socket);
  int ret;
  int err;
  SSL *ssl = SSL_val(socket);

  caml_enter_blocking_section();
  ret = SSL_do_handshake(ssl);
  err = SSL_get_error(ssl, ret);
  caml_leave_blocking_section();
  if (err != SSL_ERROR_NONE)
    caml_raise_with_arg(*caml_named_value("ssl_ext_exn_handshake_error"), Val_int(err));
  CAMLreturn(Val_unit);
}

CAMLprim value ocaml_ssl_ext_set_verify(value socket, value vmode, value vcallback)
{
  CAMLparam3(socket, vmode, vcallback);
  SSL *ssl = SSL_val(socket);
  int mode = 0;
  value mode_tl = vmode;
  int (*callback) (int, X509_STORE_CTX*) = NULL;

  if (Is_long(vmode))
    mode = SSL_VERIFY_NONE;

  while (Is_block(mode_tl))
  {
    switch(Int_val(Field(mode_tl, 0)))
    {
      case 0:
        mode |= SSL_VERIFY_PEER;
        break;

      case 1:
        mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_PEER;
        break;

      case 2:
        mode |= SSL_VERIFY_CLIENT_ONCE | SSL_VERIFY_PEER;
        break;

      default:
        caml_invalid_argument("mode");
    }

    mode_tl = Field(mode_tl, 1);
  }

  if (Is_block(vcallback))
    callback = (int(*) (int, X509_STORE_CTX*))Field(vcallback, 0);

  caml_enter_blocking_section();
  SSL_set_verify(ssl, mode, callback);
  caml_leave_blocking_section();

  CAMLreturn(Val_unit);
}

CAMLprim value ocaml_ssl_ext_set_verify_depth(value socket, value vdepth)
{
  SSL *ssl = SSL_val(socket);
  int depth = Int_val(vdepth);

  if (depth < 0)
    caml_invalid_argument("depth");

  caml_enter_blocking_section();
  SSL_set_verify_depth(ssl, depth);
  caml_leave_blocking_section();

  return Val_unit;
}

CAMLprim value ocaml_ssl_ext_set_accept_state(value socket)
{
  SSL *ssl = SSL_val(socket);

  caml_enter_blocking_section();
  SSL_set_accept_state(ssl);
  caml_leave_blocking_section();

  return Val_unit;
}

CAMLprim value ocaml_ssl_ext_set_state(value socket, value state)
{
  SSL *ssl = SSL_val(socket);
  int st = Int_val(state);

  caml_enter_blocking_section();
  ssl->state=st;
  caml_leave_blocking_section();

  return Val_unit;
}

CAMLprim value ocaml_ssl_ext_get_verify_mode(value socket)
{
  CAMLparam1(socket);
  int ans;
  SSL *ssl = SSL_val(socket);

  caml_enter_blocking_section();
  ans = SSL_get_verify_mode(ssl);
  caml_leave_blocking_section();

  CAMLreturn(Val_int(ans));
}

CAMLprim value ocaml_ssl_ext_get_state(value socket)
{
  CAMLparam1(socket);
  int ans;
  SSL *ssl = SSL_val(socket);

  caml_enter_blocking_section();
  ans = SSL_get_state(ssl);
  caml_leave_blocking_section();

  CAMLreturn(Val_int(ans));
}

CAMLprim value ocaml_ssl_ext_clear(value socket)
{
  CAMLparam1(socket);
  int ans;
  SSL *ssl = SSL_val(socket);

  caml_enter_blocking_section();
  ans = SSL_clear(ssl);
  caml_leave_blocking_section();

  CAMLreturn(Val_int(ans));
}

CAMLprim value ocaml_ssl_ext_num_renegotiations(value socket)
{
  CAMLparam1(socket);
  long ans;
  SSL *ssl = SSL_val(socket);

  caml_enter_blocking_section();
  ans = SSL_num_renegotiations(ssl);
  caml_leave_blocking_section();

  CAMLreturn(Val_long(ans));
}

// RSA

#define RSA_val(v) (*((RSA**)Data_custom_val(v)))

/* Convert a BIGNUM into a string */
char *bn_to_hex(const BIGNUM *bn)
{
  char *res = "";
  caml_enter_blocking_section();
  if (bn != NULL)
    res = BN_bn2hex(bn);
  caml_leave_blocking_section();
  return res;
}

CAMLprim value ocaml_ssl_ext_rsa_read_privkey(value vfilename)
{
  value block;
  char *filename = String_val(vfilename);
  RSA *rsa = NULL;
  FILE *fh = NULL;

  if((fh = fopen(filename, "r")) == NULL)
    caml_raise_constant(*caml_named_value("ssl_ext_exn_rsa_error"));

  caml_enter_blocking_section();
  if((PEM_read_RSAPrivateKey(fh, &rsa, PEM_def_callback, NULL)) == NULL)
  {
    fclose(fh);
    caml_leave_blocking_section();
    caml_raise_constant(*caml_named_value("ssl_ext_exn_rsa_error"));
  }
  fclose(fh);
  caml_leave_blocking_section();

  block = caml_alloc(sizeof(RSA*), 0);
  RSA_val(block) = rsa;
  return block;
}

CAMLprim value ocaml_ssl_ext_rsa_read_pubkey(value vfilename)
{
  value block;
  char *filename = String_val(vfilename);
  RSA *rsa = NULL;
  FILE *fh = NULL;

  if((fh = fopen(filename, "r")) == NULL)
    caml_raise_constant(*caml_named_value("ssl_ext_exn_rsa_error"));

  caml_enter_blocking_section();
  if((PEM_read_RSA_PUBKEY(fh, &rsa, PEM_def_callback, NULL)) == NULL)
  {
    fclose(fh);
    caml_leave_blocking_section();
    caml_raise_constant(*caml_named_value("ssl_ext_exn_rsa_error"));
  }
  fclose(fh);
  caml_leave_blocking_section();

  block = caml_alloc(sizeof(RSA*), 0);
  RSA_val(block) = rsa;
  return block;
}

CAMLprim value ocaml_ssl_ext_rsa_get_size(value key)
{
  CAMLparam1(key);
  RSA *rsa = RSA_val(key);
  int size = 0;
  caml_enter_blocking_section();
  size = RSA_size(rsa);
  caml_leave_blocking_section();
  CAMLreturn(Val_int(size));
}

CAMLprim value ocaml_ssl_ext_rsa_get_n(value key)
{
  CAMLparam1(key);
  RSA *rsa = RSA_val(key);
  CAMLreturn(caml_copy_string(String_val(bn_to_hex(rsa->n))));
}

CAMLprim value ocaml_ssl_ext_rsa_get_e(value key)
{
  CAMLparam1(key);
  RSA *rsa = RSA_val(key);
  CAMLreturn(caml_copy_string(String_val(bn_to_hex(rsa->e))));
}

CAMLprim value ocaml_ssl_ext_rsa_get_d(value key)
{
  CAMLparam1(key);
  RSA *rsa = RSA_val(key);
  CAMLreturn(caml_copy_string(String_val(bn_to_hex(rsa->d))));
}

CAMLprim value ocaml_ssl_ext_rsa_get_p(value key)
{
  CAMLparam1(key);
  RSA *rsa = RSA_val(key);
  CAMLreturn(caml_copy_string(String_val(bn_to_hex(rsa->p))));
}

CAMLprim value ocaml_ssl_ext_rsa_get_q(value key)
{
  CAMLparam1(key);
  RSA *rsa = RSA_val(key);
  CAMLreturn(caml_copy_string(String_val(bn_to_hex(rsa->q))));
}

CAMLprim value ocaml_ssl_ext_rsa_get_dp(value key)
{
  CAMLparam1(key);
  RSA *rsa = RSA_val(key);
  CAMLreturn(caml_copy_string(String_val(bn_to_hex(rsa->dmp1))));
}

CAMLprim value ocaml_ssl_ext_rsa_get_dq(value key)
{
  CAMLparam1(key);
  RSA *rsa = RSA_val(key);
  CAMLreturn(caml_copy_string(String_val(bn_to_hex(rsa->dmq1))));
}

CAMLprim value ocaml_ssl_ext_rsa_get_qinv(value key)
{
  CAMLparam1(key);
  RSA *rsa = RSA_val(key);
  CAMLreturn(caml_copy_string(String_val(bn_to_hex(rsa->iqmp))));
}

Something went wrong with that request. Please try again.