Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(453)

Side by Side Diff: components/gcm_driver/crypto/encryption_header_parsers.cc

Issue 1243563002: Teach the GCM Driver how to decrypt incoming messages. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gcm-push-keys
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/gcm_driver/crypto/encryption_header_parsers.h" 5 #include "components/gcm_driver/crypto/encryption_header_parsers.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 parser.value(); 49 parser.value();
50 } 50 }
51 51
52 if (name_value_map.size()) 52 if (name_value_map.size())
53 output->push_back(name_value_map); 53 output->push_back(name_value_map);
54 } 54 }
55 55
56 return true; 56 return true;
57 } 57 }
58 58
59 // TODO(peter): Generalize a base64url implementation.
60 // See https://tools.ietf.org/html/rfc4648#section-5
61 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) {
62 if (input.find_first_of("+/") != std::string::npos)
63 return false;
64
65 // Add padding.
66 size_t padded_size = (input.size() + 3) - (input.size() + 3) % 4;
67 std::string padded_input(input);
68 padded_input.resize(padded_size, '=');
69
70 // Convert to standard base64 alphabet.
71 base::ReplaceChars(padded_input, "-", "+", &padded_input);
72 base::ReplaceChars(padded_input, "_", "/", &padded_input);
73
74 return base::Base64Decode(padded_input, output);
75 }
76
77 // Parses the "salt" field of the Encryption header. Must be a base64url 59 // Parses the "salt" field of the Encryption header. Must be a base64url
78 // encoded string that decodes to a string exactly 16 bytes in length. 60 // encoded string that decodes to a string exactly 16 bytes in length.
79 bool ParseSalt(const std::string& value, std::string* output) { 61 bool ParseSalt(const std::string& value, std::string* output) {
80 std::string decoded_value; 62 std::string decoded_value;
81 if (!Base64DecodeUrlSafe(value, &decoded_value)) 63 if (!Base64DecodeUrlSafe(value, &decoded_value))
82 return false; 64 return false;
83 65
84 if (decoded_value.size() != 16) 66 if (decoded_value.size() != 16)
85 return false; 67 return false;
86 68
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 if (!Base64DecodeUrlSafe(dh_iter->second, &value.dh)) 171 if (!Base64DecodeUrlSafe(dh_iter->second, &value.dh))
190 return false; 172 return false;
191 } 173 }
192 174
193 result->push_back(value); 175 result->push_back(value);
194 } 176 }
195 177
196 return true; 178 return true;
197 } 179 }
198 180
181 // TODO(peter): Generalize a base64url implementation.
182 // See https://tools.ietf.org/html/rfc4648#section-5
183 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) {
184 if (input.find_first_of("+/") != std::string::npos)
185 return false;
186
187 // Add padding.
188 size_t padded_size = (input.size() + 3) - (input.size() + 3) % 4;
189 std::string padded_input(input);
190 padded_input.resize(padded_size, '=');
191
192 // Convert to standard base64 alphabet.
193 base::ReplaceChars(padded_input, "-", "+", &padded_input);
194 base::ReplaceChars(padded_input, "_", "/", &padded_input);
195
196 return base::Base64Decode(padded_input, output);
197 }
198
199 } // namespace gcm 199 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698