Class | Dnsruby::RR::TSIG |
In: |
lib/Dnsruby/resource/TSIG.rb
|
Parent: | RR |
TSIG implements RFC2845.
"This protocol allows for transaction level authentication using shared secrets and one way hashing. It can be used to authenticate dynamic updates as coming from an approved client, or to authenticate responses as coming from an approved recursive name server."
A Dnsruby::RR::TSIG can represent the data present in a TSIG RR. However, it can also represent the data (specified in RFC2845) used to sign or verify a DNS message.
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
HMAC_MD5 | = | Name.create("HMAC-MD5.SIG-ALG.REG.INT.") |
HMAC_SHA1 | = | Name.create("hmac-sha1.") |
HMAC_SHA256 | = | Name.create("hmac-sha256.") |
DEFAULT_FUDGE | = | 300 |
DEFAULT_ALGORITHM | = | HMAC_MD5 |
TypeValue | = | Types::TSIG #:nodoc: all |
algorithm | [R] |
Gets or sets the domain name that specifies the name of the algorithm. The
only algorithms currently supported are hmac-md5 and hmac-sha1.
rr.algorithm=(algorithm_name) print "algorithm = ", rr.algorithm, "\n" |
error | [RW] |
Returns the RCODE covering TSIG processing. Common
values are NOERROR, BADSIG, BADKEY, and BADTIME. See RFC 2845 for details.
print "error = ", rr.error, "\n" |
fudge | [R] |
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" |
key | [RW] | Stores the secret key used for signing/verifying messages. |
mac | [RW] |
Returns the message authentication code (MAC) as a string of hex
characters. The programmer must call a Net::DNS::Packet object‘s data
method before this will return anything meaningful.
print "MAC = ", rr.mac, "\n" |
mac_size | [RW] |
Returns the number of octets in the message authentication code (MAC). The
programmer must call a Net::DNS::Packet object‘s data method before
this will return anything meaningful.
print "MAC size = ", rr.mac_size, "\n" |
original_id | [RW] |
Gets or sets the original message ID.
rr.original_id(12345) print "original ID = ", rr.original_id, "\n" |
other_data | [RW] |
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" |
other_size | [RW] |
Returns the length of the Other Data. Should be zero unless the error is
BADTIME.
print "other len = ", rr.other_size, "\n" |
time_signed | [RW] |
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" |
Set the algorithm to use to generate the HMAC Supported values are :
# File lib/Dnsruby/resource/TSIG.rb, line 512 512: def algorithm=(alg) 513: if (alg.class == String) 514: if (alg.downcase=="hmac-md5") 515: @algorithm = HMAC_MD5; 516: elsif (alg.downcase=="hmac-sha1") 517: @algorithm = HMAC_SHA1; 518: elsif (alg.downcase=="hmac-sha256") 519: @algorithm = HMAC_SHA256; 520: else 521: raise ArgumentError.new("Invalid TSIG algorithm") 522: end 523: elsif (alg.class == Name) 524: if (alg!=HMAC_MD5 && alg!=HMAC_SHA1 && alg!=HMAC_SHA256) 525: raise ArgumentException.new("Invalid TSIG algorithm") 526: end 527: @algorithm=alg 528: else 529: raise ArgumentError.new("#{alg.class} not valid type for Dnsruby::RR::TSIG#algorithm= - use String or Name") 530: end 531: Dnsruby.log.debug{"Using #{@algorithm.to_s} algorithm"} 532: end
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 (RFC2845 3.4.1)
Message#tsigstate will be set to :Signed.
# File lib/Dnsruby/resource/TSIG.rb, line 67 67: def apply(message, original_request=nil) 68: if (!message.signed?) 69: tsig_rr = generate(message, original_request) 70: message.add_additional(tsig_rr) 71: message.tsigstate = :Signed 72: @query = message 73: tsig_rr.query = message 74: end 75: end
# File lib/Dnsruby/resource/TSIG.rb, line 143 143: def calculate_mac(algorithm, data) 144: mac=nil 145: key = @key.gsub(" ", "") 146: # key = Base64::decode64(key) 147: key = key.unpack("m*")[0] 148: if (algorithm.to_s.downcase == HMAC_MD5.to_s.downcase) 149: mac = OpenSSL::HMAC.digest(OpenSSL::Digest::MD5.new, key, data) 150: elsif (algorithm == HMAC_SHA1) 151: mac = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, key, data) 152: elsif (algorithm == HMAC_SHA256) 153: mac = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, key, data) 154: else 155: # Should we allow client to pass in their own signing function? 156: raise VerifyError.new("Algorithm #{algorithm} unsupported by TSIG") 157: end 158: return mac 159: end
# File lib/Dnsruby/resource/TSIG.rb, line 534 534: def fudge=(f) 535: if (f < 0 || f > 0x7FFF) 536: @fudge = DEFAULT_FUDGE 537: else 538: @fudge = f 539: end 540: end
# File lib/Dnsruby/resource/TSIG.rb, line 460 460: def init_defaults 461: # @TODO@ Have new() method which takes key_name and key? 462: @algorithm = DEFAULT_ALGORITHM 463: @fudge = DEFAULT_FUDGE 464: @mac_size = 0 465: @mac = "" 466: @original_id = rand(65536) 467: @error = 0 468: @other_size = 0 469: @other_data = "" 470: @time_signed = nil 471: @buf = nil 472: 473: # RFC 2845 Section 2.3 474: @klass = "ANY" 475: 476: @ttl = 0 # RFC 2845 Section 2.3 477: end
# File lib/Dnsruby/resource/TSIG.rb, line 483 483: def name=(n) 484: if (n.instance_of?String) 485: n = Name.create(n) 486: end 487: if (!n.absolute?) 488: @name = Name.create(n.to_s + ".") 489: else 490: @name = n 491: end 492: end
# File lib/Dnsruby/resource/TSIG.rb, line 542 542: def rdata_to_string 543: rdatastr="" 544: if (@algorithm!=nil) 545: error = @error 546: error = "UNDEFINED" unless error!=nil 547: rdatastr = "#{@original_id} #{@time_signed} #{@algorithm}. #{error}"; 548: if (@other_size > 0 && @other_data!=nil) 549: rdatastr += " #{@other_data}" 550: end 551: rdatastr += " " + mac.unpack("H*").to_s 552: end 553: 554: return rdatastr 555: end
Verify a response. This method will be called by Dnsruby::SingleResolver before passing a response to the client code. The TSIG record will be removed from packet before passing to client, and the Message#tsigstate and Message#tsigerror will be set accordingly. Message#tsigstate will be set to one of :
# File lib/Dnsruby/resource/TSIG.rb, line 185 185: def verify(query, response, response_bytes, buf="") 186: # 4.6. Client processing of answer 187: # 188: # When a client receives a response from a server and expects to see a 189: # TSIG, it first checks if the TSIG RR is present in the response. 190: # Otherwise, the response is treated as having a format error and 191: # discarded. The client then extracts the TSIG, adjusts the ARCOUNT, 192: # and calculates the keyed digest in the same way as the server. If 193: # the TSIG does not validate, that response MUST be discarded, unless 194: # the RCODE is 9 (NOTAUTH), in which case the client SHOULD attempt to 195: # verify the response as if it were a TSIG Error response, as specified 196: # in [4.3]. A message containing an unsigned TSIG record or a TSIG 197: # record which fails verification SHOULD not be considered an 198: # acceptable response; the client SHOULD log an error and continue to 199: # wait for a signed response until the request times out. 200: 201: # So, this verify method should simply remove the TSIG RR and calculate 202: # the MAC (using original request MAC if required). 203: # Should set tsigstate on packet appropriately, and return error. 204: # Side effect is packet is stripped of TSIG. 205: # Resolver (or client) can then decide what to do... 206: 207: msg_tsig_rr = response.tsig 208: if (!verify_common(response)) 209: return false 210: end 211: 212: new_msg_tsig_rr = generate(response, query, buf, response_bytes, msg_tsig_rr) 213: 214: if (msg_tsig_rr.mac == new_msg_tsig_rr.mac) 215: response.tsigstate = :Verified 216: response.tsigerror = RCode.NOERROR 217: return true 218: else 219: response.tsigstate = :Failed 220: response.tsigerror = RCode.BADSIG 221: return false 222: end 223: end
Checks TSIG signatures across sessions of multiple DNS envelopes. This method is called each time a new envelope comes in. The envelope is checked - if a TSIG is present, them the stream so far is verified, and the response#tsigstate set to :Verified. If a TSIG is not present, and does not need to be present, then the message is added to the digest stream and the response#tsigstate is set to :Intermediate. If there is an error with the TSIG verification, then the response#tsigstate is set to :Failed. Like verify, this method will only be called by the Dnsruby::SingleResolver class. Client code need not call this method directly.
# File lib/Dnsruby/resource/TSIG.rb, line 273 273: def verify_envelope(response, response_bytes) 274: #RFC2845 Section 4.4 275: #----- 276: #A DNS TCP session can include multiple DNS envelopes. This is, for 277: #example, commonly used by zone transfer. Using TSIG on such a 278: #connection can protect the connection from hijacking and provide data 279: #integrity. The TSIG MUST be included on the first and last DNS 280: #envelopes. It can be optionally placed on any intermediary 281: #envelopes. It is expensive to include it on every envelopes, but it 282: #MUST be placed on at least every 100'th envelope. The first envelope 283: #is processed as a standard answer, and subsequent messages have the 284: #following digest components: 285: # 286: #* Prior Digest (running) 287: #* DNS Messages (any unsigned messages since the last TSIG) 288: #* TSIG Timers (current message) 289: # 290: #This allows the client to rapidly detect when the session has been 291: #altered; at which point it can close the connection and retry. If a 292: #client TSIG verification fails, the client MUST close the connection. 293: #If the client does not receive TSIG records frequently enough (as 294: #specified above) it SHOULD assume the connection has been hijacked 295: #and it SHOULD close the connection. The client SHOULD treat this the 296: #same way as they would any other interrupted transfer (although the 297: #exact behavior is not specified). 298: #----- 299: # 300: # Each time a new envelope comes in, this method is called on the QUERY TSIG RR. 301: # It will set the response tsigstate to :Verified :Intermediate or :Failed 302: # as appropriate. 303: 304: # Keep digest going of messages as they come in (and mark them intermediate) 305: # When TSIG comes in, work out what key should be and check. If OK, mark 306: # verified. Can reset digest then. 307: if (!@buf) 308: @num_envelopes = 0 309: @last_signed = 0 310: end 311: @num_envelopes += 1 312: if (!response.tsig) 313: if ((@num_envelopes > 1) && (@num_envelopes - @last_signed < 100)) 314: Dnsruby.log.debug("Receiving intermediate envelope in TSIG TCP session") 315: response.tsigstate = :Intermediate 316: response.tsigerror = RCode.NOERROR 317: @buf = @buf + response_bytes 318: return 319: else 320: response.tsigstate = :Failed 321: Dnsruby.log.error("Expecting signed packet") 322: return false 323: end 324: end 325: @last_signed = @num_envelopes 326: 327: # We have a TSIG - process it! 328: tsig = response.tsig 329: if (@num_envelopes == 1) 330: Dnsruby.log.debug("First response in TSIG TCP session - verifying normally") 331: # Process it as a standard answer 332: ok = verify(@query, response, response_bytes) 333: if (ok) 334: mac_bytes = MessageEncoder.new {|m| 335: m.put_pack('n', tsig.mac_size) 336: m.put_bytes(tsig.mac) 337: }.to_s 338: @buf = mac_bytes 339: end 340: return ok 341: end 342: Dnsruby.log.debug("Processing TSIG on TSIG TCP session") 343: 344: if (!verify_common(response)) 345: return false 346: end 347: 348: # Now add the current message data - remember to frig the arcount 349: response_bytes = Header.decrement_arcount_encoded(response_bytes) 350: @buf += response_bytes[0, response.tsigstart] 351: 352: # Let's add the timers 353: timers_data = MessageEncoder.new { |msg| 354: time_high = (tsig.time_signed >> 32) 355: time_low = (tsig.time_signed & 0xFFFFFFFF) 356: msg.put_pack('nN', time_high, time_low) 357: msg.put_pack('n', tsig.fudge) 358: }.to_s 359: @buf += timers_data 360: 361: mac = calculate_mac(tsig.algorithm, @buf) 362: 363: if (mac != tsig.mac) 364: Dnsruby.log.error("TSIG Verify error on TSIG TCP session") 365: response.tsigstate = :Failed 366: return false 367: end 368: mac_bytes = MessageEncoder.new {|m| 369: m.put_pack('n', mac.length) 370: m.put_bytes(mac) 371: }.to_s 372: @buf=mac_bytes 373: 374: response.tsigstate = :Verified 375: response.tsigerror = RCode.NOERROR 376: return true 377: end