Skip to content

Commit a1d1493

Browse files
committed
revise finished
1 parent 0468f1d commit a1d1493

File tree

4 files changed

+42
-31
lines changed

4 files changed

+42
-31
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.2",
3+
"version": "0.4.3",
44
"exports": "./src/mod.ts",
55
"publish": {
66
"exclude": ["dist/"]

src/signaturescheme.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { Constrained, Struct, Uint16 } from "./dep.ts";
55
import { Enum } from "./enum.js";
6-
import { sha256 } from "@noble/hashes/sha256"
6+
import { sha256, sha384, sha512 } from "@noble/hashes/sha2"
77
import { HandshakeType } from "./handshaketype.js";
88

99
/**
@@ -117,7 +117,7 @@ export class Signature extends Constrained {
117117
}
118118
}
119119

120-
async function signatureFrom(clientHelloMsg, serverHelloMsg, encryptedExtensionsMsg, certificateMsg, RSAprivateKey) {
120+
async function signatureFrom(clientHelloMsg, serverHelloMsg, encryptedExtensionsMsg, certificateMsg, RSAprivateKey, sha = 256) {
121121
const leading = Uint8Array.of(
122122
//NOTE 64 space characters
123123
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
@@ -126,7 +126,12 @@ async function signatureFrom(clientHelloMsg, serverHelloMsg, encryptedExtensions
126126
//NOTE single null char
127127
0
128128
)
129-
const transcriptHash = sha256.create()
129+
130+
const hash = sha == 256 ? sha256.create() :
131+
sha == 384 ? sha384.create() :
132+
sha == 512 ? sha512.create() : sha256.create();
133+
134+
const transcriptHash = hash
130135
.update(clientHelloMsg)
131136
.update(serverHelloMsg)
132137
.update(encryptedExtensionsMsg)
@@ -141,7 +146,7 @@ async function signatureFrom(clientHelloMsg, serverHelloMsg, encryptedExtensions
141146
const signBuffer = await crypto.subtle.sign(
142147
{
143148
name: "RSA-PSS",// RSAprivateKey.algorithm.name,
144-
saltLength: 256 / 8
149+
saltLength: sha / 8
145150
},
146151
RSAprivateKey,
147152
data
@@ -157,25 +162,29 @@ async function signatureFrom(clientHelloMsg, serverHelloMsg, encryptedExtensions
157162
data
158163
) */
159164
const signature = new Uint8Array(signBuffer)
160-
signature.transcriptHash = transcriptHash;
161165
return signature
162166
}
163167

164-
export async function finished(finishedKey, certificateVerifyMsg) {
168+
export async function finished(finishedKey, sha = 256, ...messages) {
165169
//const finishedKey = hkdfExpandLabel(serverHS_secret, 'finished', new Uint8Array, 32);
166170
const finishedKeyCrypto = await crypto.subtle.importKey(
167171
"raw",
168172
finishedKey,
169173
{
170174
name: "HMAC",
171-
hash: { name: "SHA-256" },
175+
hash: { name: `SHA-${sha}` },
172176
},
173177
true,
174178
["sign", "verify"]
175179
);
176-
const transcriptHash = sha256.create()
177-
.update(certificateVerifyMsg.message.signature.transcriptHash)
178-
.update(certificateVerifyMsg.byte)
180+
181+
const hash = sha == 256 ? sha256.create() :
182+
sha == 384 ? sha384.create() : sha256.create();
183+
184+
const messagesStruct = Struct.createFrom(...messages);
185+
186+
const transcriptHash = hash
187+
.update(Uint8Array.from(messagesStruct))
179188
.digest();
180189

181190
const verify_data = await crypto.subtle.sign(

test/signaturescheme_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Deno.test("Finished", async ()=>{
8888
const test = await SignatureScheme.RSA_PSS_PSS_SHA256.certificateVerifyMsg(clientHelloMsg, serverHelloMsg, encryptedExtensionsMsg, certificateMsg, rsaKey.privateKey)
8989
//const back = CertificateVerify.from(test)
9090
const serverHS_secret_fake = crypto.getRandomValues(new Uint8Array(32));
91-
const _finished = await finished(serverHS_secret_fake, test);
91+
const _finished = await finished(serverHS_secret_fake, 256, test);
9292
const finishedBack = Finished.from(_finished);
9393
assertEquals(_finished.toString(), finishedBack.toString())
9494
})

type/signaturescheme.d.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -143,37 +143,39 @@ export class Signature extends Constrained {
143143
}
144144

145145
/**
146-
* Generates a signature from input data and a private RSA key.
147-
* @param {Uint8Array} clientHelloMsg - Client Hello message.
148-
* @param {Uint8Array} serverHelloMsg - Server Hello message.
149-
* @param {Uint8Array} certificateMsg - Certificate message.
150-
* @param {CryptoKey} RSAprivateKey - RSA private key.
151-
* @returns {Promise<Uint8Array>} The generated signature.
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.
152155
*/
153-
export function signatureFrom(
156+
export declare function signatureFrom(
154157
clientHelloMsg: Uint8Array,
155158
serverHelloMsg: Uint8Array,
156159
encryptedExtensionsMsg: Uint8Array,
157160
certificateMsg: Uint8Array,
158-
RSAprivateKey: CryptoKey
161+
RSAprivateKey: CryptoKey,
162+
sha?: 256 | 384 | 512
159163
): Promise<Uint8Array>;
160164

161165
/**
162-
* Verifies and generates the HMAC for the given data.
166+
* Computes the Finished message verify_data using the provided finished key and handshake messages.
163167
*
164-
* @param {Uint8Array} finishedKey - The key used to compute the finished message..
165-
* @param {object} certificateVerifyMsg - The certificate verify message object.
166-
* @param {Uint8Array} certificateVerifyMsg.message.transcriptHash - The transcript hash from the message.
167-
* @returns {Promise<Uint8Array>} A promise that resolves to the verify_data HMAC value as a Uint8Array.
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.
168172
*/
169173
export declare function finished(
170174
finishedKey: Uint8Array,
171-
certificateVerifyMsg: {
172-
message: {
173-
transcriptHash: Uint8Array;
174-
};
175-
}
176-
): Promise<Uint8Array>;
175+
sha?: 256 | 384,
176+
...messages: Uint8Array[]
177+
): Promise<Finished>;
178+
177179

178180
/**
179181
* Represents the output of the `finished` function.

0 commit comments

Comments
 (0)