dnsruby 1.55 → 1.56.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +96 -0
  3. data/Rakefile +30 -29
  4. data/demo/axfr.rb +93 -93
  5. data/demo/check_soa.rb +99 -99
  6. data/demo/check_zone.rb +59 -59
  7. data/demo/digdlv.rb +43 -43
  8. data/demo/digroot.rb +34 -34
  9. data/demo/example_recurse.rb +14 -14
  10. data/demo/mresolv.rb +30 -30
  11. data/demo/mx.rb +31 -31
  12. data/demo/rubydig.rb +37 -37
  13. data/demo/to_resolve.txt +3088 -3088
  14. data/demo/trace_dns.rb +46 -46
  15. data/lib/dnsruby.rb +161 -526
  16. data/lib/dnsruby/DNS.rb +305 -0
  17. data/lib/{Dnsruby/Cache.rb → dnsruby/cache.rb} +152 -152
  18. data/lib/{Dnsruby → dnsruby}/code_mapper.rb +48 -52
  19. data/lib/dnsruby/code_mappers.rb +295 -0
  20. data/lib/{Dnsruby/Config.rb → dnsruby/config.rb} +454 -454
  21. data/lib/{Dnsruby → dnsruby}/dnssec.rb +91 -91
  22. data/lib/{Dnsruby/Hosts.rb → dnsruby/hosts.rb} +125 -125
  23. data/lib/{Dnsruby → dnsruby}/ipv4.rb +26 -26
  24. data/lib/{Dnsruby → dnsruby}/ipv6.rb +42 -42
  25. data/lib/{Dnsruby → dnsruby}/key_cache.rb +29 -29
  26. data/lib/dnsruby/message/decoder.rb +164 -0
  27. data/lib/dnsruby/message/encoder.rb +75 -0
  28. data/lib/dnsruby/message/header.rb +249 -0
  29. data/lib/dnsruby/message/message.rb +629 -0
  30. data/lib/dnsruby/message/question.rb +86 -0
  31. data/lib/dnsruby/message/section.rb +96 -0
  32. data/lib/{Dnsruby → dnsruby}/name.rb +141 -141
  33. data/lib/dnsruby/packet_sender.rb +661 -0
  34. data/lib/{Dnsruby/Recursor.rb → dnsruby/recursor.rb} +235 -233
  35. data/lib/dnsruby/resolv.rb +113 -0
  36. data/lib/dnsruby/resolver.rb +1192 -0
  37. data/lib/dnsruby/resource/A.rb +56 -0
  38. data/lib/dnsruby/resource/AAAA.rb +54 -0
  39. data/lib/{Dnsruby → dnsruby}/resource/AFSDB.rb +68 -68
  40. data/lib/{Dnsruby → dnsruby}/resource/CERT.rb +105 -105
  41. data/lib/{Dnsruby → dnsruby}/resource/DHCID.rb +54 -54
  42. data/lib/dnsruby/resource/DLV.rb +27 -0
  43. data/lib/{Dnsruby → dnsruby}/resource/DNSKEY.rb +372 -372
  44. data/lib/{Dnsruby → dnsruby}/resource/DS.rb +255 -255
  45. data/lib/{Dnsruby → dnsruby}/resource/HINFO.rb +71 -71
  46. data/lib/{Dnsruby → dnsruby}/resource/HIP.rb +29 -29
  47. data/lib/{Dnsruby → dnsruby}/resource/IN.rb +30 -30
  48. data/lib/{Dnsruby → dnsruby}/resource/IPSECKEY.rb +31 -31
  49. data/lib/{Dnsruby → dnsruby}/resource/ISDN.rb +62 -62
  50. data/lib/{Dnsruby → dnsruby}/resource/KX.rb +65 -65
  51. data/lib/{Dnsruby → dnsruby}/resource/LOC.rb +263 -263
  52. data/lib/{Dnsruby → dnsruby}/resource/MINFO.rb +69 -69
  53. data/lib/{Dnsruby → dnsruby}/resource/MX.rb +65 -65
  54. data/lib/{Dnsruby → dnsruby}/resource/NAPTR.rb +98 -98
  55. data/lib/{Dnsruby → dnsruby}/resource/NSAP.rb +171 -171
  56. data/lib/dnsruby/resource/NSEC.rb +275 -0
  57. data/lib/dnsruby/resource/NSEC3.rb +332 -0
  58. data/lib/dnsruby/resource/NSEC3PARAM.rb +135 -0
  59. data/lib/dnsruby/resource/OPT.rb +272 -0
  60. data/lib/{Dnsruby → dnsruby}/resource/PX.rb +70 -70
  61. data/lib/{Dnsruby → dnsruby}/resource/RP.rb +75 -75
  62. data/lib/dnsruby/resource/RR.rb +421 -0
  63. data/lib/dnsruby/resource/RRSIG.rb +275 -0
  64. data/lib/dnsruby/resource/RRSet.rb +190 -0
  65. data/lib/{Dnsruby → dnsruby}/resource/RT.rb +67 -67
  66. data/lib/{Dnsruby → dnsruby}/resource/SOA.rb +94 -94
  67. data/lib/dnsruby/resource/SPF.rb +29 -0
  68. data/lib/dnsruby/resource/SRV.rb +112 -0
  69. data/lib/{Dnsruby → dnsruby}/resource/SSHFP.rb +14 -14
  70. data/lib/dnsruby/resource/TKEY.rb +163 -0
  71. data/lib/dnsruby/resource/TSIG.rb +593 -0
  72. data/lib/{Dnsruby → dnsruby}/resource/TXT.rb +191 -191
  73. data/lib/dnsruby/resource/X25.rb +55 -0
  74. data/lib/{Dnsruby → dnsruby}/resource/domain_name.rb +25 -25
  75. data/lib/{Dnsruby → dnsruby}/resource/generic.rb +80 -80
  76. data/lib/dnsruby/resource/resource.rb +25 -0
  77. data/lib/{Dnsruby → dnsruby}/select_thread.rb +148 -148
  78. data/lib/{Dnsruby/SingleResolver.rb → dnsruby/single_resolver.rb} +60 -60
  79. data/lib/{Dnsruby → dnsruby}/single_verifier.rb +344 -344
  80. data/lib/dnsruby/the_log.rb +44 -0
  81. data/lib/dnsruby/update.rb +278 -0
  82. data/lib/dnsruby/validator_thread.rb +124 -0
  83. data/lib/dnsruby/version.rb +3 -0
  84. data/lib/{Dnsruby → dnsruby}/zone_reader.rb +93 -93
  85. data/lib/{Dnsruby → dnsruby}/zone_transfer.rb +377 -377
  86. data/test/spec_helper.rb +16 -0
  87. data/test/tc_axfr.rb +31 -34
  88. data/test/tc_cache.rb +32 -32
  89. data/test/tc_dlv.rb +28 -28
  90. data/test/tc_dns.rb +73 -76
  91. data/test/tc_dnskey.rb +31 -32
  92. data/test/tc_dnsruby.rb +50 -44
  93. data/test/tc_ds.rb +36 -36
  94. data/test/tc_escapedchars.rb +252 -255
  95. data/test/tc_hash.rb +17 -21
  96. data/test/tc_header.rb +48 -57
  97. data/test/tc_hip.rb +19 -22
  98. data/test/tc_ipseckey.rb +18 -21
  99. data/test/tc_keith.rb +300 -0
  100. data/test/tc_message.rb +87 -0
  101. data/test/tc_misc.rb +83 -87
  102. data/test/tc_name.rb +81 -84
  103. data/test/tc_naptr.rb +18 -21
  104. data/test/tc_nsec.rb +55 -55
  105. data/test/tc_nsec3.rb +23 -24
  106. data/test/tc_nsec3param.rb +20 -21
  107. data/test/tc_packet.rb +90 -93
  108. data/test/tc_packet_unique_push.rb +48 -51
  109. data/test/tc_question.rb +30 -33
  110. data/test/tc_queue.rb +16 -17
  111. data/test/tc_recur.rb +16 -17
  112. data/test/tc_res_config.rb +38 -41
  113. data/test/tc_res_env.rb +29 -32
  114. data/test/tc_res_file.rb +26 -29
  115. data/test/tc_res_opt.rb +62 -65
  116. data/test/tc_resolver.rb +287 -242
  117. data/test/tc_rr-opt.rb +70 -63
  118. data/test/tc_rr-txt.rb +68 -71
  119. data/test/tc_rr-unknown.rb +45 -48
  120. data/test/tc_rr.rb +76 -70
  121. data/test/tc_rrset.rb +21 -22
  122. data/test/tc_rrsig.rb +19 -20
  123. data/test/tc_single_resolver.rb +294 -297
  124. data/test/tc_soak.rb +199 -202
  125. data/test/tc_soak_base.rb +29 -34
  126. data/test/tc_sshfp.rb +20 -23
  127. data/test/tc_tcp.rb +32 -35
  128. data/test/tc_tkey.rb +41 -44
  129. data/test/tc_tsig.rb +81 -84
  130. data/test/tc_update.rb +108 -111
  131. data/test/tc_validator.rb +29 -29
  132. data/test/tc_verifier.rb +81 -82
  133. data/test/ts_dnsruby.rb +16 -15
  134. data/test/ts_offline.rb +62 -63
  135. data/test/ts_online.rb +115 -115
  136. metadata +155 -90
  137. data/README +0 -59
  138. data/lib/Dnsruby/DNS.rb +0 -305
  139. data/lib/Dnsruby/PacketSender.rb +0 -656
  140. data/lib/Dnsruby/Resolver.rb +0 -1189
  141. data/lib/Dnsruby/TheLog.rb +0 -44
  142. data/lib/Dnsruby/message.rb +0 -1230
  143. data/lib/Dnsruby/resource/A.rb +0 -56
  144. data/lib/Dnsruby/resource/AAAA.rb +0 -54
  145. data/lib/Dnsruby/resource/DLV.rb +0 -27
  146. data/lib/Dnsruby/resource/NSEC.rb +0 -298
  147. data/lib/Dnsruby/resource/NSEC3.rb +0 -340
  148. data/lib/Dnsruby/resource/NSEC3PARAM.rb +0 -135
  149. data/lib/Dnsruby/resource/OPT.rb +0 -213
  150. data/lib/Dnsruby/resource/RRSIG.rb +0 -275
  151. data/lib/Dnsruby/resource/SPF.rb +0 -29
  152. data/lib/Dnsruby/resource/SRV.rb +0 -112
  153. data/lib/Dnsruby/resource/TKEY.rb +0 -163
  154. data/lib/Dnsruby/resource/TSIG.rb +0 -593
  155. data/lib/Dnsruby/resource/X25.rb +0 -55
  156. data/lib/Dnsruby/resource/resource.rb +0 -678
  157. data/lib/Dnsruby/update.rb +0 -278
  158. data/lib/Dnsruby/validator_thread.rb +0 -124
@@ -1,256 +1,256 @@
1
- #--
2
- #Copyright 2007 Nominet UK
3
- #
4
- #Licensed under the Apache License, Version 2.0 (the "License");
5
- #you may not use this file except in compliance with the License.
6
- #You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- #Unless required by applicable law or agreed to in writing, software
11
- #distributed under the License is distributed on an "AS IS" BASIS,
12
- #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- #See the License for the specific language governing permissions and
14
- #limitations under the License.
15
- #++
16
- require 'base64'
17
- begin
18
- require 'Digest/sha2'
19
- rescue LoadError
20
- require 'digest/sha2'
21
- end
22
- module Dnsruby
23
- class RR
24
- #RFC4034, section 4
25
- #The DS Resource Record refers to a DNSKEY RR and is used in the DNS
26
- #DNSKEY authentication process. A DS RR refers to a DNSKEY RR by
27
- #storing the key tag, algorithm number, and a digest of the DNSKEY RR.
28
- #Note that while the digest should be sufficient to identify the
29
- #public key, storing the key tag and key algorithm helps make the
30
- #identification process more efficient. By authenticating the DS
31
- #record, a resolver can authenticate the DNSKEY RR to which the DS
32
- #record points. The key authentication process is described in
33
- #[RFC4035].
34
-
35
- class DS < RR
36
- class DigestTypes < CodeMapper
37
- update()
38
- add_pair("SHA-1", 1)
39
- add_pair("SHA-256", 2 )
40
- add_pair("SHA-384", 4)
41
- end
42
-
43
- ClassValue = nil #:nodoc: all
44
- TypeValue = Types::DS #:nodoc: all
45
-
46
- #The RDATA for a DS RR consists of a 2 octet Key Tag field, a 1 octet
47
- #Algorithm field, a 1 octet Digest Type field, and a Digest field.
48
- #
49
- # 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
50
- # 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
51
- #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52
- #| Key Tag | Algorithm | Digest Type |
53
- #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54
- #/ /
55
- #/ Digest /
56
- #/ /
57
- #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58
-
59
-
60
- #The Key Tag field lists the key tag of the DNSKEY RR referred to by
61
- #the DS record, in network byte order.
62
- attr_accessor :key_tag
63
- #The algorithm used for this key
64
- #See Dnsruby::Algorithms for permitted values
65
- attr_reader :algorithm
66
- #The DS RR refers to a DNSKEY RR by including a digest of that DNSKEY
67
- #RR. The Digest Type field identifies the algorithm used to construct
68
- #the digest.
69
- attr_reader :digest_type
70
- #The DS record refers to a DNSKEY RR by including a digest of that
71
- #DNSKEY RR.
72
- attr_accessor :digest
73
- attr_accessor :digestbin
74
-
75
- def digest_type=(d)
76
- dig = DS.get_digest_type(d)
77
- @digest_type = dig
78
- end
79
-
80
- def DS.get_digest_type(d)
81
- if (d.instance_of?String)
82
- if (d.length == 1)
83
- d = d.to_i
84
- end
85
- end
86
- begin
87
- digest = DigestTypes.new(d)
88
- return digest
89
- rescue ArgumentError => e
90
- raise DecodeError.new(e)
91
- end
92
- end
93
-
94
- def algorithm=(a)
95
- if (a.instance_of?String)
96
- if (a.length < 3)
97
- a = a.to_i
98
- end
99
- end
100
- begin
101
- alg = Algorithms.new(a)
102
- @algorithm = alg
103
- rescue ArgumentError => e
104
- raise DecodeError.new(e)
105
- end
106
- end
107
-
108
- # Return the digest of the specified DNSKEY RR
109
- def digest_key(*args) # key, digest_type)
110
- digest_type = @digest_type
111
- key = args[0]
112
- if (args.length == 2)
113
- digest_type = args[1]
114
- end
115
-
116
-
117
- data = MessageEncoder.new {|msg|
118
- msg.put_name(key.name, true)
119
- key.encode_rdata(msg, true)
120
- }.to_s
121
-
122
-
123
- if (digest_type.code == 1)
124
- digestbin = OpenSSL::Digest::SHA1.digest(data)
125
- return digestbin
126
- elsif (digest_type.code == 2)
127
- digestbin = OpenSSL::Digest::SHA256.digest(data)
128
- return digestbin
129
- elsif (digest_type.code == 4)
130
- digestbin = OpenSSL::Digest::SHA384.digest(data)
131
- return digestbin
132
- end
133
-
134
- end
135
-
136
- # Check if the key's digest is the same as that stored in the DS record
137
- def check_key(key)
138
- if ((key.key_tag == @key_tag) && (key.algorithm == @algorithm))
139
-
140
- digestbin = digest_key(key)
141
- if (@digestbin == digestbin)
142
- if (!key.zone_key?)
143
- else
144
- return true
145
- end
146
- else
147
- end
148
- end
149
- return false
150
- end
151
-
152
-
153
- def DS.from_key(key, digest_type)
154
- ## The key must not be a NULL key.
155
- # if ((key.flags & 0xc000 ) == 0xc000 )
156
- # puts "\nCreating a DS record for a NULL key is illegal"
157
- # return
158
- # end
159
- #
160
- # # Bit 0 must not be set.
161
- # if (key.flags & 0x8000)
162
- # puts "\nCreating a DS record for a key with flag bit 0 set " +
163
- # "to 0 is illegal"
164
- # return
165
- # end
166
- #
167
- # Bit 6 must be set to 0 bit 7 must be set to 1
168
- if (( key.flags & 0x300) != 0x100)
169
- puts "\nCreating a DS record for a key with flags 6 and 7 not set "+
170
- "0 and 1 respectively is illegal"
171
- return
172
- end
173
- #
174
- #
175
- # if (key.protocol != 3 )
176
- # puts "\nCreating a DS record for a non DNSSEC (protocol=3) " +
177
- # "key is illegal"
178
- # return
179
- # end
180
- #
181
- digest_type = get_digest_type(digest_type)
182
- # Create a new DS record from the specified key
183
- ds = RR.create(:name => key.name, :type => "DS", :ttl => key.ttl,
184
- :key_tag => key.key_tag,
185
- :digest_type => digest_type, :algorithm => key.algorithm)
186
-
187
- ds.digestbin = ds.digest_key(key, digest_type)
188
- ds.digest = ds.digestbin.unpack("H*")[0]
189
- return ds
190
- end
191
-
192
- def from_data(data) #:nodoc: all
193
- key_tag, algorithm, digest_type, digest = data
194
- self.key_tag=(key_tag)
195
- self.algorithm=(algorithm)
196
- self.digest_type=(digest_type)
197
- self.digestbin=(digest)
198
- self.digest=@digestbin.unpack("H*")[0]
199
- end
200
-
201
- def from_string(input)
202
- if (input.length > 0)
203
- data = input.split(" ")
204
- self.key_tag=(data[0].to_i)
205
- self.algorithm=(data[1])
206
- self.digest_type=(data[2])
207
-
208
- buf = ""
209
- index = 3
210
- end_index = data.length - 1
211
- if (data[index]=="(")
212
- end_index = data.length - 2
213
- index = 4
214
- end
215
- (index..end_index).each {|i|
216
- if (comment_index = data[i].index(";"))
217
- buf += data[i].slice(0, comment_index)
218
- # @TODO@ We lose the comments here - we should really keep them for when we write back to string format?
219
- break
220
- else
221
- buf += data[i]
222
- end
223
- }
224
- # self.digest=Base64.decode64(buf)
225
- buf.gsub!(/\n/, "")
226
- buf.gsub!(/ /, "")
227
- # self.digest=buf.unpack("m*")[0]
228
- self.digest=buf
229
- self.digestbin = [buf].pack("H*")
230
- end
231
- end
232
-
233
- def rdata_to_string #:nodoc: all
234
- if (@key_tag != nil)
235
- # return "#{@key_tag.to_i} #{@algorithm.string} #{@digest_type} ( #{Base64.encode64(@digest)} )"
236
- # return "#{@key_tag.to_i} #{@algorithm.string} #{@digest_type.code} ( #{[@digest].pack("m*").gsub("\n", "")} )"
237
- return "#{@key_tag.to_i} #{@algorithm.string} #{@digest_type.code} ( #{@digest.upcase} )"
238
- else
239
- return ""
240
- end
241
- end
242
-
243
- def encode_rdata(msg, canonical=false) #:nodoc: all
244
- msg.put_pack("ncc", @key_tag, @algorithm.code, @digest_type.code)
245
- msg.put_bytes(@digestbin)
246
- end
247
-
248
- def self.decode_rdata(msg) #:nodoc: all
249
- key_tag, algorithm, digest_type = msg.get_unpack("ncc")
250
- digest = msg.get_bytes
251
- return self.new(
252
- [key_tag, algorithm, digest_type, digest])
253
- end
254
- end
255
- end
1
+ # --
2
+ # Copyright 2007 Nominet UK
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ # ++
16
+ require 'base64'
17
+ begin
18
+ require 'Digest/sha2'
19
+ rescue LoadError
20
+ require 'digest/sha2'
21
+ end
22
+ module Dnsruby
23
+ class RR
24
+ # RFC4034, section 4
25
+ # The DS Resource Record refers to a DNSKEY RR and is used in the DNS
26
+ # DNSKEY authentication process. A DS RR refers to a DNSKEY RR by
27
+ # storing the key tag, algorithm number, and a digest of the DNSKEY RR.
28
+ # Note that while the digest should be sufficient to identify the
29
+ # public key, storing the key tag and key algorithm helps make the
30
+ # identification process more efficient. By authenticating the DS
31
+ # record, a resolver can authenticate the DNSKEY RR to which the DS
32
+ # record points. The key authentication process is described in
33
+ # [RFC4035].
34
+
35
+ class DS < RR
36
+ class DigestTypes < CodeMapper
37
+ update()
38
+ add_pair("SHA-1", 1)
39
+ add_pair("SHA-256", 2 )
40
+ add_pair("SHA-384", 4)
41
+ end
42
+
43
+ ClassValue = nil #:nodoc: all
44
+ TypeValue = Types::DS #:nodoc: all
45
+
46
+ # The RDATA for a DS RR consists of a 2 octet Key Tag field, a 1 octet
47
+ # Algorithm field, a 1 octet Digest Type field, and a Digest field.
48
+ #
49
+ # 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
50
+ # 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
51
+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52
+ # | Key Tag | Algorithm | Digest Type |
53
+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54
+ # / /
55
+ # / Digest /
56
+ # / /
57
+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58
+
59
+
60
+ # The Key Tag field lists the key tag of the DNSKEY RR referred to by
61
+ # the DS record, in network byte order.
62
+ attr_accessor :key_tag
63
+ # The algorithm used for this key
64
+ # See Dnsruby::Algorithms for permitted values
65
+ attr_reader :algorithm
66
+ # The DS RR refers to a DNSKEY RR by including a digest of that DNSKEY
67
+ # RR. The Digest Type field identifies the algorithm used to construct
68
+ # the digest.
69
+ attr_reader :digest_type
70
+ # The DS record refers to a DNSKEY RR by including a digest of that
71
+ # DNSKEY RR.
72
+ attr_accessor :digest
73
+ attr_accessor :digestbin
74
+
75
+ def digest_type=(d)
76
+ dig = DS.get_digest_type(d)
77
+ @digest_type = dig
78
+ end
79
+
80
+ def DS.get_digest_type(d)
81
+ if (d.instance_of?String)
82
+ if (d.length == 1)
83
+ d = d.to_i
84
+ end
85
+ end
86
+ begin
87
+ digest = DigestTypes.new(d)
88
+ return digest
89
+ rescue ArgumentError => e
90
+ raise DecodeError.new(e)
91
+ end
92
+ end
93
+
94
+ def algorithm=(a)
95
+ if (a.instance_of?String)
96
+ if (a.length < 3)
97
+ a = a.to_i
98
+ end
99
+ end
100
+ begin
101
+ alg = Algorithms.new(a)
102
+ @algorithm = alg
103
+ rescue ArgumentError => e
104
+ raise DecodeError.new(e)
105
+ end
106
+ end
107
+
108
+ # Return the digest of the specified DNSKEY RR
109
+ def digest_key(*args) # key, digest_type)
110
+ digest_type = @digest_type
111
+ key = args[0]
112
+ if (args.length == 2)
113
+ digest_type = args[1]
114
+ end
115
+
116
+
117
+ data = MessageEncoder.new {|msg|
118
+ msg.put_name(key.name, true)
119
+ key.encode_rdata(msg, true)
120
+ }.to_s
121
+
122
+
123
+ if (digest_type.code == 1)
124
+ digestbin = OpenSSL::Digest::SHA1.digest(data)
125
+ return digestbin
126
+ elsif (digest_type.code == 2)
127
+ digestbin = OpenSSL::Digest::SHA256.digest(data)
128
+ return digestbin
129
+ elsif (digest_type.code == 4)
130
+ digestbin = OpenSSL::Digest::SHA384.digest(data)
131
+ return digestbin
132
+ end
133
+
134
+ end
135
+
136
+ # Check if the key's digest is the same as that stored in the DS record
137
+ def check_key(key)
138
+ if ((key.key_tag == @key_tag) && (key.algorithm == @algorithm))
139
+
140
+ digestbin = digest_key(key)
141
+ if (@digestbin == digestbin)
142
+ if (!key.zone_key?)
143
+ else
144
+ return true
145
+ end
146
+ else
147
+ end
148
+ end
149
+ return false
150
+ end
151
+
152
+
153
+ def DS.from_key(key, digest_type)
154
+ # # The key must not be a NULL key.
155
+ # if ((key.flags & 0xc000 ) == 0xc000 )
156
+ # puts "\nCreating a DS record for a NULL key is illegal"
157
+ # return
158
+ # end
159
+ #
160
+ # # Bit 0 must not be set.
161
+ # if (key.flags & 0x8000)
162
+ # puts "\nCreating a DS record for a key with flag bit 0 set " +
163
+ # "to 0 is illegal"
164
+ # return
165
+ # end
166
+ #
167
+ # Bit 6 must be set to 0 bit 7 must be set to 1
168
+ if (( key.flags & 0x300) != 0x100)
169
+ puts "\nCreating a DS record for a key with flags 6 and 7 not set "+
170
+ "0 and 1 respectively is illegal"
171
+ return
172
+ end
173
+ #
174
+ #
175
+ # if (key.protocol != 3 )
176
+ # puts "\nCreating a DS record for a non DNSSEC (protocol=3) " +
177
+ # "key is illegal"
178
+ # return
179
+ # end
180
+ #
181
+ digest_type = get_digest_type(digest_type)
182
+ # Create a new DS record from the specified key
183
+ ds = RR.create(:name => key.name, :type => "DS", :ttl => key.ttl,
184
+ :key_tag => key.key_tag,
185
+ :digest_type => digest_type, :algorithm => key.algorithm)
186
+
187
+ ds.digestbin = ds.digest_key(key, digest_type)
188
+ ds.digest = ds.digestbin.unpack("H*")[0]
189
+ return ds
190
+ end
191
+
192
+ def from_data(data) #:nodoc: all
193
+ key_tag, algorithm, digest_type, digest = data
194
+ self.key_tag=(key_tag)
195
+ self.algorithm=(algorithm)
196
+ self.digest_type=(digest_type)
197
+ self.digestbin=(digest)
198
+ self.digest=@digestbin.unpack("H*")[0]
199
+ end
200
+
201
+ def from_string(input)
202
+ if (input.length > 0)
203
+ data = input.split(" ")
204
+ self.key_tag=(data[0].to_i)
205
+ self.algorithm=(data[1])
206
+ self.digest_type=(data[2])
207
+
208
+ buf = ""
209
+ index = 3
210
+ end_index = data.length - 1
211
+ if (data[index]=="(")
212
+ end_index = data.length - 2
213
+ index = 4
214
+ end
215
+ (index..end_index).each {|i|
216
+ if (comment_index = data[i].index(";"))
217
+ buf += data[i].slice(0, comment_index)
218
+ # @TODO@ We lose the comments here - we should really keep them for when we write back to string format?
219
+ break
220
+ else
221
+ buf += data[i]
222
+ end
223
+ }
224
+ # self.digest=Base64.decode64(buf)
225
+ buf.gsub!(/\n/, "")
226
+ buf.gsub!(/ /, "")
227
+ # self.digest=buf.unpack("m*")[0]
228
+ self.digest=buf
229
+ self.digestbin = [buf].pack("H*")
230
+ end
231
+ end
232
+
233
+ def rdata_to_string #:nodoc: all
234
+ if (@key_tag != nil)
235
+ # return "#{@key_tag.to_i} #{@algorithm.string} #{@digest_type} ( #{Base64.encode64(@digest)} )"
236
+ # return "#{@key_tag.to_i} #{@algorithm.string} #{@digest_type.code} ( #{[@digest].pack("m*").gsub("\n", "")} )"
237
+ return "#{@key_tag.to_i} #{@algorithm.string} #{@digest_type.code} ( #{@digest.upcase} )"
238
+ else
239
+ return ""
240
+ end
241
+ end
242
+
243
+ def encode_rdata(msg, canonical=false) #:nodoc: all
244
+ msg.put_pack("ncc", @key_tag, @algorithm.code, @digest_type.code)
245
+ msg.put_bytes(@digestbin)
246
+ end
247
+
248
+ def self.decode_rdata(msg) #:nodoc: all
249
+ key_tag, algorithm, digest_type = msg.get_unpack("ncc")
250
+ digest = msg.get_bytes
251
+ return self.new(
252
+ [key_tag, algorithm, digest_type, digest])
253
+ end
254
+ end
255
+ end
256
256
  end