You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

check_helper.rb 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. module CheckHelper
  2. private def __label(value, color, state=true)
  3. color = :default unless color
  4. color = "state-#{color}" if state
  5. "<span class=\"label label-#{color}\">#{value}</span>"
  6. end
  7. def label(value, color, state=true)
  8. __label(value, color, state).html_safe
  9. end
  10. def cell(value, color, state=true)
  11. "<td class=\"label-state-#{color}\">#{value}</td>".html_safe
  12. end
  13. def labels(level, states, state=true)
  14. states.each_pair.collect do |name, value|
  15. color = if value.nil?
  16. :default
  17. elsif ::CryptCheck::State.bad? level
  18. value ? :danger : :success
  19. else
  20. value ? :success : :danger
  21. end
  22. __label name, color, state
  23. end.join(' ').html_safe
  24. end
  25. def states(states)
  26. ::CryptCheck::State.collect do |level|
  27. states[level].each_pair
  28. .select { |_, v| v == true }
  29. .collect { |name, _| __label name, level }
  30. end.flatten(1).join(' ').html_safe
  31. end
  32. def rank_color(rank)
  33. case rank
  34. when :'A+', :A
  35. :great
  36. when :'B+', :B
  37. :best
  38. when :'C+', :C
  39. :good
  40. when :D
  41. nil
  42. when :E
  43. :warning
  44. when :F
  45. :error
  46. else
  47. :critical
  48. end
  49. end
  50. def rank_label(rank)
  51. l = %i(V T).include? rank
  52. label rank, rank_color(rank), !l
  53. end
  54. def protocol_label(protocol)
  55. label protocol.to_sym, protocol.status
  56. end
  57. def protocol_labels(protocols)
  58. protocols.collect { |p| protocol_label p }.join("\n").html_safe
  59. end
  60. def key_label(key)
  61. return label('Aucune', :error) unless key
  62. label "#{key[:type].upcase} #{key[:size]} bits", key_color(key)
  63. end
  64. def key_labels(keys)
  65. return label('Aucune', :error) if keys.empty?
  66. keys.sort { |a, b| -1 * (a[:size] <=> b[:size]) }.collect { |k| key_label k }.join("\n").html_safe
  67. end
  68. def cipher_size_label(cipher)
  69. size = cipher.size if cipher.is_a? CryptCheck::Tls::Cipher
  70. label "#{size} bits", cipher_color(size)
  71. end
  72. def key_color(key)
  73. case key[:size]
  74. when nil then
  75. :default
  76. when 0...1024 then
  77. :error
  78. when 1024...2048 then
  79. :danger
  80. when 2048...4096 then
  81. :warning
  82. else
  83. :success
  84. end
  85. end
  86. def cipher_color(key)
  87. case key
  88. when nil then
  89. :default
  90. when 0...128 then
  91. :error
  92. when 112...128 then
  93. :danger
  94. when 128...256 then
  95. :success
  96. else
  97. :primary
  98. end
  99. end
  100. def cipher_name_label(cipher)
  101. status = cipher.status
  102. status = :success if status == :good
  103. label("&nbsp;", status) + "&nbsp;#{cipher.name}".html_safe
  104. end
  105. def cipher_labels(cipher)
  106. cipher.state.collect { |c, ls| ls.collect { |l| label l.upcase, c } }
  107. .flatten(1).join("\n").html_safe
  108. end
  109. def cipher_kex_type_cell(kex)
  110. color = case kex
  111. when :ecdh then
  112. nil
  113. when :dh then
  114. :warning
  115. when :rsa then
  116. :error
  117. else
  118. :critical
  119. end
  120. kex ||= 'None'
  121. cell kex.to_s.upcase, color
  122. end
  123. def cipher_kex_size_cell(kex)
  124. color = key_color kex
  125. cell kex&.[](:size), color
  126. end
  127. def cipher_auth_type_cell(auth)
  128. color = case auth
  129. when :ecdsa, :rsa then
  130. nil
  131. else
  132. :critical
  133. end
  134. auth ||= 'None'
  135. cell auth.to_s.upcase, color
  136. end
  137. def cipher_auth_size_cell(auth)
  138. color = key_color auth
  139. cell auth&.[](:size), color
  140. end
  141. def cipher_enc_type_cell(enc)
  142. color = case enc
  143. when :chacha20
  144. :success
  145. when nil, :rc4
  146. :critical
  147. end
  148. enc ||= 'NONE'
  149. cell enc.to_s.upcase, color
  150. end
  151. def cipher_enc_block_size_cell(enc)
  152. color = case
  153. when enc == :stream
  154. nil
  155. when enc.nil?
  156. nil
  157. when enc <= 64
  158. :critical
  159. when enc < 128
  160. :error
  161. end
  162. cell enc, color
  163. end
  164. def cipher_enc_key_size_cell(enc)
  165. color = case
  166. when enc.nil?
  167. nil
  168. when enc < 128
  169. :critical
  170. end
  171. cell enc, color
  172. end
  173. def cipher_enc_mode_cell(enc)
  174. color = case enc
  175. when :gcm, :ccm, :aead
  176. :success
  177. end
  178. cell enc.to_s.upcase, color
  179. end
  180. def cipher_mac_type_cell(mac)
  181. color = case mac
  182. when :poly1305 then
  183. :success
  184. when :sha384, :sha256 then
  185. nil
  186. when :sha1 then
  187. :warning
  188. else
  189. :critical
  190. end
  191. cell mac.to_s.upcase, color
  192. end
  193. def cipher_mac_size_cell(mac)
  194. cell mac, nil
  195. end
  196. def cipher_pfs_cell(pfs)
  197. return cell 'PFS', nil if pfs
  198. cell 'No PFS', :error
  199. end
  200. end