~ubuntu-branches/ubuntu/trusty/aria2/trusty-proposed

« back to all changes in this revision

Viewing changes to src/WinMessageDigestImpl.cc

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry
  • Date: 2013-12-16 18:41:03 UTC
  • mfrom: (2.5.21 sid)
  • Revision ID: package-import@ubuntu.com-20131216184103-xzah3019zwut429g
Tags: 1.18.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* <!-- copyright */
 
2
/*
 
3
 * aria2 - The high speed download utility
 
4
 *
 
5
 * Copyright (C) 2013 Nils Maier
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
20
 *
 
21
 * In addition, as a special exception, the copyright holders give
 
22
 * permission to link the code of portions of this program with the
 
23
 * OpenSSL library under certain conditions as described in each
 
24
 * individual source file, and distribute linked combinations
 
25
 * including the two.
 
26
 * You must obey the GNU General Public License in all respects
 
27
 * for all of the code used other than OpenSSL.  If you modify
 
28
 * file(s) with this exception, you may extend this exception to your
 
29
 * version of the file(s), but you are not obligated to do so.  If you
 
30
 * do not wish to do so, delete this exception statement from your
 
31
 * version.  If you delete this exception statement from all source
 
32
 * files in the program, then also delete it here.
 
33
 */
 
34
/* copyright --> */
 
35
 
 
36
#include "MessageDigestImpl.h"
 
37
 
 
38
#include <wincrypt.h>
 
39
 
 
40
#include "fmt.h"
 
41
#include "DlAbortEx.h"
 
42
#include "LogFactory.h"
 
