Skip to content
File

Blob: src/node/internal/crypto_spkac.ts

typescript90 lines
1// Copyright (c) 2017-2022 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4//
5// Copyright Joyent, Inc. and other Node contributors.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a
8// copy of this software and associated documentation files (the
9// "Software"), to deal in the Software without restriction, including
10// without limitation the rights to use, copy, modify, merge, publish,
11// distribute, sublicense, and/or sell copies of the Software, and to permit
12// persons to whom the Software is furnished to do so, subject to the
13// following conditions:
14//
15// The above copyright notice and this permission notice shall be included
16// in all copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
21// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24// USE OR OTHER DEALINGS IN THE SOFTWARE.
25 
26import { default as cryptoImpl } from 'node-internal:crypto';
27import { Buffer } from 'node-internal:internal_buffer';
28 
29import { getArrayBufferOrView } from 'node-internal:crypto_util';
30 
31export function verifySpkac(
32 spkac: Buffer | ArrayBuffer | ArrayBufferView | string,
33 encoding?: string
34): boolean {
35 return cryptoImpl.verifySpkac(getArrayBufferOrView(spkac, 'spkac', encoding));
36}
37 
38export function exportPublicKey(
39 spkac: Buffer | ArrayBuffer | ArrayBufferView | string,
40 encoding?: string
41): Buffer {
42 const ret = cryptoImpl.exportPublicKey(
43 getArrayBufferOrView(spkac, 'spkac', encoding)
44 );
45 return ret ? Buffer.from(ret) : Buffer.alloc(0);
46}
47 
48export function exportChallenge(
49 spkac: Buffer | ArrayBuffer | ArrayBufferView | string,
50 encoding?: string
51): Buffer {
52 const ret = cryptoImpl.exportChallenge(
53 getArrayBufferOrView(spkac, 'spkac', encoding)
54 );
55 return ret ? Buffer.from(ret) : Buffer.alloc(0);
56}
57 
58// The legacy implementation of this exposed the Certificate
59// object and required that users create an instance before
60// calling the member methods. This API pattern has been
61// deprecated, however, as the method implementations do not
62// rely on any object state.
63 
64export declare class Certificate {
65 constructor();
66 verifySpkac: typeof verifySpkac;
67 exportPublicKey: typeof exportPublicKey;
68 exportChallenge: typeof exportChallenge;
69 static verifySpkac: typeof verifySpkac;
70 static exportPublicKey: typeof exportPublicKey;
71 static exportChallenge: typeof exportChallenge;
72}
73 
74// For backwards compatibility reasons, this cannot be converted into a
75// ES6 Class.
76export function Certificate(this: unknown): Certificate {
77 if (!(this instanceof Certificate)) {
78 return new Certificate();
79 }
80 return this;
81}
82 
83Certificate.prototype.verifySpkac = verifySpkac;
84Certificate.prototype.exportPublicKey = exportPublicKey;
85Certificate.prototype.exportChallenge = exportChallenge;
86 
87Certificate.exportChallenge = exportChallenge;
88Certificate.exportPublicKey = exportPublicKey;
89Certificate.verifySpkac = verifySpkac;