Class | Dnsruby::Dnssec |
In: |
lib/Dnsruby/dnssec.rb
|
Parent: | Object |
RFC4033, section 7
"There is one more step that a security-aware stub resolver can take if, for whatever reason, it is not able to establish a useful trust relationship with the recursive name servers that it uses: it can perform its own signature validation by setting the Checking Disabled (CD) bit in its query messages. A validating stub resolver is thus able to treat the DNSSEC signatures as trust relationships between the zone administrators and the stub resolver itself. "
Dnsruby is configured to validate responses by default. However, it is not configured with any trusted keys by default. Applications may use the verify() method to perform verification with of RRSets of Messages with given keys. Alternatively, trusted keys may be added to this class (either directly, or by loading the IANA TAR or the DLV ISC ZSK). Validation will then be performed from these keys (or the DLV registry, if configured). Negative and positive responses are validation.
Messages are tagged with the current security_level (Message::SecurityLevel). UNCHECKED means Dnsruby has not attempted to validate the response. BOGUS means the response has been checked, and is bogus. INSECURE means the response has been validated to be insecure (e.g. in an unsigned zone) SECURE means that the response has been verfied to be correct.
Several validators are provided, with each maintaining its own cache of trusted keys. If validators are added or removed, the caches of the other validators are not affected.
Add a trusted Key Signing Key for the ISC DLV registry.
# File lib/Dnsruby/dnssec.rb, line 88 88: def Dnssec.add_dlv_key(dlv_key) 89: @@dlv_verifier.add_dlv_key(dlv_key) 90: end
Add a new trust anchor
# File lib/Dnsruby/dnssec.rb, line 92 92: def Dnssec.add_trust_anchor(t) 93: # @TODO@ Create a new verifier? 94: @@anchor_verifier.add_trust_anchor(t) 95: end
Add the trusted key with the given expiration time
# File lib/Dnsruby/dnssec.rb, line 97 97: def self.add_trust_anchor_with_expiration(k, expiration) 98: # Create a new verifier? 99: @@anchor_verifier.add_trust_anchor_with_expiration(k, expiration) 100: end
# File lib/Dnsruby/dnssec.rb, line 301 301: def self.anchor_verifier 302: return @@anchor_verifier 303: end
Wipes the cache of trusted keys
# File lib/Dnsruby/dnssec.rb, line 106 106: def self.clear_trust_anchors 107: @@anchor_verifier.clear_trust_anchors 108: end
# File lib/Dnsruby/dnssec.rb, line 114 114: def self.clear_trusted_keys 115: [@@anchor_verifier, @@root_verifier, @@dlv_verifier].each {|v| 116: v.clear_trusted_keys 117: } 118: end
# File lib/Dnsruby/dnssec.rb, line 181 181: def self.default_resolver 182: return @@default_resolver 183: end
This method overrides the system default resolver configuration for validation If default_resolver is set, then it will be used to follow the chain of trust. If it is not, then the default system resolver will be used (unless do_validation_with_recursor is set.
# File lib/Dnsruby/dnssec.rb, line 178 178: def self.default_resolver=(res) 179: @@default_resolver = res 180: end
# File lib/Dnsruby/dnssec.rb, line 304 304: def self.dlv_verifier 305: return @@dlv_verifier 306: end
This method defines the choice of Resolver or Recursor, when the validator is checking responses. If set to true, then a Recursor will be used to query for the DNSSEC records. Otherwise, the default system resolver will be used.
# File lib/Dnsruby/dnssec.rb, line 168 168: def self.do_validation_with_recursor(on) 169: @@do_validation_with_recursor = on 170: end
# File lib/Dnsruby/dnssec.rb, line 171 171: def self.do_validation_with_recursor? 172: return @@do_validation_with_recursor 173: end
Load the IANA TAR. THIS METHOD IS NOT SECURE!!!
# File lib/Dnsruby/dnssec.rb, line 132 132: def self.load_itar 133: # @TODO@ THIS IS VERY INSECURE!! WRITE THIS PROPERLY!! 134: # Should really check the signatures here to make sure the keys are good! 135: Net::FTP::open("ftp.iana.org") { |ftp| 136: ftp.login("anonymous") 137: ftp.passive = true 138: ftp.chdir("/itar") 139: lastname=nil 140: ftp.gettextfile("anchors.mf") {|line| 141: next if (line.strip.length == 0) 142: first = line[0] 143: if (first.class == String) 144: first = first.getbyte(0) # Ruby 1.9 145: end 146: # print "Reading ITAR : #{line}, first : #{first}\n" 147: next if (first==59) # ";") 148: if (line.strip=~(/^DS /) || line.strip=~(/^DNSKEY /)) 149: line = lastname.to_s + ((lastname.absolute?)?".":"") + " " + line 150: end 151: ds = RR.create(line) 152: if ((ds.type == Types::DS) || (ds.type == Types::DNSKEY)) 153: # assert(ds.name.absolute?) 154: Dnssec.add_trust_anchor(ds) 155: end 156: lastname = ds.name 157: } 158: } 159: end
# File lib/Dnsruby/dnssec.rb, line 120 120: def self.no_keys? 121: no_keys = true 122: [@@anchor_verifier, @@root_verifier, @@dlv_verifier].each {|v| 123: if (v.trusted_keys.length() > 0 || 124: v.trust_anchors.length() > 0) 125: no_keys = false 126: end 127: } 128: return no_keys 129: end
Remove the trusted key
# File lib/Dnsruby/dnssec.rb, line 102 102: def Dnssec.remove_trust_anchor(t) 103: @@anchor_verifier.remove_trust_anchor(t) 104: end
# File lib/Dnsruby/dnssec.rb, line 307 307: def self.root_verifier 308: return @@root_verifier 309: end
# File lib/Dnsruby/dnssec.rb, line 110 110: def self.trust_anchors 111: return @@anchor_verifier.trust_anchors 112: end
Returns true for secure/insecure, false otherwise This method will set the security_level on msg to the appropriate value. Could be : secure, insecure, bogus or indeterminate If an error is encountered during verification, then the thrown exception will define the error.
# File lib/Dnsruby/dnssec.rb, line 190 190: def self.validate(msg) 191: query = Message.new() 192: query.header.cd=true 193: return self.validate_with_query(query, msg) 194: end
# File lib/Dnsruby/dnssec.rb, line 277 277: def self.validate_with_anchors(msg, query) 278: return @@anchor_verifier.validate(msg, query) 279: end
# File lib/Dnsruby/dnssec.rb, line 285 285: def self.validate_with_dlv(msg, query) 286: return @@dlv_verifier.validate(msg, query) 287: end
# File lib/Dnsruby/dnssec.rb, line 196 196: def self.validate_with_query(query, msg) 197: if (!msg) 198: return false 199: end 200: # First, just check there is something to validate! 201: found_sigs = false 202: msg.each_resource {|rr| 203: if (rr.type == Types::RRSIG) 204: found_sigs = true 205: end 206: } 207: if (found_sigs) 208: begin 209: if (verify(msg)) 210: msg.security_level = Message::SecurityLevel.SECURE 211: return true 212: end 213: rescue VerifyError 214: end 215: end 216: 217: # SHOULD ALWAYS VERIFY DNSSEC-SIGNED RESPONSES? 218: # Yes - if a trust anchor is configured. Otherwise, act on CD bit (in query) 219: TheLog.debug("Checking whether to validate, query.cd = #{query.header.cd}") 220: if (((@@validation_policy > ValidationPolicy::ALWAYS_ROOT_ONLY) && (self.trust_anchors().length > 0)) || 221: # Check query here, and validate if CD is true 222: (query.header.cd == true)) 223: TheLog.debug("Starting validation") 224: 225: # Validate! 226: # Need to think about trapping/storing exceptions and security_levels here 227: last_error = "" 228: last_level = Message::SecurityLevel.BOGUS 229: last_error_level = Message::SecurityLevel.BOGUS 230: if (@@validation_policy == ValidationPolicy::ALWAYS_LOCAL_ANCHORS_ONLY) 231: last_level, last_error, last_error_level = try_validation(last_level, last_error, last_error_level, 232: Proc.new{|m, q| validate_with_anchors(m, q)}, msg, query) 233: elsif (@@validation_policy == ValidationPolicy::ALWAYS_ROOT_ONLY) 234: last_level, last_error, last_error_level = try_validation(last_level, last_error, last_error_level, 235: Proc.new{|m, q| validate_with_root(m, q)}, msg, query) 236: elsif (@@validation_policy == ValidationPolicy::LOCAL_ANCHORS_THEN_ROOT) 237: last_level, last_error, last_error_level = try_validation(last_level, last_error, last_error_level, 238: Proc.new{|m, q| validate_with_anchors(m, q)}, msg, query) 239: if (last_level != Message::SecurityLevel.SECURE) 240: last_level, last_error, last_error_level = try_validation(last_level, last_error, last_error_level, 241: Proc.new{|m, q| validate_with_root(m, q)}, msg, query) 242: end 243: elsif (@@validation_policy == ValidationPolicy::ROOT_THEN_LOCAL_ANCHORS) 244: last_level, last_error, last_error_level = try_validation(last_level, last_error, last_error_level, 245: Proc.new{|m, q| validate_with_root(m, q)}, msg, query) 246: if (last_level != Message::SecurityLevel.SECURE) 247: last_level, last_error, last_error_level = try_validation(last_level, last_error, last_error_level, 248: Proc.new{|m, q| validate_with_anchors(m, q)}, msg, query) 249: end 250: end 251: if (last_level != Message::SecurityLevel.SECURE) 252: last_level, last_error, last_error_level = try_validation(last_level, last_error, last_error_level, 253: Proc.new{|m, q| validate_with_dlv(m, q)}, msg, query) 254: end 255: # Set the message security level! 256: msg.security_level = last_level 257: raise VerifyError.new(last_error) if (last_level < 0) 258: return (msg.security_level.code > Message::SecurityLevel::UNCHECKED) 259: end 260: msg.security_level = Message::SecurityLevel.UNCHECKED 261: return true 262: end
# File lib/Dnsruby/dnssec.rb, line 281 281: def self.validate_with_root(msg, query) 282: return @@root_verifier.validate(msg, query) 283: end
# File lib/Dnsruby/dnssec.rb, line 74 74: def Dnssec.validation_policy 75: @@validation_policy 76: end
# File lib/Dnsruby/dnssec.rb, line 68 68: def Dnssec.validation_policy=(p) 69: if ((p >= ALWAYS_ROOT_ONY) && (p <= ALWAYS_LOCAL_ANCHORS)) 70: @@validation_policy = p 71: # @TODO@ Should we be clearing the trusted keys now? 72: end 73: end
# File lib/Dnsruby/dnssec.rb, line 289 289: def self.verify(msg) 290: begin 291: return true if @@anchor_verifier.verify(msg) 292: rescue VerifyError 293: begin 294: return true if @@root_verifier.verify(msg) 295: rescue VerifyError 296: return true if @@dlv_verifier.verify(msg) # Will carry error to client 297: end 298: end 299: end