Class: Dnsruby::RR::TSIG

Inherits:
RR
  • Object
show all
Defined in:
lib/dnsruby/resource/TSIG.rb

Overview

A TSIG record contains authentication data for a DNS message. This class also holds the shared key and provides signing and verification helpers. RFC 8945 section 4 describes the record format and MAC calculation.

Example code : res = Dnsruby::Resolver.new("ns0.validation-test-servers.nominet.org.uk")

# Now configure the resolver with the TSIG key for signing/verifying
KEY_NAME="rubytsig"
KEY = "8n6gugn4aJ7MazyNlMccGKH1WxD2B3UvN/O/RA6iBupO2/03u9CTa3Ewz3gBWTSBCH3crY4Kk+tigNdeJBAvrw=="
res.tsig=KEY_NAME, KEY

update = Dnsruby::Update.new("validation-test-servers.nominet.org.uk")
# Generate update record name, and test it has been made. Then delete it and check it has been deleted
update_name = generate_update_name
update.absent(update_name)
update.add(update_name, 'TXT', 100, "test signed update")

# Resolver will automatically sign message and verify response
response = res.send_message(update)
assert(response.verified?) # Check that the response has been verified

Constant Summary collapse

HMAC_MD5 =
Name.create("HMAC-MD5.SIG-ALG.REG.INT.")
HMAC_SHA1 =
Name.create("hmac-sha1.")
HMAC_SHA256 =
Name.create("hmac-sha256.")
HMAC_SHA512 =
Name.create("hmac-sha512.")
DEFAULT_FUDGE =
300
DEFAULT_ALGORITHM =
HMAC_SHA256
TypeValue =

:nodoc: all

Types::TSIG
ClassValue =

:nodoc: all

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#algorithm ⇒ Object

Gets or sets the domain name that specifies the name of the algorithm. Supported algorithms are hmac-md5, hmac-sha1, hmac-sha256 and hmac-sha512. SHA-256 is the default. You can select MD5 for legacy servers, but its use does not comply with RFC 8945 section 6.

rr.algorithm=(algorithm_name)
print "algorithm = ", rr.algorithm, "\n"


424
425
426
# File 'lib/dnsruby/resource/TSIG.rb', line 424

def algorithm
  @algorithm
end

#error ⇒ Object

Returns the RCODE covering TSIG processing. Common values are NOERROR, BADSIG, BADKEY, BADTIME and BADTRUNC (RFC 8945 section 5).

print "error = ", rr.error, "\n"


484
485
486
# File 'lib/dnsruby/resource/TSIG.rb', line 484

def error
  @error
end

#fudge ⇒ Object

Gets or sets the "fudge", i.e., the seconds of error permitted in the signing time.

The default fudge is 300 seconds.

rr.fudge=(60)
print "fudge = ", rr.fudge, "\n"


444
445
446
# File 'lib/dnsruby/resource/TSIG.rb', line 444

def fudge
  @fudge
end

#key ⇒ Object

Stores the secret key used for signing/verifying messages.



502
503
504
# File 'lib/dnsruby/resource/TSIG.rb', line 502

def key
  @key
end

#mac ⇒ Object

Returns the message authentication code (MAC) as a binary string. Set when a record is decoded or a message is signed.

print "MAC = ", rr.mac, "\n"


470
471
472
# File 'lib/dnsruby/resource/TSIG.rb', line 470

def mac
  @mac
end

#mac_length ⇒ Object

MAC length in bytes for signing, and minimum length to accept. nil signs with the full digest and accepts the RFC 8945 minimum. mac_size holds the actual length in a signed or decoded record.



456
457
458
# File 'lib/dnsruby/resource/TSIG.rb', line 456

def mac_length
  @mac_length
end

#mac_size ⇒ Object

Returns the number of octets in the message authentication code (MAC). Set when a record is decoded or a message is signed.

print "MAC size = ", rr.mac_size, "\n"


451
452
453
# File 'lib/dnsruby/resource/TSIG.rb', line 451

def mac_size
  @mac_size
end

#original_id ⇒ Object

Gets or sets the original message ID.

rr.original_id(12345)
print "original ID = ", rr.original_id, "\n"


477
478
479
# File 'lib/dnsruby/resource/TSIG.rb', line 477

def original_id
  @original_id
end

#other_data ⇒ Object

Returns the Other Data. This field should be empty unless the error is BADTIME, in which case it will contain the server's time as the number of seconds since 1 Jan 1970 00:00:00 UTC.

