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.
5 #include "xwalk/application/browser/installer/xpk_package.h"
7 #include "base/file_util.h"
8 #include "crypto/signature_verifier.h"
9 #include "xwalk/application/common/id_util.h"
12 namespace application {
14 const uint8 kSignatureAlgorithm[15] = {
15 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
16 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00
19 const char XPKPackage::kXPKPackageHeaderMagic[] = "CrWk";
21 XPKPackage::~XPKPackage() {
24 XPKPackage::XPKPackage(const base::FilePath& path)
26 if (!base::PathExists(path))
28 scoped_ptr<ScopedStdioHandle> file(
29 new ScopedStdioHandle(base::OpenFile(path, "rb")));
31 size_t len = fread(&header_, 1, sizeof(header_), file_->get());
33 if (len < sizeof(header_))
35 if (!strncmp(XPKPackage::kXPKPackageHeaderMagic,
37 sizeof(header_.magic)) &&
38 header_.key_size > 0 &&
39 header_.key_size <= XPKPackage::kMaxPublicKeySize &&
40 header_.signature_size > 0 &&
41 header_.signature_size <= XPKPackage::kMaxSignatureKeySize) {
43 zip_addr_ = sizeof(header_) + header_.key_size + header_.signature_size;
44 fseek(file_->get(), sizeof(header_), SEEK_SET);
45 key_.resize(header_.key_size);
47 &key_.front(), sizeof(uint8), header_.key_size, file_->get());
48 if (len < header_.key_size)
51 signature_.resize(header_.signature_size);
52 len = fread(&signature_.front(),
54 header_.signature_size,
56 if (len < header_.signature_size)
59 if (!VerifySignature())
62 std::string public_key =
63 std::string(reinterpret_cast<char*>(&key_.front()), key_.size());
64 id_ = GenerateId(public_key);
69 bool XPKPackage::VerifySignature() {
70 // Set the file read position to the beginning of compressed resource file,
71 // which is behind the magic header, public key and signature key.
72 fseek(file_->get(), zip_addr_, SEEK_SET);
73 crypto::SignatureVerifier verifier;
74 if (!verifier.VerifyInit(kSignatureAlgorithm,
75 sizeof(kSignatureAlgorithm),
81 unsigned char buf[1 << 12];
83 while ((len = fread(buf, 1, sizeof(buf), file_->get())) > 0)
84 verifier.VerifyUpdate(buf, len);
85 if (!verifier.VerifyFinal())
91 bool XPKPackage::Extract(base::FilePath* target_path) {
93 *target_path = temp_dir_.path();
98 LOG(ERROR) << "The XPK file is not valid.";
102 return Package::Extract(target_path);
105 } // namespace application