OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_mac_signature.h" | 5 #include "net/http/http_mac_signature.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 return false; | 88 return false; |
89 } | 89 } |
90 | 90 |
91 method_ = StringToUpperASCII(method); | 91 method_ = StringToUpperASCII(method); |
92 request_uri_ = request_uri; | 92 request_uri_ = request_uri; |
93 host_ = StringToLowerASCII(host); | 93 host_ = StringToLowerASCII(host); |
94 port_ = base::IntToString(port); | 94 port_ = base::IntToString(port); |
95 return true; | 95 return true; |
96 } | 96 } |
97 | 97 |
98 std::string HttpMacSignature::GenerateAuthorizationHeader() { | 98 bool HttpMacSignature::GenerateAuthorizationHeader(std::string* header) { |
99 DCHECK(!id_.empty()) << "Call AddStateInfo first."; | 99 DCHECK(!id_.empty()) << "Call AddStateInfo first."; |
100 DCHECK(!method_.empty()) << "Call AddHttpInfo first."; | 100 DCHECK(!method_.empty()) << "Call AddHttpInfo first."; |
101 | 101 |
102 if (id_.empty() || method_.empty()) | |
Denis Lagno
2011/07/28 08:13:37
nit: imho it is better to merge DCHECK(condition)
| |
103 return false; | |
104 | |
102 std::string age = base::Int64ToString( | 105 std::string age = base::Int64ToString( |
103 (base::Time::Now() - creation_date_).InSeconds()); | 106 (base::Time::Now() - creation_date_).InSeconds()); |
104 std::string nonce = GenerateNonce(); | 107 std::string nonce = GenerateNonce(); |
105 | 108 |
106 return GenerateHeaderString(age, nonce); | 109 return GenerateHeaderString(age, nonce, header); |
107 } | 110 } |
108 | 111 |
109 std::string HttpMacSignature::GenerateHeaderString(const std::string& age, | 112 bool HttpMacSignature::GenerateHeaderString(const std::string& age, |
110 const std::string& nonce) { | 113 const std::string& nonce, |
111 std::string mac = GenerateMAC(age, nonce); | 114 std::string* header) { |
115 std::string mac; | |
116 if (!GenerateMAC(age, nonce, &mac)) | |
117 return false; | |
112 | 118 |
113 DCHECK(IsPlainString(age)); | 119 DCHECK(IsPlainString(age)); |
114 DCHECK(IsPlainString(nonce)); | 120 DCHECK(IsPlainString(nonce)); |
115 DCHECK(IsPlainString(mac)); | 121 DCHECK(IsPlainString(mac)); |
116 | 122 |
117 return "MAC id=\"" + id_ + | 123 *header = "MAC id=\"" + id_ + |
118 "\", nonce=\"" + age + ":" + nonce + | 124 "\", nonce=\"" + age + ":" + nonce + |
119 "\", mac=\"" + mac + "\""; | 125 "\", mac=\"" + mac + "\""; |
126 return true; | |
120 } | 127 } |
121 | 128 |
122 std::string HttpMacSignature::GenerateNormalizedRequest( | 129 std::string HttpMacSignature::GenerateNormalizedRequest( |
123 const std::string& age, | 130 const std::string& age, |
124 const std::string& nonce) { | 131 const std::string& nonce) { |
125 static const std::string kNewLine = "\n"; | 132 static const std::string kNewLine = "\n"; |
126 | 133 |
127 std::string normalized_request = age + ":" + nonce + kNewLine; | 134 std::string normalized_request = age + ":" + nonce + kNewLine; |
128 normalized_request += method_ + kNewLine; | 135 normalized_request += method_ + kNewLine; |
129 normalized_request += request_uri_ + kNewLine; | 136 normalized_request += request_uri_ + kNewLine; |
130 normalized_request += host_ + kNewLine; | 137 normalized_request += host_ + kNewLine; |
131 normalized_request += port_ + kNewLine; | 138 normalized_request += port_ + kNewLine; |
132 normalized_request += kNewLine; | 139 normalized_request += kNewLine; |
133 normalized_request += kNewLine; | 140 normalized_request += kNewLine; |
134 | 141 |
135 return normalized_request; | 142 return normalized_request; |
136 } | 143 } |
137 | 144 |
138 std::string HttpMacSignature::GenerateMAC(const std::string& age, | 145 bool HttpMacSignature::GenerateMAC(const std::string& age, |
139 const std::string& nonce) { | 146 const std::string& nonce, |
147 std::string* mac) { | |
140 std::string request = GenerateNormalizedRequest(age, nonce); | 148 std::string request = GenerateNormalizedRequest(age, nonce); |
141 | 149 |
142 crypto::HMAC hmac(mac_algorithm_); | 150 crypto::HMAC hmac(mac_algorithm_); |
143 hmac.Init(mac_key_); | 151 if (!hmac.Init(mac_key_)) { |
152 NOTREACHED(); | |
153 return false; | |
154 } | |
144 | 155 |
145 std::string signature; | 156 std::string signature; |
146 size_t length = hmac.DigestLength(); | 157 size_t length = hmac.DigestLength(); |
147 char* buffer = WriteInto(&signature, length); | 158 char* buffer = WriteInto(&signature, length); |
148 bool result = hmac.Sign(request, | 159 if (!hmac.Sign(request, reinterpret_cast<unsigned char*>(buffer), |
149 reinterpret_cast<unsigned char*>(buffer), | 160 length)) { |
150 length); | 161 NOTREACHED(); |
151 DCHECK(result); | 162 return false; |
163 } | |
152 | 164 |
153 std::string encoded_signature; | 165 std::string encoded_signature; |
154 result = base::Base64Encode(signature, &encoded_signature); | 166 if (!base::Base64Encode(signature, &encoded_signature)) { |
155 DCHECK(result); | 167 NOTREACHED(); |
156 return encoded_signature; | 168 return false; |
169 } | |
170 | |
171 mac->swap(encoded_signature); | |
172 return true; | |
157 } | 173 } |
158 | 174 |
159 } // namespace net | 175 } // namespace net |
OLD | NEW |