print "other data = ", rr.other_data, "\n"


499
500
501
# File 'lib/dnsruby/resource/TSIG.rb', line 499

def other_data
  @other_data
end

#other_size ⇒ Object

Returns the length of the Other Data. Should be zero unless the error is BADTIME.

print "other len = ", rr.other_size, "\n"


491
492
493
# File 'lib/dnsruby/resource/TSIG.rb', line 491

def other_size
  @other_size
end

#time_signed ⇒ Object

Gets or sets the signing time as the number of seconds since 1 Jan 1970 00:00:00 UTC.

The default signing time is the current time.

rr.time_signed=(time)
print "time signed = ", rr.time_signed, "\n"


434
435
436
# File 'lib/dnsruby/resource/TSIG.rb', line 434

def time_signed
  @time_signed
end

Class Method Details

.authenticated_error(response, signature) ⇒ Object

Verification removes the TSIG RR from the Message. Keep its saved record long enough to report authenticated errors consistently for ordinary queries and zone transfers (RFC 8945 sections 5.4.3/5.4.4).



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/dnsruby/resource/TSIG.rb', line 72

def self.authenticated_error(response, signature) #:nodoc:
  return unless signature && response.verified? && response.header.get_header_rcode == RCode.NOTAUTH
  description = SIGNED_ERRORS[response.tsigerror]
  return unless description
  diagnostic = "Authenticated TSIG #{description} (#{response.tsigerror})"
  if response.tsigerror == RCode.BADTIME
    high, low = signature.other_data.unpack('nN')
    diagnostic += "; server time #{(high << 32) | low} seconds since Unix epoch"
  end
  Dnsruby.log.error { diagnostic }
  TsigError.new(diagnostic)
end

.decode_rdata(msg) ⇒ Object

:nodoc: all



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/dnsruby/resource/TSIG.rb', line 607

def self.decode_rdata(msg) #:nodoc: all
  # RFC 8945 section 4.2 forbids compression in the algorithm name.
  labels, length = [], 1
  loop do
    raise DecodeError, 'Truncated TSIG algorithm name' unless msg.has_remaining?
    size, = msg.get_unpack('C')
    break if size == 0
    raise DecodeError, 'Compressed or invalid TSIG algorithm name' unless size && size <= 63
    length += size + 1
    raise DecodeError, 'TSIG algorithm name exceeds 255 octets' if length > 255
    msg.assert_buffer_position_valid(msg.index + size)
    labels << Name::Label.new(msg.get_bytes(size))
  end
  alg = Name.new(labels)
  time_high, time_low = msg.get_unpack("nN")
  time_signed = (time_high << 32) + time_low
  fudge, = msg.get_unpack("n")
  mac_size, = msg.get_unpack("n")
  mac = msg.get_bytes(mac_size)
  original_id, = msg.get_unpack("n")
  error, = msg.get_unpack("n")
  other_size, = msg.get_unpack("n")
  other_data = msg.get_bytes(other_size)
  return self.new([alg, time_signed, fudge, mac_size, mac, original_id, error, other_size, other_data])
end

Instance Method Details

#apply(message, original_request = nil) ⇒ Object

Generates a TSIG record and adds it to the message. Takes an optional original_request argument for the case where this is a response to a query (RFC 8945 section 4.3.1).

Message#tsigstate will be set to :Signed.



90
91
92
93
94
95
96
97
98
# File 'lib/dnsruby/resource/TSIG.rb', line 90

def apply(message, original_request=nil)
  if (!message.signed?)
    tsig_rr = generate(message, original_request)
    message.add_additional(tsig_rr)
    message.tsigstate = :Signed
    @query = message
    tsig_rr.query = message
  end
end

#calculate_mac(algorithm, data) ⇒ Object

Raises:



196
197
198
199
200
201
# File 'lib/dnsruby/resource/TSIG.rb', line 196

def calculate_mac(algorithm, data)
  name = algorithm.to_s.downcase.sub(/\.$/, '')
  digest = DIGESTS[name]
  raise VerifyError, "Algorithm #{algorithm} unsupported by TSIG" unless digest
  OpenSSL::HMAC.digest(digest, @key.gsub(' ', '').unpack1('m*'), data)
end

#encode_rdata(msg, canonical = false) ⇒ Object

:nodoc: all



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/dnsruby/resource/TSIG.rb', line 591

