Certificate.hx 3.0 KB

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