Certificate.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.ssl;
  23. @:coreApi
  24. class Certificate {
  25. var __h:Null<Certificate>;
  26. var __x:Dynamic;
  27. @:allow(sys.ssl.Socket)
  28. function new(x:Dynamic, ?h:Null<Certificate>) {
  29. __x = x;
  30. __h = h;
  31. }
  32. public static function loadFile(file:String):Certificate {
  33. return new Certificate(cert_load_file(untyped file.__s));
  34. }
  35. public static function loadPath(path:String):Certificate {
  36. return new Certificate(cert_load_path(untyped path.__s));
  37. }
  38. public static function fromString(str:String):Certificate {
  39. return new Certificate(cert_add_pem(null, untyped str.__s));
  40. }
  41. public static function loadDefaults():Certificate {
  42. var x = cert_load_defaults();
  43. if (x != null)
  44. return new Certificate(x);
  45. var defPaths = null;
  46. switch (Sys.systemName()) {
  47. case "Linux":
  48. defPaths = [
  49. "/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.
  50. "/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL
  51. "/etc/ssl/ca-bundle.pem", // OpenSUSE
  52. "/etc/pki/tls/cacert.pem", // OpenELEC
  53. "/etc/ssl/certs", // SLES10/SLES11
  54. "/system/etc/security/cacerts" // Android
  55. ];
  56. case "BSD":
  57. defPaths = [
  58. "/usr/local/share/certs/ca-root-nss.crt", // FreeBSD/DragonFly
  59. "/etc/ssl/cert.pem", // OpenBSD
  60. "/etc/openssl/certs/ca-certificates.crt", // NetBSD
  61. ];
  62. case "Android":
  63. defPaths = ["/system/etc/security/cacerts"];
  64. default:
  65. }
  66. if (defPaths != null) {
  67. for (path in defPaths) {
  68. if (sys.FileSystem.exists(path)) {
  69. if (sys.FileSystem.isDirectory(path))
  70. return loadPath(path);
  71. else
  72. return loadFile(path);
  73. }
  74. }
  75. }
  76. return null;
  77. }
  78. public var commonName(get, null):Null<String>;
  79. public var altNames(get, null):Array<String>;
  80. public var notBefore(get, null):Date;
  81. public var notAfter(get, null):Date;
  82. function get_commonName():Null<String> {
  83. return subject("CN");
  84. }
  85. function get_altNames():Array<String> {
  86. var l:Dynamic = cert_get_altnames(__x);
  87. var a = new Array<String>();
  88. while (l != null) {
  89. a.push(new String(l[0]));
  90. l = l[1];
  91. }
  92. return a;
  93. }
  94. public function subject(field:String):Null<String> {
  95. var s = cert_get_subject(__x, untyped field.__s);
  96. return s == null ? null : new String(cast s);
  97. }
  98. public function issuer(field:String):Null<String> {
  99. var s = cert_get_issuer(__x, untyped field.__s);
  100. return s == null ? null : new String(cast s);
  101. }
  102. function get_notBefore():Date {
  103. var a = cert_get_notbefore(__x);
  104. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  105. }
  106. function get_notAfter():Date {
  107. var a = cert_get_notafter(__x);
  108. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  109. }
  110. public function next():Null<Certificate> {
  111. var n = cert_get_next(__x);
  112. return n == null ? null : new Certificate(n, __h == null ? this : __h);
  113. }
  114. public function add(pem:String):Void {
  115. cert_add_pem(__x, untyped pem.__s);
  116. }
  117. public function addDER(der:haxe.io.Bytes):Void {
  118. cert_add_der(__x, der.getData());
  119. }
  120. private static var cert_load_defaults = neko.Lib.loadLazy("ssl", "cert_load_defaults", 0);
  121. private static var cert_load_file = neko.Lib.loadLazy("ssl", "cert_load_file", 1);
  122. private static var cert_load_path = neko.Lib.loadLazy("ssl", "cert_load_path", 1);
  123. private static var cert_get_subject = neko.Lib.loadLazy("ssl", "cert_get_subject", 2);
  124. private static var cert_get_issuer = neko.Lib.loadLazy("ssl", "cert_get_issuer", 2);
  125. private static var cert_get_altnames = neko.Lib.loadLazy("ssl", "cert_get_altnames", 1);
  126. private static var cert_get_notbefore = neko.Lib.loadLazy("ssl", "cert_get_notbefore", 1);
  127. private static var cert_get_notafter = neko.Lib.loadLazy("ssl", "cert_get_notafter", 1);
  128. private static var cert_get_next = neko.Lib.loadLazy("ssl", "cert_get_next", 1);
  129. private static var cert_add_pem = neko.Lib.loadLazy("ssl", "cert_add_pem", 2);
  130. private static var cert_add_der = neko.Lib.loadLazy("ssl", "cert_add_der", 2);
  131. }