| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/common/net/gaia/oauth_request_signer.h" |
| 6 |
| 7 #include <cctype> |
| 8 #include <cstdlib> |
| 9 #include <ctime> |
| 10 #include <map> |
| 11 #include <string> |
| 12 |
| 13 #include "base/base64.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/rand_util.h" |
| 16 #include "base/stringprintf.h" |
| 17 #include "base/time.h" |
| 18 #include "crypto/hmac.h" |
| 19 #include "googleurl/src/gurl.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 static const int kHexBase = 16; |
| 24 static char kHexDigits[] = "0123456789ABCDEF"; |
| 25 |
| 26 static const int kMaxNonceLength = 30; |
| 27 static const int kMinNonceLength = 15; |
| 28 |
| 29 static const char kOAuthConsumerKeyLabel[] = "oauth_consumer_key"; |
| 30 static const char kOAuthConsumerSecretLabel[] = "oauth_consumer_secret"; |
| 31 static const char kOAuthNonceCharacters[] = |
| 32 "abcdefghijklmnopqrstuvwyz" |
| 33 "ABCDEFGHIJKLMNOPQRSTUVWYZ" |
| 34 "0123456789_"; |
| 35 static const char kOAuthNonceLabel[] = "oauth_nonce"; |
| 36 static const char kOAuthSignatureLabel[] = "oauth_signature"; |
| 37 static const char kOAuthSignatureMethodLabel[] = "oauth_signature_method"; |
| 38 static const char kOAuthTimestampLabel[] = "oauth_timestamp"; |
| 39 static const char kOAuthTokenLabel[] = "oauth_token"; |
| 40 static const char kOAuthTokenSecretLabel[] = "oauth_token_secret"; |
| 41 static const char kOAuthVersion[] = "1.0"; |
| 42 static const char kOAuthVersionLabel[] = "oauth_version"; |
| 43 |
| 44 enum ParseQueryState { |
| 45 START_STATE, |
| 46 KEYWORD_STATE, |
| 47 VALUE_STATE, |
| 48 }; |
| 49 |
| 50 const std::string HttpMethodName(OAuthRequestSigner::HttpMethod method) { |
| 51 switch (method) { |
| 52 case OAuthRequestSigner::GET_METHOD: |
| 53 return "GET"; |
| 54 case OAuthRequestSigner::POST_METHOD: |
| 55 return "POST"; |
| 56 } |
| 57 NOTREACHED(); |
| 58 return *(new std::string()); |
| 59 } |
| 60 |
| 61 const std::string SignatureMethodName( |
| 62 OAuthRequestSigner::SignatureMethod method) { |
| 63 switch (method) { |
| 64 case OAuthRequestSigner::HMAC_SHA1_SIGNATURE: |
| 65 return "HMAC-SHA1"; |
| 66 case OAuthRequestSigner::RSA_SHA1_SIGNATURE: |
| 67 return "RSA-SHA1"; |
| 68 case OAuthRequestSigner::PLAINTEXT_SIGNATURE: |
| 69 return "PLAINTEXT"; |
| 70 } |
| 71 NOTREACHED(); |
| 72 return *(new std::string()); |
| 73 } |
| 74 |
| 75 // The form of percent encoding used for OAuth request signing is very |
| 76 // specific and strict. See http://oauth.net/core/1.0/#encoding_parameters. |
| 77 // |
| 78 // Any character which is in the "unreserved set" must not be encoded. |
| 79 // All other characters must be encoded. |
| 80 // |
| 81 // The unreserved set is comprised of the alphanumeric characters and these |
| 82 // others: |
| 83 // - minus (-) |
| 84 // - period (.) |
| 85 // - underscore (_) |
| 86 // - tilde (~) |
| 87 std::string EncodedOAuthParameter(const std::string& text) { |
| 88 std::string result = ""; |
| 89 std::string::const_iterator cursor; |
| 90 std::string::const_iterator limit; |
| 91 for (limit = text.end(), cursor = text.begin(); cursor != limit; ++cursor) { |
| 92 char character = *cursor; |
| 93 if (isalnum(character)) { |
| 94 result += character; |
| 95 } else { |
| 96 switch (character) { |
| 97 case '-': |
| 98 case '.': |
| 99 case '_': |
| 100 case '~': |
| 101 result += character; |
| 102 break; |
| 103 default: |
| 104 unsigned char byte = static_cast<unsigned char>(character); |
| 105 result = result + '%' + kHexDigits[byte / kHexBase] + |
| 106 kHexDigits[byte % kHexBase]; |
| 107 } |
| 108 } |
| 109 } |
| 110 return result; |
| 111 } |
| 112 |
| 113 std::string BuildBaseString(const GURL& request_base_url, |
| 114 OAuthRequestSigner::HttpMethod http_method, |
| 115 const std::string base_parameters) { |
| 116 return StringPrintf("%s&%s&%s", |
| 117 HttpMethodName(http_method).c_str(), |
| 118 EncodedOAuthParameter(request_base_url.spec()).c_str(), |
| 119 EncodedOAuthParameter(base_parameters).c_str()); |
| 120 } |
| 121 |
| 122 std::string BuildBaseStringParameters( |
| 123 const OAuthRequestSigner::Parameters& parameters) { |
| 124 std::string result = ""; |
| 125 OAuthRequestSigner::Parameters::const_iterator cursor; |
| 126 OAuthRequestSigner::Parameters::const_iterator limit; |
| 127 bool first = true; |
| 128 for (cursor = parameters.begin(), limit = parameters.end(); |
| 129 cursor != limit; |
| 130 ++cursor) { |
| 131 if (first) { |
| 132 first = false; |
| 133 } else { |
| 134 result += '&'; |
| 135 } |
| 136 result += EncodedOAuthParameter(cursor->first); |
| 137 result += '='; |
| 138 result += EncodedOAuthParameter(cursor->second); |
| 139 } |
| 140 return result; |
| 141 } |
| 142 |
| 143 std::string GenerateNonce() { |
| 144 char result[kMaxNonceLength + 1]; |
| 145 int length = base::RandUint64() % (kMaxNonceLength - kMinNonceLength + 1) + |
| 146 kMinNonceLength; |
| 147 result[length] = '\0'; |
| 148 for (int index = 0; index < length; ++index) |
| 149 result[index] = kOAuthNonceCharacters[ |
| 150 base::RandUint64() % (sizeof(kOAuthNonceCharacters) - 1)]; |
| 151 return result; |
| 152 } |
| 153 |
| 154 std::string GenerateTimestamp() { |
| 155 return base::StringPrintf("%ld", base::Time::NowFromSystemTime().ToTimeT()); |
| 156 } |
| 157 |
| 158 // Creates a string-to-string, keyword-value map from a parameter/query string |
| 159 // that uses ampersand (&) to seperate paris and equals (=) to seperate |
| 160 // keyword from value. |
| 161 bool ParseQuery(const std::string& query, |
| 162 OAuthRequestSigner::Parameters* parameters_result) { |
| 163 std::string::const_iterator cursor; |
| 164 std::string keyword; |
| 165 std::string::const_iterator limit; |
| 166 OAuthRequestSigner::Parameters parameters; |
| 167 ParseQueryState state; |
| 168 std::string value; |
| 169 |
| 170 state = START_STATE; |
| 171 for (cursor = query.begin(), limit = query.end(); |
| 172 cursor != limit; |
| 173 ++cursor) { |
| 174 char character = *cursor; |
| 175 switch (state) { |
| 176 case KEYWORD_STATE: |
| 177 switch (character) { |
| 178 case '&': |
| 179 parameters[keyword] = value; |
| 180 keyword = ""; |
| 181 value = ""; |
| 182 state = START_STATE; |
| 183 break; |
| 184 case '=': |
| 185 state = VALUE_STATE; |
| 186 break; |
| 187 default: |
| 188 keyword += character; |
| 189 } |
| 190 break; |
| 191 case START_STATE: |
| 192 switch (character) { |
| 193 case '&': // Intentionally falling through |
| 194 case '=': |
| 195 return false; |
| 196 default: |
| 197 keyword += character; |
| 198 state = KEYWORD_STATE; |
| 199 } |
| 200 break; |
| 201 case VALUE_STATE: |
| 202 switch (character) { |
| 203 case '=': |
| 204 return false; |
| 205 case '&': |
| 206 parameters[keyword] = value; |
| 207 keyword = ""; |
| 208 value = ""; |
| 209 state = START_STATE; |
| 210 break; |
| 211 default: |
| 212 value += character; |
| 213 } |
| 214 break; |
| 215 } |
| 216 } |
| 217 switch (state) { |
| 218 case START_STATE: |
| 219 break; |
| 220 case KEYWORD_STATE: // Intentionally falling through |
| 221 case VALUE_STATE: |
| 222 parameters[keyword] = value; |
| 223 break; |
| 224 default: |
| 225 NOTREACHED(); |
| 226 } |
| 227 *parameters_result = parameters; |
| 228 return true; |
| 229 } |
| 230 |
| 231 // Creates the value for the oauth_signature parameter when the |
| 232 // oauth_signature_method is HMAC-SHA1. |
| 233 bool SignHmacSha1(const std::string& text, |
| 234 const std::string& key, |
| 235 std::string* signature_return) { |
| 236 crypto::HMAC hmac(crypto::HMAC::SHA1); |
| 237 size_t digest_length = hmac.DigestLength(); |
| 238 unsigned char* digest = new unsigned char [digest_length]; |
| 239 hmac.Init(key); |
| 240 return hmac.Sign(text, digest, digest_length) && |
| 241 base::Base64Encode(std::string(reinterpret_cast<const char*>(digest), |
| 242 digest_length), |
| 243 signature_return); |
| 244 } |
| 245 |
| 246 // Creates the value for the oauth_signature parameter when the |
| 247 // oauth_signature_method is PLAINTEXT. |
| 248 // |
| 249 // Not yet implemented, and might never be. |
| 250 bool SignPlaintext(const std::string& text, |
| 251 const std::string& key, |
| 252 std::string* result) { |
| 253 NOTIMPLEMENTED(); |
| 254 return false; |
| 255 } |
| 256 |
| 257 // Creates the value for the oauth_signature parameter when the |
| 258 // oauth_signature_method is RSA-SHA1. |
| 259 // |
| 260 // Not yet implemented, and might never be. |
| 261 bool SignRsaSha1(const std::string& text, |
| 262 const std::string& key, |
| 263 std::string* result) { |
| 264 NOTIMPLEMENTED(); |
| 265 return false; |
| 266 } |
| 267 |
| 268 } // namespace |
| 269 |
| 270 // static |
| 271 bool OAuthRequestSigner::ParseAndSign(const GURL& request_url_with_parameters, |
| 272 SignatureMethod signature_method, |
| 273 HttpMethod http_method, |
| 274 const std::string& consumer_key, |
| 275 const std::string& consumer_secret, |
| 276 const std::string& token_key, |
| 277 const std::string& token_secret, |
| 278 std::string* result) { |
| 279 DCHECK(request_url_with_parameters.is_valid()); |
| 280 Parameters parameters; |
| 281 if (request_url_with_parameters.has_query()) { |
| 282 const std::string& query = request_url_with_parameters.query(); |
| 283 if (!query.empty()) { |
| 284 if (!ParseQuery(query, ¶meters)) |
| 285 return false; |
| 286 } |
| 287 } |
| 288 std::string spec = request_url_with_parameters.spec(); |
| 289 std::string url_without_parameters = spec; |
| 290 std::string::size_type question = spec.find("?"); |
| 291 if (question != std::string::npos) { |
| 292 url_without_parameters = spec.substr(0,question); |
| 293 } |
| 294 return Sign (GURL(url_without_parameters), parameters, signature_method, |
| 295 http_method, consumer_key, consumer_secret, token_key, |
| 296 token_secret, result); |
| 297 } |
| 298 |
| 299 // Returns a copy of request_parameters, with parameters that are required by |
| 300 // OAuth added as needed. |
| 301 OAuthRequestSigner::Parameters |
| 302 PrepareParameters(const OAuthRequestSigner::Parameters& request_parameters, |
| 303 OAuthRequestSigner::SignatureMethod signature_method, |
| 304 OAuthRequestSigner::HttpMethod http_method, |
| 305 const std::string& consumer_key, |
| 306 const std::string& token_key) { |
| 307 OAuthRequestSigner::Parameters result(request_parameters); |
| 308 |
| 309 if (result.find(kOAuthNonceLabel) == result.end()) |
| 310 result[kOAuthNonceLabel] = GenerateNonce(); |
| 311 |
| 312 if (result.find(kOAuthTimestampLabel) == result.end()) |
| 313 result[kOAuthTimestampLabel] = GenerateTimestamp(); |
| 314 |
| 315 result[kOAuthConsumerKeyLabel] = consumer_key; |
| 316 result[kOAuthSignatureMethodLabel] = SignatureMethodName(signature_method); |
| 317 result[kOAuthTokenLabel] = token_key; |
| 318 result[kOAuthVersionLabel] = kOAuthVersion; |
| 319 |
| 320 return result; |
| 321 } |
| 322 |
| 323 // static |
| 324 bool OAuthRequestSigner::Sign( |
| 325 const GURL& request_base_url, |
| 326 const Parameters& request_parameters, |
| 327 SignatureMethod signature_method, |
| 328 HttpMethod http_method, |
| 329 const std::string& consumer_key, |
| 330 const std::string& consumer_secret, |
| 331 const std::string& token_key, |
| 332 const std::string& token_secret, |
| 333 std::string* signed_text_return) { |
| 334 DCHECK(request_base_url.is_valid()); |
| 335 Parameters parameters = PrepareParameters(request_parameters, |
| 336 signature_method, |
| 337 http_method, |
| 338 consumer_key, |
| 339 token_key); |
| 340 std::string base_parameters = BuildBaseStringParameters(parameters); |
| 341 std::string base = BuildBaseString(request_base_url, |
| 342 http_method, |
| 343 base_parameters); |
| 344 std::string key = consumer_secret + '&' + token_secret; |
| 345 bool is_signed = false; |
| 346 std::string signature; |
| 347 switch (signature_method) { |
| 348 case HMAC_SHA1_SIGNATURE: |
| 349 is_signed = SignHmacSha1(base, key, &signature); |
| 350 break; |
| 351 case RSA_SHA1_SIGNATURE: |
| 352 is_signed = SignRsaSha1(base, key, &signature); |
| 353 break; |
| 354 case PLAINTEXT_SIGNATURE: |
| 355 is_signed = SignPlaintext(base, key, &signature); |
| 356 break; |
| 357 default: |
| 358 NOTREACHED(); |
| 359 } |
| 360 if (is_signed) { |
| 361 std::string signed_text; |
| 362 switch (http_method) { |
| 363 case GET_METHOD: |
| 364 signed_text = request_base_url.spec() + '?'; |
| 365 // Intentionally falling through |
| 366 case POST_METHOD: |
| 367 signed_text += base_parameters + '&' + kOAuthSignatureLabel + '=' + |
| 368 EncodedOAuthParameter(signature); |
| 369 break; |
| 370 default: |
| 371 NOTREACHED(); |
| 372 } |
| 373 *signed_text_return = signed_text; |
| 374 } |
| 375 return is_signed; |
| 376 } |
| OLD | NEW |