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

366
}  // namespace node