Skip to content

Commit b39c91f

Browse files
committed
revision for CertificateVerify and Finished in signaturecheme.js
1 parent 90c1f5a commit b39c91f

File tree

4 files changed

+53
-102
lines changed

4 files changed

+53
-102
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tls/enum",
3-
"version": "0.4.4",
3+
"version": "0.4.5",
44
"exports": "./src/mod.ts",
55
"publish": {
66
"exclude": ["dist/"]

src/signaturescheme.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,17 @@ export class SignatureScheme extends Enum {
7676
*/
7777
get Uint16() { return Uint16.fromValue(+this); }
7878

79-
async certificateVerify(clientHelloMsg, serverHelloMsg, encryptedExtensionsMsg, certificateMsg, RSAprivateKey) {
80-
const signature = await signatureFrom(clientHelloMsg, serverHelloMsg, encryptedExtensionsMsg, certificateMsg, RSAprivateKey)
79+
async certificateVerify(clientHelloMsg, serverHelloMsg, encryptedExtensionsMsg, certificateMsg, RSAprivateKey, sha) {
80+
const signature = await signatureFrom(clientHelloMsg, serverHelloMsg, encryptedExtensionsMsg, certificateMsg, RSAprivateKey, sha)
8181
return new CertificateVerify(this, signature)
8282
}
83-
async certificateVerifyMsg(clientHelloMsg, serverHelloMsg, encryptedExtensionsMsg, certificateMsg, RSAprivateKey){
84-
const certificateVerify = await this.certificateVerify(clientHelloMsg, serverHelloMsg, encryptedExtensionsMsg, certificateMsg, RSAprivateKey);
85-
return HandshakeType.CERTIFICATE_VERIFY.handshake(certificateVerify);
86-
}
8783
}
8884

8985
export class CertificateVerify extends Uint8Array {
90-
static from(array) {
86+
static fromMsg(array) {
9187
const copy = Uint8Array.from(array)
92-
const algorithm = SignatureScheme.from(copy);
93-
const signature = Signature.from(copy.subarray(2))
88+
const algorithm = SignatureScheme.from(copy.subarray(4));
89+
const signature = Signature.from(copy.subarray(6))
9490
return new CertificateVerify(algorithm, signature.opaque)
9591
}
9692
constructor(signatureScheme, signature) {
@@ -102,6 +98,7 @@ export class CertificateVerify extends Uint8Array {
10298
super(struct);
10399
this.algorithm = signatureScheme;
104100
this.signature = signature
101+
return HandshakeType.CERTIFICATE_VERIFY.handshake(this);
105102
}
106103
}
107104

@@ -161,8 +158,7 @@ async function signatureFrom(clientHelloMsg, serverHelloMsg, encryptedExtensions
161158
sign,
162159
data
163160
) */
164-
const signature = new Uint8Array(signBuffer)
165-
return signature
161+
return new Uint8Array(signBuffer)
166162
}
167163

168164
export async function finished(finishedKey, sha = 256, ...messages) {
@@ -204,9 +200,14 @@ export async function finished(finishedKey, sha = 256, ...messages) {
204200
}
205201

206202
export class Finished extends Uint8Array {
203+
static fromMsg(message){
204+
const copy = Uint8Array.from(message)
205+
return new Finished(copy.subarray(4))
206+
}
207207
constructor(verify_data){
208208
super(verify_data);
209209
this.verify_data = verify_data
210+
return HandshakeType.FINISHED.handshake(this)
210211
}
211212
}
212213

test/signaturescheme_test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,20 @@ const rsaKey = await crypto.subtle.generateKey(
7979

8080
Deno.test("CertificateVerify", async () => {
8181
const test = await SignatureScheme.RSA_PSS_PSS_SHA256.certificateVerify(clientHelloMsg, serverHelloMsg, encryptedExtensionsMsg, certificateMsg, rsaKey.privateKey)
82-
const back = CertificateVerify.from(test)
82+
const back = CertificateVerify.fromMsg(test)
8383
assertEquals(test.toString(), back.toString())
8484
})
8585

8686

87-
Deno.test("Finished", async ()=>{
88-
const test = await SignatureScheme.RSA_PSS_PSS_SHA256.certificateVerifyMsg(clientHelloMsg, serverHelloMsg, encryptedExtensionsMsg, certificateMsg, rsaKey.privateKey)
87+
Deno.test("Finished", async () => {
88+
const test = await SignatureScheme.RSA_PSS_PSS_SHA256.certificateVerify(clientHelloMsg, serverHelloMsg, encryptedExtensionsMsg, certificateMsg, rsaKey.privateKey)
8989
//const back = CertificateVerify.from(test)
9090
const serverHS_secret_fake = crypto.getRandomValues(new Uint8Array(32));
9191
const _finished = await finished(serverHS_secret_fake, 256, test);
92-
const finishedBack = Finished.from(_finished);
92+
const finishedBack = Finished.fromMsg(_finished);
9393
assertEquals(_finished.toString(), finishedBack.toString())
9494
})
9595

9696

97+
98+

type/signaturescheme.d.ts

Lines changed: 34 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Constrained, Uint16 } from "../src/dep.ts";
22
import { Enum } from "../src/enum.js";
3-
import type { Handshake } from "../src/handshaketype.js";
43

54
/**
65
* Enumeration of signature schemes as defined in RFC 8446.
@@ -62,138 +61,87 @@ export class SignatureScheme extends Enum {
6261
get Uint16(): Uint16;
6362

6463
/**
65-
* Generates a CertificateVerify object.
66-
* @param {Uint8Array} clientHelloMsg - Client Hello message.
67-
* @param {Uint8Array} serverHelloMsg - Server Hello message.
68-
* @param {Uint8Array} encryptedExtensionsMsg - encryptedExtensions message
69-
* @param {Uint8Array} certificateMsg - Certificate message.
70-
* @param {CryptoKey} RSAprivateKey - RSA private key.
71-
* @returns {Promise<CertificateVerify>} CertificateVerify object.
64+
* Creates a CertificateVerify handshake instance.
65+
* @param clientHelloMsg The ClientHello message.
66+
* @param serverHelloMsg The ServerHello message.
67+
* @param encryptedExtensionsMsg The EncryptedExtensions message.
68+
* @param certificateMsg The Certificate message.
69+
* @param RSAprivateKey The RSA private key.
70+
* @param sha The SHA variant (256, 384, or 512).
7271
*/
7372
certificateVerify(
7473
clientHelloMsg: Uint8Array,
7574
serverHelloMsg: Uint8Array,
7675
encryptedExtensionsMsg: Uint8Array,
7776
certificateMsg: Uint8Array,
78-
RSAprivateKey: CryptoKey
77+
RSAprivateKey: CryptoKey,
78+
sha: number,
7979
): Promise<CertificateVerify>;
80-
81-
/**
82-
* Generates a CertificateVerify Handshake object.
83-
* @param {Uint8Array} clientHelloMsg - Client Hello message.
84-
* @param {Uint8Array} serverHelloMsg - Server Hello message.
85-
* @param {Uint8Array} encryptedExtensionsMsg - encryptedExtensions message
86-
* @param {Uint8Array} certificateMsg - Certificate message.
87-
* @param {CryptoKey} RSAprivateKey - RSA private key.
88-
* @returns {Promise<Handshake>} Handshake of CertificateVerify object.
89-
*/
90-
certificateVerifyMsg(
91-
clientHelloMsg: Uint8Array,
92-
serverHelloMsg: Uint8Array,
93-
encryptedExtensionsMsg: Uint8Array,
94-
certificateMsg: Uint8Array,
95-
RSAprivateKey: CryptoKey
96-
): Promise<Handshake>;
9780
}
9881

9982
/**
100-
* Represents a CertificateVerify structure.
83+
* Represents a CertificateVerify message.
10184
*/
102-
export class CertificateVerify extends Uint8Array {
103-
/** The signature algorithm used. */
104-
algorithm: SignatureScheme;
105-
106-
/** The signature. */
107-
signature: Uint8Array;
108-
85+
export declare class CertificateVerify extends Uint8Array {
10986
/**
110-
* Parses a byte array into a CertificateVerify object.
111-
* @param {Uint8Array} array - The byte array to parse.
112-
* @returns {CertificateVerify} The parsed CertificateVerify object.
87+
* Creates a CertificateVerify instance from an array.
88+
* @param array The input array.
11389
*/
114-
static from(array: Uint8Array): CertificateVerify;
90+
static fromMsg(array: Uint8Array): CertificateVerify;
11591

116-
/**
117-
* Constructs a new CertificateVerify object.
118-
* @param {SignatureScheme} signatureScheme - The signature scheme.
119-
* @param {Uint8Array} signature - The signature.
120-
*/
12192
constructor(signatureScheme: SignatureScheme, signature: Uint8Array);
93+
94+
algorithm: SignatureScheme;
95+
signature: Uint8Array;
12296
}
12397

12498
/**
125-
* Represents a constrained signature.
99+
* Represents a constrained Signature.
126100
*/
127-
export class Signature extends Constrained {
128-
/** The raw opaque signature data. */
129-
opaque: Uint8Array;
130-
101+
export declare class Signature extends Constrained {
131102
/**
132-
* Parses a byte array into a Signature object.
133-
* @param {Uint8Array} array - The byte array to parse.
134-
* @returns {Signature} The parsed Signature object.
103+
* Creates a Signature instance from an array.
104+
* @param array The input array.
135105
*/
136106
static from(array: Uint8Array): Signature;
137107

138-
/**
139-
* Constructs a new Signature object.
140-
* @param {Uint8Array} opaque - The raw opaque signature data.
141-
*/
142108
constructor(opaque: Uint8Array);
109+
110+
opaque: Uint8Array;
143111
}
144112

145113
/**
146-
* Generates a signature from the provided handshake messages and an RSA private key.
147-
*
148-
* @param clientHelloMsg - The ClientHello message as a Uint8Array.
149-
* @param serverHelloMsg - The ServerHello message as a Uint8Array.
150-
* @param encryptedExtensionsMsg - The EncryptedExtensions message as a Uint8Array.
151-
* @param certificateMsg - The Certificate message as a Uint8Array.
152-
* @param RSAprivateKey - The RSA private key used for signing.
153-
* @param sha - The hash algorithm to use (256, 384, or 512). Defaults to 256.
154-
* @returns A promise that resolves to a Uint8Array containing the signature. The resulting object also includes the `transcriptHash` property.
114+
* Generates a signature for the CertificateVerify message.
155115
*/
156116
export declare function signatureFrom(
157117
clientHelloMsg: Uint8Array,
158118
serverHelloMsg: Uint8Array,
159119
encryptedExtensionsMsg: Uint8Array,
160120
certificateMsg: Uint8Array,
161121
RSAprivateKey: CryptoKey,
162-
sha?: 256 | 384 | 512
122+
sha?: number,
163123
): Promise<Uint8Array>;
164124

165125
/**
166-
* Computes the Finished message verify_data using the provided finished key and handshake messages.
167-
*
168-
* @param finishedKey - The finished key as a Uint8Array.
169-
* @param sha - The hash algorithm to use (256 or 384). Defaults to 256.
170-
* @param messages - A variable number of handshake messages to include in the transcript hash.
171-
* @returns A promise that resolves to a Finished instance containing the verify_data. The resulting object also includes the `transcriptHash` property.
126+
* Generates a Finished message.
172127
*/
173128
export declare function finished(
174129
finishedKey: Uint8Array,
175-
sha?: 256 | 384,
130+
sha: number,
176131
...messages: Uint8Array[]
177132
): Promise<Finished>;
178133

179-
180134
/**
181-
* Represents the output of the `finished` function.
135+
* Represents a Finished handshake message.
182136
*/
183-
export declare class Finished {
137+
export declare class Finished extends Uint8Array {
184138
/**
185-
* Constructs a Finished instance.
186-
* @param verifyData - The computed verify data.
139+
* Creates a Finished instance from a message.
140+
* @param message The input message.
187141
*/
188-
constructor(verifyData: ArrayBuffer);
142+
static fromMsg(message: Uint8Array): Finished;
189143

190-
/**
191-
* The computed verify data.
192-
*/
193-
verifyData: ArrayBuffer;
144+
constructor(verify_data: Uint8Array);
194145

195-
/**
196-
* The hash of the handshake transcript.
197-
*/
198-
transcriptHash: ArrayBuffer;
146+
verify_data: Uint8Array;
199147
}

0 commit comments

Comments
 (0)