GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/crypto/crypto_dsa.cc Lines: 54 66 81.8 %
Date: 2021-04-17 04:11:57 Branches: 26 50 52.0 %

Line Branch Exec Source
1
#include "crypto/crypto_dsa.h"
2
#include "crypto/crypto_keys.h"
3
#include "crypto/crypto_util.h"
4
#include "async_wrap-inl.h"
5
#include "env-inl.h"
6
#include "memory_tracker-inl.h"
7
#include "threadpoolwork-inl.h"
8
#include "v8.h"
9
10
#include <openssl/bn.h>
11
#include <openssl/dsa.h>
12
13
#include <cstdio>
14
15
namespace node {
16
17
using v8::FunctionCallbackInfo;
18
using v8::Int32;
19
using v8::Just;
20
using v8::Local;
21
using v8::Maybe;
22
using v8::Nothing;
23
using v8::Number;
24
using v8::Object;
25
using v8::Uint32;
26
using v8::Value;
27
28
namespace crypto {
29
3
EVPKeyCtxPointer DsaKeyGenTraits::Setup(DsaKeyPairGenConfig* params) {
30
6
  EVPKeyCtxPointer param_ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_DSA, nullptr));
31
3
  EVP_PKEY* raw_params = nullptr;
32
33

9
  if (!param_ctx ||
34

6
      EVP_PKEY_paramgen_init(param_ctx.get()) <= 0 ||
35
3
      EVP_PKEY_CTX_set_dsa_paramgen_bits(
36
          param_ctx.get(),
37
          params->params.modulus_bits) <= 0) {
38
    return EVPKeyCtxPointer();
39
  }
40
41
3
  if (params->params.divisor_bits != -1) {
42
2
    if (EVP_PKEY_CTX_ctrl(
43
            param_ctx.get(),
44
            EVP_PKEY_DSA,
45
            EVP_PKEY_OP_PARAMGEN,
46
            EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS,
47
            params->params.divisor_bits,
48
            nullptr) <= 0) {
49
      return EVPKeyCtxPointer();
50
    }
51
  }
52
53
3
  if (EVP_PKEY_paramgen(param_ctx.get(), &raw_params) <= 0)
54
    return EVPKeyCtxPointer();
55
56
6
  EVPKeyPointer key_params(raw_params);
57
6
  EVPKeyCtxPointer key_ctx(EVP_PKEY_CTX_new(key_params.get(), nullptr));
58
59

3
  if (!key_ctx || EVP_PKEY_keygen_init(key_ctx.get()) <= 0)
60
    return EVPKeyCtxPointer();
61
62
3
  return key_ctx;
63
}
64
65
// Input arguments for DsaKeyPairGenJob
66
//   1. CryptoJobMode
67
//   2. Modulus Bits
68
//   3. Divisor Bits
69
//   4. Public Format
70
//   5. Public Type
71
//   6. Private Format
72
//   7. Private Type
73
//   8. Cipher
74
//   9. Passphrase
75
3
Maybe<bool> DsaKeyGenTraits::AdditionalConfig(
76
    CryptoJobMode mode,
77
    const FunctionCallbackInfo<Value>& args,
78
    unsigned int* offset,
79
    DsaKeyPairGenConfig* params) {
80
3
  Environment* env = Environment::GetCurrent(args);
81
9
  CHECK(args[*offset]->IsUint32());  // modulus bits
82
9
  CHECK(args[*offset + 1]->IsInt32());  // divisor bits
83
84
12
  params->params.modulus_bits = args[*offset].As<Uint32>()->Value();
85
12
  params->params.divisor_bits = args[*offset + 1].As<Int32>()->Value();
86
3
  if (params->params.divisor_bits < -1) {
87
    char msg[1024];
88
    snprintf(msg, sizeof(msg), "invalid value for divisor_bits");
89
    THROW_ERR_OUT_OF_RANGE(env, msg);
90
    return Nothing<bool>();
91
  }
92
93
3
  *offset += 2;
94
95
3
  return Just(true);
96
}
97
98
8
Maybe<bool> DSAKeyExportTraits::AdditionalConfig(
99
    const FunctionCallbackInfo<Value>& args,
100
    unsigned int offset,
101
    DSAKeyExportConfig* params) {
102
8
  return Just(true);
103
}
104
105
8
WebCryptoKeyExportStatus DSAKeyExportTraits::DoExport(
106
    std::shared_ptr<KeyObjectData> key_data,
107
    WebCryptoKeyFormat format,
108
    const DSAKeyExportConfig& params,
109
    ByteSource* out) {
110
8
  CHECK_NE(key_data->GetKeyType(), kKeyTypeSecret);
111
112

8
  switch (format) {
113
    case kWebCryptoKeyFormatRaw:
114
      // Not supported for RSA keys of either type
115
      return WebCryptoKeyExportStatus::FAILED;
116
    case kWebCryptoKeyFormatPKCS8:
117
4
      if (key_data->GetKeyType() != kKeyTypePrivate)
118
        return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
119
4
      return PKEY_PKCS8_Export(key_data.get(), out);
120
    case kWebCryptoKeyFormatSPKI:
121
4
      if (key_data->GetKeyType() != kKeyTypePublic)
122
        return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
123
4
      return PKEY_SPKI_Export(key_data.get(), out);
124
    default:
125
      UNREACHABLE();
126
  }
127
}
128
129
42
Maybe<bool> GetDsaKeyDetail(
130
    Environment* env,
131
    std::shared_ptr<KeyObjectData> key,
132
    Local<Object> target) {
133
  const BIGNUM* p;  // Modulus length
134
  const BIGNUM* q;  // Divisor length
135
136
84
  ManagedEVPPKey m_pkey = key->GetAsymmetricKey();
137
84
  Mutex::ScopedLock lock(*m_pkey.mutex());
138
42
  int type = EVP_PKEY_id(m_pkey.get());
139
42
  CHECK(type == EVP_PKEY_DSA);
140
141
42
  const DSA* dsa = EVP_PKEY_get0_DSA(m_pkey.get());
142
42
  CHECK_NOT_NULL(dsa);
143
144
42
  DSA_get0_pqg(dsa, &p, &q, nullptr);
145
146
42
  size_t modulus_length = BN_num_bytes(p) * CHAR_BIT;
147
42
  size_t divisor_length = BN_num_bytes(q) * CHAR_BIT;
148
149
84
  if (target
150
84
          ->Set(
151
              env->context(),
152
              env->modulus_length_string(),
153
210
              Number::New(env->isolate(), static_cast<double>(modulus_length)))
154

84
          .IsNothing() ||
155
      target
156
84
          ->Set(
157
              env->context(),
158
              env->divisor_length_string(),
159
210
              Number::New(env->isolate(), static_cast<double>(divisor_length)))
160
42
          .IsNothing()) {
161
    return Nothing<bool>();
162
  }
163
164
42
  return Just(true);
165
}
166
167
namespace DSAAlg {
168
4098
void Initialize(Environment* env, Local<Object> target) {
169
4098
  DsaKeyPairGenJob::Initialize(env, target);
170
4098
  DSAKeyExportJob::Initialize(env, target);
171
4098
}
172
}  // namespace DSAAlg
173
}  // namespace crypto
174

14310
}  // namespace node