Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
|
Sorin Jianu
2017/05/15 19:49:51
do we retain the original copyright year?
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_CRX_FILE_CRX2_FILE_H_ | |
| 6 #define COMPONENTS_CRX_FILE_CRX2_FILE_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 #include <sys/types.h> | |
| 11 | |
| 12 #include <memory> | |
| 13 #include <string> | |
| 14 #include <vector> | |
| 15 | |
| 16 namespace crx_file { | |
| 17 | |
| 18 // The magic string embedded in the header. | |
| 19 constexpr char kCrx2FileHeaderMagic[] = "Cr24"; | |
| 20 constexpr char kCrxDiffFileHeaderMagic[] = "CrOD"; | |
| 21 constexpr int kCrx2FileHeaderMagicSize = 4; | |
| 22 | |
| 23 // CRX files have a header that includes a magic key, version number, and | |
| 24 // some signature sizing information. Use Crx2File object to validate whether | |
| 25 // the header is valid or not. | |
| 26 class Crx2File { | |
| 27 public: | |
| 28 // This header is the first data at the beginning of an extension. Its | |
| 29 // contents are purposely 32-bit aligned so that it can just be slurped into | |
| 30 // a struct without manual parsing. | |
| 31 struct Header { | |
| 32 char magic[kCrx2FileHeaderMagicSize]; | |
| 33 uint32_t version; | |
| 34 uint32_t key_size; // The size of the public key, in bytes. | |
| 35 uint32_t signature_size; // The size of the signature, in bytes. | |
| 36 // An ASN.1-encoded PublicKeyInfo structure follows. | |
| 37 // The signature follows. | |
| 38 }; | |
| 39 | |
| 40 enum Error { | |
| 41 kWrongMagic, | |
| 42 kInvalidVersion, | |
| 43 kInvalidKeyTooLarge, | |
| 44 kInvalidKeyTooSmall, | |
| 45 kInvalidSignatureTooLarge, | |
| 46 kInvalidSignatureTooSmall, | |
| 47 }; | |
| 48 | |
| 49 // Construct a new header for the given key and signature sizes. | |
| 50 // Returns a null scoped_ptr if erroneous values of |key_size| and/or | |
| 51 // |signature_size| are provided. |error| contains an error code with | |
| 52 // additional information. | |
| 53 // Use this constructor and then .header() to obtain the Header | |
| 54 // for writing out to a CRX file. | |
| 55 static std::unique_ptr<Crx2File> Create(const uint32_t key_size, | |
| 56 const uint32_t signature_size, | |
| 57 Error* error); | |
| 58 | |
| 59 // Returns the header structure for writing out to a CRX file. | |
| 60 const Header& header() const { return header_; } | |
| 61 | |
| 62 private: | |
| 63 Header header_; | |
| 64 | |
| 65 // Constructor is private. Clients should use static factory methods above. | |
| 66 explicit Crx2File(const Header& header); | |
| 67 | |
| 68 // Checks the |header| for validity and returns true if the values are valid. | |
| 69 // If false is returned, more detailed error code is returned in |error|. | |
| 70 static bool HeaderIsValid(const Header& header, Error* error); | |
| 71 }; | |
| 72 | |
| 73 } // namespace crx_file | |
| 74 | |
| 75 #endif // COMPONENTS_CRX_FILE_CRX2_FILE_H_ | |
| OLD | NEW |