Chromium Code Reviews| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 | 45 |
| 46 HttpMacSignature::HttpMacSignature() | 46 HttpMacSignature::HttpMacSignature() |
| 47 : mac_algorithm_(crypto::HMAC::SHA1) { | 47 : mac_algorithm_(crypto::HMAC::SHA1) { |
| 48 } | 48 } |
| 49 | 49 |
| 50 HttpMacSignature::~HttpMacSignature() { | 50 HttpMacSignature::~HttpMacSignature() { |
| 51 } | 51 } |
| 52 | 52 |
| 53 bool HttpMacSignature::AddStateInfo(const std::string& id, | 53 bool HttpMacSignature::AddStateInfo(const std::string& id, |
| 54 const std::string& mac_key, | 54 const std::string& mac_key, |
| 55 const std::string& mac_algorithm, | 55 const std::string& mac_algorithm) { |
| 56 const std::string& issuer) { | |
| 57 DCHECK(id_.empty()); | 56 DCHECK(id_.empty()); |
| 58 | 57 |
| 59 if (!IsPlainString(id) || id.empty() || | 58 if (!IsPlainString(id) || id.empty() || |
| 60 mac_key.empty() || | 59 mac_key.empty() || |
| 61 mac_algorithm.empty() || | 60 mac_algorithm.empty()) { |
| 62 !IsPlainString(issuer) || issuer.empty()) { | |
| 63 return false; | 61 return false; |
| 64 } | 62 } |
| 65 | 63 |
| 66 if (mac_algorithm == kSHA1Name) | 64 if (mac_algorithm == kSHA1Name) |
| 67 mac_algorithm_ = crypto::HMAC::SHA1; | 65 mac_algorithm_ = crypto::HMAC::SHA1; |
| 68 else if (mac_algorithm == kSHA256Name) | 66 else if (mac_algorithm == kSHA256Name) |
| 69 mac_algorithm_ = crypto::HMAC::SHA256; | 67 mac_algorithm_ = crypto::HMAC::SHA256; |
| 70 else | 68 else |
| 71 return false; | 69 return false; |
| 72 | 70 |
| 73 id_ = id; | 71 id_ = id; |
| 74 mac_key_ = mac_key; | 72 mac_key_ = mac_key; |
| 75 issuer_ = issuer; | |
| 76 return true; | 73 return true; |
| 77 } | 74 } |
| 78 | 75 |
| 79 bool HttpMacSignature::AddHttpInfo(const std::string& method, | 76 bool HttpMacSignature::AddHttpInfo(const std::string& method, |
| 80 const std::string& request_uri, | 77 const std::string& request_uri, |
| 81 const std::string& host, | 78 const std::string& host, |
| 82 int port) { | 79 int port) { |
| 83 DCHECK(method_.empty()); | 80 DCHECK(method_.empty()); |
| 84 | 81 |
| 85 if (!IsPlainString(method) || method.empty() || | 82 if (!IsPlainString(method) || method.empty() || |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 110 std::string HttpMacSignature::GenerateHeaderString( | 107 std::string HttpMacSignature::GenerateHeaderString( |
| 111 const std::string& timestamp, | 108 const std::string& timestamp, |
| 112 const std::string& nonce) { | 109 const std::string& nonce) { |
| 113 std::string mac = GenerateMAC(timestamp, nonce); | 110 std::string mac = GenerateMAC(timestamp, nonce); |
| 114 | 111 |
| 115 DCHECK(IsPlainString(timestamp)); | 112 DCHECK(IsPlainString(timestamp)); |
| 116 DCHECK(IsPlainString(nonce)); | 113 DCHECK(IsPlainString(nonce)); |
| 117 DCHECK(IsPlainString(mac)); | 114 DCHECK(IsPlainString(mac)); |
| 118 | 115 |
| 119 return "MAC id=\"" + id_ + | 116 return "MAC id=\"" + id_ + |
| 120 "\", issuer=\"" + issuer_ + | 117 "\", nonce=\"" + timestamp + ":" + nonce + |
|
cbentzel
2011/05/13 17:55:29
uhh, why'd you get rid of the issuer? Is this a ch
| |
| 121 "\", timestamp=\"" + timestamp + | |
| 122 "\", nonce=\"" + nonce + | |
| 123 "\", mac=\"" + mac + "\""; | 118 "\", mac=\"" + mac + "\""; |
| 124 } | 119 } |
| 125 | 120 |
| 126 std::string HttpMacSignature::GenerateNormalizedRequest( | 121 std::string HttpMacSignature::GenerateNormalizedRequest( |
| 127 const std::string& timestamp, | 122 const std::string& timestamp, |
| 128 const std::string& nonce) { | 123 const std::string& nonce) { |
| 129 static const std::string kNewLine = "\n"; | 124 static const std::string kNewLine = "\n"; |
| 130 | 125 |
| 131 std::string normalized_request = id_ + kNewLine; | 126 std::string normalized_request = timestamp + ":" + nonce + kNewLine; |
| 132 normalized_request += issuer_ + kNewLine; | |
| 133 normalized_request += timestamp + kNewLine; | |
| 134 normalized_request += nonce + kNewLine; | |
| 135 normalized_request += method_ + kNewLine; | 127 normalized_request += method_ + kNewLine; |
| 136 normalized_request += request_uri_ + kNewLine; | 128 normalized_request += request_uri_ + kNewLine; |
| 137 normalized_request += host_ + kNewLine; | 129 normalized_request += host_ + kNewLine; |
| 138 normalized_request += port_ + kNewLine; | 130 normalized_request += port_ + kNewLine; |
| 131 normalized_request += kNewLine; | |
| 132 normalized_request += kNewLine; | |
| 139 | 133 |
| 140 return normalized_request; | 134 return normalized_request; |
| 141 } | 135 } |
| 142 | 136 |
| 143 std::string HttpMacSignature::GenerateMAC(const std::string& timestamp, | 137 std::string HttpMacSignature::GenerateMAC(const std::string& timestamp, |
| 144 const std::string& nonce) { | 138 const std::string& nonce) { |
| 145 std::string request = GenerateNormalizedRequest(timestamp, nonce); | 139 std::string request = GenerateNormalizedRequest(timestamp, nonce); |
| 146 | 140 |
| 147 crypto::HMAC hmac(mac_algorithm_); | 141 crypto::HMAC hmac(mac_algorithm_); |
| 148 hmac.Init(mac_key_); | 142 hmac.Init(mac_key_); |
| 149 | 143 |
| 150 std::string signature; | 144 std::string signature; |
| 151 size_t length = hmac.DigestLength(); | 145 size_t length = hmac.DigestLength(); |
| 152 char* buffer = WriteInto(&signature, length); | 146 char* buffer = WriteInto(&signature, length); |
| 153 bool result = hmac.Sign(request, | 147 bool result = hmac.Sign(request, |
| 154 reinterpret_cast<unsigned char*>(buffer), | 148 reinterpret_cast<unsigned char*>(buffer), |
| 155 length); | 149 length); |
| 156 DCHECK(result); | 150 DCHECK(result); |
| 157 | 151 |
| 158 std::string encoded_signature; | 152 std::string encoded_signature; |
| 159 result = base::Base64Encode(signature, &encoded_signature); | 153 result = base::Base64Encode(signature, &encoded_signature); |
| 160 DCHECK(result); | 154 DCHECK(result); |
| 161 return encoded_signature; | 155 return encoded_signature; |
| 162 } | 156 } |
| 163 | 157 |
| 164 } // namespace net | 158 } // namespace net |
| OLD | NEW |