OLD | NEW |
(Empty) | |
| 1 // Copyright 2005-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 #ifndef OMAHA_COMMON_EXTRACTOR_H_ |
| 18 #define OMAHA_COMMON_EXTRACTOR_H_ |
| 19 |
| 20 #include <windows.h> |
| 21 |
| 22 namespace omaha { |
| 23 |
| 24 class TagExtractor { |
| 25 public: |
| 26 TagExtractor(); |
| 27 virtual ~TagExtractor(); |
| 28 |
| 29 /** |
| 30 * @return true if we successfully opened the file. |
| 31 */ |
| 32 bool OpenFile(const TCHAR* filename); |
| 33 |
| 34 /** |
| 35 * @return true if we currently have a handle to an open file. |
| 36 */ |
| 37 bool IsFileOpen() const; |
| 38 |
| 39 void CloseFile(); |
| 40 |
| 41 /** |
| 42 * Returns the tag in the current file. |
| 43 * |
| 44 * We're exploiting the empirical observation that Windows checks the |
| 45 * signature on a PEF but doesn't care if the signature container includes |
| 46 * extra bytes after the signature. |
| 47 * |
| 48 * Logic: |
| 49 * |
| 50 * - Sanity-check that we're a PEF image. |
| 51 * - Find the signature, which should be stored in the PE "Certificates |
| 52 * Directory" (dumpbin.exe /headers "Firefox Setup 1.0.7.exe") in a |
| 53 * WIN_CERTIFICATE structure. |
| 54 * - Crudely parse the ASN.1 signature to determine its end. |
| 55 * - Read the signature starting from the first byte past the ASN.1. |
| 56 * |
| 57 * @param tag_buffer: a buffer that will be filled with the extracted tag as |
| 58 * a null-terminated string, or NULL if the caller doesn't want the tag. |
| 59 * |
| 60 * @param tag_buffer_len: a pointer to an int that represents the length in |
| 61 * bytes of the buffer pointed to by tag_buffer. If tag_buffer is NULL and |
| 62 * there is a tag to extract, then we fill this int with the size of the |
| 63 * smallest buffer needed to contain the tag (plus the null terminator). |
| 64 * |
| 65 * @return true if we found a tag and either successfully copied all of it |
| 66 * into tag_buffer, or tag_buffer was NULL and we successfully returned |
| 67 * the required buffer size in tag_buffer_len. |
| 68 */ |
| 69 bool ExtractTag(char* tag_buffer, int* tag_buffer_len); |
| 70 bool ExtractTag(const char* binary_file, |
| 71 size_t binary_file_length, |
| 72 char* tag_buffer, |
| 73 int* tag_buffer_len); |
| 74 |
| 75 int cert_length() const { return cert_length_; } |
| 76 const void* cert_dir_base() const { return cert_dir_base_; } |
| 77 |
| 78 private: |
| 79 HANDLE file_handle_; |
| 80 HANDLE file_mapping_; |
| 81 LPVOID file_base_; |
| 82 size_t file_length_; |
| 83 int cert_length_; |
| 84 const void* cert_dir_base_; |
| 85 |
| 86 bool ReadTag(const char* tag_pointer, |
| 87 char* tag_buffer, |
| 88 int* tag_buffer_len) const; |
| 89 |
| 90 const void* GetCertificateDirectoryPointer(const void* base) const; |
| 91 |
| 92 const void* GetASN1SignaturePointer(const void* base) const; |
| 93 |
| 94 int GetASN1SignatureLength(const void* base) const; |
| 95 |
| 96 bool InternalExtractTag(const char* file_buffer, |
| 97 char* tag_buffer, |
| 98 int* tag_buffer_len); |
| 99 |
| 100 bool InternalReadCertificate(const char* file_buffer); |
| 101 }; |
| 102 |
| 103 } // namespace omaha |
| 104 |
| 105 #endif // OMAHA_COMMON_EXTRACTOR_H_ |
OLD | NEW |