def encode_rdata(msg, canonical=false) #:nodoc: all
  validate_wire_fields(self, @time_signed)
  # Algorithm Name must be uncompressed (RFC 8945 section 4.2).
  msg.put_name(@algorithm.downcase, true)
  time_high = (@time_signed >> 32)
  time_low = (@time_signed & 0xFFFFFFFF)
  msg.put_pack('nN', time_high, time_low)
  msg.put_pack('n', @fudge)
  msg.put_pack('n', @mac_size)
  msg.put_bytes(@mac)
  msg.put_pack('n', @original_id)
  msg.put_pack('n', @error)
  msg.put_pack('n', @other_size)
  msg.put_bytes(@other_data)
end

#from_data(data) ⇒ Object

:nodoc: all



523
524
525
# File 'lib/dnsruby/resource/TSIG.rb', line 523

def from_data(data) #:nodoc: all
  @algorithm, @time_signed, @fudge, @mac_size, @mac, @original_id, @error, @other_size, @other_data = data
end

#from_string(str) ⇒ Object

Create the RR from a standard string



539
540
541
542
543
544
545
546
547
548
549
# File 'lib/dnsruby/resource/TSIG.rb', line 539

def from_string(str) #:nodoc: all
  parts = str.split("[:/]")
  if (parts.length < 2 || parts.length > 3)
    raise ArgumentException.new("Invalid TSIG key specification")
  end
  if (parts.length == 3)
    return TSIG.new(parts[0], parts[1], parts[2]);
  else
    return TSIG.new(DEFAULT_ALGORITHM, parts[0], parts[1]);
  end
end

#generate(msg, original_request = nil, data = "", msg_bytes = nil, tsig_rr = self) ⇒ Object

Generates a TSIG record. The caller must authenticate original_request before using it to sign a response.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/dnsruby/resource/TSIG.rb', line 107

def generate(msg, original_request = nil, data="", msg_bytes=nil, tsig_rr=self)#:nodoc: all
  verifying = !msg_bytes.nil?
  unless msg_bytes
    tsig_rr.warn_if_legacy_algorithm
    if [msg.answer, msg.authority, msg.additional].any? { |section| section.any? { |rr| rr.type == Types::TSIG } }
      raise EncodeError, 'Message already contains a TSIG'
    end
    if !msg.header.qr && tsig_rr.error != RCode.NOERROR
      raise EncodeError, 'A TSIG request must have error NOERROR'
    end
    if msg.header.qr && [RCode.BADSIG, RCode.BADKEY].include?(tsig_rr.error)
      raise EncodeError, 'BADKEY and BADSIG responses must be unsigned'
    end
    if original_request && tsig_rr.error == RCode.BADTIME
      tsig_rr = tsig_rr.dup
      tsig_rr.time_signed = original_request.tsig.time_signed
      tsig_rr.fudge = original_request.tsig.fudge
    end
  end
  time_signed=@time_signed
  if (!time_signed)
    time_signed=Time.now.to_i
  end
  if (tsig_rr.time_signed)
    time_signed = tsig_rr.time_signed
  end
  validate_wire_fields(tsig_rr, time_signed) unless msg_bytes
  if !msg_bytes && msg.header.qr && tsig_rr.other_size != (tsig_rr.error == RCode.BADTIME ? 6 : 0)
    raise EncodeError, 'Invalid TSIG response Other Data length'
  end
  original_id = verifying ? tsig_rr.original_id : (original_request ? original_request.tsig.original_id : msg.header.id)

  if (original_request)
    # Responses include the request MAC exactly as transmitted.
    mac_bytes = MessageEncoder.new {|m|
      m.put_pack('n', original_request.tsig.mac_size)
      m.put_bytes(original_request.tsig.mac)
    }.to_s
    data  += mac_bytes
    msg.header.id = original_request.header.id unless msg_bytes
  end

  if (!msg_bytes)
    msg_bytes = msg.encode
    data += [original_id].pack('n') + msg_bytes.byteslice(2..-1)
  else
    data += verification_message(msg, msg_bytes, tsig_rr)
  end

  data += sig_data(tsig_rr, time_signed)

  mac = calculate_mac(tsig_rr.algorithm, data)
  if !verifying && @mac_length
    minimum = [10, mac.bytesize / 2].max
    unless (minimum..mac.bytesize).cover?(@mac_length)
      raise EncodeError, "TSIG MAC length must be between #{minimum} and #{mac.bytesize} octets"
    end
    if original_request && @mac_length < original_request.tsig.mac_size
      raise EncodeError, 'TSIG response MAC must not be shorter than the request MAC'
    end
    mac = mac.byteslice(0, @mac_length)
  end

  mac_size = mac.length

  new_tsig_rr = Dnsruby::RR.create(name: tsig_rr.name, type: Types.TSIG,
    ttl: tsig_rr.ttl, klass: tsig_rr.klass, key: @key, mac_length: @mac_length)
  # Copy fields directly so verifying an incoming record doesn't trigger
  # the warning for configuring a legacy key.
  new_tsig_rr.from_data([tsig_rr.algorithm, time_signed, tsig_rr.fudge,
    mac_size, mac, original_id, tsig_rr.error, tsig_rr.other_size, tsig_rr.other_data])
  return new_tsig_rr

