Branch data Line data Source code
1 : : // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
2 : : //
3 : : // Permission to use, copy, modify, and/or distribute this software for any
4 : : // purpose with or without fee is hereby granted, provided that the above
5 : : // copyright notice and this permission notice appear in all copies.
6 : : //
7 : : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
8 : : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9 : : // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
10 : : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11 : : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
12 : : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13 : : // PERFORMANCE OF THIS SOFTWARE.
14 : :
15 : : #include <exceptions/exceptions.h>
16 : :
17 : : #include <util/encode/hex.h>
18 : : #include <util/buffer.h>
19 : :
20 : : #include <dns/exceptions.h>
21 : : #include <dns/rdata.h>
22 : : #include <dns/rdata/generic/detail/nsec3param_common.h>
23 : :
24 : : #include <boost/lexical_cast.hpp>
25 : :
26 : : #include <sstream>
27 : : #include <vector>
28 : : #include <stdint.h>
29 : :
30 : : using namespace std;
31 : : using namespace isc::util;
32 : : using namespace isc::util::encode;
33 : :
34 : : namespace isc {
35 : : namespace dns {
36 : : namespace rdata {
37 : : namespace generic {
38 : : namespace detail {
39 : : namespace nsec3 {
40 : :
41 : : ParseNSEC3ParamResult
42 : 829 : parseNSEC3ParamText(const char* const rrtype_name,
43 : : const string& rdata_str, istringstream& iss,
44 : : vector<uint8_t>& salt)
45 : : {
46 : : unsigned int hashalg, flags, iterations;
47 : 829 : string iterations_str, salthex;
48 : :
49 [ + - ][ + - ]: 829 : iss >> hashalg >> flags >> iterations_str >> salthex;
50 [ + - ][ + + ]: 829 : if (iss.bad() || iss.fail()) {
[ + + ]
51 [ + - ][ + - ]: 2 : isc_throw(InvalidRdataText, "Invalid " << rrtype_name <<
[ + - ][ + - ]
[ + - ]
52 : : " text: " << rdata_str);
53 : : }
54 [ + + ]: 828 : if (hashalg > 0xff) {
55 [ + - ][ + - ]: 6 : isc_throw(InvalidRdataText, rrtype_name <<
[ + - ]
56 : : " hash algorithm out of range: " << hashalg);
57 : : }
58 [ + + ]: 826 : if (flags > 0xff) {
59 [ + - ][ + - ]: 6 : isc_throw(InvalidRdataText, rrtype_name << " flags out of range: " <<
[ + - ]
60 : : flags);
61 : : }
62 : : // Convert iteration. To reject an invalid case where there's no space
63 : : // between iteration and salt, we extract this field as string and convert
64 : : // to integer.
65 : : try {
66 : 823 : iterations = boost::lexical_cast<unsigned int>(iterations_str);
67 [ - + ]: 2 : } catch (const boost::bad_lexical_cast&) {
68 [ - + ][ - + ]: 2 : isc_throw(InvalidRdataText, "Bad " << rrtype_name <<
[ - + ][ - + ]
[ - + ]
69 : : " iteration: " << iterations_str);
70 : : }
71 [ + + ]: 823 : if (iterations > 0xffff) {
72 [ + - ][ + - ]: 9 : isc_throw(InvalidRdataText, rrtype_name <<
[ + - ]
73 : : " iterations out of range: " <<
74 : : iterations);
75 : : }
76 : :
77 : : // Salt is up to 255 bytes, and space is not allowed in the HEX encoding,
78 : : // so the encoded string cannot be longer than the double of max length
79 : : // of the actual salt.
80 [ + + ]: 820 : if (salthex.size() > 255 * 2) {
81 [ + - ][ + - ]: 6 : isc_throw(InvalidRdataText, rrtype_name << " salt is too long: "
[ + - ][ + - ]
82 : : << salthex.size() << " (encoded) bytes");
83 : : }
84 [ + + ]: 818 : if (salthex != "-") { // "-" means a 0-length salt
85 [ + + ]: 799 : decodeHex(salthex, salt);
86 : : }
87 : :
88 : 812 : return (ParseNSEC3ParamResult(hashalg, flags, iterations));
89 : : }
90 : :
91 : : ParseNSEC3ParamResult
92 : 78 : parseNSEC3ParamWire(const char* const rrtype_name,
93 : : InputBuffer& buffer,
94 : : size_t& rdata_len, std::vector<uint8_t>& salt)
95 : : {
96 : : // NSEC3/NSEC3PARAM RR must have at least 5 octets:
97 : : // hash algorithm(1), flags(1), iteration(2), saltlen(1)
98 [ + + ]: 78 : if (rdata_len < 5) {
99 [ + - ][ + - ]: 6 : isc_throw(DNSMessageFORMERR, rrtype_name << " too short, length: "
100 : : << rdata_len);
101 : : }
102 : :
103 : 76 : const uint8_t hashalg = buffer.readUint8();
104 : 74 : const uint8_t flags = buffer.readUint8();
105 : 68 : const uint16_t iterations = buffer.readUint16();
106 : :
107 : 68 : const uint8_t saltlen = buffer.readUint8();
108 : 66 : rdata_len -= 5;
109 [ + + ]: 66 : if (rdata_len < saltlen) {
110 [ + - ][ + - ]: 6 : isc_throw(DNSMessageFORMERR, rrtype_name <<
111 : : " salt length is too large: " <<
112 : : static_cast<unsigned int>(saltlen));
113 : : }
114 : :
115 : 64 : salt.resize(saltlen);
116 [ + + ]: 64 : if (saltlen > 0) {
117 : 116 : buffer.readData(&salt[0], saltlen);
118 : 50 : rdata_len -= saltlen;
119 : : }
120 : :
121 : 56 : return (ParseNSEC3ParamResult(hashalg, flags, iterations));
122 : : }
123 : :
124 : : } // end of nsec3
125 : : } // end of detail
126 : : } // end of generic
127 : : } // end of rdata
128 : : } // end of dns
129 [ # # ]: 0 : } // end of isc
130 : :
|