Certificate.hx 4.7 KB

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