OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_EXTENSIONS_CRX_FILE_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_CRX_FILE_H_ | |
7 | |
8 #include <sys/types.h> | |
9 #include "base/basictypes.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 | |
12 namespace extensions { | |
13 | |
14 // CRX files have a header that includes a magic key, version number, and | |
15 // some signature sizing information. Use CrxFile object to validate whether | |
16 // the header is valid or not. | |
17 class CrxFile { | |
18 public: | |
19 | |
20 // The size of the magic character sequence at the beginning of each crx | |
21 // file, in bytes. This should be a multiple of 4. | |
22 static const size_t kCrxFileHeaderMagicSize = 4; | |
23 | |
24 // This header is the first data at the beginning of an extension. Its | |
25 // contents are purposely 32-bit aligned so that it can just be slurped into | |
26 // a struct without manual parsing. | |
27 struct Header { | |
28 char magic[kCrxFileHeaderMagicSize]; | |
29 uint32 version; | |
30 uint32 key_size; // The size of the public key, in bytes. | |
31 uint32 signature_size; // The size of the signature, in bytes. | |
32 // An ASN.1-encoded PublicKeyInfo structure follows. | |
33 // The signature follows. | |
34 }; | |
35 | |
36 enum Error { | |
37 kWrongMagic, | |
38 kInvalidVersion, | |
39 kInvalidKeyTooLarge, | |
40 kInvalidKeyTooSmall, | |
41 kInvalidSignatureTooLarge, | |
42 kInvalidSignatureTooSmall, | |
43 }; | |
44 | |
45 // Construct a new CRX file header object with bytes of a header | |
46 // read from a CRX file. If a null scoped_ptr is returned, |error| | |
47 // contains an error code with additional information. | |
48 static scoped_ptr<CrxFile> Parse(const Header& header, Error* error); | |
49 | |
50 // Construct a new header for the given key and signature sizes. | |
51 // Returns a null scoped_ptr if erroneous values of |key_size| and/or | |
52 // |signature_size| are provided. |error| contains an error code with | |
53 // additional information. | |
54 // Use this constructor and then .header() to obtain the Header | |
55 // for writing out to a CRX file. | |
56 static scoped_ptr<CrxFile> Create(const uint32 key_size, | |
57 const uint32 signature_size, | |
58 Error* error); | |
59 | |
60 // Returns the header structure for writing out to a CRX file. | |
61 const Header& header() const { return header_; } | |
62 | |
63 private: | |
64 Header header_; | |
65 | |
66 // Constructor is private. Clients should use static factory methods above. | |
67 explicit CrxFile(const Header& header); | |
68 | |
69 // Checks the |header| for validity and returns true if the values are valid. | |
70 // If false is returned, more detailed error code is returned in |error|. | |
71 static bool HeaderIsValid(const Header& header, Error* error); | |
72 }; | |
73 | |
74 } // namespace extensions | |
75 | |
76 #endif // CHROME_BROWSER_EXTENSIONS_CRX_FILE_H_ | |
OLD | NEW |