ssl.nim 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. {.deprecated: [TSecureSocket: SecureSocket].}
  21. proc connect*(sock: var SecureSocket, address: string,
  22. port: int): int =
  23. ## Connects to the specified `address` on the specified `port`.
  24. ## Returns the result of the certificate validation.
  25. SslLoadErrorStrings()
  26. ERR_load_BIO_strings()
  27. if SSL_library_init() != 1:
  28. raiseOSError(osLastError())
  29. var ctx = SSL_CTX_new(SSLv23_client_method())
  30. if ctx == nil:
  31. ERR_print_errors_fp(stderr)
  32. raiseOSError(osLastError())
  33. #if SSL_CTX_load_verify_locations(ctx,
  34. # "/tmp/openssl-0.9.8e/certs/vsign1.pem", NIL) == 0:
  35. # echo("Failed load verify locations")
  36. # ERR_print_errors_fp(stderr)
  37. sock.bio = BIO_new_ssl_connect(ctx)
  38. if BIO_get_ssl(sock.bio, addr(sock.ssl)) == 0:
  39. raiseOSError(osLastError())
  40. if BIO_set_conn_hostname(sock.bio, address & ":" & $port) != 1:
  41. raiseOSError(osLastError())
  42. if BIO_do_connect(sock.bio) <= 0:
  43. ERR_print_errors_fp(stderr)
  44. raiseOSError(osLastError())
  45. result = SSL_get_verify_result(sock.ssl)
  46. proc recvLine*(sock: SecureSocket, line: var TaintedString): bool =
  47. ## Acts in a similar fashion to the `recvLine` in the sockets module.
  48. ## Returns false when no data is available to be read.
  49. ## `Line` must be initialized and not nil!
  50. setLen(line.string, 0)
  51. while true:
  52. var c: array[0..0, char]
  53. var n = BIO_read(sock.bio, addr c, c.len.cint)
  54. if n <= 0: return false
  55. if c[0] == '\r':
  56. n = BIO_read(sock.bio, addr c, c.len.cint)
  57. if n > 0 and c[0] == '\L':
  58. return true
  59. elif n <= 0:
  60. return false
  61. elif c[0] == '\L': return true
  62. add(line.string, c[0])
  63. proc send*(sock: SecureSocket, data: string) =
  64. ## Writes `data` to the socket.
  65. if BIO_write(sock.bio, data, data.len.cint) <= 0:
  66. raiseOSError(osLastError())
  67. proc close*(sock: SecureSocket) =
  68. ## Closes the socket
  69. if BIO_free(sock.bio) <= 0:
  70. ERR_print_errors_fp(stderr)
  71. raiseOSError(osLastError())
  72. when not defined(testing) and isMainModule:
  73. var s: SecureSocket
  74. echo connect(s, "smtp.gmail.com", 465)
  75. #var buffer: array[0..255, char]
  76. #echo BIO_read(bio, buffer, buffer.len)
  77. var buffer: string = ""
  78. echo s.recvLine(buffer)
  79. echo buffer
  80. echo buffer.len