-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87b8648
commit 6292817
Showing
12 changed files
with
2,165 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
forge-std/=lib/forge-std/src/ | ||
forge-std/=lib/forge-std/src/ | ||
openzeppelin-contracts/=lib/openzeppelin-contracts/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.21; | ||
|
||
import {Base64URL} from "./utils/Base64URL.sol"; | ||
import {P256} from "./P256.sol"; | ||
|
||
/** | ||
* Helper library for external contracts to verify WebAuthn signatures. | ||
**/ | ||
library WebAuthn { | ||
function contains( | ||
string memory substr, | ||
string memory str, | ||
uint256 location | ||
) internal pure returns (bool) { | ||
bytes memory substr_bytes = bytes(substr); | ||
bytes memory str_bytes = bytes(str); | ||
|
||
uint256 substr_len = substr_bytes.length; | ||
uint256 str_len = str_bytes.length; | ||
|
||
for (uint256 i = 0; i < substr_len; i++) { | ||
if (location + i >= str_len) { | ||
return false; | ||
} | ||
|
||
if (substr_bytes[i] != str_bytes[location + i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
function checkAuthFlags( | ||
bytes1 flags, | ||
bool requireUserVerification | ||
) internal pure returns (bool) { | ||
// Check the UP bit | ||
if (flags & 0x01 != 0x01) { | ||
return false; | ||
} | ||
|
||
// Check the UV bit | ||
if (requireUserVerification && (flags & 0x04) != 0x04) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function verifySignature( | ||
bytes memory challenge, | ||
bytes memory authenticatorData, | ||
bool requireUserVerification, | ||
string memory clientDataJSON, | ||
uint256 challengeLocation, | ||
uint256 r, | ||
uint256 s, | ||
uint256 x, | ||
uint256 y | ||
) public view returns (bool) { | ||
// Check that authenticatorData has good flags | ||
if (!checkAuthFlags(authenticatorData[32], requireUserVerification)) { | ||
return false; | ||
} | ||
|
||
// Check that challenge is in the clientDataJSON | ||
string memory challenge_b64url = Base64URL.encode(challenge); | ||
string memory challenge_property = string.concat( | ||
'"challenge":"', | ||
challenge_b64url, | ||
'"' | ||
); | ||
|
||
if (!contains(challenge_property, clientDataJSON, challengeLocation)) { | ||
return false; | ||
} | ||
|
||
bytes32 clientDataJSON_hash = sha256(bytes(clientDataJSON)); | ||
bytes32 message_hash = sha256( | ||
abi.encodePacked(authenticatorData, clientDataJSON_hash) | ||
); | ||
|
||
return P256.verifySignature(message_hash, r, s, x, y); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.21; | ||
|
||
import "openzeppelin-contracts/contracts/utils/Base64.sol"; | ||
|
||
library Base64URL { | ||
function encode(bytes memory data) public pure returns (string memory) { | ||
string memory strb64 = Base64.encode(data); | ||
bytes memory b64 = bytes(strb64); | ||
|
||
// count and ignore all "=" symbols from the end of the string | ||
uint256 equalsCount = 0; | ||
for (int256 i = int256(b64.length) - 1; i >= 0; i--) { | ||
if (b64[uint256(i)] == "=") { | ||
equalsCount++; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
uint256 len = b64.length - equalsCount; | ||
bytes memory result = new bytes(len); | ||
|
||
for (uint256 i = 0; i < len; i++) { | ||
if (b64[i] == "+") { | ||
result[i] = "-"; | ||
} else if (b64[i] == "/") { | ||
result[i] = "_"; | ||
} else { | ||
result[i] = b64[i]; | ||
} | ||
} | ||
|
||
return string(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import fetch from "cross-fetch"; | ||
import fs from "fs"; | ||
import { base64urlnopad } from "@scure/base"; | ||
|
||
const scureVectorsURL = | ||
"https://github.com/paulmillr/scure-base/raw/main/test/vectors/base_vectors.json"; | ||
|
||
type ScureVector = { | ||
fn_name: string; | ||
data: string; | ||
exp: string; | ||
}; | ||
|
||
// Fetch scure's test vectors and filter out base64url | ||
async function main() { | ||
const sourceObj: { v: ScureVector[] } = await fetch(scureVectorsURL).then( | ||
(res) => res.json() | ||
); | ||
const vectors = sourceObj.v.filter( | ||
(v: ScureVector) => v.fn_name === "base64url" | ||
); | ||
|
||
// Write to JSON | ||
const filepath = "./vectors_scure_base64url.jsonl"; | ||
console.log(`Writing ${vectors.length} vectors to ${filepath}`); | ||
const lines = vectors.map((v) => { | ||
const data = Uint8Array.from(Buffer.from(v.data, "hex")); | ||
return JSON.stringify({ | ||
data: v.data, | ||
exp: base64urlnopad.encode(data), | ||
}); | ||
}); | ||
fs.writeFileSync(filepath, lines.join("\n")); | ||
} | ||
|
||
main() | ||
.then(() => console.log("Done")) | ||
.catch((err) => console.error(err)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.