Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / crx_file / crx_file.h
blobbdb27db263466492258faa84810711fde55d023d
1 // Copyright 2014 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.
5 #ifndef COMPONENTS_CRX_FILE_CRX_FILE_H_
6 #define COMPONENTS_CRX_FILE_CRX_FILE_H_
8 #include <string>
9 #include <vector>
11 #include <sys/types.h>
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
15 namespace base {
16 class FilePath;
19 namespace crx_file {
21 // CRX files have a header that includes a magic key, version number, and
22 // some signature sizing information. Use CrxFile object to validate whether
23 // the header is valid or not.
24 class CrxFile {
25 public:
26 // The size of the magic character sequence at the beginning of each crx
27 // file, in bytes. This should be a multiple of 4.
28 static const size_t kCrxFileHeaderMagicSize = 4;
30 // This header is the first data at the beginning of an extension. Its
31 // contents are purposely 32-bit aligned so that it can just be slurped into
32 // a struct without manual parsing.
33 struct Header {
34 char magic[kCrxFileHeaderMagicSize];
35 uint32 version;
36 uint32 key_size; // The size of the public key, in bytes.
37 uint32 signature_size; // The size of the signature, in bytes.
38 // An ASN.1-encoded PublicKeyInfo structure follows.
39 // The signature follows.
42 enum Error {
43 kWrongMagic,
44 kInvalidVersion,
45 kInvalidKeyTooLarge,
46 kInvalidKeyTooSmall,
47 kInvalidSignatureTooLarge,
48 kInvalidSignatureTooSmall,
51 // Construct a new CRX file header object with bytes of a header
52 // read from a CRX file. If a null scoped_ptr is returned, |error|
53 // contains an error code with additional information.
54 static scoped_ptr<CrxFile> Parse(const Header& header, Error* error);
56 // Construct a new header for the given key and signature sizes.
57 // Returns a null scoped_ptr if erroneous values of |key_size| and/or
58 // |signature_size| are provided. |error| contains an error code with
59 // additional information.
60 // Use this constructor and then .header() to obtain the Header
61 // for writing out to a CRX file.
62 static scoped_ptr<CrxFile> Create(const uint32 key_size,
63 const uint32 signature_size,
64 Error* error);
66 // Returns the header structure for writing out to a CRX file.
67 const Header& header() const { return header_; }
69 // Checks a valid |header| to determine whether or not the CRX represents a
70 // differential CRX.
71 static bool HeaderIsDelta(const Header& header);
73 enum class ValidateError {
74 NONE,
75 CRX_FILE_NOT_READABLE,
76 CRX_HEADER_INVALID,
77 CRX_MAGIC_NUMBER_INVALID,
78 CRX_VERSION_NUMBER_INVALID,
79 CRX_EXCESSIVELY_LARGE_KEY_OR_SIGNATURE,
80 CRX_ZERO_KEY_LENGTH,
81 CRX_ZERO_SIGNATURE_LENGTH,
82 CRX_PUBLIC_KEY_INVALID,
83 CRX_SIGNATURE_INVALID,
84 CRX_SIGNATURE_VERIFICATION_INITIALIZATION_FAILED,
85 CRX_SIGNATURE_VERIFICATION_FAILED,
86 CRX_HASH_VERIFICATION_FAILED,
89 // Validates that the .crx file at |crx_path| is properly signed. If
90 // |expected_hash| is non-empty, it should specify a hex-encoded SHA256 hash
91 // for the entire .crx file which will be checked as we read it, and any
92 // mismatch will cause a CRX_HASH_VERIFICATION_FAILED result. The
93 // |public_key| argument can be provided to receive a copy of the
94 // base64-encoded public key pem bytes extracted from the .crx. The
95 // |extension_id| argument can be provided to receive the extension id (which
96 // is computed as a hash of the public key provided in the .crx). The
97 // |header| argument can be provided to receive a copy of the crx header.
98 static ValidateError ValidateSignature(const base::FilePath& crx_path,
99 const std::string& expected_hash,
100 std::string* public_key,
101 std::string* extension_id,
102 CrxFile::Header* header);
104 private:
105 Header header_;
107 // Constructor is private. Clients should use static factory methods above.
108 explicit CrxFile(const Header& header);
110 // Checks the |header| for validity and returns true if the values are valid.
111 // If false is returned, more detailed error code is returned in |error|.
112 static bool HeaderIsValid(const Header& header, Error* error);
115 } // namespace crx_file
117 #endif // COMPONENTS_CRX_FILE_CRX_FILE_H_