2
0

Certificate.hx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 sys.ssl.Lib;
  24. @:noDoc
  25. typedef CertificatePtr = hl.Abstract<"hl_ssl_cert">;
  26. @:coreApi
  27. class Certificate {
  28. var __h:Null<Certificate>;
  29. var __x:CertificatePtr;
  30. @:allow(sys.ssl.Socket)
  31. function new(x:CertificatePtr, ?h:Null<Certificate>) {
  32. __x = x;
  33. __h = h;
  34. }
  35. public static function loadFile(file:String):Certificate {
  36. return new Certificate(cert_load_file(@:privateAccess file.toUtf8()));
  37. }
  38. public static function loadPath(path:String):Certificate {
  39. return new Certificate(cert_load_path(@:privateAccess path.toUtf8()));
  40. }
  41. public static function fromString(str:String):Certificate {
  42. return new Certificate(cert_add_pem(null, @:privateAccess str.toUtf8()));
  43. }
  44. public static function loadDefaults():Certificate {
  45. var x = cert_load_defaults();
  46. if (x != null)
  47. return new Certificate(x);
  48. var defPaths = null;
  49. switch (Sys.systemName()) {
  50. case "Linux":
  51. defPaths = [
  52. "/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.
  53. "/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL
  54. "/etc/ssl/ca-bundle.pem", // OpenSUSE
  55. "/etc/pki/tls/cacert.pem", // OpenELEC
  56. "/etc/ssl/certs", // SLES10/SLES11
  57. "/system/etc/security/cacerts" // Android
  58. ];
  59. case "BSD":
  60. defPaths = [
  61. "/usr/local/share/certs/ca-root-nss.crt", // FreeBSD/DragonFly
  62. "/etc/ssl/cert.pem", // OpenBSD
  63. "/etc/openssl/certs/ca-certificates.crt", // NetBSD
  64. ];
  65. case "Android":
  66. defPaths = ["/system/etc/security/cacerts"];
  67. default:
  68. }
  69. if (defPaths != null) {
  70. for (path in defPaths) {
  71. if (sys.FileSystem.exists(path)) {
  72. if (sys.FileSystem.isDirectory(path))
  73. return loadPath(path);
  74. else
  75. return loadFile(path);
  76. }
  77. }
  78. }
  79. return null;
  80. }
  81. public var commonName(get, null):Null<String>;
  82. public var altNames(get, null):Array<String>;
  83. public var notBefore(get, null):Date;
  84. public var notAfter(get, null):Date;
  85. function get_commonName():Null<String> {
  86. return subject("CN");
  87. }
  88. function get_altNames():Array<String> {
  89. var a = cert_get_altnames(__x);
  90. return [for (e in a) @:privateAccess String.fromUCS2(e)];
  91. }
  92. public function subject(field:String):Null<String> {
  93. var s = cert_get_subject(__x, @:privateAccess field.toUtf8());
  94. return s == null ? null : @:privateAccess String.fromUCS2(cast s);
  95. }
  96. public function issuer(field:String):Null<String> {
  97. var s = cert_get_issuer(__x, @:privateAccess field.toUtf8());
  98. return s == null ? null : @:privateAccess String.fromUCS2(cast s);
  99. }
  100. function get_notBefore():Date {
  101. var a = cert_get_notbefore(__x);
  102. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  103. }
  104. function get_notAfter():Date {
  105. var a = cert_get_notafter(__x);
  106. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  107. }
  108. public function next():Null<Certificate> {
  109. var n = cert_get_next(__x);
  110. return n == null ? null : new Certificate(n, __h == null ? this : __h);
  111. }
  112. public function add(pem:String):Void {
  113. cert_add_pem(__x, @:privateAccess pem.toUtf8());
  114. }
  115. public function addDER(der:haxe.io.Bytes):Void {
  116. cert_add_der(__x, @:privateAccess der.b, @:privateAccess der.length);
  117. }
  118. @:hlNative("ssl", "cert_load_defaults") static function cert_load_defaults():CertificatePtr {
  119. return null;
  120. }
  121. @:hlNative("ssl", "cert_load_file") static function cert_load_file(file:hl.Bytes):CertificatePtr {
  122. return null;
  123. }
  124. @:hlNative("ssl", "cert_load_path") static function cert_load_path(path:hl.Bytes):CertificatePtr {
  125. return null;
  126. }
  127. @:hlNative("ssl", "cert_get_subject") static function cert_get_subject(cert:CertificatePtr, obj:hl.Bytes):hl.Bytes {
  128. return null;
  129. }
  130. @:hlNative("ssl", "cert_get_issuer") static function cert_get_issuer(cert:CertificatePtr, obj:hl.Bytes):hl.Bytes {
  131. return null;
  132. }
  133. @:hlNative("ssl", "cert_get_altnames") static function cert_get_altnames(cert:CertificatePtr):hl.NativeArray<hl.Bytes> {
  134. return null;
  135. }
  136. @:hlNative("ssl", "cert_get_notbefore") static function cert_get_notbefore(cert:CertificatePtr):hl.NativeArray<Int> {
  137. return null;
  138. }
  139. @:hlNative("ssl", "cert_get_notafter") static function cert_get_notafter(cert:CertificatePtr):hl.NativeArray<Int> {
  140. return null;
  141. }
  142. @:hlNative("ssl", "cert_get_next") static function cert_get_next(cert:CertificatePtr):Null<CertificatePtr> {
  143. return null;
  144. }
  145. @:hlNative("ssl", "cert_add_pem") static function cert_add_pem(cert:Null<CertificatePtr>, data:hl.Bytes):CertificatePtr {
  146. return null;
  147. }
  148. @:hlNative("ssl", "cert_add_der") static function cert_add_der(cert:Null<CertificatePtr>, data:hl.Bytes, len:Int):CertificatePtr {
  149. return null;
  150. }
  151. }