43
 
 
44
namespace {
 
45
using namespace aria2;
 
46
 
 
47
class Context {
 
48
private:
 
49
  HCRYPTPROV provider_;
 
50
public:
 
51
  Context() {
 
52
    if (!::CryptAcquireContext(&provider_, nullptr, nullptr,
 
53
                               PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
 
54
      if (!::CryptAcquireContext(&provider_, nullptr, nullptr, PROV_RSA_AES,
 
55
                                CRYPT_VERIFYCONTEXT)) {
 
56
        throw DL_ABORT_EX("Failed to get cryptographic provider");
 
57
      }
 
58
    }
 
59
  }
 
60
  ~Context() {
 
61
    ::CryptReleaseContext(provider_, 0);
 
62
  }
 
63
 
 
64
  HCRYPTPROV get() {
 
65
    return provider_;
 
66
  }
 
67
};
 
68
 
 
69
// XXX static OK?
 
70
static Context context_;
 
71
 
 
72
inline size_t getAlgLength(ALG_ID id)
 
73
{
 
74
  Context context;
 
75
  HCRYPTHASH hash;
 
76
  if (!::CryptCreateHash(context.get(), id, 0, 0, &hash)) {
 
77
    throw DL_ABORT_EX(fmt("Failed to initialize hash %d", id));
 
78
  }
 
79
 
 
80
  DWORD rv = 0;
 
81
  DWORD len = sizeof(rv);
 
82
  if (!::CryptGetHashParam(hash, HP_HASHSIZE, reinterpret_cast<BYTE*>(&rv),
 
83
                            &len, 0)) {
 
84
    throw DL_ABORT_EX("Failed to initialize hash(2)");
 
85
  }
 
86
  ::CryptDestroyHash(hash);
 
87
 
 
88
  return rv;
 
89
}
 
90
 
 
91
} // namespace
 
92
 
 
93
namespace aria2 {
 
94
 
 
95
 
 
96
template<ALG_ID id>
 
97
class MessageDigestBase : public MessageDigestImpl {
 
98
private:
 
99
  HCRYPTHASH hash_;
 
100
  DWORD len_;
 
101
 
 
102
  void destroy() {
 
103
    if (hash_) {
 
104
      ::CryptDestroyHash(hash_);
 
105
      hash_ = 0;
 
106
    }
 
107
  }
 
108
 
 
109
public:
 
110
  MessageDigestBase() : hash_(0), len_(0) { reset(); }
 
111
  virtual ~MessageDigestBase() { destroy(); }
 
112
 
 
113
  static size_t length() {
 
114
    MessageDigestBase<id> rv;
 
115
    return rv.getDigestLength();
 
116
  }
 
117
  virtual size_t getDigestLength() const CXX11_OVERRIDE {
 
118
    return len_;
 
119
  }
 
120
  virtual void reset() CXX11_OVERRIDE {
 
121
    destroy();
 
122
    if (!::CryptCreateHash(context_.get(), id, 0, 0, &hash_)) {
 
123
      throw DL_ABORT_EX("Failed to create hash");
 
124
    }
 
125
    DWORD len = sizeof(len_);
 
126
    if (!::CryptGetHashParam(hash_, HP_HASHSIZE, reinterpret_cast<BYTE*>(&len_),
 
127
                             &len, 0)) {
 
128
      throw DL_ABORT_EX("Failed to initialize hash");
 
129
    }
 
130
  }
 
131
  virtual void update(const void* data, size_t length) CXX11_OVERRIDE {
 
132
    auto bytes = reinterpret_cast<const unsigned char*>(data);
 
133
    while (length) {
 
134
      DWORD l = std::min(length, (size_t)std::numeric_limits<uint32_t>::max());
 
135
      if (!::CryptHashData(hash_, bytes, l, 0)) {
 
136
        throw DL_ABORT_EX("Failed to update hash");
 
137
      }
 
138
      length -= l;
 
139
      bytes += l;
 
140
    }
 
141
  }
 
142
  virtual void digest(unsigned char* md) CXX11_OVERRIDE {
 
143
    DWORD len = len_;
 
144
    if (!::CryptGetHashParam(hash_, HP_HASHVAL, md, &len, 0)) {
 
145
      throw DL_ABORT_EX("Failed to create hash digest");
 
146
    }
 
147
  }
 
148
};
 
149
 
 
150
typedef MessageDigestBase<CALG_MD5> MessageDigestMD5;
 
151
typedef MessageDigestBase<CALG_SHA1> MessageDigestSHA1;
 
152
typedef MessageDigestBase<CALG_SHA_256> MessageDigestSHA256;
 
153
typedef MessageDigestBase<CALG_SHA_384> MessageDigestSHA384;
 
154
typedef MessageDigestBase<CALG_SHA_512> MessageDigestSHA512;
 
155
 
 
156
std::unique_ptr<MessageDigestImpl> MessageDigestImpl::sha1()
 
157
{
 
158
  return std::unique_ptr<MessageDigestImpl>(new MessageDigestSHA1());
 
159
}
 
160
 
 
161
namespace {
 
162
MessageDigestImpl::hashes_t initialize() {
 
163
  MessageDigestImpl::hashes_t rv = {
 
164
    { "sha-1", MessageDigestImpl::make_hi<MessageDigestSHA1>() },
 
165
    { "md5", MessageDigestImpl::make_hi<MessageDigestMD5>() },
 
166
  };
 
167
 
 
168
  try {
 
169
    rv.insert({ "sha-256", MessageDigestImpl::make_hi<MessageDigestSHA256>() });
 
170
  }
 
171
  catch (RecoverableException &ex) {
 
172
    printf("SHA-256 is not supported on this machine");
 
173
  }
 
174
  try {
 
175
    rv.insert({ "sha-384", MessageDigestImpl::make_hi<MessageDigestSHA384>() });
 
176
  }
 
177
  catch (RecoverableException &ex) {
 
178
    printf("SHA-384 is not supported on this machine");
 
179
  }
 
180
  try {
 
181
    rv.insert({ "sha-512", MessageDigestImpl::make_hi<MessageDigestSHA512>() });
 
182
  }
 
183
  catch (RecoverableException &ex) {
 
184
    printf("SHA-512 is not supported on this machine");
 
185
  }
 
186
 
 
187
  return rv;
 
188
};
 
189
} // namespace
 
190
 
 
191
MessageDigestImpl::hashes_t MessageDigestImpl::hashes = initialize();
 
192
 
 
193
} // namespace aria2