ssl_config_parser.nim 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #
  2. #
  3. # SSL configuration generator
  4. # (c) Copyright 2020 Leorize <leorize+oss@disroot.org>
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. import httpclient, json, net, strformat, strutils, sequtils, times
  10. const
  11. ConfigSource = "https://ssl-config.mozilla.org/guidelines/latest.json"
  12. OutputFile = "ssl_config.nim"
  13. proc main() =
  14. let
  15. client = newHttpClient(sslContext = newContext(verifyMode = CVerifyPeer))
  16. resp = client.get(ConfigSource)
  17. defer: client.close()
  18. if not resp.code.is2xx:
  19. quit "Couldn't fetch configuration, server returned: " & $resp.code
  20. let configs = resp.bodyStream.parseJson("ssl-config.json")
  21. let generationTime = now().utc()
  22. let output = open(OutputFile, fmWrite)
  23. echo "Generating ", OutputFile
  24. output.writeLine(&"""
  25. # This file was automatically generated by tools/ssl_config_parser on {generationTime}. DO NOT EDIT.
  26. ## This module contains SSL configuration parameters obtained from
  27. ## `Mozilla OpSec <https://wiki.mozilla.org/Security/Server_Side_TLS>`_.
  28. ##
  29. ## The configuration file used to generate this module: {configs["href"].getStr}
  30. """)
  31. for name, config in configs["configurations"]:
  32. let
  33. constantName = "Ciphers" & name[0].toUpperAscii & name[1..^1]
  34. var ciphers: string
  35. for c in config["ciphersuites"].getElems & config["ciphers"]["openssl"].getElems:
  36. if ciphers.len == 0:
  37. ciphers.add c.getStr
  38. else:
  39. ciphers.add ':'
  40. ciphers.add c.getStr
  41. var constant = &"""
  42. const {constantName}* = "{ciphers}"
  43. ## An OpenSSL-compatible list of secure ciphers for ``{name}`` compatibility
  44. ## per Mozilla's recommendations.
  45. ##
  46. ## Oldest clients supported by this list:
  47. """
  48. for c in config["oldest_clients"]:
  49. constant.add " ## * " & c.getStr
  50. constant.add '\n'
  51. output.writeLine constant
  52. when isMainModule: main()