Certificate.hx 4.0 KB

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