GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_spkac.cc Lines: 61 64 95.3 %
Date: 2021-11-16 04:13:57 Branches: 25 32 78.1 %

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