class Dnskey < ActiveRecord::Base include Versions # version/dnskey_version.rb include EppErrors belongs_to :domain validates :alg, :protocol, :flags, :public_key, presence: true, if: :validate_key_data validate :validate_algorithm validate :validate_protocol validate :validate_flags before_save -> { generate_digest if public_key_changed? && !ds_digest_changed? } before_save lambda { if (public_key_changed? || flags_changed? || alg_changed? || protocol_changed?) && !ds_key_tag_changed? generate_ds_key_tag end } ALGORITHMS = %w(3 5 6 7 8 252 253 254 255) PROTOCOLS = %w(3) FLAGS = %w(0 256 257) # 256 = ZSK, 257 = KSK def epp_code_map { '2005' => [ [:alg, :invalid, { value: { obj: 'alg', val: alg }, values: ALGORITHMS.join(', ') }], [:protocol, :invalid, { value: { obj: 'protocol', val: protocol }, values: PROTOCOLS.join(', ') }], [:flags, :invalid, { value: { obj: 'flags', val: flags }, values: FLAGS.join(', ') }] ], '2302' => [ [:public_key, :taken, { value: { obj: 'pubKey', val: public_key } }] ], '2303' => [ [:base, :dnskey_not_found, { value: { obj: 'pubKey', val: public_key } }] ], '2306' => [ [:alg, :blank], [:protocol, :blank], [:flags, :blank], [:public_key, :blank] ] } end def validate_key_data alg.present? || protocol.present? || flags.present? || public_key.present? end def validate_algorithm return if alg.blank? return if ALGORITHMS.include?(alg.to_s) errors.add(:alg, :invalid, values: ALGORITHMS.join(', ')) end def validate_protocol return if protocol.blank? return if PROTOCOLS.include?(protocol.to_s) errors.add(:protocol, :invalid, values: PROTOCOLS.join(', ')) end def validate_flags return if flags.blank? return if FLAGS.include?(flags.to_s) errors.add(:flags, :invalid, values: FLAGS.join(', ')) end def generate_digest return if flags != 257 # generate ds only with KSK flags_hex = self.class.int_to_hex(flags) protocol_hex = self.class.int_to_hex(protocol) alg_hex = self.class.int_to_hex(alg) hex = [domain.name_in_wire_format, flags_hex, protocol_hex, alg_hex, public_key_hex].join bin = self.class.hex_to_bin(hex) if ds_digest_type == 1 self.ds_digest = Digest::SHA1.hexdigest(bin).upcase elsif ds_digest_type == 2 self.ds_digest = Digest::SHA256.hexdigest(bin).upcase end end def public_key_hex self.class.bin_to_hex(Base64.decode64(public_key)) end def generate_ds_key_tag return if flags != 257 # generate ds key tag only with KSK pk = public_key.gsub(' ', '') wire_format = [flags, protocol, alg].pack('S!>CC') wire_format += Base64.decode64(pk) c = 0 wire_format.each_byte.with_index do |b, i| if i.even? c += b << 8 else c += b end end self.ds_key_tag = ((c & 0xFFFF) + (c >> 16)) & 0xFFFF end class << self def int_to_hex(s) s = s.to_s(16) s.prepend('0') if s.length.odd? end def hex_to_bin(s) s.scan(/../).map(&:hex).pack('c*') end def bin_to_hex(s) s.each_byte.map { |b| format('%02X', b) }.join end end end