Certificate.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. import cpp.NativeSsl;
  24. @:coreApi
  25. class Certificate {
  26. var __h:Null<Certificate>;
  27. var __x:Dynamic;
  28. @:allow(sys.ssl.Socket)
  29. function new(x:Dynamic, ?h:Certificate) {
  30. __x = x;
  31. __h = h;
  32. }
  33. public static function loadFile(file:String):Certificate {
  34. return new Certificate(NativeSsl.cert_load_file(file));
  35. }
  36. public static function loadPath(path:String):Certificate {
  37. return new Certificate(NativeSsl.cert_load_path(path));
  38. }
  39. public static function fromString(str:String):Certificate {
  40. return new Certificate(NativeSsl.cert_add_pem(null, str));
  41. }
  42. public static function loadDefaults():Certificate {
  43. var x = NativeSsl.cert_load_defaults();
  44. if (x != null)
  45. return new Certificate(x);
  46. var defPaths = null;
  47. switch (Sys.systemName()) {
  48. case "Linux":
  49. defPaths = [
  50. "/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.
  51. "/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL
  52. "/etc/ssl/ca-bundle.pem", // OpenSUSE
  53. "/etc/pki/tls/cacert.pem", // OpenELEC
  54. "/etc/ssl/certs", // SLES10/SLES11
  55. "/system/etc/security/cacerts" // Android
  56. ];
  57. case "BSD":
  58. defPaths = [
  59. "/usr/local/share/certs/ca-root-nss.crt", // FreeBSD/DragonFly
  60. "/etc/ssl/cert.pem", // OpenBSD
  61. "/etc/openssl/certs/ca-certificates.crt", // NetBSD
  62. ];
  63. case "Android":
  64. defPaths = ["/system/etc/security/cacerts"];
  65. default:
  66. }
  67. if (defPaths != null) {
  68. for (path in defPaths) {
  69. if (sys.FileSystem.exists(path)) {
  70. if (sys.FileSystem.isDirectory(path))
  71. return loadPath(path);
  72. else
  73. return loadFile(path);
  74. }
  75. }
  76. }
  77. return null;
  78. }
  79. public var commonName(get, null):Null<String>;
  80. public var altNames(get, null):Array<String>;
  81. public var notBefore(get, null):Date;
  82. public var notAfter(get, null):Date;
  83. function get_commonName():Null<String> {
  84. return subject("CN");
  85. }
  86. function get_altNames():Array<String> {
  87. return NativeSsl.cert_get_altnames(__x);
  88. }
  89. public function subject(field:String):Null<String> {
  90. return NativeSsl.cert_get_subject(__x, field);
  91. }
  92. public function issuer(field:String):Null<String> {
  93. return NativeSsl.cert_get_issuer(__x, field);
  94. }
  95. function get_notBefore():Date {
  96. var a = NativeSsl.cert_get_notbefore(__x);
  97. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  98. }
  99. function get_notAfter():Date {
  100. var a = NativeSsl.cert_get_notafter(__x);
  101. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  102. }
  103. public function next():Null<Certificate> {
  104. var n = NativeSsl.cert_get_next(__x);
  105. return n == null ? null : new Certificate(n, __h == null ? this : __h);
  106. }
  107. public function add(pem:String):Void {
  108. NativeSsl.cert_add_pem(__x, pem);
  109. }
  110. public function addDER(der:haxe.io.Bytes):Void {
  111. NativeSsl.cert_add_der(__x, der.getData());
  112. }
  113. static function __init__():Void {
  114. NativeSsl.init();
  115. }
  116. }