OLD | NEW |
| (Empty) |
1 // Copyright 2006-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 | |
17 #include "omaha/base/encrypt.h" | |
18 | |
19 #include <vector> | |
20 #include "omaha/base/debug.h" | |
21 #include "omaha/base/error.h" | |
22 | |
23 namespace omaha { | |
24 | |
25 namespace encrypt { | |
26 | |
27 // TODO(omaha): consider loading crypt32.dll dynamically, as these functions | |
28 // are used infrequently. | |
29 | |
30 HRESULT EncryptData(const void* key, size_t key_len, | |
31 const void* data, size_t data_len, | |
32 std::vector<uint8>* data_out) { | |
33 // key may be null. | |
34 ASSERT1(data); | |
35 ASSERT1(data_out); | |
36 DATA_BLOB blob_out = {0, NULL}; | |
37 DATA_BLOB blob_in = { data_len, static_cast<BYTE*>(const_cast<void*>(data)) }; | |
38 DATA_BLOB entropy = { 0, NULL }; | |
39 if (key != NULL && key_len != 0) { | |
40 entropy.cbData = key_len; | |
41 entropy.pbData = static_cast<BYTE*>(const_cast<void*>(key)); | |
42 } | |
43 | |
44 // The description parameter is required on W2K. | |
45 if (!::CryptProtectData(&blob_in, _T("gupdate"), &entropy, NULL, NULL, | |
46 CRYPTPROTECT_UI_FORBIDDEN, &blob_out)) { | |
47 return HRESULTFromLastError(); | |
48 } | |
49 | |
50 data_out->clear(); | |
51 const uint8* first = reinterpret_cast<const uint8*>(blob_out.pbData); | |
52 const uint8* last = first + blob_out.cbData; | |
53 data_out->insert(data_out->begin(), first, last); | |
54 ::LocalFree(blob_out.pbData); | |
55 | |
56 ASSERT1(data_out->size() == blob_out.cbData); | |
57 return S_OK; | |
58 } | |
59 | |
60 HRESULT DecryptData(const void* key, size_t key_len, | |
61 const void* data, size_t data_len, | |
62 std::vector<uint8>* data_out) { | |
63 // key may be null. | |
64 ASSERT1(data); | |
65 ASSERT1(data_out); | |
66 | |
67 DATA_BLOB blob_out = {0, NULL}; | |
68 DATA_BLOB blob_in = { data_len, static_cast<BYTE*>(const_cast<void*>(data)) }; | |
69 DATA_BLOB entropy = { 0, NULL }; | |
70 | |
71 if (key != NULL && key_len != 0) { | |
72 entropy.cbData = key_len; | |
73 entropy.pbData = static_cast<BYTE*>(const_cast<void*>(key)); | |
74 } | |
75 | |
76 if (!::CryptUnprotectData(&blob_in, NULL, &entropy, NULL, NULL, | |
77 CRYPTPROTECT_UI_FORBIDDEN, &blob_out)) { | |
78 return (::GetLastError() != ERROR_SUCCESS) ? | |
79 HRESULTFromLastError() : HRESULT_FROM_WIN32(ERROR_INVALID_DATA); | |
80 } | |
81 | |
82 data_out->clear(); | |
83 const uint8* first = reinterpret_cast<const uint8*>(blob_out.pbData); | |
84 const uint8* last = first + blob_out.cbData; | |
85 data_out->insert(data_out->begin(), first, last); | |
86 ::LocalFree(blob_out.pbData); | |
87 | |
88 ASSERT1(data_out->size() == blob_out.cbData); | |
89 return S_OK; | |
90 } | |
91 | |
92 } // namespace encrypt | |
93 | |
94 } // namespace omaha | |
95 | |
OLD | NEW |