end

#init_defaults ⇒ Object



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/dnsruby/resource/TSIG.rb', line 504

def init_defaults
  #  @TODO@ Have new() method which takes key_name and key?
  @algorithm   = DEFAULT_ALGORITHM
  @fudge       = DEFAULT_FUDGE
  @mac_size    = 0
  @mac_length  = nil
  @mac         = ""
  @original_id = rand(65536)
  @error       = 0
  @other_size   = 0
  @other_data  = ""
  @time_signed = nil
  @buf = nil

  # TSIG records use class ANY and TTL zero (RFC 8945 section 4.2).
  @klass = Classes.ANY
  @ttl = 0
end

#name=(n) ⇒ Object



527
528
529
530
531
532
533
534
535
536
# File 'lib/dnsruby/resource/TSIG.rb', line 527

def name=(n)
  if (n.instance_of?String)
    n = Name.create(n)
  end
  if (!n.absolute?)
    @name = Name.create(n.to_s + ".")
  else
    @name = n
  end
end

#query=(q) ⇒ Object

:nodoc: all



100
101
102
# File 'lib/dnsruby/resource/TSIG.rb', line 100

def query=q#:nodoc: all
  @query = q
end

#rdata_to_string ⇒ Object



576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/dnsruby/resource/TSIG.rb', line 576

def rdata_to_string
  rdatastr=""
  if (@algorithm!=nil)
    error = @error
    error = "UNDEFINED" unless error!=nil
    rdatastr = "#{@original_id} #{@time_signed} #{@algorithm.to_s(true)} #{error}";
    if (@other_size > 0 && @other_data!=nil)
      rdatastr += " #{@other_data}"
    end
    rdatastr += " " + mac.unpack("H*").to_s
  end

  return rdatastr
end

#sig_data(tsig_rr, time_signed = @time_signed) ⇒ Object

Private method to return the TSIG RR data to be signed



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/dnsruby/resource/TSIG.rb', line 223

def sig_data(tsig_rr, time_signed=@time_signed) #:nodoc: all
  return MessageEncoder.new { |msg|
    msg.put_name(tsig_rr.name.downcase, true)
    msg.put_pack('nN', tsig_rr.klass.code, tsig_rr.ttl)
    msg.put_name(tsig_rr.algorithm.downcase, true)

    time_high = (time_signed >> 32)
    time_low = (time_signed & 0xFFFFFFFF)
    msg.put_pack('nN', time_high, time_low)
    msg.put_pack('n', tsig_rr.fudge)
    msg.put_pack('n', tsig_rr.error)
    msg.put_pack('n', tsig_rr.other_size)
    msg.put_bytes(tsig_rr.other_data)
  }.to_s
end

#verify(query, response, response_bytes, buf = "") ⇒ Object

Verify a response using the original query's MAC (RFC 8945 section 5.4). Removes the TSIG record and sets Message#tsigstate and Message#tsigerror. Returns false for server errors, even with a valid MAC. Check the Verified state and error code to identify authenticated errors.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/dnsruby/resource/TSIG.rb', line 243

def verify(query, response, response_bytes, buf="")
  signature = response.tsig
  signed_error = signature && response.header.get_header_rcode == RCode.NOTAUTH &&
    SIGNED_ERRORS.key?(signature.error)
  return false unless verify_common_fields(response, signed_error)

  expected = generate(response, query, buf, response_bytes, signature).mac
  return false unless verify_mac(response, signature, expected)
  # A BADTIME reply echoes the request's timers; only Other Data carries
  # the server's clock (RFC 8945 section 5.2.3).
  if signed_error && signature.error == RCode.BADTIME && query && query.tsig &&
      (signature.time_signed != query.tsig.time_signed || signature.fudge != query.tsig.fudge)
    return verification_failed(response, RCode.BADTIME)
  end
  return false unless verify_time(response, signature)
  return false unless verify_truncation_policy(response, signature)
  response.tsigstate = :Verified
  response.tsigerror = signature.error
  signature.error == RCode.NOERROR
