Home | History | Annotate | Download | only in common
      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 EXTENSIONS_COMMON_CRX_FILE_H_
      6 #define EXTENSIONS_COMMON_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   // Checks a valid |header| to determine whether or not the CRX represents a
     64   // differential CRX.
     65   static bool HeaderIsDelta(const Header& header);
     66 
     67  private:
     68   Header header_;
     69 
     70   // Constructor is private. Clients should use static factory methods above.
     71   explicit CrxFile(const Header& header);
     72 
     73   // Checks the |header| for validity and returns true if the values are valid.
     74   // If false is returned, more detailed error code is returned in |error|.
     75   static bool HeaderIsValid(const Header& header, Error* error);
     76 };
     77 
     78 }  // namespace extensions
     79 
     80 #endif  // EXTENSIONS_COMMON_CRX_FILE_H_
     81