| 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 if (id_.empty()) { |
| 100 DCHECK(!method_.empty()) << "Call AddHttpInfo first."; | 100 NOTREACHED() << "Call AddStateInfo first."; |
| 101 return false; |
| 102 } |
| 103 if (method_.empty()) { |
| 104 NOTREACHED() << "Call AddHttpInfo first."; |
| 105 return false; |
| 106 } |
| 101 | 107 |
| 102 std::string age = base::Int64ToString( | 108 std::string age = base::Int64ToString( |
| 103 (base::Time::Now() - creation_date_).InSeconds()); | 109 (base::Time::Now() - creation_date_).InSeconds()); |
| 104 std::string nonce = GenerateNonce(); | 110 std::string nonce = GenerateNonce(); |
| 105 | 111 |
| 106 return GenerateHeaderString(age, nonce); | 112 return GenerateHeaderString(age, nonce, header); |
| 107 } | 113 } |
| 108 | 114 |
| 109 std::string HttpMacSignature::GenerateHeaderString(const std::string& age, | 115 bool HttpMacSignature::GenerateHeaderString(const std::string& age, |
| 110 const std::string& nonce) { | 116 const std::string& nonce, |
| 111 std::string mac = GenerateMAC(age, nonce); | 117 std::string* header) { |
| 118 std::string mac; |
| 119 if (!GenerateMAC(age, nonce, &mac)) |
| 120 return false; |
| 112 | 121 |
| 113 DCHECK(IsPlainString(age)); | 122 DCHECK(IsPlainString(age)); |
| 114 DCHECK(IsPlainString(nonce)); | 123 DCHECK(IsPlainString(nonce)); |
| 115 DCHECK(IsPlainString(mac)); | 124 DCHECK(IsPlainString(mac)); |
| 116 | 125 |
| 117 return "MAC id=\"" + id_ + | 126 *header = "MAC id=\"" + id_ + |
| 118 "\", nonce=\"" + age + ":" + nonce + | 127 "\", nonce=\"" + age + ":" + nonce + |
| 119 "\", mac=\"" + mac + "\""; | 128 "\", mac=\"" + mac + "\""; |
| 129 return true; |
| 120 } | 130 } |
| 121 | 131 |
| 122 std::string HttpMacSignature::GenerateNormalizedRequest( | 132 std::string HttpMacSignature::GenerateNormalizedRequest( |
| 123 const std::string& age, | 133 const std::string& age, |
| 124 const std::string& nonce) { | 134 const std::string& nonce) { |
| 125 static const std::string kNewLine = "\n"; | 135 static const std::string kNewLine = "\n"; |
| 126 | 136 |
| 127 std::string normalized_request = age + ":" + nonce + kNewLine; | 137 std::string normalized_request = age + ":" + nonce + kNewLine; |
| 128 normalized_request += method_ + kNewLine; | 138 normalized_request += method_ + kNewLine; |
| 129 normalized_request += request_uri_ + kNewLine; | 139 normalized_request += request_uri_ + kNewLine; |
| 130 normalized_request += host_ + kNewLine; | 140 normalized_request += host_ + kNewLine; |
| 131 normalized_request += port_ + kNewLine; | 141 normalized_request += port_ + kNewLine; |
| 132 normalized_request += kNewLine; | 142 normalized_request += kNewLine; |
| 133 normalized_request += kNewLine; | 143 normalized_request += kNewLine; |
| 134 | 144 |
| 135 return normalized_request; | 145 return normalized_request; |
| 136 } | 146 } |
| 137 | 147 |
| 138 std::string HttpMacSignature::GenerateMAC(const std::string& age, | 148 bool HttpMacSignature::GenerateMAC(const std::string& age, |
| 139 const std::string& nonce) { | 149 const std::string& nonce, |
| 150 std::string* mac) { |
| 140 std::string request = GenerateNormalizedRequest(age, nonce); | 151 std::string request = GenerateNormalizedRequest(age, nonce); |
| 141 | 152 |
| 142 crypto::HMAC hmac(mac_algorithm_); | 153 crypto::HMAC hmac(mac_algorithm_); |
| 143 hmac.Init(mac_key_); | 154 if (!hmac.Init(mac_key_)) { |
| 155 NOTREACHED(); |
| 156 return false; |
| 157 } |
| 144 | 158 |
| 145 std::string signature; | 159 std::string signature; |
| 146 size_t length = hmac.DigestLength(); | 160 size_t length = hmac.DigestLength(); |
| 147 char* buffer = WriteInto(&signature, length); | 161 char* buffer = WriteInto(&signature, length); |
| 148 bool result = hmac.Sign(request, | 162 if (!hmac.Sign(request, reinterpret_cast<unsigned char*>(buffer), |
| 149 reinterpret_cast<unsigned char*>(buffer), | 163 length)) { |
| 150 length); | 164 NOTREACHED(); |
| 151 DCHECK(result); | 165 return false; |
| 166 } |
| 152 | 167 |
| 153 std::string encoded_signature; | 168 std::string encoded_signature; |
| 154 result = base::Base64Encode(signature, &encoded_signature); | 169 if (!base::Base64Encode(signature, &encoded_signature)) { |
| 155 DCHECK(result); | 170 NOTREACHED(); |
| 156 return encoded_signature; | 171 return false; |
| 172 } |
| 173 |
| 174 mac->swap(encoded_signature); |
| 175 return true; |
| 157 } | 176 } |
| 158 | 177 |
| 159 } // namespace net | 178 } // namespace net |
| OLD | NEW |