end

#verify_common(response) ⇒ Object

:nodoc: all



264
265
266
# File 'lib/dnsruby/resource/TSIG.rb', line 264

def verify_common(response) #:nodoc: all
  verify_common_fields(response, false)
end

#verify_envelope(response, response_bytes) ⇒ Object

Verify a stream, such as a zone transfer. Each signature covers the messages since the previous one. RFC 8945 section 5.3.1 requires senders to sign every message. For compatibility, receivers must accept up to 99 unsigned messages between signatures. The caller must also check that the final message is signed.

Sets response.tsigstate to :Verified, :Intermediate or :Failed.



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/dnsruby/resource/TSIG.rb', line 338

def verify_envelope(response, response_bytes)
  if (!@buf)
    @num_envelopes = 0
    @last_signed = 0
  end
  @num_envelopes += 1
  if (!response.tsig)
    if ((@num_envelopes > 1) && (@num_envelopes - @last_signed < 100))
      Dnsruby.log.debug("Receiving intermediate envelope in TSIG TCP session")
      response.tsigstate = :Intermediate
      response.tsigerror = RCode.NOERROR
      @buf << response_bytes
      return true
    else
      response.tsigerror = RCode.FORMERR
      response.tsigstate = :Failed
      Dnsruby.log.error("Expecting signed packet")
      return false
    end
  end
  #  We have a TSIG - process it!
  tsig = response.tsig
  if (@num_envelopes == 1)
    Dnsruby.log.debug("First response in TSIG TCP session - verifying normally")
    #  Process it as a standard answer
    ok = verify(@query, response, response_bytes)
    if (ok)
      mac_bytes = MessageEncoder.new {|m|
        m.put_pack('n', tsig.mac_size)
        m.put_bytes(tsig.mac)
      }.to_s
      @buf = mac_bytes
      @last_signed = @num_envelopes
      @last_time_signed = tsig.time_signed
    end
    return ok
  end
  Dnsruby.log.debug("Processing TSIG on TSIG TCP session")

  if (!verify_common(response))
    return false
  end

  data = @buf + verification_message(response, response_bytes, tsig)

  #  Let's add the timers
  timers_data = MessageEncoder.new { |msg|
    time_high = (tsig.time_signed >> 32)
    time_low = (tsig.time_signed & 0xFFFFFFFF)
    msg.put_pack('nN', time_high, time_low)
    msg.put_pack('n', tsig.fudge)
  }.to_s
  data += timers_data
  mac = calculate_mac(tsig.algorithm, data)
  return false unless verify_mac(response, tsig, mac)
  return false unless verify_time(response, tsig)
  if @last_time_signed && tsig.time_signed < @last_time_signed
    return verification_failed(response, RCode.BADTIME)
  end
  return false unless verify_truncation_policy(response, tsig)
  mac_bytes = MessageEncoder.new {|m|
    m.put_pack('n', tsig.mac_size)
    m.put_bytes(tsig.mac)
  }.to_s
  @buf=mac_bytes
  @last_signed = @num_envelopes
  @last_time_signed = tsig.time_signed

  response.tsigstate = :Verified
  response.tsigerror = RCode.NOERROR
  return true
end

#warn_if_legacy_algorithm ⇒ Object

:nodoc: all



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/dnsruby/resource/TSIG.rb', line 182

def warn_if_legacy_algorithm #:nodoc: all
  name = @algorithm.to_s.downcase.sub(/\.$/, '')
  return unless [HMAC_MD5.to_s.downcase.sub(/\.$/, ''), 'hmac-md5'].include?(name)
  warn = LEGACY_WARNING_MUTEX.synchronize do
    unless TSIG.instance_variable_get(:@legacy_warning_issued)
      TSIG.instance_variable_set(:@legacy_warning_issued, true)
      true
    end
  end
  if warn
    Dnsruby.log.warn('HMAC-MD5 TSIG is enabled as a legacy compatibility exception. RFC 8945 says it MUST NOT be used. Update the TSIG key and configure hmac-sha256 on both the DNS server and client.')
  end
end