Upstream version 8.36.156.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / installer / xpk_package.cc
1 // Copyright (c) 2013 Intel Corporation. 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 #include "xwalk/application/common/installer/xpk_package.h"
6
7 #include "base/file_util.h"
8 #include "crypto/signature_verifier.h"
9 #include "xwalk/application/common/id_util.h"
10
11 namespace xwalk {
12 namespace application {
13
14 const uint8 kSignatureAlgorithm[15] = {
15   0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
16   0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00
17 };
18
19 const char XPKPackage::kXPKPackageHeaderMagic[] = "CrWk";
20
21 XPKPackage::~XPKPackage() {
22 }
23
24 XPKPackage::XPKPackage(const base::FilePath& path)
25   : Package(path) {
26   if (!base::PathExists(path))
27     return;
28   type_ = XPK;
29   scoped_ptr<ScopedStdioHandle> file(
30       new ScopedStdioHandle(base::OpenFile(path, "rb")));
31   file_ = file.Pass();
32   size_t len = fread(&header_, 1, sizeof(header_), file_->get());
33   is_valid_ = false;
34   if (len < sizeof(header_))
35     return;
36   if (!strncmp(XPKPackage::kXPKPackageHeaderMagic,
37                header_.magic,
38                sizeof(header_.magic)) &&
39       header_.key_size > 0 &&
40       header_.key_size <= XPKPackage::kMaxPublicKeySize &&
41       header_.signature_size > 0 &&
42       header_.signature_size <= XPKPackage::kMaxSignatureKeySize) {
43       is_valid_ = true;
44       zip_addr_ = sizeof(header_) + header_.key_size + header_.signature_size;
45         fseek(file_->get(), sizeof(header_), SEEK_SET);
46         key_.resize(header_.key_size);
47         size_t len = fread(
48             &key_.front(), sizeof(uint8), header_.key_size, file_->get());
49         if (len < header_.key_size)
50           is_valid_ = false;
51
52         signature_.resize(header_.signature_size);
53         len = fread(&signature_.front(),
54                     sizeof(uint8),
55                     header_.signature_size,
56                     file_->get());
57         if (len < header_.signature_size)
58           is_valid_ = false;
59
60         if (!VerifySignature())
61           is_valid_ = false;
62
63         std::string public_key =
64             std::string(reinterpret_cast<char*>(&key_.front()), key_.size());
65         id_ = GenerateId(public_key);
66   }
67   return;
68 }
69
70 bool XPKPackage::VerifySignature() {
71 // Set the file read position to the beginning of compressed resource file,
72 // which is behind the magic header, public key and signature key.
73   fseek(file_->get(), zip_addr_, SEEK_SET);
74   crypto::SignatureVerifier verifier;
75   if (!verifier.VerifyInit(kSignatureAlgorithm,
76                            sizeof(kSignatureAlgorithm),
77                            &signature_.front(),
78                            signature_.size(),
79                            &key_.front(),
80                            key_.size()))
81     return false;
82   unsigned char buf[1 << 12];
83   size_t len = 0;
84   while ((len = fread(buf, 1, sizeof(buf), file_->get())) > 0)
85     verifier.VerifyUpdate(buf, len);
86   if (!verifier.VerifyFinal())
87     return false;
88
89   return true;
90 }
91
92 bool XPKPackage::Extract(base::FilePath* target_path) {
93   if (is_extracted_) {
94     *target_path = temp_dir_.path();
95     return true;
96   }
97
98   if (!IsValid()) {
99     LOG(ERROR) << "The XPK file is not valid.";
100     return false;
101   }
102
103   return Package::Extract(target_path);
104 }
105
106 }  // namespace application
107 }  // namespace xwalk