Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. Initialize OpenSSL
  2. void init_openssl()
  3. {
  4. SSL_load_error_strings();
  5. OpenSSL_add_ssl_algorithms();
  6. }
  7. Copyright @ 2017 School of MCIT, Nile University, Cairo, Egypt (ksobh@nu.edu.eg)
  8. Create SSL Context
  9. SSL_CTX * create_ssl_context ()
  10. {
  11. SSL_METHOD * method = TLSv1_2_server_method ();
  12. SSL_CTX * ctx = SSL_CTX_new(method);
  13. if (!ctx)
  14. {
  15. // Error and exit
  16. }
  17. return ctx;
  18. }
  19. Copyright @ 2017 School of MCIT, Nile University, Cairo, Egypt (ksobh@nu.edu.eg)
  20. Configure SSL Context
  21. void configure_context (SSL_CTX * ctx)
  22. {
  23. If (SSL_CTX_use_certificate_file
  24. (ctx,”cert.pem”,SSL_FILETYPE_PEM) < 0 )
  25. {
  26. // Error and exit
  27. }
  28. SSL_CTX_set_default_password_cb(ctx,passwd_cb);
  29. // passwd_cb is a callback function that return the password
  30. If (SSL_CTX_use_PrivateKey_file
  31. (ctx,”key.pem”,SSL_FILETYPE_PEM) < 0 )
  32. {
  33. // Error and exit
  34. }
  35. }
  36. Copyright @ 2017 School of MCIT, Nile University, Cairo, Egypt (ksobh@nu.edu.eg)
  37. Accept Socket Connection
  38. struct sockaddr_in clientAddr;
  39. socklen_t sin_size = sizeof(clientAddr);
  40. int client_sock =
  41. accept (server_sock,
  42. (struct sockaddr *) &clientAddr,
  43. & sin_size);
  44. Copyright @ 2017 School of MCIT, Nile University, Cairo, Egypt (ksobh@nu.edu.eg)
  45. Bind Client Socket to SSL Context
  46. SSL * ssl = SSL_new(ctx);
  47. const long flags = SSL_OP_NO_SSLv2 |
  48. SSL_OP_NO_SSLv3 |
  49. SSL_OP_NO_COMPRESSION;
  50. SSL_CTX_set_options(ctx,flags);
  51. SSL_set_fd(ssl,client_sock);
  52. Copyright @ 2017 School of MCIT, Nile University, Cairo, Egypt (ksobh@nu.edu.eg)
  53. TSL Handshaking
  54. int maxBytes=1024;
  55. char buffer [maxBytes+1];s
  56. If ( SSL_accept (ssl) > 0 )
  57. {
  58. //Do read and write with SSL routines
  59. SSL_read(ssl,buffer,maxBytes);
  60. .
  61. .
  62. .
  63. SSL_write(ssl,buffer,maxBytes);
  64. }
  65. Copyright @ 2017 School of MCIT, Nile University, Cairo, Egypt (ksobh@nu.edu.eg)
  66. TSL Handshaking
  67. void cleanup_ssl()
  68. {
  69. EVP_cleanup();
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement