| 1 | package dns |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto" |
| 6 | "crypto/ecdsa" |
| 7 | "crypto/ed25519" |
| 8 | "crypto/elliptic" |
| 9 | "crypto/rand" |
| 10 | "crypto/rsa" |
| 11 | _ "crypto/sha1" // need its init function |
| 12 | _ "crypto/sha256" // need its init function |
| 13 | _ "crypto/sha512" // need its init function |
| 14 | "encoding/asn1" |
| 15 | "encoding/hex" |
| 16 | "math/big" |
| 17 | "sort" |
| 18 | "strings" |
| 19 | "time" |
| 20 | ) |
| 21 | |
| 22 | // DNSSEC encryption algorithm codes. |
| 23 | const ( |
| 24 | _ uint8 = iota |
| 25 | RSAMD5 |
| 26 | DH |
| 27 | DSA |
| 28 | _ // Skip 4, RFC 6725, section 2.1 |
| 29 | RSASHA1 |
| 30 | DSANSEC3SHA1 |
| 31 | RSASHA1NSEC3SHA1 |
| 32 | RSASHA256 |
| 33 | _ // Skip 9, RFC 6725, section 2.1 |
| 34 | RSASHA512 |
| 35 | _ // Skip 11, RFC 6725, section 2.1 |
| 36 | ECCGOST |
| 37 | ECDSAP256SHA256 |
| 38 | ECDSAP384SHA384 |
| 39 | ED25519 |
| 40 | ED448 |
| 41 | INDIRECT uint8 = 252 |
| 42 | PRIVATEDNS uint8 = 253 // Private (experimental keys) |
| 43 | PRIVATEOID uint8 = 254 |
| 44 | ) |
| 45 | |
| 46 | // AlgorithmToString is a map of algorithm IDs to algorithm names. |
| 47 | var AlgorithmToString = map[uint8]string{ |
| 48 | RSAMD5: "RSAMD5", |
| 49 | DH: "DH", |
| 50 | DSA: "DSA", |
| 51 | RSASHA1: "RSASHA1", |
| 52 | DSANSEC3SHA1: "DSA-NSEC3-SHA1", |
| 53 | RSASHA1NSEC3SHA1: "RSASHA1-NSEC3-SHA1", |
| 54 | RSASHA256: "RSASHA256", |
| 55 | RSASHA512: "RSASHA512", |
| 56 | ECCGOST: "ECC-GOST", |
| 57 | ECDSAP256SHA256: "ECDSAP256SHA256", |
| 58 | ECDSAP384SHA384: "ECDSAP384SHA384", |
| 59 | ED25519: "ED25519", |
| 60 | ED448: "ED448", |
| 61 | INDIRECT: "INDIRECT", |
| 62 | PRIVATEDNS: "PRIVATEDNS", |
| 63 | PRIVATEOID: "PRIVATEOID", |
| 64 | } |
| 65 | |
| 66 | // AlgorithmToHash is a map of algorithm crypto hash IDs to crypto.Hash's. |
| 67 | // For newer algorithm that do their own hashing (i.e. ED25519) the returned value |
| 68 | // is 0, implying no (external) hashing should occur. The non-exported identityHash is then |
| 69 | // used. |
| 70 | var AlgorithmToHash = map[uint8]crypto.Hash{ |
| 71 | RSAMD5: crypto.MD5, // Deprecated in RFC 6725 |
| 72 | DSA: crypto.SHA1, |
| 73 | RSASHA1: crypto.SHA1, |
| 74 | RSASHA1NSEC3SHA1: crypto.SHA1, |
| 75 | RSASHA256: crypto.SHA256, |
| 76 | ECDSAP256SHA256: crypto.SHA256, |
| 77 | ECDSAP384SHA384: crypto.SHA384, |
| 78 | RSASHA512: crypto.SHA512, |
| 79 | ED25519: 0, |
| 80 | } |
| 81 | |
| 82 | // DNSSEC hashing algorithm codes. |
| 83 | const ( |
| 84 | _ uint8 = iota |
| 85 | SHA1 // RFC 4034 |
| 86 | SHA256 // RFC 4509 |
| 87 | GOST94 // RFC 5933 |
| 88 | SHA384 // Experimental |
| 89 | SHA512 // Experimental |
| 90 | ) |
| 91 | |
| 92 | // HashToString is a map of hash IDs to names. |
| 93 | var HashToString = map[uint8]string{ |
| 94 | SHA1: "SHA1", |
| 95 | SHA256: "SHA256", |
| 96 | GOST94: "GOST94", |
| 97 | SHA384: "SHA384", |
| 98 | SHA512: "SHA512", |
| 99 | } |
| 100 | |
| 101 | // DNSKEY flag values. |
| 102 | const ( |
| 103 | SEP = 1 |
| 104 | REVOKE = 1 << 7 |
| 105 | ZONE = 1 << 8 |
| 106 | ) |
| 107 | |
| 108 | // The RRSIG needs to be converted to wireformat with some of the rdata (the signature) missing. |
| 109 | type rrsigWireFmt struct { |
| 110 | TypeCovered uint16 |
| 111 | Algorithm uint8 |
| 112 | Labels uint8 |
| 113 | OrigTtl uint32 |
| 114 | Expiration uint32 |
| 115 | Inception uint32 |
| 116 | KeyTag uint16 |
| 117 | SignerName string `dns:"domain-name"` |
| 118 | /* No Signature */ |
| 119 | } |
| 120 | |
| 121 | // Used for converting DNSKEY's rdata to wirefmt. |
| 122 | type dnskeyWireFmt struct { |
| 123 | Flags uint16 |
| 124 | Protocol uint8 |
| 125 | Algorithm uint8 |
| 126 | PublicKey string `dns:"base64"` |
| 127 | /* Nothing is left out */ |
| 128 | } |
| 129 | |
| 130 | // KeyTag calculates the keytag (or key-id) of the DNSKEY. |
| 131 | func (k *DNSKEY) KeyTag() uint16 { |
| 132 | if k == nil { |
| 133 | return 0 |
| 134 | } |
| 135 | var keytag int |
| 136 | keywire := new(dnskeyWireFmt) |
| 137 | keywire.Flags = k.Flags |
| 138 | keywire.Protocol = k.Protocol |
| 139 | keywire.Algorithm = k.Algorithm |
| 140 | keywire.PublicKey = k.PublicKey |
| 141 | wire := make([]byte, DefaultMsgSize) |
| 142 | n, err := packKeyWire(keywire, wire) |
| 143 | if err != nil { |
| 144 | return 0 |
| 145 | } |
| 146 | wire = wire[:n] |
| 147 | for i, v := range wire { |
| 148 | if i&1 != 0 { |
| 149 | keytag += int(v) // must be larger than uint32 |
| 150 | } else { |
| 151 | keytag += int(v) << 8 |
| 152 | } |
| 153 | } |
| 154 | keytag += keytag >> 16 & 0xFFFF |
| 155 | keytag &= 0xFFFF |
| 156 | |
| 157 | return uint16(keytag) |
| 158 | } |
| 159 | |
| 160 | // ToDS converts a DNSKEY record to a DS record. |
| 161 | func (k *DNSKEY) ToDS(h uint8) *DS { |
| 162 | if k == nil { |
| 163 | return nil |
| 164 | } |
| 165 | ds := new(DS) |
| 166 | ds.Hdr.Name = k.Hdr.Name |
| 167 | ds.Hdr.Class = k.Hdr.Class |
| 168 | ds.Hdr.Rrtype = TypeDS |
| 169 | ds.Hdr.Ttl = k.Hdr.Ttl |
| 170 | ds.Algorithm = k.Algorithm |
| 171 | ds.DigestType = h |
| 172 | ds.KeyTag = k.KeyTag() |
| 173 | |
| 174 | keywire := new(dnskeyWireFmt) |
| 175 | keywire.Flags = k.Flags |
| 176 | keywire.Protocol = k.Protocol |
| 177 | keywire.Algorithm = k.Algorithm |
| 178 | keywire.PublicKey = k.PublicKey |
| 179 | wire := make([]byte, DefaultMsgSize) |
| 180 | n, err := packKeyWire(keywire, wire) |
| 181 | if err != nil { |
| 182 | return nil |
| 183 | } |
| 184 | wire = wire[:n] |
| 185 | |
| 186 | owner := make([]byte, 255) |
| 187 | off, err1 := PackDomainName(CanonicalName(k.Hdr.Name), owner, 0, nil, false) |
| 188 | if err1 != nil { |
| 189 | return nil |
| 190 | } |
| 191 | owner = owner[:off] |
| 192 | // RFC4034: |
| 193 | // digest = digest_algorithm( DNSKEY owner name | DNSKEY RDATA); |
| 194 | // "|" denotes concatenation |
| 195 | // DNSKEY RDATA = Flags | Protocol | Algorithm | Public Key. |
| 196 | |
| 197 | var hash crypto.Hash |
| 198 | switch h { |
| 199 | case SHA1: |
| 200 | hash = crypto.SHA1 |
| 201 | case SHA256: |
| 202 | hash = crypto.SHA256 |
| 203 | case SHA384: |
| 204 | hash = crypto.SHA384 |
| 205 | case SHA512: |
| 206 | hash = crypto.SHA512 |
| 207 | default: |
| 208 | return nil |
| 209 | } |
| 210 | |
| 211 | s := hash.New() |
| 212 | s.Write(owner) |
| 213 | s.Write(wire) |
| 214 | ds.Digest = hex.EncodeToString(s.Sum(nil)) |
| 215 | return ds |
| 216 | } |
| 217 | |
| 218 | // ToCDNSKEY converts a DNSKEY record to a CDNSKEY record. |
| 219 | func (k *DNSKEY) ToCDNSKEY() *CDNSKEY { |
| 220 | c := &CDNSKEY{DNSKEY: *k} |
| 221 | c.Hdr = k.Hdr |
| 222 | c.Hdr.Rrtype = TypeCDNSKEY |
| 223 | return c |
| 224 | } |
| 225 | |
| 226 | // ToCDS converts a DS record to a CDS record. |
| 227 | func (d *DS) ToCDS() *CDS { |
| 228 | c := &CDS{DS: *d} |
| 229 | c.Hdr = d.Hdr |
| 230 | c.Hdr.Rrtype = TypeCDS |
| 231 | return c |
| 232 | } |
| 233 | |
| 234 | // Sign signs an RRSet. The signature needs to be filled in with the values: |
| 235 | // Inception, Expiration, KeyTag, SignerName and Algorithm. The rest is copied |
| 236 | // from the RRset. Sign returns a non-nill error when the signing went OK. |
| 237 | // There is no check if RRSet is a proper (RFC 2181) RRSet. If OrigTTL is non |
| 238 | // zero, it is used as-is, otherwise the TTL of the RRset is used as the |
| 239 | // OrigTTL. |
| 240 | func (rr *RRSIG) Sign(k crypto.Signer, rrset []RR) error { |
| 241 | h0 := rrset[0].Header() |
| 242 | rr.Hdr.Rrtype = TypeRRSIG |
| 243 | rr.Hdr.Name = h0.Name |
| 244 | rr.Hdr.Class = h0.Class |
| 245 | if rr.OrigTtl == 0 { // If set don't override |
| 246 | rr.OrigTtl = h0.Ttl |
| 247 | } |
| 248 | rr.TypeCovered = h0.Rrtype |
| 249 | rr.Labels = uint8(CountLabel(h0.Name)) |
| 250 | |
| 251 | if strings.HasPrefix(h0.Name, "*") { |
| 252 | rr.Labels-- // wildcard, remove from label count |
| 253 | } |
| 254 | |
| 255 | return rr.signAsIs(k, rrset) |
| 256 | } |
| 257 | |
| 258 | func (rr *RRSIG) signAsIs(k crypto.Signer, rrset []RR) error { |
| 259 | if k == nil { |
| 260 | return ErrPrivKey |
| 261 | } |
| 262 | // s.Inception and s.Expiration may be 0 (rollover etc.), the rest must be set |
| 263 | if rr.KeyTag == 0 || len(rr.SignerName) == 0 || rr.Algorithm == 0 { |
| 264 | return ErrKey |
| 265 | } |
| 266 | |
| 267 | sigwire := new(rrsigWireFmt) |
| 268 | sigwire.TypeCovered = rr.TypeCovered |
| 269 | sigwire.Algorithm = rr.Algorithm |
| 270 | sigwire.Labels = rr.Labels |
| 271 | sigwire.OrigTtl = rr.OrigTtl |
| 272 | sigwire.Expiration = rr.Expiration |
| 273 | sigwire.Inception = rr.Inception |
| 274 | sigwire.KeyTag = rr.KeyTag |
| 275 | // For signing, lowercase this name |
| 276 | sigwire.SignerName = CanonicalName(rr.SignerName) |
| 277 | |
| 278 | // Create the desired binary blob |
| 279 | signdata := make([]byte, DefaultMsgSize) |
| 280 | n, err := packSigWire(sigwire, signdata) |
| 281 | if err != nil { |
| 282 | return err |
| 283 | } |
| 284 | signdata = signdata[:n] |
| 285 | wire, err := rawSignatureData(rrset, rr) |
| 286 | if err != nil { |
| 287 | return err |
| 288 | } |
| 289 | |
| 290 | h, cryptohash, err := hashFromAlgorithm(rr.Algorithm) |
| 291 | if err != nil { |
| 292 | return err |
| 293 | } |
| 294 | |
| 295 | switch rr.Algorithm { |
| 296 | case RSAMD5, DSA, DSANSEC3SHA1: |
| 297 | // See RFC 6944. |
| 298 | return ErrAlg |
| 299 | default: |
| 300 | h.Write(signdata) |
| 301 | h.Write(wire) |
| 302 | |
| 303 | signature, err := sign(k, h.Sum(nil), cryptohash, rr.Algorithm) |
| 304 | if err != nil { |
| 305 | return err |
| 306 | } |
| 307 | |
| 308 | rr.Signature = toBase64(signature) |
| 309 | return nil |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | func sign(k crypto.Signer, hashed []byte, hash crypto.Hash, alg uint8) ([]byte, error) { |
| 314 | signature, err := k.Sign(rand.Reader, hashed, hash) |
| 315 | if err != nil { |
| 316 | return nil, err |
| 317 | } |
| 318 | |
| 319 | switch alg { |
| 320 | case RSASHA1, RSASHA1NSEC3SHA1, RSASHA256, RSASHA512, ED25519: |
| 321 | return signature, nil |
| 322 | case ECDSAP256SHA256, ECDSAP384SHA384: |
| 323 | ecdsaSignature := &struct { |
| 324 | R, S *big.Int |
| 325 | }{} |
| 326 | if _, err := asn1.Unmarshal(signature, ecdsaSignature); err != nil { |
| 327 | return nil, err |
| 328 | } |
| 329 | |
| 330 | var intlen int |
| 331 | switch alg { |
| 332 | case ECDSAP256SHA256: |
| 333 | intlen = 32 |
| 334 | case ECDSAP384SHA384: |
| 335 | intlen = 48 |
| 336 | } |
| 337 | |
| 338 | signature := intToBytes(ecdsaSignature.R, intlen) |
| 339 | signature = append(signature, intToBytes(ecdsaSignature.S, intlen)...) |
| 340 | return signature, nil |
| 341 | default: |
| 342 | return nil, ErrAlg |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // Verify validates an RRSet with the signature and key. This is only the |
| 347 | // cryptographic test, the signature validity period must be checked separately. |
| 348 | // This function copies the rdata of some RRs (to lowercase domain names) for the validation to work. |
| 349 | // It also checks that the Zone Key bit (RFC 4034 2.1.1) is set on the DNSKEY |
| 350 | // and that the Protocol field is set to 3 (RFC 4034 2.1.2). |
| 351 | func (rr *RRSIG) Verify(k *DNSKEY, rrset []RR) error { |
| 352 | // First the easy checks |
| 353 | if !IsRRset(rrset) { |
| 354 | return ErrRRset |
| 355 | } |
| 356 | if rr.KeyTag != k.KeyTag() { |
| 357 | return ErrKey |
| 358 | } |
| 359 | if rr.Hdr.Class != k.Hdr.Class { |
| 360 | return ErrKey |
| 361 | } |
| 362 | if rr.Algorithm != k.Algorithm { |
| 363 | return ErrKey |
| 364 | } |
| 365 | |
| 366 | signerName := CanonicalName(rr.SignerName) |
| 367 | if !equal(signerName, k.Hdr.Name) { |
| 368 | return ErrKey |
| 369 | } |
| 370 | |
| 371 | if k.Protocol != 3 { |
| 372 | return ErrKey |
| 373 | } |
| 374 | // RFC 4034 2.1.1 If bit 7 has value 0, then the DNSKEY record holds some |
| 375 | // other type of DNS public key and MUST NOT be used to verify RRSIGs that |
| 376 | // cover RRsets. |
| 377 | if k.Flags&ZONE == 0 { |
| 378 | return ErrKey |
| 379 | } |
| 380 | |
| 381 | // IsRRset checked that we have at least one RR and that the RRs in |
| 382 | // the set have consistent type, class, and name. Also check that type, |
| 383 | // class and name matches the RRSIG record. |
| 384 | // Also checks RFC 4035 5.3.1 the number of labels in the RRset owner |
| 385 | // name MUST be greater than or equal to the value in the RRSIG RR's Labels field. |
| 386 | // RFC 4035 5.3.1 Signer's Name MUST be the name of the zone that [contains the RRset]. |
| 387 | // Since we don't have SOA info, checking suffix may be the best we can do...? |
| 388 | if h0 := rrset[0].Header(); h0.Class != rr.Hdr.Class || |
| 389 | h0.Rrtype != rr.TypeCovered || |
| 390 | uint8(CountLabel(h0.Name)) < rr.Labels || |
| 391 | !equal(h0.Name, rr.Hdr.Name) || |
| 392 | !strings.HasSuffix(CanonicalName(h0.Name), signerName) { |
| 393 | |
| 394 | return ErrRRset |
| 395 | } |
| 396 | |
| 397 | // RFC 4035 5.3.2. Reconstructing the Signed Data |
| 398 | // Copy the sig, except the rrsig data |
| 399 | sigwire := new(rrsigWireFmt) |
| 400 | sigwire.TypeCovered = rr.TypeCovered |
| 401 | sigwire.Algorithm = rr.Algorithm |
| 402 | sigwire.Labels = rr.Labels |
| 403 | sigwire.OrigTtl = rr.OrigTtl |
| 404 | sigwire.Expiration = rr.Expiration |
| 405 | sigwire.Inception = rr.Inception |
| 406 | sigwire.KeyTag = rr.KeyTag |
| 407 | sigwire.SignerName = signerName |
| 408 | // Create the desired binary blob |
| 409 | signeddata := make([]byte, DefaultMsgSize) |
| 410 | n, err := packSigWire(sigwire, signeddata) |
| 411 | if err != nil { |
| 412 | return err |
| 413 | } |
| 414 | signeddata = signeddata[:n] |
| 415 | wire, err := rawSignatureData(rrset, rr) |
| 416 | if err != nil { |
| 417 | return err |
| 418 | } |
| 419 | |
| 420 | sigbuf := rr.sigBuf() // Get the binary signature data |
| 421 | // TODO(miek) |
| 422 | // remove the domain name and assume its ours? |
| 423 | // if rr.Algorithm == PRIVATEDNS { // PRIVATEOID |
| 424 | // } |
| 425 | |
| 426 | h, cryptohash, err := hashFromAlgorithm(rr.Algorithm) |
| 427 | if err != nil { |
| 428 | return err |
| 429 | } |
| 430 | |
| 431 | switch rr.Algorithm { |
| 432 | case RSASHA1, RSASHA1NSEC3SHA1, RSASHA256, RSASHA512: |
| 433 | // TODO(mg): this can be done quicker, ie. cache the pubkey data somewhere?? |
| 434 | pubkey := k.publicKeyRSA() // Get the key |
| 435 | if pubkey == nil { |
| 436 | return ErrKey |
| 437 | } |
| 438 | |
| 439 | h.Write(signeddata) |
| 440 | h.Write(wire) |
| 441 | return rsa.VerifyPKCS1v15(pubkey, cryptohash, h.Sum(nil), sigbuf) |
| 442 | |
| 443 | case ECDSAP256SHA256, ECDSAP384SHA384: |
| 444 | pubkey := k.publicKeyECDSA() |
| 445 | if pubkey == nil { |
| 446 | return ErrKey |
| 447 | } |
| 448 | |
| 449 | // Split sigbuf into the r and s coordinates |
| 450 | r := new(big.Int).SetBytes(sigbuf[:len(sigbuf)/2]) |
| 451 | s := new(big.Int).SetBytes(sigbuf[len(sigbuf)/2:]) |
| 452 | |
| 453 | h.Write(signeddata) |
| 454 | h.Write(wire) |
| 455 | if ecdsa.Verify(pubkey, h.Sum(nil), r, s) { |
| 456 | return nil |
| 457 | } |
| 458 | return ErrSig |
| 459 | |
| 460 | case ED25519: |
| 461 | pubkey := k.publicKeyED25519() |
| 462 | if pubkey == nil { |
| 463 | return ErrKey |
| 464 | } |
| 465 | |
| 466 | if ed25519.Verify(pubkey, append(signeddata, wire...), sigbuf) { |
| 467 | return nil |
| 468 | } |
| 469 | return ErrSig |
| 470 | |
| 471 | default: |
| 472 | return ErrAlg |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | // ValidityPeriod uses RFC1982 serial arithmetic to calculate |
| 477 | // if a signature period is valid. If t is the zero time, the |
| 478 | // current time is taken other t is. Returns true if the signature |
| 479 | // is valid at the given time, otherwise returns false. |
| 480 | func (rr *RRSIG) ValidityPeriod(t time.Time) bool { |
| 481 | var utc int64 |
| 482 | if t.IsZero() { |
| 483 | utc = time.Now().UTC().Unix() |
| 484 | } else { |
| 485 | utc = t.UTC().Unix() |
| 486 | } |
| 487 | modi := (int64(rr.Inception) - utc) / year68 |
| 488 | mode := (int64(rr.Expiration) - utc) / year68 |
| 489 | ti := int64(rr.Inception) + modi*year68 |
| 490 | te := int64(rr.Expiration) + mode*year68 |
| 491 | return ti <= utc && utc <= te |
| 492 | } |
| 493 | |
| 494 | // Return the signatures base64 encoding sigdata as a byte slice. |
| 495 | func (rr *RRSIG) sigBuf() []byte { |
| 496 | sigbuf, err := fromBase64([]byte(rr.Signature)) |
| 497 | if err != nil { |
| 498 | return nil |
| 499 | } |
| 500 | return sigbuf |
| 501 | } |
| 502 | |
| 503 | // publicKeyRSA returns the RSA public key from a DNSKEY record. |
| 504 | func (k *DNSKEY) publicKeyRSA() *rsa.PublicKey { |
| 505 | keybuf, err := fromBase64([]byte(k.PublicKey)) |
| 506 | if err != nil { |
| 507 | return nil |
| 508 | } |
| 509 | |
| 510 | if len(keybuf) < 1+1+64 { |
| 511 | // Exponent must be at least 1 byte and modulus at least 64 |
| 512 | return nil |
| 513 | } |
| 514 | |
| 515 | // RFC 2537/3110, section 2. RSA Public KEY Resource Records |
| 516 | // Length is in the 0th byte, unless its zero, then it |
| 517 | // it in bytes 1 and 2 and its a 16 bit number |
| 518 | explen := uint16(keybuf[0]) |
| 519 | keyoff := 1 |
| 520 | if explen == 0 { |
| 521 | explen = uint16(keybuf[1])<<8 | uint16(keybuf[2]) |
| 522 | keyoff = 3 |
| 523 | } |
| 524 | |
| 525 | if explen > 4 || explen == 0 || keybuf[keyoff] == 0 { |
| 526 | // Exponent larger than supported by the crypto package, |
| 527 | // empty, or contains prohibited leading zero. |
| 528 | return nil |
| 529 | } |
| 530 | |
| 531 | modoff := keyoff + int(explen) |
| 532 | modlen := len(keybuf) - modoff |
| 533 | if modlen < 64 || modlen > 512 || keybuf[modoff] == 0 { |
| 534 | // Modulus is too small, large, or contains prohibited leading zero. |
| 535 | return nil |
| 536 | } |
| 537 | |
| 538 | pubkey := new(rsa.PublicKey) |
| 539 | |
| 540 | var expo uint64 |
| 541 | // The exponent of length explen is between keyoff and modoff. |
| 542 | for _, v := range keybuf[keyoff:modoff] { |
| 543 | expo <<= 8 |
| 544 | expo |= uint64(v) |
| 545 | } |
| 546 | if expo > 1<<31-1 { |
| 547 | // Larger exponent than supported by the crypto package. |
| 548 | return nil |
| 549 | } |
| 550 | |
| 551 | pubkey.E = int(expo) |
| 552 | pubkey.N = new(big.Int).SetBytes(keybuf[modoff:]) |
| 553 | return pubkey |
| 554 | } |
| 555 | |
| 556 | // publicKeyECDSA returns the Curve public key from the DNSKEY record. |
| 557 | func (k *DNSKEY) publicKeyECDSA() *ecdsa.PublicKey { |
| 558 | keybuf, err := fromBase64([]byte(k.PublicKey)) |
| 559 | if err != nil { |
| 560 | return nil |
| 561 | } |
| 562 | pubkey := new(ecdsa.PublicKey) |
| 563 | switch k.Algorithm { |
| 564 | case ECDSAP256SHA256: |
| 565 | pubkey.Curve = elliptic.P256() |
| 566 | if len(keybuf) != 64 { |
| 567 | // wrongly encoded key |
| 568 | return nil |
| 569 | } |
| 570 | case ECDSAP384SHA384: |
| 571 | pubkey.Curve = elliptic.P384() |
| 572 | if len(keybuf) != 96 { |
| 573 | // Wrongly encoded key |
| 574 | return nil |
| 575 | } |
| 576 | } |
| 577 | pubkey.X = new(big.Int).SetBytes(keybuf[:len(keybuf)/2]) |
| 578 | pubkey.Y = new(big.Int).SetBytes(keybuf[len(keybuf)/2:]) |
| 579 | return pubkey |
| 580 | } |
| 581 | |
| 582 | func (k *DNSKEY) publicKeyED25519() ed25519.PublicKey { |
| 583 | keybuf, err := fromBase64([]byte(k.PublicKey)) |
| 584 | if err != nil { |
| 585 | return nil |
| 586 | } |
| 587 | if len(keybuf) != ed25519.PublicKeySize { |
| 588 | return nil |
| 589 | } |
| 590 | return keybuf |
| 591 | } |
| 592 | |
| 593 | type wireSlice [][]byte |
| 594 | |
| 595 | func (p wireSlice) Len() int { return len(p) } |
| 596 | func (p wireSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 597 | func (p wireSlice) Less(i, j int) bool { |
| 598 | _, ioff, _ := UnpackDomainName(p[i], 0) |
| 599 | _, joff, _ := UnpackDomainName(p[j], 0) |
| 600 | return bytes.Compare(p[i][ioff+10:], p[j][joff+10:]) < 0 |
| 601 | } |
| 602 | |
| 603 | // Return the raw signature data. |
| 604 | func rawSignatureData(rrset []RR, s *RRSIG) (buf []byte, err error) { |
| 605 | wires := make(wireSlice, len(rrset)) |
| 606 | for i, r := range rrset { |
| 607 | r1 := r.copy() |
| 608 | h := r1.Header() |
| 609 | h.Ttl = s.OrigTtl |
| 610 | labels := SplitDomainName(h.Name) |
| 611 | // 6.2. Canonical RR Form. (4) - wildcards |
| 612 | if len(labels) > int(s.Labels) { |
| 613 | // Wildcard |
| 614 | h.Name = "*." + strings.Join(labels[len(labels)-int(s.Labels):], ".") + "." |
| 615 | } |
| 616 | // RFC 4034: 6.2. Canonical RR Form. (2) - domain name to lowercase |
| 617 | h.Name = CanonicalName(h.Name) |
| 618 | // 6.2. Canonical RR Form. (3) - domain rdata to lowercase. |
| 619 | // NS, MD, MF, CNAME, SOA, MB, MG, MR, PTR, |
| 620 | // HINFO, MINFO, MX, RP, AFSDB, RT, SIG, PX, NXT, NAPTR, KX, |
| 621 | // SRV, DNAME, A6 |
| 622 | // |
| 623 | // RFC 6840 - Clarifications and Implementation Notes for DNS Security (DNSSEC): |
| 624 | // Section 6.2 of [RFC4034] also erroneously lists HINFO as a record |
| 625 | // that needs conversion to lowercase, and twice at that. Since HINFO |
| 626 | // records contain no domain names, they are not subject to case |
| 627 | // conversion. |
| 628 | switch x := r1.(type) { |
| 629 | case *NS: |
| 630 | x.Ns = CanonicalName(x.Ns) |
| 631 | case *MD: |
| 632 | x.Md = CanonicalName(x.Md) |
| 633 | case *MF: |
| 634 | x.Mf = CanonicalName(x.Mf) |
| 635 | case *CNAME: |
| 636 | x.Target = CanonicalName(x.Target) |
| 637 | case *SOA: |
| 638 | x.Ns = CanonicalName(x.Ns) |
| 639 | x.Mbox = CanonicalName(x.Mbox) |
| 640 | case *MB: |
| 641 | x.Mb = CanonicalName(x.Mb) |
| 642 | case *MG: |
| 643 | x.Mg = CanonicalName(x.Mg) |
| 644 | case *MR: |
| 645 | x.Mr = CanonicalName(x.Mr) |
| 646 | case *PTR: |
| 647 | x.Ptr = CanonicalName(x.Ptr) |
| 648 | case *MINFO: |
| 649 | x.Rmail = CanonicalName(x.Rmail) |
| 650 | x.Email = CanonicalName(x.Email) |
| 651 | case *MX: |
| 652 | x.Mx = CanonicalName(x.Mx) |
| 653 | case *RP: |
| 654 | x.Mbox = CanonicalName(x.Mbox) |
| 655 | x.Txt = CanonicalName(x.Txt) |
| 656 | case *AFSDB: |
| 657 | x.Hostname = CanonicalName(x.Hostname) |
| 658 | case *RT: |
| 659 | x.Host = CanonicalName(x.Host) |
| 660 | case *SIG: |
| 661 | x.SignerName = CanonicalName(x.SignerName) |
| 662 | case *PX: |
| 663 | x.Map822 = CanonicalName(x.Map822) |
| 664 | x.Mapx400 = CanonicalName(x.Mapx400) |
| 665 | case *NAPTR: |
| 666 | x.Replacement = CanonicalName(x.Replacement) |
| 667 | case *KX: |
| 668 | x.Exchanger = CanonicalName(x.Exchanger) |
| 669 | case *SRV: |
| 670 | x.Target = CanonicalName(x.Target) |
| 671 | case *DNAME: |
| 672 | x.Target = CanonicalName(x.Target) |
| 673 | } |
| 674 | // 6.2. Canonical RR Form. (5) - origTTL |
| 675 | wire := make([]byte, Len(r1)+1) // +1 to be safe(r) |
| 676 | off, err1 := PackRR(r1, wire, 0, nil, false) |
| 677 | if err1 != nil { |
| 678 | return nil, err1 |
| 679 | } |
| 680 | wire = wire[:off] |
| 681 | wires[i] = wire |
| 682 | } |
| 683 | sort.Sort(wires) |
| 684 | for i, wire := range wires { |
| 685 | if i > 0 && bytes.Equal(wire, wires[i-1]) { |
| 686 | continue |
| 687 | } |
| 688 | buf = append(buf, wire...) |
| 689 | } |
| 690 | return buf, nil |
| 691 | } |
| 692 | |
| 693 | func packSigWire(sw *rrsigWireFmt, msg []byte) (int, error) { |
| 694 | // copied from zmsg.go RRSIG packing |
| 695 | off, err := packUint16(sw.TypeCovered, msg, 0) |
| 696 | if err != nil { |
| 697 | return off, err |
| 698 | } |
| 699 | off, err = packUint8(sw.Algorithm, msg, off) |
| 700 | if err != nil { |
| 701 | return off, err |
| 702 | } |
| 703 | off, err = packUint8(sw.Labels, msg, off) |
| 704 | if err != nil { |
| 705 | return off, err |
| 706 | } |
| 707 | off, err = packUint32(sw.OrigTtl, msg, off) |
| 708 | if err != nil { |
| 709 | return off, err |
| 710 | } |
| 711 | off, err = packUint32(sw.Expiration, msg, off) |
| 712 | if err != nil { |
| 713 | return off, err |
| 714 | } |
| 715 | off, err = packUint32(sw.Inception, msg, off) |
| 716 | if err != nil { |
| 717 | return off, err |
| 718 | } |
| 719 | off, err = packUint16(sw.KeyTag, msg, off) |
| 720 | if err != nil { |
| 721 | return off, err |
| 722 | } |
| 723 | off, err = PackDomainName(sw.SignerName, msg, off, nil, false) |
| 724 | if err != nil { |
| 725 | return off, err |
| 726 | } |
| 727 | return off, nil |
| 728 | } |
| 729 | |
| 730 | func packKeyWire(dw *dnskeyWireFmt, msg []byte) (int, error) { |
| 731 | // copied from zmsg.go DNSKEY packing |
| 732 | off, err := packUint16(dw.Flags, msg, 0) |
| 733 | if err != nil { |
| 734 | return off, err |
| 735 | } |
| 736 | off, err = packUint8(dw.Protocol, msg, off) |
| 737 | if err != nil { |
| 738 | return off, err |
| 739 | } |
| 740 | off, err = packUint8(dw.Algorithm, msg, off) |
| 741 | if err != nil { |
| 742 | return off, err |
| 743 | } |
| 744 | off, err = packStringBase64(dw.PublicKey, msg, off) |
| 745 | if err != nil { |
| 746 | return off, err |
| 747 | } |
| 748 | return off, nil |
| 749 | } |