Browse Source

Export result to hash

aeris 1 year ago
parent
commit
971f05c524

+ 24
- 0
lib/cryptcheck/tls/cert.rb View File

@@ -107,6 +107,30 @@ module CryptCheck
107 107
 				@cert.issuer
108 108
 			end
109 109
 
110
+			def lifetime
111
+				{ not_before: @cert.not_before, not_after: @cert.not_after }
112
+			end
113
+
114
+			def to_h
115
+				{
116
+						subject:     self.subject.to_s,
117
+						serial:      self.serial.to_s,
118
+						issuer:      self.issuer.to_s,
119
+						lifetime:    self.lifetime,
120
+						fingerprint: self.fingerprint,
121
+						chain:       @chain.collect do |cert|
122
+							{
123
+									subject:  cert.subject.to_s,
124
+									serial:   cert.serial.to_s,
125
+									issuer:   cert.issuer.to_s,
126
+									lifetime: { not_before: cert.not_before, not_after: cert.not_after }
127
+							}
128
+						end,
129
+						key:         self.key.to_h,
130
+						states:      self.states
131
+				}
132
+			end
133
+
110 134
 			protected
111 135
 			include State
112 136
 

+ 9
- 0
lib/cryptcheck/tls/cipher.rb View File

@@ -111,6 +111,15 @@ module CryptCheck
111 111
 				end
112 112
 			end
113 113
 
114
+			def to_h
115
+				hmac = self.hmac
116
+				{
117
+						protocol:   @method, name: self.name, key_exchange: self.kex, authentication: self.auth,
118
+						encryption: { name: self.encryption, mode: self.mode, block_size: self.block_size },
119
+						hmac:       { name: hmac.first, size: hmac.last }, states: self.states
120
+				}
121
+			end
122
+
114 123
 			def <=>(other)
115 124
 				compare = State.compare self, other
116 125
 				return compare unless compare == 0

+ 4
- 0
lib/cryptcheck/tls/curve.rb View File

@@ -29,6 +29,10 @@ module CryptCheck
29 29
 				@name
30 30
 			end
31 31
 
32
+			def to_h
33
+				{ name: @name, states: self.states }
34
+			end
35
+
32 36
 			def ==(other)
33 37
 				case other
34 38
 					when String

+ 2
- 14
lib/cryptcheck/tls/engine.rb View File

@@ -139,7 +139,7 @@ module CryptCheck
139 139
 			def fetch_dh
140 140
 				@dh = @supported_ciphers.collect do |_, ciphers|
141 141
 					ciphers.values.collect(&:tmp_key).select { |d| d.is_a? OpenSSL::PKey::DH }
142
-				end.flatten
142
+				end.flatten.uniq &:fingerprint
143 143
 			end
144 144
 
145 145
 			def fetch_ecdsa_certs
@@ -455,21 +455,9 @@ module CryptCheck
455 455
 				@keys = @certs.collect &:key
456 456
 			end
457 457
 
458
-			def uniq_dh
459
-				dh, find = [], []
460
-				@dh.each do |k|
461
-					f = [k.type, k.size]
462
-					unless find.include? f
463
-						dh << k
464
-						find << f
465
-					end
466
-				end
467
-				@dh = dh
468
-			end
469
-
470 458
 			private
471 459
 			def uniq_supported_ciphers
472
-				@supported_ciphers.values.collect(&:keys).flatten.uniq
460
+				@uniq_supported_ciphers ||= @supported_ciphers.values.collect(&:keys).flatten.uniq
473 461
 			end
474 462
 		end
475 463
 	end

+ 22
- 0
lib/cryptcheck/tls/fixture.rb View File

@@ -1,5 +1,11 @@
1 1
 require 'openssl'
2 2
 
3
+class ::OpenSSL::PKey::PKey
4
+	def fingerprint
5
+		::OpenSSL::Digest::SHA256.hexdigest self.to_der
6
+	end
7
+end
8
+
3 9
 class ::OpenSSL::PKey::EC
4 10
 	def type
5 11
 		:ecc
@@ -17,6 +23,10 @@ class ::OpenSSL::PKey::EC
17 23
 		"ECC #{self.size} bits"
18 24
 	end
19 25
 
26
+	def to_h
27
+		{ type: :ecc, curve: self.curve, size: self.size, fingerprint: self.fingerprint, states: self.states }
28
+	end
29
+
20 30
 	protected
21 31
 	include ::CryptCheck::State
22 32
 
@@ -51,6 +61,10 @@ class ::OpenSSL::PKey::RSA
51 61
 		"RSA #{self.size} bits"
52 62
 	end
53 63
 
64
+	def to_h
65
+		{ type: :rsa, size: self.size, fingerprint: self.fingerprint, states: self.states }
66
+	end
67
+
54 68
 	protected
55 69
 	include ::CryptCheck::State
56 70
 
@@ -83,6 +97,10 @@ class ::OpenSSL::PKey::DSA
83 97
 		"DSA #{self.size} bits"
84 98
 	end
85 99
 
100
+	def to_h
101
+		{ type: :dsa, size: self.size, fingerprint: self.fingerprint, states: self.states }
102
+	end
103
+
86 104
 	include ::CryptCheck::State
87 105
 
88 106
 	CHECKS = [
@@ -108,6 +126,10 @@ class ::OpenSSL::PKey::DH
108 126
 		"DH #{self.size} bits"
109 127
 	end
110 128
 
129
+	def to_h
130
+		{ size: self.size, fingerprint: self.fingerprint, states: self.states }
131
+	end
132
+
111 133
 	protected
112 134
 	include ::CryptCheck::State
113 135
 

+ 4
- 0
lib/cryptcheck/tls/https/server.rb View File

@@ -48,6 +48,10 @@ module CryptCheck
48 48
 					hsts? and @hsts >= LONG_HSTS
49 49
 				end
50 50
 
51
+				def to_h
52
+					super.merge({ hsts: @hsts })
53
+				end
54
+
51 55
 				protected
52 56
 				def available_checks
53 57
 					super + [

+ 4
- 0
lib/cryptcheck/tls/method.rb View File

@@ -27,6 +27,10 @@ module CryptCheck
27 27
 				super.colorize colors
28 28
 			end
29 29
 
30
+			def to_h
31
+				{ protocol: self.to_sym, states: self.states }
32
+			end
33
+
30 34
 			alias :to_sym :__getobj__
31 35
 
32 36
 			def <=>(other)

+ 10
- 1
lib/cryptcheck/tls/server.rb View File

@@ -62,7 +62,16 @@ module CryptCheck
62 62
 			end
63 63
 
64 64
 			def to_h
65
-
65
+				{
66
+						certs:            @certs.collect(&:to_h),
67
+						dh:               @dh.collect(&:to_h),
68
+						protocols:        @supported_methods.collect(&:to_h),
69
+						ciphers:          uniq_supported_ciphers.collect(&:to_h),
70
+						cipher_suites:    @preferences.collect { |p, cs| { protocol: p, cipher_suite: cs.collect(&:name) } },
71
+						curves:           @supported_curves.collect(&:to_h),
72
+						curve_preference: @curves_preference.collect(&:name),
73
+						fallback_scsv:    @fallback_scsv
74
+				}
66 75
 			end
67 76
 
68 77
 			protected

Loading…
Cancel
Save