-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
/
Copy pathcrypto_spkac.cc
90 lines (72 loc) Β· 2.7 KB
/
crypto_spkac.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "crypto/crypto_spkac.h"
#include "crypto/crypto_common.h"
#include "crypto/crypto_util.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
#include "ncrypto.h"
#include "node.h"
#include "string_bytes.h"
#include "v8.h"
namespace node {
using ncrypto::BIOPointer;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::Local;
using v8::Object;
using v8::Value;
namespace crypto {
namespace SPKAC {
void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
ArrayBufferOrViewContents<char> input(args[0]);
if (input.empty()) return args.GetReturnValue().SetEmptyString();
if (!input.CheckSizeInt32()) [[unlikely]]
return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
args.GetReturnValue().Set(ncrypto::VerifySpkac(input.data(), input.size()));
}
void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
ArrayBufferOrViewContents<char> input(args[0]);
if (input.empty()) return args.GetReturnValue().SetEmptyString();
if (!input.CheckSizeInt32()) [[unlikely]]
return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
BIOPointer bio = ncrypto::ExportPublicKey(input.data(), input.size());
if (!bio) return args.GetReturnValue().SetEmptyString();
auto pkey = ByteSource::FromBIO(bio);
args.GetReturnValue().Set(pkey.ToBuffer(env).FromMaybe(Local<Value>()));
}
void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
ArrayBufferOrViewContents<char> input(args[0]);
if (input.empty()) return args.GetReturnValue().SetEmptyString();
if (!input.CheckSizeInt32()) [[unlikely]] {
return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
}
auto cert = ByteSource::Allocated(
ncrypto::ExportChallenge(input.data(), input.size()));
if (!cert) {
return args.GetReturnValue().SetEmptyString();
}
Local<Value> outString;
if (StringBytes::Encode(
env->isolate(), cert.data<char>(), cert.size(), BUFFER)
.ToLocal(&outString)) {
args.GetReturnValue().Set(outString);
}
}
void Initialize(Environment* env, Local<Object> target) {
Local<Context> context = env->context();
SetMethodNoSideEffect(context, target, "certVerifySpkac", VerifySpkac);
SetMethodNoSideEffect(
context, target, "certExportPublicKey", ExportPublicKey);
SetMethodNoSideEffect(
context, target, "certExportChallenge", ExportChallenge);
}
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(VerifySpkac);
registry->Register(ExportPublicKey);
registry->Register(ExportChallenge);
}
} // namespace SPKAC
} // namespace crypto
} // namespace node