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_spkac.cc Lines: 61 66 92.4 %
Date: 2020-12-12 04:11:07 Branches: 35 44 79.5 %

Line Branch Exec Source
1
#include "crypto/crypto_spkac.h"
2
#include "crypto/crypto_common.h"
3
#include "crypto/crypto_util.h"
4
#include "allocated_buffer-inl.h"
5
#include "env-inl.h"
6
#include "memory_tracker-inl.h"
7
#include "node.h"
8
#include "v8.h"
9
10
namespace node {
11
12
using v8::FunctionCallbackInfo;
13
using v8::Local;
14
using v8::Object;
15
using v8::Value;
16
17
namespace crypto {
18
namespace SPKAC {
19
10
bool VerifySpkac(const ArrayBufferOrViewContents<char>& input) {
20
  NetscapeSPKIPointer spki(
21
20
      NETSCAPE_SPKI_b64_decode(input.data(), input.size()));
22
10
  if (!spki)
23
2
    return false;
24
25
16
  EVPKeyPointer pkey(X509_PUBKEY_get(spki->spkac->pubkey));
26
8
  if (!pkey)
27
    return false;
28
29
8
  return NETSCAPE_SPKI_verify(spki.get(), pkey.get()) > 0;
30
}
31
32
11
void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
33
11
  Environment* env = Environment::GetCurrent(args);
34
21
  ArrayBufferOrViewContents<char> input(args[0]);
35
11
  if (input.size() == 0)
36
    return args.GetReturnValue().SetEmptyString();
37
38
11
  if (UNLIKELY(!input.CheckSizeInt32()))
39
1
    return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
40
41
30
  args.GetReturnValue().Set(VerifySpkac(input));
42
}
43
44
4
AllocatedBuffer ExportPublicKey(Environment* env,
45
                                const ArrayBufferOrViewContents<char>& input,
46
                                size_t* size) {
47
8
  BIOPointer bio(BIO_new(BIO_s_mem()));
48
4
  if (!bio) return AllocatedBuffer();
49
50
  NetscapeSPKIPointer spki(
51
8
      NETSCAPE_SPKI_b64_decode(input.data(), input.size()));
52
4
  if (!spki) return AllocatedBuffer();
53
54
4
  EVPKeyPointer pkey(NETSCAPE_SPKI_get_pubkey(spki.get()));
55
2
  if (!pkey) return AllocatedBuffer();
56
57
2
  if (PEM_write_bio_PUBKEY(bio.get(), pkey.get()) <= 0)
58
    return AllocatedBuffer();
59
60
  BUF_MEM* ptr;
61
2
  BIO_get_mem_ptr(bio.get(), &ptr);
62
63
2
  *size = ptr->length;
64
4
  AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, *size);
65
2
  memcpy(buf.data(), ptr->data, *size);
66
67
2
  return buf;
68
}
69
70
5
void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
71
5
  Environment* env = Environment::GetCurrent(args);
72
73
7
  ArrayBufferOrViewContents<char> input(args[0]);
74
5
  if (input.size() == 0)
75
    return args.GetReturnValue().SetEmptyString();
76
77
5
  if (UNLIKELY(!input.CheckSizeInt32()))
78
1
    return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
79
80
  size_t pkey_size;
81
6
  AllocatedBuffer pkey = ExportPublicKey(env, input, &pkey_size);
82
4
  if (pkey.data() == nullptr)
83
4
    return args.GetReturnValue().SetEmptyString();
84
85
6
  args.GetReturnValue().Set(pkey.ToBuffer().FromMaybe(Local<Value>()));
86
}
87
88
4
ByteSource ExportChallenge(const ArrayBufferOrViewContents<char>& input) {
89
  NetscapeSPKIPointer sp(
90
8
      NETSCAPE_SPKI_b64_decode(input.data(), input.size()));
91
4
  if (!sp)
92
2
    return ByteSource();
93
94
2
  char* buf = nullptr;
95
  ASN1_STRING_to_UTF8(
96
    reinterpret_cast<unsigned char**>(&buf),
97
2
    sp->spkac->challenge);
98
99
2
  return ByteSource::Allocated(buf, strlen(buf));
100
}
101
102
5
void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
103
5
  Environment* env = Environment::GetCurrent(args);
104
105
7
  ArrayBufferOrViewContents<char> input(args[0]);
106
5
  if (input.size() == 0)
107
    return args.GetReturnValue().SetEmptyString();
108
109
5
  if (UNLIKELY(!input.CheckSizeInt32()))
110
1
    return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
111
112
6
  ByteSource cert = ExportChallenge(input);
113
4
  if (!cert)
114
4
    return args.GetReturnValue().SetEmptyString();
115
116
  Local<Value> outString =
117
2
      Encode(env->isolate(), cert.get(), cert.size(), BUFFER);
118
119
4
  args.GetReturnValue().Set(outString);
120
}
121
122
656
void Initialize(Environment* env, Local<Object> target) {
123
656
  env->SetMethodNoSideEffect(target, "certVerifySpkac", VerifySpkac);
124
656
  env->SetMethodNoSideEffect(target, "certExportPublicKey", ExportPublicKey);
125
656
  env->SetMethodNoSideEffect(target, "certExportChallenge", ExportChallenge);
126
656
}
127
128
}  // namespace SPKAC
129
}  // namespace crypto
130

14073
}  // namespace node