ssl.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Dominik Picheta
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module provides an easy to use sockets-style
  10. ## nim interface to the OpenSSL library.
  11. ##
  12. ## **Warning:** This module is deprecated, use the SSL procedures defined in
  13. ## the ``net`` module instead.
  14. {.deprecated.}
  15. import openssl, strutils, os
  16. type
  17. SecureSocket* = object
  18. ssl: SslPtr
  19. bio: BIO
  20. proc connect*(sock: var SecureSocket, address: string,
  21. port: int): int =
  22. ## Connects to the specified `address` on the specified `port`.
  23. ## Returns the result of the certificate validation.
  24. SslLoadErrorStrings()
  25. ERR_load_BIO_strings()
  26. if SSL_library_init() != 1:
  27. raiseOSError(osLastError())
  28. var ctx = SSL_CTX_new(SSLv23_client_method())
  29. if ctx == nil:
  30. ERR_print_errors_fp(stderr)
  31. raiseOSError(osLastError())
  32. #if SSL_CTX_load_verify_locations(ctx,
  33. # "/tmp/openssl-0.9.8e/certs/vsign1.pem", NIL) == 0:
  34. # echo("Failed load verify locations")
  35. # ERR_print_errors_fp(stderr)
  36. sock.bio = BIO_new_ssl_connect(ctx)
  37. if BIO_get_ssl(sock.bio, addr(sock.ssl)) == 0:
  38. raiseOSError(osLastError())
  39. if BIO_set_conn_hostname(sock.bio, address & ":" & $port) != 1:
  40. raiseOSError(osLastError())
  41. if BIO_do_connect(sock.bio) <= 0:
  42. ERR_print_errors_fp(stderr)
  43. raiseOSError(osLastError())
  44. result = SSL_get_verify_result(sock.ssl)
  45. proc recvLine*(sock: SecureSocket, line: var TaintedString): bool =
  46. ## Acts in a similar fashion to the `recvLine` in the sockets module.
  47. ## Returns false when no data is available to be read.
  48. ## `Line` must be initialized and not nil!
  49. setLen(line.string, 0)
  50. while true:
  51. var c: array[0..0, char]
  52. var n = BIO_read(sock.bio, addr c, c.len.cint)
  53. if n <= 0: return false
  54. if c[0] == '\r':
  55. n = BIO_read(sock.bio, addr c, c.len.cint)
  56. if n > 0 and c[0] == '\L':
  57. return true
  58. elif n <= 0:
  59. return false
  60. elif c[0] == '\L': return true
  61. add(line.string, c[0])
  62. proc send*(sock: SecureSocket, data: string) =
  63. ## Writes `data` to the socket.
  64. if BIO_write(sock.bio, data, data.len.cint) <= 0:
  65. raiseOSError(osLastError())
  66. proc close*(sock: SecureSocket) =
  67. ## Closes the socket
  68. if BIO_free(sock.bio) <= 0:
  69. ERR_print_errors_fp(stderr)
  70. raiseOSError(osLastError())
  71. when not defined(testing) and isMainModule:
  72. var s: SecureSocket
  73. echo connect(s, "smtp.gmail.com", 465)
  74. #var buffer: array[0..255, char]
  75. #echo BIO_read(bio, buffer, buffer.len)
  76. var buffer: string = ""
  77. echo s.recvLine(buffer)
  78. echo buffer
  79. echo buffer.len