Upload 77 files
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitignore +4 -0
- README.md +116 -3
- index.js +249 -0
- package-lock.json +0 -0
- package.json +18 -0
- phase_4_proposal.md +45 -0
- public/index.html +58 -0
- public/playground.html +108 -0
- public/portfolio.html +260 -0
- public/vendor/jose/index.js +32 -0
- public/vendor/jose/jwe/compact/decrypt.js +27 -0
- public/vendor/jose/jwe/compact/encrypt.js +27 -0
- public/vendor/jose/jwe/flattened/decrypt.js +165 -0
- public/vendor/jose/jwe/flattened/encrypt.js +169 -0
- public/vendor/jose/jwe/general/decrypt.js +31 -0
- public/vendor/jose/jwe/general/encrypt.js +187 -0
- public/vendor/jose/jwk/embedded.js +17 -0
- public/vendor/jose/jwk/thumbprint.js +68 -0
- public/vendor/jose/jwks/local.js +119 -0
- public/vendor/jose/jwks/remote.js +179 -0
- public/vendor/jose/jws/compact/sign.js +18 -0
- public/vendor/jose/jws/compact/verify.js +21 -0
- public/vendor/jose/jws/flattened/sign.js +92 -0
- public/vendor/jose/jws/flattened/verify.js +120 -0
- public/vendor/jose/jws/general/sign.js +73 -0
- public/vendor/jose/jws/general/verify.js +24 -0
- public/vendor/jose/jwt/decrypt.js +23 -0
- public/vendor/jose/jwt/encrypt.js +108 -0
- public/vendor/jose/jwt/sign.js +52 -0
- public/vendor/jose/jwt/unsecured.js +63 -0
- public/vendor/jose/jwt/verify.js +15 -0
- public/vendor/jose/key/export.js +11 -0
- public/vendor/jose/key/generate_key_pair.js +97 -0
- public/vendor/jose/key/generate_secret.js +40 -0
- public/vendor/jose/key/import.js +57 -0
- public/vendor/jose/lib/aesgcmkw.js +16 -0
- public/vendor/jose/lib/aeskw.js +25 -0
- public/vendor/jose/lib/asn1.js +243 -0
- public/vendor/jose/lib/base64.js +22 -0
- public/vendor/jose/lib/buffer_utils.js +43 -0
- public/vendor/jose/lib/cek.js +19 -0
- public/vendor/jose/lib/check_cek_length.js +7 -0
- public/vendor/jose/lib/check_iv_length.js +7 -0
- public/vendor/jose/lib/check_key_length.js +8 -0
- public/vendor/jose/lib/check_key_type.js +122 -0
- public/vendor/jose/lib/crypto_key.js +143 -0
- public/vendor/jose/lib/decrypt.js +106 -0
- public/vendor/jose/lib/decrypt_key_management.js +127 -0
- public/vendor/jose/lib/digest.js +4 -0
- public/vendor/jose/lib/ecdhes.js +52 -0
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
node_modules
|
| 3 |
+
server.log
|
| 4 |
+
keys.json
|
README.md
CHANGED
|
@@ -1,3 +1,116 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# microipfs: The Permanent Record as a Service
|
| 2 |
+
|
| 3 |
+
> A scalable microservice for creating permanent, verifiable academic records on IPFS.
|
| 4 |
+
|
| 5 |
+
This project provides a suite of tools for educational institutions to transition to a model of student-owned, verifiable digital records. It acts as a "Permanent Record as a Service," leveraging the power of IPFS for immutable storage and Verifiable Credentials for cryptographic proof.
|
| 6 |
+
|
| 7 |
+
# Core Features
|
| 8 |
+
|
| 9 |
+
1. **Batch Archiving (`/batch-archive`):** A scalable endpoint for institutions to archive large volumes of student work (e.g., end-of-semester projects) to IPFS. It returns a single, easily-managed root CID for the entire batch.
|
| 10 |
+
2. **Verifiable Credentials with IPFS Proof (`/issue-credential`):** A system for issuing tamper-proof academic credentials (e.g., for a diploma or a specific skill) that are cryptographically linked to the underlying student work stored on IPFS.
|
| 11 |
+
3. **Student-Owned Identity & Data:** The platform includes reference implementations for a student-run digital wallet (`/portfolio.html`) with client-side encryption, and a public verifier (`/verify.html`) that can perform "deep verification" of a credential and its linked proof of work.
|
| 12 |
+
4. **Learning Object Repository (`/add-learning-object`):** A tool for creating permanent, resilient archives of open educational resources.
|
| 13 |
+
5. **AI Model Playground (`/playground.html`):** An interactive tool for running small AI/ML models directly in the browser from IPFS.
|
| 14 |
+
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# Installation and Setup
|
| 18 |
+
|
| 19 |
+
## 1. Install Dependencies
|
| 20 |
+
```bash
|
| 21 |
+
npm install
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
## 2. Configure Environment
|
| 25 |
+
Create a `.env` file in the root of the project and add your `NFT_STORAGE_KEY`.
|
| 26 |
+
```
|
| 27 |
+
NFT_STORAGE_KEY=<YOUR_API_KEY>
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
## 3. Run the Server
|
| 31 |
+
```bash
|
| 32 |
+
node index.js
|
| 33 |
+
```
|
| 34 |
+
The server will start, and if `keys.json` is not found, it will generate a new key pair for signing Verifiable Credentials.
|
| 35 |
+
|
| 36 |
+
---
|
| 37 |
+
|
| 38 |
+
# API Reference
|
| 39 |
+
|
| 40 |
+
## 1. Batch Archive
|
| 41 |
+
|
| 42 |
+
A scalable endpoint for creating a permanent archive of multiple files in a single transaction.
|
| 43 |
+
|
| 44 |
+
**Endpoint:** `POST /batch-archive`
|
| 45 |
+
|
| 46 |
+
**Body (JSON):** An object with an `items` property, which is an array of objects. Each object must have a `url` (the URL of the content to pin) and can have an optional `metadata` object.
|
| 47 |
+
```json
|
| 48 |
+
{
|
| 49 |
+
"items": [
|
| 50 |
+
{
|
| 51 |
+
"url": "https://example.com/student-a/thesis.pdf",
|
| 52 |
+
"metadata": { "studentId": "123", "title": "My Final Thesis" }
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"url": "https://example.com/student-b/project.zip",
|
| 56 |
+
"metadata": { "studentId": "456", "title": "Capstone Project" }
|
| 57 |
+
}
|
| 58 |
+
]
|
| 59 |
+
}
|
| 60 |
+
```
|
| 61 |
+
The endpoint returns a single `manifestCid` for the entire batch.
|
| 62 |
+
|
| 63 |
+
## 2. Issue Verifiable Credential
|
| 64 |
+
|
| 65 |
+
Issues a W3C-compliant Verifiable Credential (VC) as a JWT.
|
| 66 |
+
|
| 67 |
+
**Endpoint:** `POST /issue-credential`
|
| 68 |
+
|
| 69 |
+
**Body (JSON):**
|
| 70 |
+
* `studentDid` (string): The Decentralized Identifier of the student.
|
| 71 |
+
* `claims` (object): A JSON object containing the claims to be included in the credential (e.g., `{ "degree": "Bachelor of Science" }`).
|
| 72 |
+
* `proofOfWorkCID` (string, optional): The IPFS CID of a manifest file (e.g., from a batch archive) that contains the evidence supporting this credential. This enables "deep verification."
|
| 73 |
+
|
| 74 |
+
The server signs the credential with its private key. The public key is available at `/.well-known/jwks.json` for verifiers.
|
| 75 |
+
|
| 76 |
+
## 3. Pin Learning Object
|
| 77 |
+
|
| 78 |
+
Pins a single educational resource with rich metadata.
|
| 79 |
+
|
| 80 |
+
**Endpoint:** `POST /add-learning-object` (multipart/form-data)
|
| 81 |
+
|
| 82 |
+
**Fields:** `files`, `title`, `author`, `subject`, `gradeLevel`, `license`, `description`.
|
| 83 |
+
|
| 84 |
+
## 4. Pin Encrypted Object (Student Portfolio)
|
| 85 |
+
|
| 86 |
+
Stores a pre-encrypted blob of data. The server has no knowledge of the contents.
|
| 87 |
+
|
| 88 |
+
**Endpoint:** `POST /add-encrypted-object` (multipart/form-data)
|
| 89 |
+
|
| 90 |
+
**Fields:** `file`
|
| 91 |
+
|
| 92 |
+
## 5. Check CID Status
|
| 93 |
+
|
| 94 |
+
Checks the pinning status of a CID on NFT.storage.
|
| 95 |
+
|
| 96 |
+
**Endpoint:** `GET /status/:cid`
|
| 97 |
+
|
| 98 |
+
---
|
| 99 |
+
|
| 100 |
+
# Future Vision: DIDs in K-12 Education
|
| 101 |
+
|
| 102 |
+
The question of how a K-12 school in the U.S. could issue DIDs requires a thoughtful approach that balances state-level educational requirements with federal privacy laws. A successful implementation would not be a single, monolithic system, but a federated trust network.
|
| 103 |
+
|
| 104 |
+
## A Potential Roadmap
|
| 105 |
+
|
| 106 |
+
1. **Adopt the `did:web` Method:** This is the most practical starting point. Each school district could host their DID documents on their own web servers, making their DIDs resolvable via standard HTTPS. This avoids the need for a custom blockchain and leverages existing, trusted infrastructure. A school's DID might look like `did:web:bostonschools.org:students:12345`.
|
| 107 |
+
|
| 108 |
+
2. **State-Level Trust Registries:** Each state's Department of Public Instruction (DPI) could maintain a cryptographically-verifiable list of all accredited school districts in their state. This registry would essentially be a "DID of DIDs," allowing anyone to confirm that a particular school district is a legitimate issuer of student credentials.
|
| 109 |
+
|
| 110 |
+
3. **Cross-State Interoperability:** A national body, perhaps facilitated by the U.S. Department of Education, could maintain a registry of all state DPIs. This would create a chain of trust from the federal level down to the individual school district, allowing a university in California to verify a credential from a high school in Massachusetts.
|
| 111 |
+
|
| 112 |
+
## Key Considerations
|
| 113 |
+
|
| 114 |
+
* **Privacy (FERPA & COPPA):** Verifiable Credentials allow for "selective disclosure." A student could prove they are over 13 without revealing their exact birthdate, or prove they are a student at a particular school without revealing their student ID number. This is a significant privacy enhancement over traditional, all-or-nothing ID cards.
|
| 115 |
+
* **Key Management for Minors:** For younger students, the school or district could act as a "custodial wallet," managing the student's private keys. As students get older, they could be taught to manage their own keys, perhaps in a dedicated digital literacy course.
|
| 116 |
+
* **Verifiable Credentials for Everything:** This system could be used for more than just identity. It could be used to issue verifiable credentials for transcripts, attendance records, and even individual skills or competencies, creating a rich, student-owned "learning ledger."
|
index.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
require('dotenv').config()
|
| 2 |
+
var cors = require('cors')
|
| 3 |
+
var { I } = require('ipfsio')
|
| 4 |
+
var express = require('express')
|
| 5 |
+
var multer = require('multer')
|
| 6 |
+
const fs = require('fs')
|
| 7 |
+
const fsp = require('fs').promises
|
| 8 |
+
const { randomUUID } = require('crypto');
|
| 9 |
+
const axios = require('axios')
|
| 10 |
+
const jose = require('jose')
|
| 11 |
+
var app = express()
|
| 12 |
+
var i = new I(process.env.NFT_STORAGE_KEY)
|
| 13 |
+
|
| 14 |
+
let institutionKeyPair;
|
| 15 |
+
const KEY_FILE = 'keys.json';
|
| 16 |
+
|
| 17 |
+
async function initializeKeys() {
|
| 18 |
+
try {
|
| 19 |
+
if (fs.existsSync(KEY_FILE)) {
|
| 20 |
+
const keyFileContent = await fsp.readFile(KEY_FILE, 'utf-8');
|
| 21 |
+
const jwk = JSON.parse(keyFileContent);
|
| 22 |
+
institutionKeyPair = {
|
| 23 |
+
publicKey: await jose.importJWK(jwk.publicKey, 'ES256'),
|
| 24 |
+
privateKey: await jose.importJWK(jwk.privateKey, 'ES256'),
|
| 25 |
+
};
|
| 26 |
+
console.log('Loaded institutional key pair from file.');
|
| 27 |
+
} else {
|
| 28 |
+
const { publicKey, privateKey } = await jose.generateKeyPair('ES256');
|
| 29 |
+
institutionKeyPair = { publicKey, privateKey };
|
| 30 |
+
const jwk = {
|
| 31 |
+
publicKey: await jose.exportJWK(publicKey),
|
| 32 |
+
privateKey: await jose.exportJWK(privateKey),
|
| 33 |
+
};
|
| 34 |
+
await fsp.writeFile(KEY_FILE, JSON.stringify(jwk, null, 2));
|
| 35 |
+
console.log('Generated new institutional key pair and saved to file.');
|
| 36 |
+
}
|
| 37 |
+
} catch (error) {
|
| 38 |
+
console.error('Failed to initialize keys:', error);
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
initializeKeys();
|
| 43 |
+
|
| 44 |
+
const recentUploads = []
|
| 45 |
+
const MAX_RECENT_UPLOADS = 10
|
| 46 |
+
|
| 47 |
+
const UPLOAD_DIR = 'uploads/'
|
| 48 |
+
if (!fs.existsSync(UPLOAD_DIR)) {
|
| 49 |
+
fs.mkdirSync(UPLOAD_DIR)
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
const allowed = (process.env.ALLOWED ? process.env.ALLOWED.split(",") : [])
|
| 53 |
+
const port = (process.env.PORT ? process.env.PORT : 3000)
|
| 54 |
+
const issuerDid = process.env.ISSUER_DID || 'did:web:example.com';
|
| 55 |
+
const storage = multer.diskStorage({
|
| 56 |
+
destination: function (req, file, cb) {
|
| 57 |
+
cb(null, UPLOAD_DIR)
|
| 58 |
+
},
|
| 59 |
+
filename: function (req, file, cb) {
|
| 60 |
+
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
|
| 61 |
+
cb(null, file.fieldname + '-' + uniqueSuffix)
|
| 62 |
+
}
|
| 63 |
+
})
|
| 64 |
+
const upload = multer({
|
| 65 |
+
storage: storage,
|
| 66 |
+
limits: {
|
| 67 |
+
fileSize: 1024 * 1024 * 5, // 5MB
|
| 68 |
+
files: 10
|
| 69 |
+
}
|
| 70 |
+
})
|
| 71 |
+
app.use(express.json())
|
| 72 |
+
app.use(express.static('public'))
|
| 73 |
+
if (allowed && allowed.length > 0) {
|
| 74 |
+
app.use(cors({ origin: allowed }))
|
| 75 |
+
} else {
|
| 76 |
+
app.use(cors())
|
| 77 |
+
}
|
| 78 |
+
app.use(express.urlencoded({ extended: true }));
|
| 79 |
+
app.post('/add', async (req, res) => {
|
| 80 |
+
let cid;
|
| 81 |
+
if (req.body.url) {
|
| 82 |
+
cid = await i.url(req.body.url)
|
| 83 |
+
} else if (req.body.object) {
|
| 84 |
+
cid = await i.object(req.body.object)
|
| 85 |
+
}
|
| 86 |
+
console.log("cid", cid)
|
| 87 |
+
res.json({ success: cid })
|
| 88 |
+
})
|
| 89 |
+
app.get('/.well-known/jwks.json', async (req, res) => {
|
| 90 |
+
if (!institutionKeyPair || !institutionKeyPair.publicKey) {
|
| 91 |
+
return res.status(500).json({ error: 'Key pair not generated yet.' });
|
| 92 |
+
}
|
| 93 |
+
const jwk = await jose.exportJWK(institutionKeyPair.publicKey);
|
| 94 |
+
res.json({ keys: [jwk] });
|
| 95 |
+
});
|
| 96 |
+
|
| 97 |
+
app.post('/issue-credential', async (req, res) => {
|
| 98 |
+
if (!institutionKeyPair || !institutionKeyPair.privateKey) {
|
| 99 |
+
return res.status(500).json({ error: 'Key pair not generated yet.' });
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
const { studentDid, proofOfWorkCID, claims } = req.body;
|
| 103 |
+
if (!studentDid || !claims) {
|
| 104 |
+
return res.status(400).json({ error: 'Missing studentDid or claims.' });
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
try {
|
| 108 |
+
const vcPayload = {
|
| 109 |
+
'@context': [
|
| 110 |
+
'https://www.w3.org/2018/credentials/v1',
|
| 111 |
+
'https://www.w3.org/2018/credentials/examples/v1'
|
| 112 |
+
],
|
| 113 |
+
id: `urn:uuid:${randomUUID()}`,
|
| 114 |
+
type: ['VerifiableCredential', 'AcademicCredential'],
|
| 115 |
+
issuer: issuerDid,
|
| 116 |
+
issuanceDate: new Date().toISOString(),
|
| 117 |
+
credentialSubject: {
|
| 118 |
+
id: studentDid,
|
| 119 |
+
proofOfWorkCID: proofOfWorkCID,
|
| 120 |
+
...claims
|
| 121 |
+
}
|
| 122 |
+
};
|
| 123 |
+
|
| 124 |
+
const jwt = await new jose.SignJWT(vcPayload)
|
| 125 |
+
.setProtectedHeader({ alg: 'ES256' })
|
| 126 |
+
.setIssuedAt()
|
| 127 |
+
.setIssuer(issuerDid)
|
| 128 |
+
.setSubject(studentDid)
|
| 129 |
+
.sign(institutionKeyPair.privateKey);
|
| 130 |
+
|
| 131 |
+
res.json({ vcJwt: jwt });
|
| 132 |
+
} catch (error) {
|
| 133 |
+
console.error('Failed to issue credential:', error);
|
| 134 |
+
res.status(500).json({ error: 'Failed to issue credential.' });
|
| 135 |
+
}
|
| 136 |
+
});
|
| 137 |
+
app.post('/batch-archive', async (req, res) => {
|
| 138 |
+
const { items } = req.body; // Expects an array of objects with { url, metadata }
|
| 139 |
+
|
| 140 |
+
if (!items || !Array.isArray(items) || items.length === 0) {
|
| 141 |
+
return res.status(400).json({ error: 'Invalid or empty "items" array in request body.' });
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
try {
|
| 145 |
+
const processingPromises = items.map(async (item) => {
|
| 146 |
+
const cid = await i.url(item.url);
|
| 147 |
+
return { cid, metadata: item.metadata };
|
| 148 |
+
});
|
| 149 |
+
|
| 150 |
+
const processedItems = await Promise.all(processingPromises);
|
| 151 |
+
|
| 152 |
+
const manifest = {
|
| 153 |
+
archiveDate: new Date().toISOString(),
|
| 154 |
+
items: processedItems,
|
| 155 |
+
};
|
| 156 |
+
|
| 157 |
+
const manifestCid = await i.object(manifest);
|
| 158 |
+
res.json({ success: true, manifestCid });
|
| 159 |
+
|
| 160 |
+
} catch (error) {
|
| 161 |
+
console.error('Batch archive failed:', error);
|
| 162 |
+
res.status(500).json({ error: 'Failed to process batch archive.' });
|
| 163 |
+
}
|
| 164 |
+
});
|
| 165 |
+
app.get('/recent', (req, res) => {
|
| 166 |
+
res.json(recentUploads)
|
| 167 |
+
})
|
| 168 |
+
|
| 169 |
+
app.get('/status/:cid', async (req, res) => {
|
| 170 |
+
const { cid } = req.params
|
| 171 |
+
try {
|
| 172 |
+
const response = await axios.get(`https://api.nft.storage/check/${cid}`, {
|
| 173 |
+
headers: {
|
| 174 |
+
'Authorization': `Bearer ${process.env.NFT_STORAGE_KEY}`
|
| 175 |
+
}
|
| 176 |
+
})
|
| 177 |
+
res.json(response.data)
|
| 178 |
+
} catch (error) {
|
| 179 |
+
if (error.response) {
|
| 180 |
+
res.status(error.response.status).json({ error: error.response.data })
|
| 181 |
+
} else {
|
| 182 |
+
res.status(500).json({ error: 'Failed to check CID status' })
|
| 183 |
+
}
|
| 184 |
+
}
|
| 185 |
+
})
|
| 186 |
+
|
| 187 |
+
app.post('/add-encrypted-object', upload.single('file'), async (req, res) => {
|
| 188 |
+
const file = req.file
|
| 189 |
+
if (!file) {
|
| 190 |
+
return res.status(400).json({ error: 'No file uploaded' })
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
try {
|
| 194 |
+
const cid = await i.path(file.path)
|
| 195 |
+
res.json({ success: cid })
|
| 196 |
+
} catch (error) {
|
| 197 |
+
console.error('Failed to pin encrypted object:', error)
|
| 198 |
+
res.status(500).json({ error: 'Failed to pin encrypted object' })
|
| 199 |
+
} finally {
|
| 200 |
+
// Clean up the temporary file
|
| 201 |
+
await fsp.unlink(file.path)
|
| 202 |
+
}
|
| 203 |
+
})
|
| 204 |
+
|
| 205 |
+
app.post('/add-learning-object', upload.array('files'), async (req, res) => {
|
| 206 |
+
const files = req.files
|
| 207 |
+
if (!files || files.length === 0) {
|
| 208 |
+
return res.status(400).json({ error: 'No files uploaded' })
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
const { title, author, subject, gradeLevel, license, description } = req.body
|
| 212 |
+
|
| 213 |
+
try {
|
| 214 |
+
const cids = []
|
| 215 |
+
for (const file of files) {
|
| 216 |
+
const cid = await i.path(file.path)
|
| 217 |
+
cids.push({
|
| 218 |
+
filename: file.originalname,
|
| 219 |
+
cid: cid
|
| 220 |
+
})
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
const manifest = {
|
| 224 |
+
title: title || 'Untitled Learning Object',
|
| 225 |
+
author: author || 'Unknown',
|
| 226 |
+
subject: subject || 'Uncategorized',
|
| 227 |
+
gradeLevel: gradeLevel || 'N/A',
|
| 228 |
+
license: license || 'CC-BY-4.0',
|
| 229 |
+
description: description || '',
|
| 230 |
+
files: cids
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
const manifestCid = await i.object(manifest)
|
| 234 |
+
|
| 235 |
+
recentUploads.unshift(manifestCid)
|
| 236 |
+
if (recentUploads.length > MAX_RECENT_UPLOADS) {
|
| 237 |
+
recentUploads.pop()
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
res.json({ success: manifestCid })
|
| 241 |
+
} catch (error) {
|
| 242 |
+
console.error('Failed to process dataset:', error)
|
| 243 |
+
res.status(500).json({ error: 'Failed to process dataset' })
|
| 244 |
+
} finally {
|
| 245 |
+
const unlinkPromises = files.map(file => fsp.unlink(file.path))
|
| 246 |
+
await Promise.all(unlinkPromises)
|
| 247 |
+
}
|
| 248 |
+
})
|
| 249 |
+
app.listen(port)
|
package-lock.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "microipfs",
|
| 3 |
+
"version": "0.0.1",
|
| 4 |
+
"description": "dead simple microservice for pinning NFT related files to IPFS",
|
| 5 |
+
"main": "index.js",
|
| 6 |
+
"author": "skogard",
|
| 7 |
+
"license": "MIT",
|
| 8 |
+
"dependencies": {
|
| 9 |
+
"axios": "^1.13.2",
|
| 10 |
+
"cors": "^2.8.5",
|
| 11 |
+
"dotenv": "^10.0.0",
|
| 12 |
+
"express": "^4.17.2",
|
| 13 |
+
"ipfsio": "^0.0.8",
|
| 14 |
+
"jose": "^6.1.3",
|
| 15 |
+
"multer": "^2.0.1"
|
| 16 |
+
},
|
| 17 |
+
"devDependencies": {}
|
| 18 |
+
}
|
phase_4_proposal.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Proposal: Phase 4 — The Verifiable Student
|
| 2 |
+
|
| 3 |
+
## 1. Vision: From a Private Portfolio to a Verifiable Record
|
| 4 |
+
|
| 5 |
+
Phase 2 of this project gave students a tool for **private, self-sovereign storage**. Phase 4 will give them a tool for **public, verifiable achievement**.
|
| 6 |
+
|
| 7 |
+
The goal is to integrate the concepts of Decentralized Identifiers (DIDs) and Verifiable Credentials (VCs) directly into the student portfolio workflow. This will transform the system from a simple storage locker into a true lifelong learning and career passport. When this phase is complete, a student will not only be able to store their work on IPFS but also to hold a cryptographically signed "attestation" from their institution about that work (e.g., a grade, a skill badge, or a formal credit).
|
| 8 |
+
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
## 2. Proposed Features
|
| 12 |
+
|
| 13 |
+
This phase will introduce two core components:
|
| 14 |
+
|
| 15 |
+
### A. The "Institutional Issuer" Endpoint
|
| 16 |
+
|
| 17 |
+
* We will create a new, secure API endpoint, `/issue-credential`.
|
| 18 |
+
* This endpoint will be used by the **institution** (e.g., a teacher or administrator).
|
| 19 |
+
* It will accept a student's DID, the IPFS CID of a piece of student work, and a set of claims (e.g., `grade: "A"`, `skill: "Critical Thinking"`).
|
| 20 |
+
* The endpoint will then generate a W3C-compliant Verifiable Credential, sign it with the institution's private key (held on the server), and return the VC to the issuer, who can then transmit it to the student.
|
| 21 |
+
|
| 22 |
+
### B. The Student Wallet and Verifier UI
|
| 23 |
+
|
| 24 |
+
* We will enhance the `portfolio.html` page to act as a rudimentary **digital wallet**.
|
| 25 |
+
* Students will be able to upload and store the VCs they receive from their school.
|
| 26 |
+
* We will add a "Share Proof" feature. This will allow a student to generate a **Verifiable Presentation**, which is a package containing their DID, the original credential, and a link to the work on IPFS.
|
| 27 |
+
* We will create a simple, public-facing "Verifier" page. Anyone (e.g., an employer) with a link to a Verifiable Presentation can visit this page, and it will automatically verify the cryptographic signatures and confirm that the student's credential is authentic.
|
| 28 |
+
|
| 29 |
+
---
|
| 30 |
+
|
| 31 |
+
## 3. Technical Stack
|
| 32 |
+
|
| 33 |
+
* **DIDs:** We will use the `did:key` method for simplicity in this proof-of-concept. This method is easy to generate and requires no blockchain interaction.
|
| 34 |
+
* **VCs:** We will use a standard JavaScript library (e.g., `veramo` or a similar W3C VC-JWT library) on the server to create and sign the JSON Web Token (JWT)-based credentials.
|
| 35 |
+
* **Cryptography:** The server will hold a private key for signing credentials. The verification process on the public page will use the corresponding public key.
|
| 36 |
+
|
| 37 |
+
---
|
| 38 |
+
|
| 39 |
+
## 4. User Flow
|
| 40 |
+
|
| 41 |
+
1. **Student:** A student uploads an essay using the existing client-side encryption feature and gets an IPFS CID. They generate a `did:key` in their portfolio and share it and the CID with their teacher.
|
| 42 |
+
2. **Teacher:** The teacher grades the essay. They access an internal school dashboard and use the `/issue-credential` endpoint, providing the student's DID, the essay's CID, and the grade.
|
| 43 |
+
3. **Student:** The student receives the signed VC from their teacher and adds it to their portfolio "wallet."
|
| 44 |
+
4. **Student (later):** The student applies for a job. They use the "Share Proof" feature to generate a single URL.
|
| 45 |
+
5. **Employer:** The employer clicks the URL, which opens the Verifier page. The page automatically fetches the data, verifies the signatures, and displays a confirmation: "✓ This credential is authentic and was issued by [School Name]."
|
public/index.html
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Learning Object Repository</title>
|
| 7 |
+
<style>
|
| 8 |
+
body { font-family: sans-serif; margin: 2rem; }
|
| 9 |
+
#gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 1.5rem; }
|
| 10 |
+
.card { border: 1px solid #ddd; border-radius: 8px; padding: 1.5rem; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
|
| 11 |
+
.card h2 { font-size: 1.25rem; margin-top: 0; }
|
| 12 |
+
.card p { margin: 0.5rem 0; }
|
| 13 |
+
.card-meta { font-size: 0.9rem; color: #555; }
|
| 14 |
+
.card-files { margin-top: 1rem; }
|
| 15 |
+
.card-files a { display: block; }
|
| 16 |
+
</style>
|
| 17 |
+
</head>
|
| 18 |
+
<body>
|
| 19 |
+
<h1>Learning Object Repository</h1>
|
| 20 |
+
<div id="gallery"></div>
|
| 21 |
+
|
| 22 |
+
<script>
|
| 23 |
+
async function fetchRecent() {
|
| 24 |
+
const response = await fetch('/recent');
|
| 25 |
+
const cids = await response.json();
|
| 26 |
+
const gallery = document.getElementById('gallery');
|
| 27 |
+
gallery.innerHTML = '';
|
| 28 |
+
|
| 29 |
+
for (const cid of cids) {
|
| 30 |
+
const manifestUrl = `https://ipfs.io/ipfs/${cid}`;
|
| 31 |
+
const manifestResponse = await fetch(manifestUrl);
|
| 32 |
+
const manifest = await manifestResponse.json();
|
| 33 |
+
|
| 34 |
+
const filesHtml = manifest.files.map(file =>
|
| 35 |
+
`<a href="https://ipfs.io/ipfs/${file.cid}" target="_blank">${file.filename}</a>`
|
| 36 |
+
).join('');
|
| 37 |
+
|
| 38 |
+
const card = document.createElement('div');
|
| 39 |
+
card.className = 'card';
|
| 40 |
+
card.innerHTML = `
|
| 41 |
+
<h2>${manifest.title}</h2>
|
| 42 |
+
<p class="card-meta">By: ${manifest.author}</p>
|
| 43 |
+
<p class="card-meta">Subject: ${manifest.subject} | Grade: ${manifest.gradeLevel}</p>
|
| 44 |
+
<p>${manifest.description}</p>
|
| 45 |
+
<div class="card-files">
|
| 46 |
+
<strong>Files:</strong>
|
| 47 |
+
${filesHtml}
|
| 48 |
+
</div>
|
| 49 |
+
<p class="card-meta" style="margin-top: 1rem;">License: ${manifest.license}</p>
|
| 50 |
+
`;
|
| 51 |
+
gallery.appendChild(card);
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
fetchRecent();
|
| 56 |
+
</script>
|
| 57 |
+
</body>
|
| 58 |
+
</html>
|
public/playground.html
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>AI Model Playground</title>
|
| 7 |
+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest/dist/tf.min.js"></script>
|
| 8 |
+
<style>
|
| 9 |
+
body { font-family: sans-serif; margin: 2rem; }
|
| 10 |
+
.container { max-width: 800px; margin: 0 auto; }
|
| 11 |
+
.input-group { margin-bottom: 1rem; }
|
| 12 |
+
#canvas { border: 2px solid black; cursor: crosshair; }
|
| 13 |
+
#prediction { font-size: 1.5rem; font-weight: bold; margin-top: 1rem; }
|
| 14 |
+
</style>
|
| 15 |
+
</head>
|
| 16 |
+
<body>
|
| 17 |
+
<div class="container">
|
| 18 |
+
<h1>Interactive AI Model Playground</h1>
|
| 19 |
+
<p>This playground is designed to run a pre-trained image classification model (like the MNIST handwritten digit recognizer) that has been stored on IPFS.</p>
|
| 20 |
+
|
| 21 |
+
<div class="input-group">
|
| 22 |
+
<label for="cid">Model CID:</label>
|
| 23 |
+
<input type="text" id="cid" placeholder="Enter the IPFS CID of a model.json file..." style="width: 100%;">
|
| 24 |
+
<button id="load-btn">Load Model</button>
|
| 25 |
+
</div>
|
| 26 |
+
|
| 27 |
+
<canvas id="canvas" width="280" height="280"></canvas>
|
| 28 |
+
<br>
|
| 29 |
+
<button id="clear-btn">Clear Drawing</button>
|
| 30 |
+
|
| 31 |
+
<h2>Prediction: <span id="prediction"></span></h2>
|
| 32 |
+
<div id="status"></div>
|
| 33 |
+
</div>
|
| 34 |
+
|
| 35 |
+
<script>
|
| 36 |
+
const canvas = document.getElementById('canvas');
|
| 37 |
+
const ctx = canvas.getContext('2d');
|
| 38 |
+
const cidInput = document.getElementById('cid');
|
| 39 |
+
const loadBtn = document.getElementById('load-btn');
|
| 40 |
+
const clearBtn = document.getElementById('clear-btn');
|
| 41 |
+
const predictionSpan = document.getElementById('prediction');
|
| 42 |
+
const statusDiv = document.getElementById('status');
|
| 43 |
+
|
| 44 |
+
let model;
|
| 45 |
+
let isDrawing = false;
|
| 46 |
+
|
| 47 |
+
// Canvas setup
|
| 48 |
+
ctx.lineWidth = 15;
|
| 49 |
+
ctx.lineCap = 'round';
|
| 50 |
+
ctx.strokeStyle = 'white';
|
| 51 |
+
ctx.fillStyle = 'black';
|
| 52 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
| 53 |
+
|
| 54 |
+
canvas.addEventListener('mousedown', () => isDrawing = true);
|
| 55 |
+
canvas.addEventListener('mouseup', () => {
|
| 56 |
+
isDrawing = false;
|
| 57 |
+
ctx.beginPath();
|
| 58 |
+
if (model) predict();
|
| 59 |
+
});
|
| 60 |
+
canvas.addEventListener('mousemove', draw);
|
| 61 |
+
|
| 62 |
+
function draw(event) {
|
| 63 |
+
if (!isDrawing) return;
|
| 64 |
+
ctx.lineTo(event.offsetX, event.offsetY);
|
| 65 |
+
ctx.stroke();
|
| 66 |
+
ctx.beginPath();
|
| 67 |
+
ctx.moveTo(event.offsetX, event.offsetY);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
clearBtn.addEventListener('click', () => {
|
| 71 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
| 72 |
+
predictionSpan.textContent = '';
|
| 73 |
+
});
|
| 74 |
+
|
| 75 |
+
loadBtn.addEventListener('click', async () => {
|
| 76 |
+
const cid = cidInput.value;
|
| 77 |
+
if (!cid) {
|
| 78 |
+
statusDiv.textContent = 'Please enter a model CID.';
|
| 79 |
+
return;
|
| 80 |
+
}
|
| 81 |
+
try {
|
| 82 |
+
statusDiv.textContent = 'Loading model...';
|
| 83 |
+
const modelUrl = `https://ipfs.io/ipfs/${cid}`;
|
| 84 |
+
model = await tf.loadLayersModel(modelUrl);
|
| 85 |
+
statusDiv.textContent = 'Model loaded successfully! Draw a digit (0-9).';
|
| 86 |
+
} catch (error) {
|
| 87 |
+
console.error('Failed to load model:', error);
|
| 88 |
+
statusDiv.textContent = `Error: ${error.message}`;
|
| 89 |
+
}
|
| 90 |
+
});
|
| 91 |
+
|
| 92 |
+
async function predict() {
|
| 93 |
+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
| 94 |
+
|
| 95 |
+
// Pre-process the image data to a 28x28 grayscale tensor
|
| 96 |
+
let tensor = tf.browser.fromPixels(imageData, 1)
|
| 97 |
+
.resizeNearestNeighbor([28, 28])
|
| 98 |
+
.toFloat()
|
| 99 |
+
.div(tf.scalar(255.0))
|
| 100 |
+
.expandDims();
|
| 101 |
+
|
| 102 |
+
const prediction = await model.predict(tensor).data();
|
| 103 |
+
const predictedDigit = tf.argMax(prediction).dataSync()[0];
|
| 104 |
+
predictionSpan.textContent = predictedDigit;
|
| 105 |
+
}
|
| 106 |
+
</script>
|
| 107 |
+
</body>
|
| 108 |
+
</html>
|
public/portfolio.html
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Student Portfolio - Encrypt & Upload</title>
|
| 7 |
+
<style>
|
| 8 |
+
body { font-family: sans-serif; margin: 2rem; }
|
| 9 |
+
.container { max-width: 600px; margin: 0 auto; }
|
| 10 |
+
.form-group { margin-bottom: 1rem; }
|
| 11 |
+
label { display: block; margin-bottom: 0.5rem; }
|
| 12 |
+
input[type="file"], input[type="password"] { width: 100%; padding: 0.5rem; }
|
| 13 |
+
button { padding: 0.75rem 1.5rem; }
|
| 14 |
+
#status { margin-top: 1rem; font-weight: bold; }
|
| 15 |
+
</style>
|
| 16 |
+
</head>
|
| 17 |
+
<body>
|
| 18 |
+
<div class="container">
|
| 19 |
+
<h1>Encrypt and Upload Portfolio Item</h1>
|
| 20 |
+
<div class="form-group">
|
| 21 |
+
<label for="file">Select File:</label>
|
| 22 |
+
<input type="file" id="file" required>
|
| 23 |
+
</div>
|
| 24 |
+
<div class="form-group">
|
| 25 |
+
<label for="password">Encryption Password:</label>
|
| 26 |
+
<input type="password" id="password" required>
|
| 27 |
+
</div>
|
| 28 |
+
<button id="upload-btn">Encrypt and Upload</button>
|
| 29 |
+
<div id="status"></div>
|
| 30 |
+
</div>
|
| 31 |
+
|
| 32 |
+
<hr style="margin: 3rem 0;">
|
| 33 |
+
|
| 34 |
+
<div class="container">
|
| 35 |
+
<h1>Your Digital Wallet</h1>
|
| 36 |
+
<div class="form-group">
|
| 37 |
+
<label for="did">Your Decentralized ID (DID):</label>
|
| 38 |
+
<input type="text" id="did" readonly>
|
| 39 |
+
<button id="generate-did-btn">Generate New DID</button>
|
| 40 |
+
</div>
|
| 41 |
+
<div class="form-group">
|
| 42 |
+
<label for="vc-jwt">Received VC (Paste JWT here):</label>
|
| 43 |
+
<textarea id="vc-jwt" rows="4" style="width: 100%;"></textarea>
|
| 44 |
+
<button id="store-vc-btn">Store Credential</button>
|
| 45 |
+
</div>
|
| 46 |
+
<div id="credentials-list"></div>
|
| 47 |
+
</div>
|
| 48 |
+
|
| 49 |
+
<hr style="margin: 3rem 0;">
|
| 50 |
+
|
| 51 |
+
<div class="container">
|
| 52 |
+
<h1>Decrypt and Download Portfolio Item</h1>
|
| 53 |
+
<div class="form-group">
|
| 54 |
+
<label for="cid">IPFS CID:</label>
|
| 55 |
+
<input type="text" id="cid" required>
|
| 56 |
+
</div>
|
| 57 |
+
<div class="form-group">
|
| 58 |
+
<label for="password-decrypt">Decryption Password:</label>
|
| 59 |
+
<input type="password" id="password-decrypt" required>
|
| 60 |
+
</div>
|
| 61 |
+
<button id="decrypt-btn">Decrypt and Download</button>
|
| 62 |
+
<div id="status-decrypt"></div>
|
| 63 |
+
</div>
|
| 64 |
+
|
| 65 |
+
<script>
|
| 66 |
+
const fileInput = document.getElementById('file');
|
| 67 |
+
const passwordInput = document.getElementById('password');
|
| 68 |
+
const uploadBtn = document.getElementById('upload-btn');
|
| 69 |
+
const statusDiv = document.getElementById('status');
|
| 70 |
+
|
| 71 |
+
const didInput = document.getElementById('did');
|
| 72 |
+
const generateDidBtn = document.getElementById('generate-did-btn');
|
| 73 |
+
const vcJwtInput = document.getElementById('vc-jwt');
|
| 74 |
+
const storeVcBtn = document.getElementById('store-vc-btn');
|
| 75 |
+
const credentialsListDiv = document.getElementById('credentials-list');
|
| 76 |
+
|
| 77 |
+
const cidInput = document.getElementById('cid');
|
| 78 |
+
const passwordDecryptInput = document.getElementById('password-decrypt');
|
| 79 |
+
const decryptBtn = document.getElementById('decrypt-btn');
|
| 80 |
+
const statusDecryptDiv = document.getElementById('status-decrypt');
|
| 81 |
+
|
| 82 |
+
uploadBtn.addEventListener('click', async () => {
|
| 83 |
+
const file = fileInput.files[0];
|
| 84 |
+
const password = passwordInput.value;
|
| 85 |
+
|
| 86 |
+
if (!file || !password) {
|
| 87 |
+
statusDiv.textContent = 'Please select a file and enter a password.';
|
| 88 |
+
return;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
try {
|
| 92 |
+
statusDiv.textContent = 'Encrypting file... (this may take a moment)';
|
| 93 |
+
|
| 94 |
+
// 1. Derive a key from the password using PBKDF2
|
| 95 |
+
const salt = window.crypto.getRandomValues(new Uint8Array(16));
|
| 96 |
+
const keyMaterial = await window.crypto.subtle.importKey(
|
| 97 |
+
'raw',
|
| 98 |
+
new TextEncoder().encode(password),
|
| 99 |
+
{ name: 'PBKDF2' },
|
| 100 |
+
false,
|
| 101 |
+
['deriveBits', 'deriveKey']
|
| 102 |
+
);
|
| 103 |
+
const key = await window.crypto.subtle.deriveKey(
|
| 104 |
+
{ name: 'PBKDF2', salt: salt, iterations: 100000, hash: 'SHA-256' },
|
| 105 |
+
keyMaterial,
|
| 106 |
+
{ name: 'AES-GCM', length: 256 },
|
| 107 |
+
true,
|
| 108 |
+
['encrypt', 'decrypt']
|
| 109 |
+
);
|
| 110 |
+
|
| 111 |
+
// 2. Encrypt the file content using AES-GCM
|
| 112 |
+
const iv = window.crypto.getRandomValues(new Uint8Array(12));
|
| 113 |
+
const fileBuffer = await file.arrayBuffer();
|
| 114 |
+
const encryptedContent = await window.crypto.subtle.encrypt(
|
| 115 |
+
{ name: 'AES-GCM', iv: iv },
|
| 116 |
+
key,
|
| 117 |
+
fileBuffer
|
| 118 |
+
);
|
| 119 |
+
|
| 120 |
+
// 3. Prepare the payload
|
| 121 |
+
// We need to send the salt, iv, and encrypted content to the server.
|
| 122 |
+
const encryptedBlob = new Blob([salt, iv, new Uint8Array(encryptedContent)], { type: 'application/octet-stream' });
|
| 123 |
+
|
| 124 |
+
statusDiv.textContent = 'Uploading encrypted file...';
|
| 125 |
+
|
| 126 |
+
const formData = new FormData();
|
| 127 |
+
formData.append('file', encryptedBlob, file.name);
|
| 128 |
+
|
| 129 |
+
const response = await fetch('/add-encrypted-object', {
|
| 130 |
+
method: 'POST',
|
| 131 |
+
body: formData,
|
| 132 |
+
});
|
| 133 |
+
|
| 134 |
+
if (!response.ok) {
|
| 135 |
+
throw new Error(`Upload failed: ${response.statusText}`);
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
const result = await response.json();
|
| 139 |
+
statusDiv.innerHTML = `Upload successful! CID: <a href="https://ipfs.io/ipfs/${result.success}" target="_blank">${result.success}</a>`;
|
| 140 |
+
|
| 141 |
+
} catch (error) {
|
| 142 |
+
console.error('Encryption/upload failed:', error);
|
| 143 |
+
statusDiv.textContent = `Error: ${error.message}`;
|
| 144 |
+
}
|
| 145 |
+
});
|
| 146 |
+
|
| 147 |
+
decryptBtn.addEventListener('click', async () => {
|
| 148 |
+
const cid = cidInput.value;
|
| 149 |
+
const password = passwordDecryptInput.value;
|
| 150 |
+
|
| 151 |
+
if (!cid || !password) {
|
| 152 |
+
statusDecryptDiv.textContent = 'Please enter a CID and a password.';
|
| 153 |
+
return;
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
try {
|
| 157 |
+
statusDecryptDiv.textContent = 'Downloading encrypted file...';
|
| 158 |
+
|
| 159 |
+
const response = await fetch(`https://ipfs.io/ipfs/${cid}`);
|
| 160 |
+
if (!response.ok) {
|
| 161 |
+
throw new Error(`Failed to fetch from IPFS: ${response.statusText}`);
|
| 162 |
+
}
|
| 163 |
+
const encryptedBuffer = await response.arrayBuffer();
|
| 164 |
+
|
| 165 |
+
statusDecryptDiv.textContent = 'Decrypting file...';
|
| 166 |
+
|
| 167 |
+
const salt = new Uint8Array(encryptedBuffer.slice(0, 16));
|
| 168 |
+
const iv = new Uint8Array(encryptedBuffer.slice(16, 28));
|
| 169 |
+
const encryptedContent = new Uint8Array(encryptedBuffer.slice(28));
|
| 170 |
+
|
| 171 |
+
const keyMaterial = await window.crypto.subtle.importKey(
|
| 172 |
+
'raw',
|
| 173 |
+
new TextEncoder().encode(password),
|
| 174 |
+
{ name: 'PBKDF2' },
|
| 175 |
+
false,
|
| 176 |
+
['deriveBits', 'deriveKey']
|
| 177 |
+
);
|
| 178 |
+
const key = await window.crypto.subtle.deriveKey(
|
| 179 |
+
{ name: 'PBKDF2', salt: salt, iterations: 100000, hash: 'SHA-256' },
|
| 180 |
+
keyMaterial,
|
| 181 |
+
{ name: 'AES-GCM', length: 256 },
|
| 182 |
+
true,
|
| 183 |
+
['encrypt', 'decrypt']
|
| 184 |
+
);
|
| 185 |
+
|
| 186 |
+
const decryptedContent = await window.crypto.subtle.decrypt(
|
| 187 |
+
{ name: 'AES-GCM', iv: iv },
|
| 188 |
+
key,
|
| 189 |
+
encryptedContent
|
| 190 |
+
);
|
| 191 |
+
|
| 192 |
+
statusDecryptDiv.textContent = 'Decryption successful! Preparing download...';
|
| 193 |
+
|
| 194 |
+
const blob = new Blob([decryptedContent]);
|
| 195 |
+
const a = document.createElement('a');
|
| 196 |
+
const url = window.URL.createObjectURL(blob);
|
| 197 |
+
a.href = url;
|
| 198 |
+
a.download = 'decrypted-file'; // We don't have the original filename here
|
| 199 |
+
document.body.appendChild(a);
|
| 200 |
+
a.click();
|
| 201 |
+
window.URL.revokeObjectURL(url);
|
| 202 |
+
a.remove();
|
| 203 |
+
|
| 204 |
+
} catch (error) {
|
| 205 |
+
console.error('Decryption/download failed:', error);
|
| 206 |
+
statusDecryptDiv.textContent = `Error: ${error.message}`;
|
| 207 |
+
}
|
| 208 |
+
});
|
| 209 |
+
|
| 210 |
+
generateDidBtn.addEventListener('click', async () => {
|
| 211 |
+
// This is a simplified did:key generation for demo purposes.
|
| 212 |
+
// A real implementation would use a proper library.
|
| 213 |
+
const keyPair = await window.crypto.subtle.generateKey(
|
| 214 |
+
{ name: 'ECDSA', namedCurve: 'P-256' },
|
| 215 |
+
true,
|
| 216 |
+
['sign', 'verify']
|
| 217 |
+
);
|
| 218 |
+
const publicKeyJwk = await window.crypto.subtle.exportJwk(keyPair.publicKey);
|
| 219 |
+
const did = `did:key:z${btoa(JSON.stringify(publicKeyJwk)).replace(/=/g, '')}`;
|
| 220 |
+
didInput.value = did;
|
| 221 |
+
localStorage.setItem('studentDid', did);
|
| 222 |
+
});
|
| 223 |
+
|
| 224 |
+
storeVcBtn.addEventListener('click', () => {
|
| 225 |
+
const vcJwt = vcJwtInput.value;
|
| 226 |
+
if (!vcJwt) return;
|
| 227 |
+
let credentials = JSON.parse(localStorage.getItem('credentials') || '[]');
|
| 228 |
+
credentials.push(vcJwt);
|
| 229 |
+
localStorage.setItem('credentials', JSON.stringify(credentials));
|
| 230 |
+
vcJwtInput.value = '';
|
| 231 |
+
renderCredentials();
|
| 232 |
+
});
|
| 233 |
+
|
| 234 |
+
function renderCredentials() {
|
| 235 |
+
let credentials = JSON.parse(localStorage.getItem('credentials') || '[]');
|
| 236 |
+
credentialsListDiv.innerHTML = '<h3>Your Credentials:</h3>';
|
| 237 |
+
if (credentials.length === 0) {
|
| 238 |
+
credentialsListDiv.innerHTML += '<p>No credentials stored yet.</p>';
|
| 239 |
+
return;
|
| 240 |
+
}
|
| 241 |
+
credentials.forEach((vcJwt, index) => {
|
| 242 |
+
const decoded = JSON.parse(atob(vcJwt.split('.')[1]));
|
| 243 |
+
const shareLink = `${window.location.origin}/verify.html?vc=${encodeURIComponent(vcJwt)}`;
|
| 244 |
+
credentialsListDiv.innerHTML += `
|
| 245 |
+
<div>
|
| 246 |
+
<p><b>Credential ${index + 1}:</b> ${decoded.vc.type.join(', ')}</p>
|
| 247 |
+
<p><b>Issued for:</b> ${decoded.sub}</p>
|
| 248 |
+
<input type="text" value="${shareLink}" readonly style="width: 80%;">
|
| 249 |
+
<button onclick="navigator.clipboard.writeText('${shareLink}')">Copy Link</button>
|
| 250 |
+
</div>
|
| 251 |
+
`;
|
| 252 |
+
});
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
// Load DID and credentials on page load
|
| 256 |
+
didInput.value = localStorage.getItem('studentDid') || '';
|
| 257 |
+
renderCredentials();
|
| 258 |
+
</script>
|
| 259 |
+
</body>
|
| 260 |
+
</html>
|
public/vendor/jose/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export { compactDecrypt } from './jwe/compact/decrypt.js';
|
| 2 |
+
export { flattenedDecrypt } from './jwe/flattened/decrypt.js';
|
| 3 |
+
export { generalDecrypt } from './jwe/general/decrypt.js';
|
| 4 |
+
export { GeneralEncrypt } from './jwe/general/encrypt.js';
|
| 5 |
+
export { compactVerify } from './jws/compact/verify.js';
|
| 6 |
+
export { flattenedVerify } from './jws/flattened/verify.js';
|
| 7 |
+
export { generalVerify } from './jws/general/verify.js';
|
| 8 |
+
export { jwtVerify } from './jwt/verify.js';
|
| 9 |
+
export { jwtDecrypt } from './jwt/decrypt.js';
|
| 10 |
+
export { CompactEncrypt } from './jwe/compact/encrypt.js';
|
| 11 |
+
export { FlattenedEncrypt } from './jwe/flattened/encrypt.js';
|
| 12 |
+
export { CompactSign } from './jws/compact/sign.js';
|
| 13 |
+
export { FlattenedSign } from './jws/flattened/sign.js';
|
| 14 |
+
export { GeneralSign } from './jws/general/sign.js';
|
| 15 |
+
export { SignJWT } from './jwt/sign.js';
|
| 16 |
+
export { EncryptJWT } from './jwt/encrypt.js';
|
| 17 |
+
export { calculateJwkThumbprint, calculateJwkThumbprintUri } from './jwk/thumbprint.js';
|
| 18 |
+
export { EmbeddedJWK } from './jwk/embedded.js';
|
| 19 |
+
export { createLocalJWKSet } from './jwks/local.js';
|
| 20 |
+
export { createRemoteJWKSet, jwksCache, customFetch } from './jwks/remote.js';
|
| 21 |
+
export { UnsecuredJWT } from './jwt/unsecured.js';
|
| 22 |
+
export { exportPKCS8, exportSPKI, exportJWK } from './key/export.js';
|
| 23 |
+
export { importSPKI, importPKCS8, importX509, importJWK } from './key/import.js';
|
| 24 |
+
export { decodeProtectedHeader } from './util/decode_protected_header.js';
|
| 25 |
+
export { decodeJwt } from './util/decode_jwt.js';
|
| 26 |
+
import * as errors from './util/errors.js';
|
| 27 |
+
export { errors };
|
| 28 |
+
export { generateKeyPair } from './key/generate_key_pair.js';
|
| 29 |
+
export { generateSecret } from './key/generate_secret.js';
|
| 30 |
+
import * as base64url from './util/base64url.js';
|
| 31 |
+
export { base64url };
|
| 32 |
+
export const cryptoRuntime = 'WebCryptoAPI';
|
public/vendor/jose/jwe/compact/decrypt.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { flattenedDecrypt } from '../flattened/decrypt.js';
|
| 2 |
+
import { JWEInvalid } from '../../util/errors.js';
|
| 3 |
+
import { decoder } from '../../lib/buffer_utils.js';
|
| 4 |
+
export async function compactDecrypt(jwe, key, options) {
|
| 5 |
+
if (jwe instanceof Uint8Array) {
|
| 6 |
+
jwe = decoder.decode(jwe);
|
| 7 |
+
}
|
| 8 |
+
if (typeof jwe !== 'string') {
|
| 9 |
+
throw new JWEInvalid('Compact JWE must be a string or Uint8Array');
|
| 10 |
+
}
|
| 11 |
+
const { 0: protectedHeader, 1: encryptedKey, 2: iv, 3: ciphertext, 4: tag, length, } = jwe.split('.');
|
| 12 |
+
if (length !== 5) {
|
| 13 |
+
throw new JWEInvalid('Invalid Compact JWE');
|
| 14 |
+
}
|
| 15 |
+
const decrypted = await flattenedDecrypt({
|
| 16 |
+
ciphertext,
|
| 17 |
+
iv: iv || undefined,
|
| 18 |
+
protected: protectedHeader,
|
| 19 |
+
tag: tag || undefined,
|
| 20 |
+
encrypted_key: encryptedKey || undefined,
|
| 21 |
+
}, key, options);
|
| 22 |
+
const result = { plaintext: decrypted.plaintext, protectedHeader: decrypted.protectedHeader };
|
| 23 |
+
if (typeof key === 'function') {
|
| 24 |
+
return { ...result, key: decrypted.key };
|
| 25 |
+
}
|
| 26 |
+
return result;
|
| 27 |
+
}
|
public/vendor/jose/jwe/compact/encrypt.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { FlattenedEncrypt } from '../flattened/encrypt.js';
|
| 2 |
+
export class CompactEncrypt {
|
| 3 |
+
#flattened;
|
| 4 |
+
constructor(plaintext) {
|
| 5 |
+
this.#flattened = new FlattenedEncrypt(plaintext);
|
| 6 |
+
}
|
| 7 |
+
setContentEncryptionKey(cek) {
|
| 8 |
+
this.#flattened.setContentEncryptionKey(cek);
|
| 9 |
+
return this;
|
| 10 |
+
}
|
| 11 |
+
setInitializationVector(iv) {
|
| 12 |
+
this.#flattened.setInitializationVector(iv);
|
| 13 |
+
return this;
|
| 14 |
+
}
|
| 15 |
+
setProtectedHeader(protectedHeader) {
|
| 16 |
+
this.#flattened.setProtectedHeader(protectedHeader);
|
| 17 |
+
return this;
|
| 18 |
+
}
|
| 19 |
+
setKeyManagementParameters(parameters) {
|
| 20 |
+
this.#flattened.setKeyManagementParameters(parameters);
|
| 21 |
+
return this;
|
| 22 |
+
}
|
| 23 |
+
async encrypt(key, options) {
|
| 24 |
+
const jwe = await this.#flattened.encrypt(key, options);
|
| 25 |
+
return [jwe.protected, jwe.encrypted_key, jwe.iv, jwe.ciphertext, jwe.tag].join('.');
|
| 26 |
+
}
|
| 27 |
+
}
|
public/vendor/jose/jwe/flattened/decrypt.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { decode as b64u } from '../../util/base64url.js';
|
| 2 |
+
import { decrypt } from '../../lib/decrypt.js';
|
| 3 |
+
import { JOSEAlgNotAllowed, JOSENotSupported, JWEInvalid } from '../../util/errors.js';
|
| 4 |
+
import { isDisjoint } from '../../lib/is_disjoint.js';
|
| 5 |
+
import { isObject } from '../../lib/is_object.js';
|
| 6 |
+
import { decryptKeyManagement } from '../../lib/decrypt_key_management.js';
|
| 7 |
+
import { decoder, concat, encode } from '../../lib/buffer_utils.js';
|
| 8 |
+
import { generateCek } from '../../lib/cek.js';
|
| 9 |
+
import { validateCrit } from '../../lib/validate_crit.js';
|
| 10 |
+
import { validateAlgorithms } from '../../lib/validate_algorithms.js';
|
| 11 |
+
import { normalizeKey } from '../../lib/normalize_key.js';
|
| 12 |
+
import { checkKeyType } from '../../lib/check_key_type.js';
|
| 13 |
+
export async function flattenedDecrypt(jwe, key, options) {
|
| 14 |
+
if (!isObject(jwe)) {
|
| 15 |
+
throw new JWEInvalid('Flattened JWE must be an object');
|
| 16 |
+
}
|
| 17 |
+
if (jwe.protected === undefined && jwe.header === undefined && jwe.unprotected === undefined) {
|
| 18 |
+
throw new JWEInvalid('JOSE Header missing');
|
| 19 |
+
}
|
| 20 |
+
if (jwe.iv !== undefined && typeof jwe.iv !== 'string') {
|
| 21 |
+
throw new JWEInvalid('JWE Initialization Vector incorrect type');
|
| 22 |
+
}
|
| 23 |
+
if (typeof jwe.ciphertext !== 'string') {
|
| 24 |
+
throw new JWEInvalid('JWE Ciphertext missing or incorrect type');
|
| 25 |
+
}
|
| 26 |
+
if (jwe.tag !== undefined && typeof jwe.tag !== 'string') {
|
| 27 |
+
throw new JWEInvalid('JWE Authentication Tag incorrect type');
|
| 28 |
+
}
|
| 29 |
+
if (jwe.protected !== undefined && typeof jwe.protected !== 'string') {
|
| 30 |
+
throw new JWEInvalid('JWE Protected Header incorrect type');
|
| 31 |
+
}
|
| 32 |
+
if (jwe.encrypted_key !== undefined && typeof jwe.encrypted_key !== 'string') {
|
| 33 |
+
throw new JWEInvalid('JWE Encrypted Key incorrect type');
|
| 34 |
+
}
|
| 35 |
+
if (jwe.aad !== undefined && typeof jwe.aad !== 'string') {
|
| 36 |
+
throw new JWEInvalid('JWE AAD incorrect type');
|
| 37 |
+
}
|
| 38 |
+
if (jwe.header !== undefined && !isObject(jwe.header)) {
|
| 39 |
+
throw new JWEInvalid('JWE Shared Unprotected Header incorrect type');
|
| 40 |
+
}
|
| 41 |
+
if (jwe.unprotected !== undefined && !isObject(jwe.unprotected)) {
|
| 42 |
+
throw new JWEInvalid('JWE Per-Recipient Unprotected Header incorrect type');
|
| 43 |
+
}
|
| 44 |
+
let parsedProt;
|
| 45 |
+
if (jwe.protected) {
|
| 46 |
+
try {
|
| 47 |
+
const protectedHeader = b64u(jwe.protected);
|
| 48 |
+
parsedProt = JSON.parse(decoder.decode(protectedHeader));
|
| 49 |
+
}
|
| 50 |
+
catch {
|
| 51 |
+
throw new JWEInvalid('JWE Protected Header is invalid');
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
if (!isDisjoint(parsedProt, jwe.header, jwe.unprotected)) {
|
| 55 |
+
throw new JWEInvalid('JWE Protected, JWE Unprotected Header, and JWE Per-Recipient Unprotected Header Parameter names must be disjoint');
|
| 56 |
+
}
|
| 57 |
+
const joseHeader = {
|
| 58 |
+
...parsedProt,
|
| 59 |
+
...jwe.header,
|
| 60 |
+
...jwe.unprotected,
|
| 61 |
+
};
|
| 62 |
+
validateCrit(JWEInvalid, new Map(), options?.crit, parsedProt, joseHeader);
|
| 63 |
+
if (joseHeader.zip !== undefined) {
|
| 64 |
+
throw new JOSENotSupported('JWE "zip" (Compression Algorithm) Header Parameter is not supported.');
|
| 65 |
+
}
|
| 66 |
+
const { alg, enc } = joseHeader;
|
| 67 |
+
if (typeof alg !== 'string' || !alg) {
|
| 68 |
+
throw new JWEInvalid('missing JWE Algorithm (alg) in JWE Header');
|
| 69 |
+
}
|
| 70 |
+
if (typeof enc !== 'string' || !enc) {
|
| 71 |
+
throw new JWEInvalid('missing JWE Encryption Algorithm (enc) in JWE Header');
|
| 72 |
+
}
|
| 73 |
+
const keyManagementAlgorithms = options && validateAlgorithms('keyManagementAlgorithms', options.keyManagementAlgorithms);
|
| 74 |
+
const contentEncryptionAlgorithms = options &&
|
| 75 |
+
validateAlgorithms('contentEncryptionAlgorithms', options.contentEncryptionAlgorithms);
|
| 76 |
+
if ((keyManagementAlgorithms && !keyManagementAlgorithms.has(alg)) ||
|
| 77 |
+
(!keyManagementAlgorithms && alg.startsWith('PBES2'))) {
|
| 78 |
+
throw new JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter value not allowed');
|
| 79 |
+
}
|
| 80 |
+
if (contentEncryptionAlgorithms && !contentEncryptionAlgorithms.has(enc)) {
|
| 81 |
+
throw new JOSEAlgNotAllowed('"enc" (Encryption Algorithm) Header Parameter value not allowed');
|
| 82 |
+
}
|
| 83 |
+
let encryptedKey;
|
| 84 |
+
if (jwe.encrypted_key !== undefined) {
|
| 85 |
+
try {
|
| 86 |
+
encryptedKey = b64u(jwe.encrypted_key);
|
| 87 |
+
}
|
| 88 |
+
catch {
|
| 89 |
+
throw new JWEInvalid('Failed to base64url decode the encrypted_key');
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
let resolvedKey = false;
|
| 93 |
+
if (typeof key === 'function') {
|
| 94 |
+
key = await key(parsedProt, jwe);
|
| 95 |
+
resolvedKey = true;
|
| 96 |
+
}
|
| 97 |
+
checkKeyType(alg === 'dir' ? enc : alg, key, 'decrypt');
|
| 98 |
+
const k = await normalizeKey(key, alg);
|
| 99 |
+
let cek;
|
| 100 |
+
try {
|
| 101 |
+
cek = await decryptKeyManagement(alg, k, encryptedKey, joseHeader, options);
|
| 102 |
+
}
|
| 103 |
+
catch (err) {
|
| 104 |
+
if (err instanceof TypeError || err instanceof JWEInvalid || err instanceof JOSENotSupported) {
|
| 105 |
+
throw err;
|
| 106 |
+
}
|
| 107 |
+
cek = generateCek(enc);
|
| 108 |
+
}
|
| 109 |
+
let iv;
|
| 110 |
+
let tag;
|
| 111 |
+
if (jwe.iv !== undefined) {
|
| 112 |
+
try {
|
| 113 |
+
iv = b64u(jwe.iv);
|
| 114 |
+
}
|
| 115 |
+
catch {
|
| 116 |
+
throw new JWEInvalid('Failed to base64url decode the iv');
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
if (jwe.tag !== undefined) {
|
| 120 |
+
try {
|
| 121 |
+
tag = b64u(jwe.tag);
|
| 122 |
+
}
|
| 123 |
+
catch {
|
| 124 |
+
throw new JWEInvalid('Failed to base64url decode the tag');
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
const protectedHeader = jwe.protected !== undefined ? encode(jwe.protected) : new Uint8Array();
|
| 128 |
+
let additionalData;
|
| 129 |
+
if (jwe.aad !== undefined) {
|
| 130 |
+
additionalData = concat(protectedHeader, encode('.'), encode(jwe.aad));
|
| 131 |
+
}
|
| 132 |
+
else {
|
| 133 |
+
additionalData = protectedHeader;
|
| 134 |
+
}
|
| 135 |
+
let ciphertext;
|
| 136 |
+
try {
|
| 137 |
+
ciphertext = b64u(jwe.ciphertext);
|
| 138 |
+
}
|
| 139 |
+
catch {
|
| 140 |
+
throw new JWEInvalid('Failed to base64url decode the ciphertext');
|
| 141 |
+
}
|
| 142 |
+
const plaintext = await decrypt(enc, cek, ciphertext, iv, tag, additionalData);
|
| 143 |
+
const result = { plaintext };
|
| 144 |
+
if (jwe.protected !== undefined) {
|
| 145 |
+
result.protectedHeader = parsedProt;
|
| 146 |
+
}
|
| 147 |
+
if (jwe.aad !== undefined) {
|
| 148 |
+
try {
|
| 149 |
+
result.additionalAuthenticatedData = b64u(jwe.aad);
|
| 150 |
+
}
|
| 151 |
+
catch {
|
| 152 |
+
throw new JWEInvalid('Failed to base64url decode the aad');
|
| 153 |
+
}
|
| 154 |
+
}
|
| 155 |
+
if (jwe.unprotected !== undefined) {
|
| 156 |
+
result.sharedUnprotectedHeader = jwe.unprotected;
|
| 157 |
+
}
|
| 158 |
+
if (jwe.header !== undefined) {
|
| 159 |
+
result.unprotectedHeader = jwe.header;
|
| 160 |
+
}
|
| 161 |
+
if (resolvedKey) {
|
| 162 |
+
return { ...result, key: k };
|
| 163 |
+
}
|
| 164 |
+
return result;
|
| 165 |
+
}
|
public/vendor/jose/jwe/flattened/encrypt.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { encode as b64u } from '../../util/base64url.js';
|
| 2 |
+
import { unprotected } from '../../lib/private_symbols.js';
|
| 3 |
+
import { encrypt } from '../../lib/encrypt.js';
|
| 4 |
+
import { encryptKeyManagement } from '../../lib/encrypt_key_management.js';
|
| 5 |
+
import { JOSENotSupported, JWEInvalid } from '../../util/errors.js';
|
| 6 |
+
import { isDisjoint } from '../../lib/is_disjoint.js';
|
| 7 |
+
import { concat, encode } from '../../lib/buffer_utils.js';
|
| 8 |
+
import { validateCrit } from '../../lib/validate_crit.js';
|
| 9 |
+
import { normalizeKey } from '../../lib/normalize_key.js';
|
| 10 |
+
import { checkKeyType } from '../../lib/check_key_type.js';
|
| 11 |
+
export class FlattenedEncrypt {
|
| 12 |
+
#plaintext;
|
| 13 |
+
#protectedHeader;
|
| 14 |
+
#sharedUnprotectedHeader;
|
| 15 |
+
#unprotectedHeader;
|
| 16 |
+
#aad;
|
| 17 |
+
#cek;
|
| 18 |
+
#iv;
|
| 19 |
+
#keyManagementParameters;
|
| 20 |
+
constructor(plaintext) {
|
| 21 |
+
if (!(plaintext instanceof Uint8Array)) {
|
| 22 |
+
throw new TypeError('plaintext must be an instance of Uint8Array');
|
| 23 |
+
}
|
| 24 |
+
this.#plaintext = plaintext;
|
| 25 |
+
}
|
| 26 |
+
setKeyManagementParameters(parameters) {
|
| 27 |
+
if (this.#keyManagementParameters) {
|
| 28 |
+
throw new TypeError('setKeyManagementParameters can only be called once');
|
| 29 |
+
}
|
| 30 |
+
this.#keyManagementParameters = parameters;
|
| 31 |
+
return this;
|
| 32 |
+
}
|
| 33 |
+
setProtectedHeader(protectedHeader) {
|
| 34 |
+
if (this.#protectedHeader) {
|
| 35 |
+
throw new TypeError('setProtectedHeader can only be called once');
|
| 36 |
+
}
|
| 37 |
+
this.#protectedHeader = protectedHeader;
|
| 38 |
+
return this;
|
| 39 |
+
}
|
| 40 |
+
setSharedUnprotectedHeader(sharedUnprotectedHeader) {
|
| 41 |
+
if (this.#sharedUnprotectedHeader) {
|
| 42 |
+
throw new TypeError('setSharedUnprotectedHeader can only be called once');
|
| 43 |
+
}
|
| 44 |
+
this.#sharedUnprotectedHeader = sharedUnprotectedHeader;
|
| 45 |
+
return this;
|
| 46 |
+
}
|
| 47 |
+
setUnprotectedHeader(unprotectedHeader) {
|
| 48 |
+
if (this.#unprotectedHeader) {
|
| 49 |
+
throw new TypeError('setUnprotectedHeader can only be called once');
|
| 50 |
+
}
|
| 51 |
+
this.#unprotectedHeader = unprotectedHeader;
|
| 52 |
+
return this;
|
| 53 |
+
}
|
| 54 |
+
setAdditionalAuthenticatedData(aad) {
|
| 55 |
+
this.#aad = aad;
|
| 56 |
+
return this;
|
| 57 |
+
}
|
| 58 |
+
setContentEncryptionKey(cek) {
|
| 59 |
+
if (this.#cek) {
|
| 60 |
+
throw new TypeError('setContentEncryptionKey can only be called once');
|
| 61 |
+
}
|
| 62 |
+
this.#cek = cek;
|
| 63 |
+
return this;
|
| 64 |
+
}
|
| 65 |
+
setInitializationVector(iv) {
|
| 66 |
+
if (this.#iv) {
|
| 67 |
+
throw new TypeError('setInitializationVector can only be called once');
|
| 68 |
+
}
|
| 69 |
+
this.#iv = iv;
|
| 70 |
+
return this;
|
| 71 |
+
}
|
| 72 |
+
async encrypt(key, options) {
|
| 73 |
+
if (!this.#protectedHeader && !this.#unprotectedHeader && !this.#sharedUnprotectedHeader) {
|
| 74 |
+
throw new JWEInvalid('either setProtectedHeader, setUnprotectedHeader, or sharedUnprotectedHeader must be called before #encrypt()');
|
| 75 |
+
}
|
| 76 |
+
if (!isDisjoint(this.#protectedHeader, this.#unprotectedHeader, this.#sharedUnprotectedHeader)) {
|
| 77 |
+
throw new JWEInvalid('JWE Protected, JWE Shared Unprotected and JWE Per-Recipient Header Parameter names must be disjoint');
|
| 78 |
+
}
|
| 79 |
+
const joseHeader = {
|
| 80 |
+
...this.#protectedHeader,
|
| 81 |
+
...this.#unprotectedHeader,
|
| 82 |
+
...this.#sharedUnprotectedHeader,
|
| 83 |
+
};
|
| 84 |
+
validateCrit(JWEInvalid, new Map(), options?.crit, this.#protectedHeader, joseHeader);
|
| 85 |
+
if (joseHeader.zip !== undefined) {
|
| 86 |
+
throw new JOSENotSupported('JWE "zip" (Compression Algorithm) Header Parameter is not supported.');
|
| 87 |
+
}
|
| 88 |
+
const { alg, enc } = joseHeader;
|
| 89 |
+
if (typeof alg !== 'string' || !alg) {
|
| 90 |
+
throw new JWEInvalid('JWE "alg" (Algorithm) Header Parameter missing or invalid');
|
| 91 |
+
}
|
| 92 |
+
if (typeof enc !== 'string' || !enc) {
|
| 93 |
+
throw new JWEInvalid('JWE "enc" (Encryption Algorithm) Header Parameter missing or invalid');
|
| 94 |
+
}
|
| 95 |
+
let encryptedKey;
|
| 96 |
+
if (this.#cek && (alg === 'dir' || alg === 'ECDH-ES')) {
|
| 97 |
+
throw new TypeError(`setContentEncryptionKey cannot be called with JWE "alg" (Algorithm) Header ${alg}`);
|
| 98 |
+
}
|
| 99 |
+
checkKeyType(alg === 'dir' ? enc : alg, key, 'encrypt');
|
| 100 |
+
let cek;
|
| 101 |
+
{
|
| 102 |
+
let parameters;
|
| 103 |
+
const k = await normalizeKey(key, alg);
|
| 104 |
+
({ cek, encryptedKey, parameters } = await encryptKeyManagement(alg, enc, k, this.#cek, this.#keyManagementParameters));
|
| 105 |
+
if (parameters) {
|
| 106 |
+
if (options && unprotected in options) {
|
| 107 |
+
if (!this.#unprotectedHeader) {
|
| 108 |
+
this.setUnprotectedHeader(parameters);
|
| 109 |
+
}
|
| 110 |
+
else {
|
| 111 |
+
this.#unprotectedHeader = { ...this.#unprotectedHeader, ...parameters };
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
else if (!this.#protectedHeader) {
|
| 115 |
+
this.setProtectedHeader(parameters);
|
| 116 |
+
}
|
| 117 |
+
else {
|
| 118 |
+
this.#protectedHeader = { ...this.#protectedHeader, ...parameters };
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
let additionalData;
|
| 123 |
+
let protectedHeaderS;
|
| 124 |
+
let protectedHeaderB;
|
| 125 |
+
let aadMember;
|
| 126 |
+
if (this.#protectedHeader) {
|
| 127 |
+
protectedHeaderS = b64u(JSON.stringify(this.#protectedHeader));
|
| 128 |
+
protectedHeaderB = encode(protectedHeaderS);
|
| 129 |
+
}
|
| 130 |
+
else {
|
| 131 |
+
protectedHeaderS = '';
|
| 132 |
+
protectedHeaderB = new Uint8Array();
|
| 133 |
+
}
|
| 134 |
+
if (this.#aad) {
|
| 135 |
+
aadMember = b64u(this.#aad);
|
| 136 |
+
const aadMemberBytes = encode(aadMember);
|
| 137 |
+
additionalData = concat(protectedHeaderB, encode('.'), aadMemberBytes);
|
| 138 |
+
}
|
| 139 |
+
else {
|
| 140 |
+
additionalData = protectedHeaderB;
|
| 141 |
+
}
|
| 142 |
+
const { ciphertext, tag, iv } = await encrypt(enc, this.#plaintext, cek, this.#iv, additionalData);
|
| 143 |
+
const jwe = {
|
| 144 |
+
ciphertext: b64u(ciphertext),
|
| 145 |
+
};
|
| 146 |
+
if (iv) {
|
| 147 |
+
jwe.iv = b64u(iv);
|
| 148 |
+
}
|
| 149 |
+
if (tag) {
|
| 150 |
+
jwe.tag = b64u(tag);
|
| 151 |
+
}
|
| 152 |
+
if (encryptedKey) {
|
| 153 |
+
jwe.encrypted_key = b64u(encryptedKey);
|
| 154 |
+
}
|
| 155 |
+
if (aadMember) {
|
| 156 |
+
jwe.aad = aadMember;
|
| 157 |
+
}
|
| 158 |
+
if (this.#protectedHeader) {
|
| 159 |
+
jwe.protected = protectedHeaderS;
|
| 160 |
+
}
|
| 161 |
+
if (this.#sharedUnprotectedHeader) {
|
| 162 |
+
jwe.unprotected = this.#sharedUnprotectedHeader;
|
| 163 |
+
}
|
| 164 |
+
if (this.#unprotectedHeader) {
|
| 165 |
+
jwe.header = this.#unprotectedHeader;
|
| 166 |
+
}
|
| 167 |
+
return jwe;
|
| 168 |
+
}
|
| 169 |
+
}
|
public/vendor/jose/jwe/general/decrypt.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { flattenedDecrypt } from '../flattened/decrypt.js';
|
| 2 |
+
import { JWEDecryptionFailed, JWEInvalid } from '../../util/errors.js';
|
| 3 |
+
import { isObject } from '../../lib/is_object.js';
|
| 4 |
+
export async function generalDecrypt(jwe, key, options) {
|
| 5 |
+
if (!isObject(jwe)) {
|
| 6 |
+
throw new JWEInvalid('General JWE must be an object');
|
| 7 |
+
}
|
| 8 |
+
if (!Array.isArray(jwe.recipients) || !jwe.recipients.every(isObject)) {
|
| 9 |
+
throw new JWEInvalid('JWE Recipients missing or incorrect type');
|
| 10 |
+
}
|
| 11 |
+
if (!jwe.recipients.length) {
|
| 12 |
+
throw new JWEInvalid('JWE Recipients has no members');
|
| 13 |
+
}
|
| 14 |
+
for (const recipient of jwe.recipients) {
|
| 15 |
+
try {
|
| 16 |
+
return await flattenedDecrypt({
|
| 17 |
+
aad: jwe.aad,
|
| 18 |
+
ciphertext: jwe.ciphertext,
|
| 19 |
+
encrypted_key: recipient.encrypted_key,
|
| 20 |
+
header: recipient.header,
|
| 21 |
+
iv: jwe.iv,
|
| 22 |
+
protected: jwe.protected,
|
| 23 |
+
tag: jwe.tag,
|
| 24 |
+
unprotected: jwe.unprotected,
|
| 25 |
+
}, key, options);
|
| 26 |
+
}
|
| 27 |
+
catch {
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
throw new JWEDecryptionFailed();
|
| 31 |
+
}
|
public/vendor/jose/jwe/general/encrypt.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { FlattenedEncrypt } from '../flattened/encrypt.js';
|
| 2 |
+
import { unprotected } from '../../lib/private_symbols.js';
|
| 3 |
+
import { JOSENotSupported, JWEInvalid } from '../../util/errors.js';
|
| 4 |
+
import { generateCek } from '../../lib/cek.js';
|
| 5 |
+
import { isDisjoint } from '../../lib/is_disjoint.js';
|
| 6 |
+
import { encryptKeyManagement } from '../../lib/encrypt_key_management.js';
|
| 7 |
+
import { encode as b64u } from '../../util/base64url.js';
|
| 8 |
+
import { validateCrit } from '../../lib/validate_crit.js';
|
| 9 |
+
import { normalizeKey } from '../../lib/normalize_key.js';
|
| 10 |
+
import { checkKeyType } from '../../lib/check_key_type.js';
|
| 11 |
+
class IndividualRecipient {
|
| 12 |
+
#parent;
|
| 13 |
+
unprotectedHeader;
|
| 14 |
+
keyManagementParameters;
|
| 15 |
+
key;
|
| 16 |
+
options;
|
| 17 |
+
constructor(enc, key, options) {
|
| 18 |
+
this.#parent = enc;
|
| 19 |
+
this.key = key;
|
| 20 |
+
this.options = options;
|
| 21 |
+
}
|
| 22 |
+
setUnprotectedHeader(unprotectedHeader) {
|
| 23 |
+
if (this.unprotectedHeader) {
|
| 24 |
+
throw new TypeError('setUnprotectedHeader can only be called once');
|
| 25 |
+
}
|
| 26 |
+
this.unprotectedHeader = unprotectedHeader;
|
| 27 |
+
return this;
|
| 28 |
+
}
|
| 29 |
+
setKeyManagementParameters(parameters) {
|
| 30 |
+
if (this.keyManagementParameters) {
|
| 31 |
+
throw new TypeError('setKeyManagementParameters can only be called once');
|
| 32 |
+
}
|
| 33 |
+
this.keyManagementParameters = parameters;
|
| 34 |
+
return this;
|
| 35 |
+
}
|
| 36 |
+
addRecipient(...args) {
|
| 37 |
+
return this.#parent.addRecipient(...args);
|
| 38 |
+
}
|
| 39 |
+
encrypt(...args) {
|
| 40 |
+
return this.#parent.encrypt(...args);
|
| 41 |
+
}
|
| 42 |
+
done() {
|
| 43 |
+
return this.#parent;
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
export class GeneralEncrypt {
|
| 47 |
+
#plaintext;
|
| 48 |
+
#recipients = [];
|
| 49 |
+
#protectedHeader;
|
| 50 |
+
#unprotectedHeader;
|
| 51 |
+
#aad;
|
| 52 |
+
constructor(plaintext) {
|
| 53 |
+
this.#plaintext = plaintext;
|
| 54 |
+
}
|
| 55 |
+
addRecipient(key, options) {
|
| 56 |
+
const recipient = new IndividualRecipient(this, key, { crit: options?.crit });
|
| 57 |
+
this.#recipients.push(recipient);
|
| 58 |
+
return recipient;
|
| 59 |
+
}
|
| 60 |
+
setProtectedHeader(protectedHeader) {
|
| 61 |
+
if (this.#protectedHeader) {
|
| 62 |
+
throw new TypeError('setProtectedHeader can only be called once');
|
| 63 |
+
}
|
| 64 |
+
this.#protectedHeader = protectedHeader;
|
| 65 |
+
return this;
|
| 66 |
+
}
|
| 67 |
+
setSharedUnprotectedHeader(sharedUnprotectedHeader) {
|
| 68 |
+
if (this.#unprotectedHeader) {
|
| 69 |
+
throw new TypeError('setSharedUnprotectedHeader can only be called once');
|
| 70 |
+
}
|
| 71 |
+
this.#unprotectedHeader = sharedUnprotectedHeader;
|
| 72 |
+
return this;
|
| 73 |
+
}
|
| 74 |
+
setAdditionalAuthenticatedData(aad) {
|
| 75 |
+
this.#aad = aad;
|
| 76 |
+
return this;
|
| 77 |
+
}
|
| 78 |
+
async encrypt() {
|
| 79 |
+
if (!this.#recipients.length) {
|
| 80 |
+
throw new JWEInvalid('at least one recipient must be added');
|
| 81 |
+
}
|
| 82 |
+
if (this.#recipients.length === 1) {
|
| 83 |
+
const [recipient] = this.#recipients;
|
| 84 |
+
const flattened = await new FlattenedEncrypt(this.#plaintext)
|
| 85 |
+
.setAdditionalAuthenticatedData(this.#aad)
|
| 86 |
+
.setProtectedHeader(this.#protectedHeader)
|
| 87 |
+
.setSharedUnprotectedHeader(this.#unprotectedHeader)
|
| 88 |
+
.setUnprotectedHeader(recipient.unprotectedHeader)
|
| 89 |
+
.encrypt(recipient.key, { ...recipient.options });
|
| 90 |
+
const jwe = {
|
| 91 |
+
ciphertext: flattened.ciphertext,
|
| 92 |
+
iv: flattened.iv,
|
| 93 |
+
recipients: [{}],
|
| 94 |
+
tag: flattened.tag,
|
| 95 |
+
};
|
| 96 |
+
if (flattened.aad)
|
| 97 |
+
jwe.aad = flattened.aad;
|
| 98 |
+
if (flattened.protected)
|
| 99 |
+
jwe.protected = flattened.protected;
|
| 100 |
+
if (flattened.unprotected)
|
| 101 |
+
jwe.unprotected = flattened.unprotected;
|
| 102 |
+
if (flattened.encrypted_key)
|
| 103 |
+
jwe.recipients[0].encrypted_key = flattened.encrypted_key;
|
| 104 |
+
if (flattened.header)
|
| 105 |
+
jwe.recipients[0].header = flattened.header;
|
| 106 |
+
return jwe;
|
| 107 |
+
}
|
| 108 |
+
let enc;
|
| 109 |
+
for (let i = 0; i < this.#recipients.length; i++) {
|
| 110 |
+
const recipient = this.#recipients[i];
|
| 111 |
+
if (!isDisjoint(this.#protectedHeader, this.#unprotectedHeader, recipient.unprotectedHeader)) {
|
| 112 |
+
throw new JWEInvalid('JWE Protected, JWE Shared Unprotected and JWE Per-Recipient Header Parameter names must be disjoint');
|
| 113 |
+
}
|
| 114 |
+
const joseHeader = {
|
| 115 |
+
...this.#protectedHeader,
|
| 116 |
+
...this.#unprotectedHeader,
|
| 117 |
+
...recipient.unprotectedHeader,
|
| 118 |
+
};
|
| 119 |
+
const { alg } = joseHeader;
|
| 120 |
+
if (typeof alg !== 'string' || !alg) {
|
| 121 |
+
throw new JWEInvalid('JWE "alg" (Algorithm) Header Parameter missing or invalid');
|
| 122 |
+
}
|
| 123 |
+
if (alg === 'dir' || alg === 'ECDH-ES') {
|
| 124 |
+
throw new JWEInvalid('"dir" and "ECDH-ES" alg may only be used with a single recipient');
|
| 125 |
+
}
|
| 126 |
+
if (typeof joseHeader.enc !== 'string' || !joseHeader.enc) {
|
| 127 |
+
throw new JWEInvalid('JWE "enc" (Encryption Algorithm) Header Parameter missing or invalid');
|
| 128 |
+
}
|
| 129 |
+
if (!enc) {
|
| 130 |
+
enc = joseHeader.enc;
|
| 131 |
+
}
|
| 132 |
+
else if (enc !== joseHeader.enc) {
|
| 133 |
+
throw new JWEInvalid('JWE "enc" (Encryption Algorithm) Header Parameter must be the same for all recipients');
|
| 134 |
+
}
|
| 135 |
+
validateCrit(JWEInvalid, new Map(), recipient.options.crit, this.#protectedHeader, joseHeader);
|
| 136 |
+
if (joseHeader.zip !== undefined) {
|
| 137 |
+
throw new JOSENotSupported('JWE "zip" (Compression Algorithm) Header Parameter is not supported.');
|
| 138 |
+
}
|
| 139 |
+
}
|
| 140 |
+
const cek = generateCek(enc);
|
| 141 |
+
const jwe = {
|
| 142 |
+
ciphertext: '',
|
| 143 |
+
recipients: [],
|
| 144 |
+
};
|
| 145 |
+
for (let i = 0; i < this.#recipients.length; i++) {
|
| 146 |
+
const recipient = this.#recipients[i];
|
| 147 |
+
const target = {};
|
| 148 |
+
jwe.recipients.push(target);
|
| 149 |
+
if (i === 0) {
|
| 150 |
+
const flattened = await new FlattenedEncrypt(this.#plaintext)
|
| 151 |
+
.setAdditionalAuthenticatedData(this.#aad)
|
| 152 |
+
.setContentEncryptionKey(cek)
|
| 153 |
+
.setProtectedHeader(this.#protectedHeader)
|
| 154 |
+
.setSharedUnprotectedHeader(this.#unprotectedHeader)
|
| 155 |
+
.setUnprotectedHeader(recipient.unprotectedHeader)
|
| 156 |
+
.setKeyManagementParameters(recipient.keyManagementParameters)
|
| 157 |
+
.encrypt(recipient.key, {
|
| 158 |
+
...recipient.options,
|
| 159 |
+
[unprotected]: true,
|
| 160 |
+
});
|
| 161 |
+
jwe.ciphertext = flattened.ciphertext;
|
| 162 |
+
jwe.iv = flattened.iv;
|
| 163 |
+
jwe.tag = flattened.tag;
|
| 164 |
+
if (flattened.aad)
|
| 165 |
+
jwe.aad = flattened.aad;
|
| 166 |
+
if (flattened.protected)
|
| 167 |
+
jwe.protected = flattened.protected;
|
| 168 |
+
if (flattened.unprotected)
|
| 169 |
+
jwe.unprotected = flattened.unprotected;
|
| 170 |
+
target.encrypted_key = flattened.encrypted_key;
|
| 171 |
+
if (flattened.header)
|
| 172 |
+
target.header = flattened.header;
|
| 173 |
+
continue;
|
| 174 |
+
}
|
| 175 |
+
const alg = recipient.unprotectedHeader?.alg ||
|
| 176 |
+
this.#protectedHeader?.alg ||
|
| 177 |
+
this.#unprotectedHeader?.alg;
|
| 178 |
+
checkKeyType(alg === 'dir' ? enc : alg, recipient.key, 'encrypt');
|
| 179 |
+
const k = await normalizeKey(recipient.key, alg);
|
| 180 |
+
const { encryptedKey, parameters } = await encryptKeyManagement(alg, enc, k, cek, recipient.keyManagementParameters);
|
| 181 |
+
target.encrypted_key = b64u(encryptedKey);
|
| 182 |
+
if (recipient.unprotectedHeader || parameters)
|
| 183 |
+
target.header = { ...recipient.unprotectedHeader, ...parameters };
|
| 184 |
+
}
|
| 185 |
+
return jwe;
|
| 186 |
+
}
|
| 187 |
+
}
|
public/vendor/jose/jwk/embedded.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { importJWK } from '../key/import.js';
|
| 2 |
+
import { isObject } from '../lib/is_object.js';
|
| 3 |
+
import { JWSInvalid } from '../util/errors.js';
|
| 4 |
+
export async function EmbeddedJWK(protectedHeader, token) {
|
| 5 |
+
const joseHeader = {
|
| 6 |
+
...protectedHeader,
|
| 7 |
+
...token?.header,
|
| 8 |
+
};
|
| 9 |
+
if (!isObject(joseHeader.jwk)) {
|
| 10 |
+
throw new JWSInvalid('"jwk" (JSON Web Key) Header Parameter must be a JSON object');
|
| 11 |
+
}
|
| 12 |
+
const key = await importJWK({ ...joseHeader.jwk, ext: true }, joseHeader.alg);
|
| 13 |
+
if (key instanceof Uint8Array || key.type !== 'public') {
|
| 14 |
+
throw new JWSInvalid('"jwk" (JSON Web Key) Header Parameter must be a public key');
|
| 15 |
+
}
|
| 16 |
+
return key;
|
| 17 |
+
}
|
public/vendor/jose/jwk/thumbprint.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { digest } from '../lib/digest.js';
|
| 2 |
+
import { encode as b64u } from '../util/base64url.js';
|
| 3 |
+
import { JOSENotSupported, JWKInvalid } from '../util/errors.js';
|
| 4 |
+
import { encode } from '../lib/buffer_utils.js';
|
| 5 |
+
import { isKeyLike } from '../lib/is_key_like.js';
|
| 6 |
+
import { isJWK } from '../lib/is_jwk.js';
|
| 7 |
+
import { exportJWK } from '../key/export.js';
|
| 8 |
+
import { invalidKeyInput } from '../lib/invalid_key_input.js';
|
| 9 |
+
const check = (value, description) => {
|
| 10 |
+
if (typeof value !== 'string' || !value) {
|
| 11 |
+
throw new JWKInvalid(`${description} missing or invalid`);
|
| 12 |
+
}
|
| 13 |
+
};
|
| 14 |
+
export async function calculateJwkThumbprint(key, digestAlgorithm) {
|
| 15 |
+
let jwk;
|
| 16 |
+
if (isJWK(key)) {
|
| 17 |
+
jwk = key;
|
| 18 |
+
}
|
| 19 |
+
else if (isKeyLike(key)) {
|
| 20 |
+
jwk = await exportJWK(key);
|
| 21 |
+
}
|
| 22 |
+
else {
|
| 23 |
+
throw new TypeError(invalidKeyInput(key, 'CryptoKey', 'KeyObject', 'JSON Web Key'));
|
| 24 |
+
}
|
| 25 |
+
digestAlgorithm ??= 'sha256';
|
| 26 |
+
if (digestAlgorithm !== 'sha256' &&
|
| 27 |
+
digestAlgorithm !== 'sha384' &&
|
| 28 |
+
digestAlgorithm !== 'sha512') {
|
| 29 |
+
throw new TypeError('digestAlgorithm must one of "sha256", "sha384", or "sha512"');
|
| 30 |
+
}
|
| 31 |
+
let components;
|
| 32 |
+
switch (jwk.kty) {
|
| 33 |
+
case 'AKP':
|
| 34 |
+
check(jwk.alg, '"alg" (Algorithm) Parameter');
|
| 35 |
+
check(jwk.pub, '"pub" (Public key) Parameter');
|
| 36 |
+
components = { alg: jwk.alg, kty: jwk.kty, pub: jwk.pub };
|
| 37 |
+
break;
|
| 38 |
+
case 'EC':
|
| 39 |
+
check(jwk.crv, '"crv" (Curve) Parameter');
|
| 40 |
+
check(jwk.x, '"x" (X Coordinate) Parameter');
|
| 41 |
+
check(jwk.y, '"y" (Y Coordinate) Parameter');
|
| 42 |
+
components = { crv: jwk.crv, kty: jwk.kty, x: jwk.x, y: jwk.y };
|
| 43 |
+
break;
|
| 44 |
+
case 'OKP':
|
| 45 |
+
check(jwk.crv, '"crv" (Subtype of Key Pair) Parameter');
|
| 46 |
+
check(jwk.x, '"x" (Public Key) Parameter');
|
| 47 |
+
components = { crv: jwk.crv, kty: jwk.kty, x: jwk.x };
|
| 48 |
+
break;
|
| 49 |
+
case 'RSA':
|
| 50 |
+
check(jwk.e, '"e" (Exponent) Parameter');
|
| 51 |
+
check(jwk.n, '"n" (Modulus) Parameter');
|
| 52 |
+
components = { e: jwk.e, kty: jwk.kty, n: jwk.n };
|
| 53 |
+
break;
|
| 54 |
+
case 'oct':
|
| 55 |
+
check(jwk.k, '"k" (Key Value) Parameter');
|
| 56 |
+
components = { k: jwk.k, kty: jwk.kty };
|
| 57 |
+
break;
|
| 58 |
+
default:
|
| 59 |
+
throw new JOSENotSupported('"kty" (Key Type) Parameter missing or unsupported');
|
| 60 |
+
}
|
| 61 |
+
const data = encode(JSON.stringify(components));
|
| 62 |
+
return b64u(await digest(digestAlgorithm, data));
|
| 63 |
+
}
|
| 64 |
+
export async function calculateJwkThumbprintUri(key, digestAlgorithm) {
|
| 65 |
+
digestAlgorithm ??= 'sha256';
|
| 66 |
+
const thumbprint = await calculateJwkThumbprint(key, digestAlgorithm);
|
| 67 |
+
return `urn:ietf:params:oauth:jwk-thumbprint:sha-${digestAlgorithm.slice(-3)}:${thumbprint}`;
|
| 68 |
+
}
|
public/vendor/jose/jwks/local.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { importJWK } from '../key/import.js';
|
| 2 |
+
import { JWKSInvalid, JOSENotSupported, JWKSNoMatchingKey, JWKSMultipleMatchingKeys, } from '../util/errors.js';
|
| 3 |
+
import { isObject } from '../lib/is_object.js';
|
| 4 |
+
function getKtyFromAlg(alg) {
|
| 5 |
+
switch (typeof alg === 'string' && alg.slice(0, 2)) {
|
| 6 |
+
case 'RS':
|
| 7 |
+
case 'PS':
|
| 8 |
+
return 'RSA';
|
| 9 |
+
case 'ES':
|
| 10 |
+
return 'EC';
|
| 11 |
+
case 'Ed':
|
| 12 |
+
return 'OKP';
|
| 13 |
+
case 'ML':
|
| 14 |
+
return 'AKP';
|
| 15 |
+
default:
|
| 16 |
+
throw new JOSENotSupported('Unsupported "alg" value for a JSON Web Key Set');
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
function isJWKSLike(jwks) {
|
| 20 |
+
return (jwks &&
|
| 21 |
+
typeof jwks === 'object' &&
|
| 22 |
+
Array.isArray(jwks.keys) &&
|
| 23 |
+
jwks.keys.every(isJWKLike));
|
| 24 |
+
}
|
| 25 |
+
function isJWKLike(key) {
|
| 26 |
+
return isObject(key);
|
| 27 |
+
}
|
| 28 |
+
class LocalJWKSet {
|
| 29 |
+
#jwks;
|
| 30 |
+
#cached = new WeakMap();
|
| 31 |
+
constructor(jwks) {
|
| 32 |
+
if (!isJWKSLike(jwks)) {
|
| 33 |
+
throw new JWKSInvalid('JSON Web Key Set malformed');
|
| 34 |
+
}
|
| 35 |
+
this.#jwks = structuredClone(jwks);
|
| 36 |
+
}
|
| 37 |
+
jwks() {
|
| 38 |
+
return this.#jwks;
|
| 39 |
+
}
|
| 40 |
+
async getKey(protectedHeader, token) {
|
| 41 |
+
const { alg, kid } = { ...protectedHeader, ...token?.header };
|
| 42 |
+
const kty = getKtyFromAlg(alg);
|
| 43 |
+
const candidates = this.#jwks.keys.filter((jwk) => {
|
| 44 |
+
let candidate = kty === jwk.kty;
|
| 45 |
+
if (candidate && typeof kid === 'string') {
|
| 46 |
+
candidate = kid === jwk.kid;
|
| 47 |
+
}
|
| 48 |
+
if (candidate && (typeof jwk.alg === 'string' || kty === 'AKP')) {
|
| 49 |
+
candidate = alg === jwk.alg;
|
| 50 |
+
}
|
| 51 |
+
if (candidate && typeof jwk.use === 'string') {
|
| 52 |
+
candidate = jwk.use === 'sig';
|
| 53 |
+
}
|
| 54 |
+
if (candidate && Array.isArray(jwk.key_ops)) {
|
| 55 |
+
candidate = jwk.key_ops.includes('verify');
|
| 56 |
+
}
|
| 57 |
+
if (candidate) {
|
| 58 |
+
switch (alg) {
|
| 59 |
+
case 'ES256':
|
| 60 |
+
candidate = jwk.crv === 'P-256';
|
| 61 |
+
break;
|
| 62 |
+
case 'ES384':
|
| 63 |
+
candidate = jwk.crv === 'P-384';
|
| 64 |
+
break;
|
| 65 |
+
case 'ES512':
|
| 66 |
+
candidate = jwk.crv === 'P-521';
|
| 67 |
+
break;
|
| 68 |
+
case 'Ed25519':
|
| 69 |
+
case 'EdDSA':
|
| 70 |
+
candidate = jwk.crv === 'Ed25519';
|
| 71 |
+
break;
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
return candidate;
|
| 75 |
+
});
|
| 76 |
+
const { 0: jwk, length } = candidates;
|
| 77 |
+
if (length === 0) {
|
| 78 |
+
throw new JWKSNoMatchingKey();
|
| 79 |
+
}
|
| 80 |
+
if (length !== 1) {
|
| 81 |
+
const error = new JWKSMultipleMatchingKeys();
|
| 82 |
+
const _cached = this.#cached;
|
| 83 |
+
error[Symbol.asyncIterator] = async function* () {
|
| 84 |
+
for (const jwk of candidates) {
|
| 85 |
+
try {
|
| 86 |
+
yield await importWithAlgCache(_cached, jwk, alg);
|
| 87 |
+
}
|
| 88 |
+
catch { }
|
| 89 |
+
}
|
| 90 |
+
};
|
| 91 |
+
throw error;
|
| 92 |
+
}
|
| 93 |
+
return importWithAlgCache(this.#cached, jwk, alg);
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
+
async function importWithAlgCache(cache, jwk, alg) {
|
| 97 |
+
const cached = cache.get(jwk) || cache.set(jwk, {}).get(jwk);
|
| 98 |
+
if (cached[alg] === undefined) {
|
| 99 |
+
const key = await importJWK({ ...jwk, ext: true }, alg);
|
| 100 |
+
if (key instanceof Uint8Array || key.type !== 'public') {
|
| 101 |
+
throw new JWKSInvalid('JSON Web Key Set members must be public keys');
|
| 102 |
+
}
|
| 103 |
+
cached[alg] = key;
|
| 104 |
+
}
|
| 105 |
+
return cached[alg];
|
| 106 |
+
}
|
| 107 |
+
export function createLocalJWKSet(jwks) {
|
| 108 |
+
const set = new LocalJWKSet(jwks);
|
| 109 |
+
const localJWKSet = async (protectedHeader, token) => set.getKey(protectedHeader, token);
|
| 110 |
+
Object.defineProperties(localJWKSet, {
|
| 111 |
+
jwks: {
|
| 112 |
+
value: () => structuredClone(set.jwks()),
|
| 113 |
+
enumerable: false,
|
| 114 |
+
configurable: false,
|
| 115 |
+
writable: false,
|
| 116 |
+
},
|
| 117 |
+
});
|
| 118 |
+
return localJWKSet;
|
| 119 |
+
}
|
public/vendor/jose/jwks/remote.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { JOSEError, JWKSNoMatchingKey, JWKSTimeout } from '../util/errors.js';
|
| 2 |
+
import { createLocalJWKSet } from './local.js';
|
| 3 |
+
import { isObject } from '../lib/is_object.js';
|
| 4 |
+
function isCloudflareWorkers() {
|
| 5 |
+
return (typeof WebSocketPair !== 'undefined' ||
|
| 6 |
+
(typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') ||
|
| 7 |
+
(typeof EdgeRuntime !== 'undefined' && EdgeRuntime === 'vercel'));
|
| 8 |
+
}
|
| 9 |
+
let USER_AGENT;
|
| 10 |
+
if (typeof navigator === 'undefined' || !navigator.userAgent?.startsWith?.('Mozilla/5.0 ')) {
|
| 11 |
+
const NAME = 'jose';
|
| 12 |
+
const VERSION = 'v6.1.3';
|
| 13 |
+
USER_AGENT = `${NAME}/${VERSION}`;
|
| 14 |
+
}
|
| 15 |
+
export const customFetch = Symbol();
|
| 16 |
+
async function fetchJwks(url, headers, signal, fetchImpl = fetch) {
|
| 17 |
+
const response = await fetchImpl(url, {
|
| 18 |
+
method: 'GET',
|
| 19 |
+
signal,
|
| 20 |
+
redirect: 'manual',
|
| 21 |
+
headers,
|
| 22 |
+
}).catch((err) => {
|
| 23 |
+
if (err.name === 'TimeoutError') {
|
| 24 |
+
throw new JWKSTimeout();
|
| 25 |
+
}
|
| 26 |
+
throw err;
|
| 27 |
+
});
|
| 28 |
+
if (response.status !== 200) {
|
| 29 |
+
throw new JOSEError('Expected 200 OK from the JSON Web Key Set HTTP response');
|
| 30 |
+
}
|
| 31 |
+
try {
|
| 32 |
+
return await response.json();
|
| 33 |
+
}
|
| 34 |
+
catch {
|
| 35 |
+
throw new JOSEError('Failed to parse the JSON Web Key Set HTTP response as JSON');
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
export const jwksCache = Symbol();
|
| 39 |
+
function isFreshJwksCache(input, cacheMaxAge) {
|
| 40 |
+
if (typeof input !== 'object' || input === null) {
|
| 41 |
+
return false;
|
| 42 |
+
}
|
| 43 |
+
if (!('uat' in input) || typeof input.uat !== 'number' || Date.now() - input.uat >= cacheMaxAge) {
|
| 44 |
+
return false;
|
| 45 |
+
}
|
| 46 |
+
if (!('jwks' in input) ||
|
| 47 |
+
!isObject(input.jwks) ||
|
| 48 |
+
!Array.isArray(input.jwks.keys) ||
|
| 49 |
+
!Array.prototype.every.call(input.jwks.keys, isObject)) {
|
| 50 |
+
return false;
|
| 51 |
+
}
|
| 52 |
+
return true;
|
| 53 |
+
}
|
| 54 |
+
class RemoteJWKSet {
|
| 55 |
+
#url;
|
| 56 |
+
#timeoutDuration;
|
| 57 |
+
#cooldownDuration;
|
| 58 |
+
#cacheMaxAge;
|
| 59 |
+
#jwksTimestamp;
|
| 60 |
+
#pendingFetch;
|
| 61 |
+
#headers;
|
| 62 |
+
#customFetch;
|
| 63 |
+
#local;
|
| 64 |
+
#cache;
|
| 65 |
+
constructor(url, options) {
|
| 66 |
+
if (!(url instanceof URL)) {
|
| 67 |
+
throw new TypeError('url must be an instance of URL');
|
| 68 |
+
}
|
| 69 |
+
this.#url = new URL(url.href);
|
| 70 |
+
this.#timeoutDuration =
|
| 71 |
+
typeof options?.timeoutDuration === 'number' ? options?.timeoutDuration : 5000;
|
| 72 |
+
this.#cooldownDuration =
|
| 73 |
+
typeof options?.cooldownDuration === 'number' ? options?.cooldownDuration : 30000;
|
| 74 |
+
this.#cacheMaxAge = typeof options?.cacheMaxAge === 'number' ? options?.cacheMaxAge : 600000;
|
| 75 |
+
this.#headers = new Headers(options?.headers);
|
| 76 |
+
if (USER_AGENT && !this.#headers.has('User-Agent')) {
|
| 77 |
+
this.#headers.set('User-Agent', USER_AGENT);
|
| 78 |
+
}
|
| 79 |
+
if (!this.#headers.has('accept')) {
|
| 80 |
+
this.#headers.set('accept', 'application/json');
|
| 81 |
+
this.#headers.append('accept', 'application/jwk-set+json');
|
| 82 |
+
}
|
| 83 |
+
this.#customFetch = options?.[customFetch];
|
| 84 |
+
if (options?.[jwksCache] !== undefined) {
|
| 85 |
+
this.#cache = options?.[jwksCache];
|
| 86 |
+
if (isFreshJwksCache(options?.[jwksCache], this.#cacheMaxAge)) {
|
| 87 |
+
this.#jwksTimestamp = this.#cache.uat;
|
| 88 |
+
this.#local = createLocalJWKSet(this.#cache.jwks);
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
pendingFetch() {
|
| 93 |
+
return !!this.#pendingFetch;
|
| 94 |
+
}
|
| 95 |
+
coolingDown() {
|
| 96 |
+
return typeof this.#jwksTimestamp === 'number'
|
| 97 |
+
? Date.now() < this.#jwksTimestamp + this.#cooldownDuration
|
| 98 |
+
: false;
|
| 99 |
+
}
|
| 100 |
+
fresh() {
|
| 101 |
+
return typeof this.#jwksTimestamp === 'number'
|
| 102 |
+
? Date.now() < this.#jwksTimestamp + this.#cacheMaxAge
|
| 103 |
+
: false;
|
| 104 |
+
}
|
| 105 |
+
jwks() {
|
| 106 |
+
return this.#local?.jwks();
|
| 107 |
+
}
|
| 108 |
+
async getKey(protectedHeader, token) {
|
| 109 |
+
if (!this.#local || !this.fresh()) {
|
| 110 |
+
await this.reload();
|
| 111 |
+
}
|
| 112 |
+
try {
|
| 113 |
+
return await this.#local(protectedHeader, token);
|
| 114 |
+
}
|
| 115 |
+
catch (err) {
|
| 116 |
+
if (err instanceof JWKSNoMatchingKey) {
|
| 117 |
+
if (this.coolingDown() === false) {
|
| 118 |
+
await this.reload();
|
| 119 |
+
return this.#local(protectedHeader, token);
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
throw err;
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
async reload() {
|
| 126 |
+
if (this.#pendingFetch && isCloudflareWorkers()) {
|
| 127 |
+
this.#pendingFetch = undefined;
|
| 128 |
+
}
|
| 129 |
+
this.#pendingFetch ||= fetchJwks(this.#url.href, this.#headers, AbortSignal.timeout(this.#timeoutDuration), this.#customFetch)
|
| 130 |
+
.then((json) => {
|
| 131 |
+
this.#local = createLocalJWKSet(json);
|
| 132 |
+
if (this.#cache) {
|
| 133 |
+
this.#cache.uat = Date.now();
|
| 134 |
+
this.#cache.jwks = json;
|
| 135 |
+
}
|
| 136 |
+
this.#jwksTimestamp = Date.now();
|
| 137 |
+
this.#pendingFetch = undefined;
|
| 138 |
+
})
|
| 139 |
+
.catch((err) => {
|
| 140 |
+
this.#pendingFetch = undefined;
|
| 141 |
+
throw err;
|
| 142 |
+
});
|
| 143 |
+
await this.#pendingFetch;
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
export function createRemoteJWKSet(url, options) {
|
| 147 |
+
const set = new RemoteJWKSet(url, options);
|
| 148 |
+
const remoteJWKSet = async (protectedHeader, token) => set.getKey(protectedHeader, token);
|
| 149 |
+
Object.defineProperties(remoteJWKSet, {
|
| 150 |
+
coolingDown: {
|
| 151 |
+
get: () => set.coolingDown(),
|
| 152 |
+
enumerable: true,
|
| 153 |
+
configurable: false,
|
| 154 |
+
},
|
| 155 |
+
fresh: {
|
| 156 |
+
get: () => set.fresh(),
|
| 157 |
+
enumerable: true,
|
| 158 |
+
configurable: false,
|
| 159 |
+
},
|
| 160 |
+
reload: {
|
| 161 |
+
value: () => set.reload(),
|
| 162 |
+
enumerable: true,
|
| 163 |
+
configurable: false,
|
| 164 |
+
writable: false,
|
| 165 |
+
},
|
| 166 |
+
reloading: {
|
| 167 |
+
get: () => set.pendingFetch(),
|
| 168 |
+
enumerable: true,
|
| 169 |
+
configurable: false,
|
| 170 |
+
},
|
| 171 |
+
jwks: {
|
| 172 |
+
value: () => set.jwks(),
|
| 173 |
+
enumerable: true,
|
| 174 |
+
configurable: false,
|
| 175 |
+
writable: false,
|
| 176 |
+
},
|
| 177 |
+
});
|
| 178 |
+
return remoteJWKSet;
|
| 179 |
+
}
|
public/vendor/jose/jws/compact/sign.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { FlattenedSign } from '../flattened/sign.js';
|
| 2 |
+
export class CompactSign {
|
| 3 |
+
#flattened;
|
| 4 |
+
constructor(payload) {
|
| 5 |
+
this.#flattened = new FlattenedSign(payload);
|
| 6 |
+
}
|
| 7 |
+
setProtectedHeader(protectedHeader) {
|
| 8 |
+
this.#flattened.setProtectedHeader(protectedHeader);
|
| 9 |
+
return this;
|
| 10 |
+
}
|
| 11 |
+
async sign(key, options) {
|
| 12 |
+
const jws = await this.#flattened.sign(key, options);
|
| 13 |
+
if (jws.payload === undefined) {
|
| 14 |
+
throw new TypeError('use the flattened module for creating JWS with b64: false');
|
| 15 |
+
}
|
| 16 |
+
return `${jws.protected}.${jws.payload}.${jws.signature}`;
|
| 17 |
+
}
|
| 18 |
+
}
|
public/vendor/jose/jws/compact/verify.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { flattenedVerify } from '../flattened/verify.js';
|
| 2 |
+
import { JWSInvalid } from '../../util/errors.js';
|
| 3 |
+
import { decoder } from '../../lib/buffer_utils.js';
|
| 4 |
+
export async function compactVerify(jws, key, options) {
|
| 5 |
+
if (jws instanceof Uint8Array) {
|
| 6 |
+
jws = decoder.decode(jws);
|
| 7 |
+
}
|
| 8 |
+
if (typeof jws !== 'string') {
|
| 9 |
+
throw new JWSInvalid('Compact JWS must be a string or Uint8Array');
|
| 10 |
+
}
|
| 11 |
+
const { 0: protectedHeader, 1: payload, 2: signature, length } = jws.split('.');
|
| 12 |
+
if (length !== 3) {
|
| 13 |
+
throw new JWSInvalid('Invalid Compact JWS');
|
| 14 |
+
}
|
| 15 |
+
const verified = await flattenedVerify({ payload, protected: protectedHeader, signature }, key, options);
|
| 16 |
+
const result = { payload: verified.payload, protectedHeader: verified.protectedHeader };
|
| 17 |
+
if (typeof key === 'function') {
|
| 18 |
+
return { ...result, key: verified.key };
|
| 19 |
+
}
|
| 20 |
+
return result;
|
| 21 |
+
}
|
public/vendor/jose/jws/flattened/sign.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { encode as b64u } from '../../util/base64url.js';
|
| 2 |
+
import { sign } from '../../lib/sign.js';
|
| 3 |
+
import { isDisjoint } from '../../lib/is_disjoint.js';
|
| 4 |
+
import { JWSInvalid } from '../../util/errors.js';
|
| 5 |
+
import { concat, encode } from '../../lib/buffer_utils.js';
|
| 6 |
+
import { checkKeyType } from '../../lib/check_key_type.js';
|
| 7 |
+
import { validateCrit } from '../../lib/validate_crit.js';
|
| 8 |
+
import { normalizeKey } from '../../lib/normalize_key.js';
|
| 9 |
+
export class FlattenedSign {
|
| 10 |
+
#payload;
|
| 11 |
+
#protectedHeader;
|
| 12 |
+
#unprotectedHeader;
|
| 13 |
+
constructor(payload) {
|
| 14 |
+
if (!(payload instanceof Uint8Array)) {
|
| 15 |
+
throw new TypeError('payload must be an instance of Uint8Array');
|
| 16 |
+
}
|
| 17 |
+
this.#payload = payload;
|
| 18 |
+
}
|
| 19 |
+
setProtectedHeader(protectedHeader) {
|
| 20 |
+
if (this.#protectedHeader) {
|
| 21 |
+
throw new TypeError('setProtectedHeader can only be called once');
|
| 22 |
+
}
|
| 23 |
+
this.#protectedHeader = protectedHeader;
|
| 24 |
+
return this;
|
| 25 |
+
}
|
| 26 |
+
setUnprotectedHeader(unprotectedHeader) {
|
| 27 |
+
if (this.#unprotectedHeader) {
|
| 28 |
+
throw new TypeError('setUnprotectedHeader can only be called once');
|
| 29 |
+
}
|
| 30 |
+
this.#unprotectedHeader = unprotectedHeader;
|
| 31 |
+
return this;
|
| 32 |
+
}
|
| 33 |
+
async sign(key, options) {
|
| 34 |
+
if (!this.#protectedHeader && !this.#unprotectedHeader) {
|
| 35 |
+
throw new JWSInvalid('either setProtectedHeader or setUnprotectedHeader must be called before #sign()');
|
| 36 |
+
}
|
| 37 |
+
if (!isDisjoint(this.#protectedHeader, this.#unprotectedHeader)) {
|
| 38 |
+
throw new JWSInvalid('JWS Protected and JWS Unprotected Header Parameter names must be disjoint');
|
| 39 |
+
}
|
| 40 |
+
const joseHeader = {
|
| 41 |
+
...this.#protectedHeader,
|
| 42 |
+
...this.#unprotectedHeader,
|
| 43 |
+
};
|
| 44 |
+
const extensions = validateCrit(JWSInvalid, new Map([['b64', true]]), options?.crit, this.#protectedHeader, joseHeader);
|
| 45 |
+
let b64 = true;
|
| 46 |
+
if (extensions.has('b64')) {
|
| 47 |
+
b64 = this.#protectedHeader.b64;
|
| 48 |
+
if (typeof b64 !== 'boolean') {
|
| 49 |
+
throw new JWSInvalid('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
const { alg } = joseHeader;
|
| 53 |
+
if (typeof alg !== 'string' || !alg) {
|
| 54 |
+
throw new JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
| 55 |
+
}
|
| 56 |
+
checkKeyType(alg, key, 'sign');
|
| 57 |
+
let payloadS;
|
| 58 |
+
let payloadB;
|
| 59 |
+
if (b64) {
|
| 60 |
+
payloadS = b64u(this.#payload);
|
| 61 |
+
payloadB = encode(payloadS);
|
| 62 |
+
}
|
| 63 |
+
else {
|
| 64 |
+
payloadB = this.#payload;
|
| 65 |
+
payloadS = '';
|
| 66 |
+
}
|
| 67 |
+
let protectedHeaderString;
|
| 68 |
+
let protectedHeaderBytes;
|
| 69 |
+
if (this.#protectedHeader) {
|
| 70 |
+
protectedHeaderString = b64u(JSON.stringify(this.#protectedHeader));
|
| 71 |
+
protectedHeaderBytes = encode(protectedHeaderString);
|
| 72 |
+
}
|
| 73 |
+
else {
|
| 74 |
+
protectedHeaderString = '';
|
| 75 |
+
protectedHeaderBytes = new Uint8Array();
|
| 76 |
+
}
|
| 77 |
+
const data = concat(protectedHeaderBytes, encode('.'), payloadB);
|
| 78 |
+
const k = await normalizeKey(key, alg);
|
| 79 |
+
const signature = await sign(alg, k, data);
|
| 80 |
+
const jws = {
|
| 81 |
+
signature: b64u(signature),
|
| 82 |
+
payload: payloadS,
|
| 83 |
+
};
|
| 84 |
+
if (this.#unprotectedHeader) {
|
| 85 |
+
jws.header = this.#unprotectedHeader;
|
| 86 |
+
}
|
| 87 |
+
if (this.#protectedHeader) {
|
| 88 |
+
jws.protected = protectedHeaderString;
|
| 89 |
+
}
|
| 90 |
+
return jws;
|
| 91 |
+
}
|
| 92 |
+
}
|
public/vendor/jose/jws/flattened/verify.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { decode as b64u } from '../../util/base64url.js';
|
| 2 |
+
import { verify } from '../../lib/verify.js';
|
| 3 |
+
import { JOSEAlgNotAllowed, JWSInvalid, JWSSignatureVerificationFailed } from '../../util/errors.js';
|
| 4 |
+
import { concat, encoder, decoder, encode } from '../../lib/buffer_utils.js';
|
| 5 |
+
import { isDisjoint } from '../../lib/is_disjoint.js';
|
| 6 |
+
import { isObject } from '../../lib/is_object.js';
|
| 7 |
+
import { checkKeyType } from '../../lib/check_key_type.js';
|
| 8 |
+
import { validateCrit } from '../../lib/validate_crit.js';
|
| 9 |
+
import { validateAlgorithms } from '../../lib/validate_algorithms.js';
|
| 10 |
+
import { normalizeKey } from '../../lib/normalize_key.js';
|
| 11 |
+
export async function flattenedVerify(jws, key, options) {
|
| 12 |
+
if (!isObject(jws)) {
|
| 13 |
+
throw new JWSInvalid('Flattened JWS must be an object');
|
| 14 |
+
}
|
| 15 |
+
if (jws.protected === undefined && jws.header === undefined) {
|
| 16 |
+
throw new JWSInvalid('Flattened JWS must have either of the "protected" or "header" members');
|
| 17 |
+
}
|
| 18 |
+
if (jws.protected !== undefined && typeof jws.protected !== 'string') {
|
| 19 |
+
throw new JWSInvalid('JWS Protected Header incorrect type');
|
| 20 |
+
}
|
| 21 |
+
if (jws.payload === undefined) {
|
| 22 |
+
throw new JWSInvalid('JWS Payload missing');
|
| 23 |
+
}
|
| 24 |
+
if (typeof jws.signature !== 'string') {
|
| 25 |
+
throw new JWSInvalid('JWS Signature missing or incorrect type');
|
| 26 |
+
}
|
| 27 |
+
if (jws.header !== undefined && !isObject(jws.header)) {
|
| 28 |
+
throw new JWSInvalid('JWS Unprotected Header incorrect type');
|
| 29 |
+
}
|
| 30 |
+
let parsedProt = {};
|
| 31 |
+
if (jws.protected) {
|
| 32 |
+
try {
|
| 33 |
+
const protectedHeader = b64u(jws.protected);
|
| 34 |
+
parsedProt = JSON.parse(decoder.decode(protectedHeader));
|
| 35 |
+
}
|
| 36 |
+
catch {
|
| 37 |
+
throw new JWSInvalid('JWS Protected Header is invalid');
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
if (!isDisjoint(parsedProt, jws.header)) {
|
| 41 |
+
throw new JWSInvalid('JWS Protected and JWS Unprotected Header Parameter names must be disjoint');
|
| 42 |
+
}
|
| 43 |
+
const joseHeader = {
|
| 44 |
+
...parsedProt,
|
| 45 |
+
...jws.header,
|
| 46 |
+
};
|
| 47 |
+
const extensions = validateCrit(JWSInvalid, new Map([['b64', true]]), options?.crit, parsedProt, joseHeader);
|
| 48 |
+
let b64 = true;
|
| 49 |
+
if (extensions.has('b64')) {
|
| 50 |
+
b64 = parsedProt.b64;
|
| 51 |
+
if (typeof b64 !== 'boolean') {
|
| 52 |
+
throw new JWSInvalid('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
const { alg } = joseHeader;
|
| 56 |
+
if (typeof alg !== 'string' || !alg) {
|
| 57 |
+
throw new JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
| 58 |
+
}
|
| 59 |
+
const algorithms = options && validateAlgorithms('algorithms', options.algorithms);
|
| 60 |
+
if (algorithms && !algorithms.has(alg)) {
|
| 61 |
+
throw new JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter value not allowed');
|
| 62 |
+
}
|
| 63 |
+
if (b64) {
|
| 64 |
+
if (typeof jws.payload !== 'string') {
|
| 65 |
+
throw new JWSInvalid('JWS Payload must be a string');
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
else if (typeof jws.payload !== 'string' && !(jws.payload instanceof Uint8Array)) {
|
| 69 |
+
throw new JWSInvalid('JWS Payload must be a string or an Uint8Array instance');
|
| 70 |
+
}
|
| 71 |
+
let resolvedKey = false;
|
| 72 |
+
if (typeof key === 'function') {
|
| 73 |
+
key = await key(parsedProt, jws);
|
| 74 |
+
resolvedKey = true;
|
| 75 |
+
}
|
| 76 |
+
checkKeyType(alg, key, 'verify');
|
| 77 |
+
const data = concat(jws.protected !== undefined ? encode(jws.protected) : new Uint8Array(), encode('.'), typeof jws.payload === 'string'
|
| 78 |
+
? b64
|
| 79 |
+
? encode(jws.payload)
|
| 80 |
+
: encoder.encode(jws.payload)
|
| 81 |
+
: jws.payload);
|
| 82 |
+
let signature;
|
| 83 |
+
try {
|
| 84 |
+
signature = b64u(jws.signature);
|
| 85 |
+
}
|
| 86 |
+
catch {
|
| 87 |
+
throw new JWSInvalid('Failed to base64url decode the signature');
|
| 88 |
+
}
|
| 89 |
+
const k = await normalizeKey(key, alg);
|
| 90 |
+
const verified = await verify(alg, k, signature, data);
|
| 91 |
+
if (!verified) {
|
| 92 |
+
throw new JWSSignatureVerificationFailed();
|
| 93 |
+
}
|
| 94 |
+
let payload;
|
| 95 |
+
if (b64) {
|
| 96 |
+
try {
|
| 97 |
+
payload = b64u(jws.payload);
|
| 98 |
+
}
|
| 99 |
+
catch {
|
| 100 |
+
throw new JWSInvalid('Failed to base64url decode the payload');
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
else if (typeof jws.payload === 'string') {
|
| 104 |
+
payload = encoder.encode(jws.payload);
|
| 105 |
+
}
|
| 106 |
+
else {
|
| 107 |
+
payload = jws.payload;
|
| 108 |
+
}
|
| 109 |
+
const result = { payload };
|
| 110 |
+
if (jws.protected !== undefined) {
|
| 111 |
+
result.protectedHeader = parsedProt;
|
| 112 |
+
}
|
| 113 |
+
if (jws.header !== undefined) {
|
| 114 |
+
result.unprotectedHeader = jws.header;
|
| 115 |
+
}
|
| 116 |
+
if (resolvedKey) {
|
| 117 |
+
return { ...result, key: k };
|
| 118 |
+
}
|
| 119 |
+
return result;
|
| 120 |
+
}
|
public/vendor/jose/jws/general/sign.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { FlattenedSign } from '../flattened/sign.js';
|
| 2 |
+
import { JWSInvalid } from '../../util/errors.js';
|
| 3 |
+
class IndividualSignature {
|
| 4 |
+
#parent;
|
| 5 |
+
protectedHeader;
|
| 6 |
+
unprotectedHeader;
|
| 7 |
+
options;
|
| 8 |
+
key;
|
| 9 |
+
constructor(sig, key, options) {
|
| 10 |
+
this.#parent = sig;
|
| 11 |
+
this.key = key;
|
| 12 |
+
this.options = options;
|
| 13 |
+
}
|
| 14 |
+
setProtectedHeader(protectedHeader) {
|
| 15 |
+
if (this.protectedHeader) {
|
| 16 |
+
throw new TypeError('setProtectedHeader can only be called once');
|
| 17 |
+
}
|
| 18 |
+
this.protectedHeader = protectedHeader;
|
| 19 |
+
return this;
|
| 20 |
+
}
|
| 21 |
+
setUnprotectedHeader(unprotectedHeader) {
|
| 22 |
+
if (this.unprotectedHeader) {
|
| 23 |
+
throw new TypeError('setUnprotectedHeader can only be called once');
|
| 24 |
+
}
|
| 25 |
+
this.unprotectedHeader = unprotectedHeader;
|
| 26 |
+
return this;
|
| 27 |
+
}
|
| 28 |
+
addSignature(...args) {
|
| 29 |
+
return this.#parent.addSignature(...args);
|
| 30 |
+
}
|
| 31 |
+
sign(...args) {
|
| 32 |
+
return this.#parent.sign(...args);
|
| 33 |
+
}
|
| 34 |
+
done() {
|
| 35 |
+
return this.#parent;
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
export class GeneralSign {
|
| 39 |
+
#payload;
|
| 40 |
+
#signatures = [];
|
| 41 |
+
constructor(payload) {
|
| 42 |
+
this.#payload = payload;
|
| 43 |
+
}
|
| 44 |
+
addSignature(key, options) {
|
| 45 |
+
const signature = new IndividualSignature(this, key, options);
|
| 46 |
+
this.#signatures.push(signature);
|
| 47 |
+
return signature;
|
| 48 |
+
}
|
| 49 |
+
async sign() {
|
| 50 |
+
if (!this.#signatures.length) {
|
| 51 |
+
throw new JWSInvalid('at least one signature must be added');
|
| 52 |
+
}
|
| 53 |
+
const jws = {
|
| 54 |
+
signatures: [],
|
| 55 |
+
payload: '',
|
| 56 |
+
};
|
| 57 |
+
for (let i = 0; i < this.#signatures.length; i++) {
|
| 58 |
+
const signature = this.#signatures[i];
|
| 59 |
+
const flattened = new FlattenedSign(this.#payload);
|
| 60 |
+
flattened.setProtectedHeader(signature.protectedHeader);
|
| 61 |
+
flattened.setUnprotectedHeader(signature.unprotectedHeader);
|
| 62 |
+
const { payload, ...rest } = await flattened.sign(signature.key, signature.options);
|
| 63 |
+
if (i === 0) {
|
| 64 |
+
jws.payload = payload;
|
| 65 |
+
}
|
| 66 |
+
else if (jws.payload !== payload) {
|
| 67 |
+
throw new JWSInvalid('inconsistent use of JWS Unencoded Payload (RFC7797)');
|
| 68 |
+
}
|
| 69 |
+
jws.signatures.push(rest);
|
| 70 |
+
}
|
| 71 |
+
return jws;
|
| 72 |
+
}
|
| 73 |
+
}
|
public/vendor/jose/jws/general/verify.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { flattenedVerify } from '../flattened/verify.js';
|
| 2 |
+
import { JWSInvalid, JWSSignatureVerificationFailed } from '../../util/errors.js';
|
| 3 |
+
import { isObject } from '../../lib/is_object.js';
|
| 4 |
+
export async function generalVerify(jws, key, options) {
|
| 5 |
+
if (!isObject(jws)) {
|
| 6 |
+
throw new JWSInvalid('General JWS must be an object');
|
| 7 |
+
}
|
| 8 |
+
if (!Array.isArray(jws.signatures) || !jws.signatures.every(isObject)) {
|
| 9 |
+
throw new JWSInvalid('JWS Signatures missing or incorrect type');
|
| 10 |
+
}
|
| 11 |
+
for (const signature of jws.signatures) {
|
| 12 |
+
try {
|
| 13 |
+
return await flattenedVerify({
|
| 14 |
+
header: signature.header,
|
| 15 |
+
payload: jws.payload,
|
| 16 |
+
protected: signature.protected,
|
| 17 |
+
signature: signature.signature,
|
| 18 |
+
}, key, options);
|
| 19 |
+
}
|
| 20 |
+
catch {
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
throw new JWSSignatureVerificationFailed();
|
| 24 |
+
}
|
public/vendor/jose/jwt/decrypt.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { compactDecrypt } from '../jwe/compact/decrypt.js';
|
| 2 |
+
import { validateClaimsSet } from '../lib/jwt_claims_set.js';
|
| 3 |
+
import { JWTClaimValidationFailed } from '../util/errors.js';
|
| 4 |
+
export async function jwtDecrypt(jwt, key, options) {
|
| 5 |
+
const decrypted = await compactDecrypt(jwt, key, options);
|
| 6 |
+
const payload = validateClaimsSet(decrypted.protectedHeader, decrypted.plaintext, options);
|
| 7 |
+
const { protectedHeader } = decrypted;
|
| 8 |
+
if (protectedHeader.iss !== undefined && protectedHeader.iss !== payload.iss) {
|
| 9 |
+
throw new JWTClaimValidationFailed('replicated "iss" claim header parameter mismatch', payload, 'iss', 'mismatch');
|
| 10 |
+
}
|
| 11 |
+
if (protectedHeader.sub !== undefined && protectedHeader.sub !== payload.sub) {
|
| 12 |
+
throw new JWTClaimValidationFailed('replicated "sub" claim header parameter mismatch', payload, 'sub', 'mismatch');
|
| 13 |
+
}
|
| 14 |
+
if (protectedHeader.aud !== undefined &&
|
| 15 |
+
JSON.stringify(protectedHeader.aud) !== JSON.stringify(payload.aud)) {
|
| 16 |
+
throw new JWTClaimValidationFailed('replicated "aud" claim header parameter mismatch', payload, 'aud', 'mismatch');
|
| 17 |
+
}
|
| 18 |
+
const result = { payload, protectedHeader };
|
| 19 |
+
if (typeof key === 'function') {
|
| 20 |
+
return { ...result, key: decrypted.key };
|
| 21 |
+
}
|
| 22 |
+
return result;
|
| 23 |
+
}
|
public/vendor/jose/jwt/encrypt.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { CompactEncrypt } from '../jwe/compact/encrypt.js';
|
| 2 |
+
import { JWTClaimsBuilder } from '../lib/jwt_claims_set.js';
|
| 3 |
+
export class EncryptJWT {
|
| 4 |
+
#cek;
|
| 5 |
+
#iv;
|
| 6 |
+
#keyManagementParameters;
|
| 7 |
+
#protectedHeader;
|
| 8 |
+
#replicateIssuerAsHeader;
|
| 9 |
+
#replicateSubjectAsHeader;
|
| 10 |
+
#replicateAudienceAsHeader;
|
| 11 |
+
#jwt;
|
| 12 |
+
constructor(payload = {}) {
|
| 13 |
+
this.#jwt = new JWTClaimsBuilder(payload);
|
| 14 |
+
}
|
| 15 |
+
setIssuer(issuer) {
|
| 16 |
+
this.#jwt.iss = issuer;
|
| 17 |
+
return this;
|
| 18 |
+
}
|
| 19 |
+
setSubject(subject) {
|
| 20 |
+
this.#jwt.sub = subject;
|
| 21 |
+
return this;
|
| 22 |
+
}
|
| 23 |
+
setAudience(audience) {
|
| 24 |
+
this.#jwt.aud = audience;
|
| 25 |
+
return this;
|
| 26 |
+
}
|
| 27 |
+
setJti(jwtId) {
|
| 28 |
+
this.#jwt.jti = jwtId;
|
| 29 |
+
return this;
|
| 30 |
+
}
|
| 31 |
+
setNotBefore(input) {
|
| 32 |
+
this.#jwt.nbf = input;
|
| 33 |
+
return this;
|
| 34 |
+
}
|
| 35 |
+
setExpirationTime(input) {
|
| 36 |
+
this.#jwt.exp = input;
|
| 37 |
+
return this;
|
| 38 |
+
}
|
| 39 |
+
setIssuedAt(input) {
|
| 40 |
+
this.#jwt.iat = input;
|
| 41 |
+
return this;
|
| 42 |
+
}
|
| 43 |
+
setProtectedHeader(protectedHeader) {
|
| 44 |
+
if (this.#protectedHeader) {
|
| 45 |
+
throw new TypeError('setProtectedHeader can only be called once');
|
| 46 |
+
}
|
| 47 |
+
this.#protectedHeader = protectedHeader;
|
| 48 |
+
return this;
|
| 49 |
+
}
|
| 50 |
+
setKeyManagementParameters(parameters) {
|
| 51 |
+
if (this.#keyManagementParameters) {
|
| 52 |
+
throw new TypeError('setKeyManagementParameters can only be called once');
|
| 53 |
+
}
|
| 54 |
+
this.#keyManagementParameters = parameters;
|
| 55 |
+
return this;
|
| 56 |
+
}
|
| 57 |
+
setContentEncryptionKey(cek) {
|
| 58 |
+
if (this.#cek) {
|
| 59 |
+
throw new TypeError('setContentEncryptionKey can only be called once');
|
| 60 |
+
}
|
| 61 |
+
this.#cek = cek;
|
| 62 |
+
return this;
|
| 63 |
+
}
|
| 64 |
+
setInitializationVector(iv) {
|
| 65 |
+
if (this.#iv) {
|
| 66 |
+
throw new TypeError('setInitializationVector can only be called once');
|
| 67 |
+
}
|
| 68 |
+
this.#iv = iv;
|
| 69 |
+
return this;
|
| 70 |
+
}
|
| 71 |
+
replicateIssuerAsHeader() {
|
| 72 |
+
this.#replicateIssuerAsHeader = true;
|
| 73 |
+
return this;
|
| 74 |
+
}
|
| 75 |
+
replicateSubjectAsHeader() {
|
| 76 |
+
this.#replicateSubjectAsHeader = true;
|
| 77 |
+
return this;
|
| 78 |
+
}
|
| 79 |
+
replicateAudienceAsHeader() {
|
| 80 |
+
this.#replicateAudienceAsHeader = true;
|
| 81 |
+
return this;
|
| 82 |
+
}
|
| 83 |
+
async encrypt(key, options) {
|
| 84 |
+
const enc = new CompactEncrypt(this.#jwt.data());
|
| 85 |
+
if (this.#protectedHeader &&
|
| 86 |
+
(this.#replicateIssuerAsHeader ||
|
| 87 |
+
this.#replicateSubjectAsHeader ||
|
| 88 |
+
this.#replicateAudienceAsHeader)) {
|
| 89 |
+
this.#protectedHeader = {
|
| 90 |
+
...this.#protectedHeader,
|
| 91 |
+
iss: this.#replicateIssuerAsHeader ? this.#jwt.iss : undefined,
|
| 92 |
+
sub: this.#replicateSubjectAsHeader ? this.#jwt.sub : undefined,
|
| 93 |
+
aud: this.#replicateAudienceAsHeader ? this.#jwt.aud : undefined,
|
| 94 |
+
};
|
| 95 |
+
}
|
| 96 |
+
enc.setProtectedHeader(this.#protectedHeader);
|
| 97 |
+
if (this.#iv) {
|
| 98 |
+
enc.setInitializationVector(this.#iv);
|
| 99 |
+
}
|
| 100 |
+
if (this.#cek) {
|
| 101 |
+
enc.setContentEncryptionKey(this.#cek);
|
| 102 |
+
}
|
| 103 |
+
if (this.#keyManagementParameters) {
|
| 104 |
+
enc.setKeyManagementParameters(this.#keyManagementParameters);
|
| 105 |
+
}
|
| 106 |
+
return enc.encrypt(key, options);
|
| 107 |
+
}
|
| 108 |
+
}
|
public/vendor/jose/jwt/sign.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { CompactSign } from '../jws/compact/sign.js';
|
| 2 |
+
import { JWTInvalid } from '../util/errors.js';
|
| 3 |
+
import { JWTClaimsBuilder } from '../lib/jwt_claims_set.js';
|
| 4 |
+
export class SignJWT {
|
| 5 |
+
#protectedHeader;
|
| 6 |
+
#jwt;
|
| 7 |
+
constructor(payload = {}) {
|
| 8 |
+
this.#jwt = new JWTClaimsBuilder(payload);
|
| 9 |
+
}
|
| 10 |
+
setIssuer(issuer) {
|
| 11 |
+
this.#jwt.iss = issuer;
|
| 12 |
+
return this;
|
| 13 |
+
}
|
| 14 |
+
setSubject(subject) {
|
| 15 |
+
this.#jwt.sub = subject;
|
| 16 |
+
return this;
|
| 17 |
+
}
|
| 18 |
+
setAudience(audience) {
|
| 19 |
+
this.#jwt.aud = audience;
|
| 20 |
+
return this;
|
| 21 |
+
}
|
| 22 |
+
setJti(jwtId) {
|
| 23 |
+
this.#jwt.jti = jwtId;
|
| 24 |
+
return this;
|
| 25 |
+
}
|
| 26 |
+
setNotBefore(input) {
|
| 27 |
+
this.#jwt.nbf = input;
|
| 28 |
+
return this;
|
| 29 |
+
}
|
| 30 |
+
setExpirationTime(input) {
|
| 31 |
+
this.#jwt.exp = input;
|
| 32 |
+
return this;
|
| 33 |
+
}
|
| 34 |
+
setIssuedAt(input) {
|
| 35 |
+
this.#jwt.iat = input;
|
| 36 |
+
return this;
|
| 37 |
+
}
|
| 38 |
+
setProtectedHeader(protectedHeader) {
|
| 39 |
+
this.#protectedHeader = protectedHeader;
|
| 40 |
+
return this;
|
| 41 |
+
}
|
| 42 |
+
async sign(key, options) {
|
| 43 |
+
const sig = new CompactSign(this.#jwt.data());
|
| 44 |
+
sig.setProtectedHeader(this.#protectedHeader);
|
| 45 |
+
if (Array.isArray(this.#protectedHeader?.crit) &&
|
| 46 |
+
this.#protectedHeader.crit.includes('b64') &&
|
| 47 |
+
this.#protectedHeader.b64 === false) {
|
| 48 |
+
throw new JWTInvalid('JWTs MUST NOT use unencoded payload');
|
| 49 |
+
}
|
| 50 |
+
return sig.sign(key, options);
|
| 51 |
+
}
|
| 52 |
+
}
|
public/vendor/jose/jwt/unsecured.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import * as b64u from '../util/base64url.js';
|
| 2 |
+
import { decoder } from '../lib/buffer_utils.js';
|
| 3 |
+
import { JWTInvalid } from '../util/errors.js';
|
| 4 |
+
import { validateClaimsSet, JWTClaimsBuilder } from '../lib/jwt_claims_set.js';
|
| 5 |
+
export class UnsecuredJWT {
|
| 6 |
+
#jwt;
|
| 7 |
+
constructor(payload = {}) {
|
| 8 |
+
this.#jwt = new JWTClaimsBuilder(payload);
|
| 9 |
+
}
|
| 10 |
+
encode() {
|
| 11 |
+
const header = b64u.encode(JSON.stringify({ alg: 'none' }));
|
| 12 |
+
const payload = b64u.encode(this.#jwt.data());
|
| 13 |
+
return `${header}.${payload}.`;
|
| 14 |
+
}
|
| 15 |
+
setIssuer(issuer) {
|
| 16 |
+
this.#jwt.iss = issuer;
|
| 17 |
+
return this;
|
| 18 |
+
}
|
| 19 |
+
setSubject(subject) {
|
| 20 |
+
this.#jwt.sub = subject;
|
| 21 |
+
return this;
|
| 22 |
+
}
|
| 23 |
+
setAudience(audience) {
|
| 24 |
+
this.#jwt.aud = audience;
|
| 25 |
+
return this;
|
| 26 |
+
}
|
| 27 |
+
setJti(jwtId) {
|
| 28 |
+
this.#jwt.jti = jwtId;
|
| 29 |
+
return this;
|
| 30 |
+
}
|
| 31 |
+
setNotBefore(input) {
|
| 32 |
+
this.#jwt.nbf = input;
|
| 33 |
+
return this;
|
| 34 |
+
}
|
| 35 |
+
setExpirationTime(input) {
|
| 36 |
+
this.#jwt.exp = input;
|
| 37 |
+
return this;
|
| 38 |
+
}
|
| 39 |
+
setIssuedAt(input) {
|
| 40 |
+
this.#jwt.iat = input;
|
| 41 |
+
return this;
|
| 42 |
+
}
|
| 43 |
+
static decode(jwt, options) {
|
| 44 |
+
if (typeof jwt !== 'string') {
|
| 45 |
+
throw new JWTInvalid('Unsecured JWT must be a string');
|
| 46 |
+
}
|
| 47 |
+
const { 0: encodedHeader, 1: encodedPayload, 2: signature, length } = jwt.split('.');
|
| 48 |
+
if (length !== 3 || signature !== '') {
|
| 49 |
+
throw new JWTInvalid('Invalid Unsecured JWT');
|
| 50 |
+
}
|
| 51 |
+
let header;
|
| 52 |
+
try {
|
| 53 |
+
header = JSON.parse(decoder.decode(b64u.decode(encodedHeader)));
|
| 54 |
+
if (header.alg !== 'none')
|
| 55 |
+
throw new Error();
|
| 56 |
+
}
|
| 57 |
+
catch {
|
| 58 |
+
throw new JWTInvalid('Invalid Unsecured JWT');
|
| 59 |
+
}
|
| 60 |
+
const payload = validateClaimsSet(header, b64u.decode(encodedPayload), options);
|
| 61 |
+
return { payload, header };
|
| 62 |
+
}
|
| 63 |
+
}
|
public/vendor/jose/jwt/verify.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { compactVerify } from '../jws/compact/verify.js';
|
| 2 |
+
import { validateClaimsSet } from '../lib/jwt_claims_set.js';
|
| 3 |
+
import { JWTInvalid } from '../util/errors.js';
|
| 4 |
+
export async function jwtVerify(jwt, key, options) {
|
| 5 |
+
const verified = await compactVerify(jwt, key, options);
|
| 6 |
+
if (verified.protectedHeader.crit?.includes('b64') && verified.protectedHeader.b64 === false) {
|
| 7 |
+
throw new JWTInvalid('JWTs MUST NOT use unencoded payload');
|
| 8 |
+
}
|
| 9 |
+
const payload = validateClaimsSet(verified.protectedHeader, verified.payload, options);
|
| 10 |
+
const result = { payload, protectedHeader: verified.protectedHeader };
|
| 11 |
+
if (typeof key === 'function') {
|
| 12 |
+
return { ...result, key: verified.key };
|
| 13 |
+
}
|
| 14 |
+
return result;
|
| 15 |
+
}
|
public/vendor/jose/key/export.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { toSPKI as exportPublic, toPKCS8 as exportPrivate } from '../lib/asn1.js';
|
| 2 |
+
import { keyToJWK } from '../lib/key_to_jwk.js';
|
| 3 |
+
export async function exportSPKI(key) {
|
| 4 |
+
return exportPublic(key);
|
| 5 |
+
}
|
| 6 |
+
export async function exportPKCS8(key) {
|
| 7 |
+
return exportPrivate(key);
|
| 8 |
+
}
|
| 9 |
+
export async function exportJWK(key) {
|
| 10 |
+
return keyToJWK(key);
|
| 11 |
+
}
|
public/vendor/jose/key/generate_key_pair.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { JOSENotSupported } from '../util/errors.js';
|
| 2 |
+
function getModulusLengthOption(options) {
|
| 3 |
+
const modulusLength = options?.modulusLength ?? 2048;
|
| 4 |
+
if (typeof modulusLength !== 'number' || modulusLength < 2048) {
|
| 5 |
+
throw new JOSENotSupported('Invalid or unsupported modulusLength option provided, 2048 bits or larger keys must be used');
|
| 6 |
+
}
|
| 7 |
+
return modulusLength;
|
| 8 |
+
}
|
| 9 |
+
export async function generateKeyPair(alg, options) {
|
| 10 |
+
let algorithm;
|
| 11 |
+
let keyUsages;
|
| 12 |
+
switch (alg) {
|
| 13 |
+
case 'PS256':
|
| 14 |
+
case 'PS384':
|
| 15 |
+
case 'PS512':
|
| 16 |
+
algorithm = {
|
| 17 |
+
name: 'RSA-PSS',
|
| 18 |
+
hash: `SHA-${alg.slice(-3)}`,
|
| 19 |
+
publicExponent: Uint8Array.of(0x01, 0x00, 0x01),
|
| 20 |
+
modulusLength: getModulusLengthOption(options),
|
| 21 |
+
};
|
| 22 |
+
keyUsages = ['sign', 'verify'];
|
| 23 |
+
break;
|
| 24 |
+
case 'RS256':
|
| 25 |
+
case 'RS384':
|
| 26 |
+
case 'RS512':
|
| 27 |
+
algorithm = {
|
| 28 |
+
name: 'RSASSA-PKCS1-v1_5',
|
| 29 |
+
hash: `SHA-${alg.slice(-3)}`,
|
| 30 |
+
publicExponent: Uint8Array.of(0x01, 0x00, 0x01),
|
| 31 |
+
modulusLength: getModulusLengthOption(options),
|
| 32 |
+
};
|
| 33 |
+
keyUsages = ['sign', 'verify'];
|
| 34 |
+
break;
|
| 35 |
+
case 'RSA-OAEP':
|
| 36 |
+
case 'RSA-OAEP-256':
|
| 37 |
+
case 'RSA-OAEP-384':
|
| 38 |
+
case 'RSA-OAEP-512':
|
| 39 |
+
algorithm = {
|
| 40 |
+
name: 'RSA-OAEP',
|
| 41 |
+
hash: `SHA-${parseInt(alg.slice(-3), 10) || 1}`,
|
| 42 |
+
publicExponent: Uint8Array.of(0x01, 0x00, 0x01),
|
| 43 |
+
modulusLength: getModulusLengthOption(options),
|
| 44 |
+
};
|
| 45 |
+
keyUsages = ['decrypt', 'unwrapKey', 'encrypt', 'wrapKey'];
|
| 46 |
+
break;
|
| 47 |
+
case 'ES256':
|
| 48 |
+
algorithm = { name: 'ECDSA', namedCurve: 'P-256' };
|
| 49 |
+
keyUsages = ['sign', 'verify'];
|
| 50 |
+
break;
|
| 51 |
+
case 'ES384':
|
| 52 |
+
algorithm = { name: 'ECDSA', namedCurve: 'P-384' };
|
| 53 |
+
keyUsages = ['sign', 'verify'];
|
| 54 |
+
break;
|
| 55 |
+
case 'ES512':
|
| 56 |
+
algorithm = { name: 'ECDSA', namedCurve: 'P-521' };
|
| 57 |
+
keyUsages = ['sign', 'verify'];
|
| 58 |
+
break;
|
| 59 |
+
case 'Ed25519':
|
| 60 |
+
case 'EdDSA': {
|
| 61 |
+
keyUsages = ['sign', 'verify'];
|
| 62 |
+
algorithm = { name: 'Ed25519' };
|
| 63 |
+
break;
|
| 64 |
+
}
|
| 65 |
+
case 'ML-DSA-44':
|
| 66 |
+
case 'ML-DSA-65':
|
| 67 |
+
case 'ML-DSA-87': {
|
| 68 |
+
keyUsages = ['sign', 'verify'];
|
| 69 |
+
algorithm = { name: alg };
|
| 70 |
+
break;
|
| 71 |
+
}
|
| 72 |
+
case 'ECDH-ES':
|
| 73 |
+
case 'ECDH-ES+A128KW':
|
| 74 |
+
case 'ECDH-ES+A192KW':
|
| 75 |
+
case 'ECDH-ES+A256KW': {
|
| 76 |
+
keyUsages = ['deriveBits'];
|
| 77 |
+
const crv = options?.crv ?? 'P-256';
|
| 78 |
+
switch (crv) {
|
| 79 |
+
case 'P-256':
|
| 80 |
+
case 'P-384':
|
| 81 |
+
case 'P-521': {
|
| 82 |
+
algorithm = { name: 'ECDH', namedCurve: crv };
|
| 83 |
+
break;
|
| 84 |
+
}
|
| 85 |
+
case 'X25519':
|
| 86 |
+
algorithm = { name: 'X25519' };
|
| 87 |
+
break;
|
| 88 |
+
default:
|
| 89 |
+
throw new JOSENotSupported('Invalid or unsupported crv option provided, supported values are P-256, P-384, P-521, and X25519');
|
| 90 |
+
}
|
| 91 |
+
break;
|
| 92 |
+
}
|
| 93 |
+
default:
|
| 94 |
+
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
|
| 95 |
+
}
|
| 96 |
+
return crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages);
|
| 97 |
+
}
|
public/vendor/jose/key/generate_secret.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { JOSENotSupported } from '../util/errors.js';
|
| 2 |
+
export async function generateSecret(alg, options) {
|
| 3 |
+
let length;
|
| 4 |
+
let algorithm;
|
| 5 |
+
let keyUsages;
|
| 6 |
+
switch (alg) {
|
| 7 |
+
case 'HS256':
|
| 8 |
+
case 'HS384':
|
| 9 |
+
case 'HS512':
|
| 10 |
+
length = parseInt(alg.slice(-3), 10);
|
| 11 |
+
algorithm = { name: 'HMAC', hash: `SHA-${length}`, length };
|
| 12 |
+
keyUsages = ['sign', 'verify'];
|
| 13 |
+
break;
|
| 14 |
+
case 'A128CBC-HS256':
|
| 15 |
+
case 'A192CBC-HS384':
|
| 16 |
+
case 'A256CBC-HS512':
|
| 17 |
+
length = parseInt(alg.slice(-3), 10);
|
| 18 |
+
return crypto.getRandomValues(new Uint8Array(length >> 3));
|
| 19 |
+
case 'A128KW':
|
| 20 |
+
case 'A192KW':
|
| 21 |
+
case 'A256KW':
|
| 22 |
+
length = parseInt(alg.slice(1, 4), 10);
|
| 23 |
+
algorithm = { name: 'AES-KW', length };
|
| 24 |
+
keyUsages = ['wrapKey', 'unwrapKey'];
|
| 25 |
+
break;
|
| 26 |
+
case 'A128GCMKW':
|
| 27 |
+
case 'A192GCMKW':
|
| 28 |
+
case 'A256GCMKW':
|
| 29 |
+
case 'A128GCM':
|
| 30 |
+
case 'A192GCM':
|
| 31 |
+
case 'A256GCM':
|
| 32 |
+
length = parseInt(alg.slice(1, 4), 10);
|
| 33 |
+
algorithm = { name: 'AES-GCM', length };
|
| 34 |
+
keyUsages = ['encrypt', 'decrypt'];
|
| 35 |
+
break;
|
| 36 |
+
default:
|
| 37 |
+
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
|
| 38 |
+
}
|
| 39 |
+
return crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages);
|
| 40 |
+
}
|
public/vendor/jose/key/import.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { decode as decodeBase64URL } from '../util/base64url.js';
|
| 2 |
+
import { fromSPKI, fromPKCS8, fromX509 } from '../lib/asn1.js';
|
| 3 |
+
import { jwkToKey } from '../lib/jwk_to_key.js';
|
| 4 |
+
import { JOSENotSupported } from '../util/errors.js';
|
| 5 |
+
import { isObject } from '../lib/is_object.js';
|
| 6 |
+
export async function importSPKI(spki, alg, options) {
|
| 7 |
+
if (typeof spki !== 'string' || spki.indexOf('-----BEGIN PUBLIC KEY-----') !== 0) {
|
| 8 |
+
throw new TypeError('"spki" must be SPKI formatted string');
|
| 9 |
+
}
|
| 10 |
+
return fromSPKI(spki, alg, options);
|
| 11 |
+
}
|
| 12 |
+
export async function importX509(x509, alg, options) {
|
| 13 |
+
if (typeof x509 !== 'string' || x509.indexOf('-----BEGIN CERTIFICATE-----') !== 0) {
|
| 14 |
+
throw new TypeError('"x509" must be X.509 formatted string');
|
| 15 |
+
}
|
| 16 |
+
return fromX509(x509, alg, options);
|
| 17 |
+
}
|
| 18 |
+
export async function importPKCS8(pkcs8, alg, options) {
|
| 19 |
+
if (typeof pkcs8 !== 'string' || pkcs8.indexOf('-----BEGIN PRIVATE KEY-----') !== 0) {
|
| 20 |
+
throw new TypeError('"pkcs8" must be PKCS#8 formatted string');
|
| 21 |
+
}
|
| 22 |
+
return fromPKCS8(pkcs8, alg, options);
|
| 23 |
+
}
|
| 24 |
+
export async function importJWK(jwk, alg, options) {
|
| 25 |
+
if (!isObject(jwk)) {
|
| 26 |
+
throw new TypeError('JWK must be an object');
|
| 27 |
+
}
|
| 28 |
+
let ext;
|
| 29 |
+
alg ??= jwk.alg;
|
| 30 |
+
ext ??= options?.extractable ?? jwk.ext;
|
| 31 |
+
switch (jwk.kty) {
|
| 32 |
+
case 'oct':
|
| 33 |
+
if (typeof jwk.k !== 'string' || !jwk.k) {
|
| 34 |
+
throw new TypeError('missing "k" (Key Value) Parameter value');
|
| 35 |
+
}
|
| 36 |
+
return decodeBase64URL(jwk.k);
|
| 37 |
+
case 'RSA':
|
| 38 |
+
if ('oth' in jwk && jwk.oth !== undefined) {
|
| 39 |
+
throw new JOSENotSupported('RSA JWK "oth" (Other Primes Info) Parameter value is not supported');
|
| 40 |
+
}
|
| 41 |
+
return jwkToKey({ ...jwk, alg, ext });
|
| 42 |
+
case 'AKP': {
|
| 43 |
+
if (typeof jwk.alg !== 'string' || !jwk.alg) {
|
| 44 |
+
throw new TypeError('missing "alg" (Algorithm) Parameter value');
|
| 45 |
+
}
|
| 46 |
+
if (alg !== undefined && alg !== jwk.alg) {
|
| 47 |
+
throw new TypeError('JWK alg and alg option value mismatch');
|
| 48 |
+
}
|
| 49 |
+
return jwkToKey({ ...jwk, ext });
|
| 50 |
+
}
|
| 51 |
+
case 'EC':
|
| 52 |
+
case 'OKP':
|
| 53 |
+
return jwkToKey({ ...jwk, alg, ext });
|
| 54 |
+
default:
|
| 55 |
+
throw new JOSENotSupported('Unsupported "kty" (Key Type) Parameter value');
|
| 56 |
+
}
|
| 57 |
+
}
|
public/vendor/jose/lib/aesgcmkw.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { encrypt } from './encrypt.js';
|
| 2 |
+
import { decrypt } from './decrypt.js';
|
| 3 |
+
import { encode as b64u } from '../util/base64url.js';
|
| 4 |
+
export async function wrap(alg, key, cek, iv) {
|
| 5 |
+
const jweAlgorithm = alg.slice(0, 7);
|
| 6 |
+
const wrapped = await encrypt(jweAlgorithm, cek, key, iv, new Uint8Array());
|
| 7 |
+
return {
|
| 8 |
+
encryptedKey: wrapped.ciphertext,
|
| 9 |
+
iv: b64u(wrapped.iv),
|
| 10 |
+
tag: b64u(wrapped.tag),
|
| 11 |
+
};
|
| 12 |
+
}
|
| 13 |
+
export async function unwrap(alg, key, encryptedKey, iv, tag) {
|
| 14 |
+
const jweAlgorithm = alg.slice(0, 7);
|
| 15 |
+
return decrypt(jweAlgorithm, key, encryptedKey, iv, tag, new Uint8Array());
|
| 16 |
+
}
|
public/vendor/jose/lib/aeskw.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { checkEncCryptoKey } from './crypto_key.js';
|
| 2 |
+
function checkKeySize(key, alg) {
|
| 3 |
+
if (key.algorithm.length !== parseInt(alg.slice(1, 4), 10)) {
|
| 4 |
+
throw new TypeError(`Invalid key size for alg: ${alg}`);
|
| 5 |
+
}
|
| 6 |
+
}
|
| 7 |
+
function getCryptoKey(key, alg, usage) {
|
| 8 |
+
if (key instanceof Uint8Array) {
|
| 9 |
+
return crypto.subtle.importKey('raw', key, 'AES-KW', true, [usage]);
|
| 10 |
+
}
|
| 11 |
+
checkEncCryptoKey(key, alg, usage);
|
| 12 |
+
return key;
|
| 13 |
+
}
|
| 14 |
+
export async function wrap(alg, key, cek) {
|
| 15 |
+
const cryptoKey = await getCryptoKey(key, alg, 'wrapKey');
|
| 16 |
+
checkKeySize(cryptoKey, alg);
|
| 17 |
+
const cryptoKeyCek = await crypto.subtle.importKey('raw', cek, { hash: 'SHA-256', name: 'HMAC' }, true, ['sign']);
|
| 18 |
+
return new Uint8Array(await crypto.subtle.wrapKey('raw', cryptoKeyCek, cryptoKey, 'AES-KW'));
|
| 19 |
+
}
|
| 20 |
+
export async function unwrap(alg, key, encryptedKey) {
|
| 21 |
+
const cryptoKey = await getCryptoKey(key, alg, 'unwrapKey');
|
| 22 |
+
checkKeySize(cryptoKey, alg);
|
| 23 |
+
const cryptoKeyCek = await crypto.subtle.unwrapKey('raw', encryptedKey, cryptoKey, 'AES-KW', { hash: 'SHA-256', name: 'HMAC' }, true, ['sign']);
|
| 24 |
+
return new Uint8Array(await crypto.subtle.exportKey('raw', cryptoKeyCek));
|
| 25 |
+
}
|
public/vendor/jose/lib/asn1.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { invalidKeyInput } from './invalid_key_input.js';
|
| 2 |
+
import { encodeBase64, decodeBase64 } from '../lib/base64.js';
|
| 3 |
+
import { JOSENotSupported } from '../util/errors.js';
|
| 4 |
+
import { isCryptoKey, isKeyObject } from './is_key_like.js';
|
| 5 |
+
const formatPEM = (b64, descriptor) => {
|
| 6 |
+
const newlined = (b64.match(/.{1,64}/g) || []).join('\n');
|
| 7 |
+
return `-----BEGIN ${descriptor}-----\n${newlined}\n-----END ${descriptor}-----`;
|
| 8 |
+
};
|
| 9 |
+
const genericExport = async (keyType, keyFormat, key) => {
|
| 10 |
+
if (isKeyObject(key)) {
|
| 11 |
+
if (key.type !== keyType) {
|
| 12 |
+
throw new TypeError(`key is not a ${keyType} key`);
|
| 13 |
+
}
|
| 14 |
+
return key.export({ format: 'pem', type: keyFormat });
|
| 15 |
+
}
|
| 16 |
+
if (!isCryptoKey(key)) {
|
| 17 |
+
throw new TypeError(invalidKeyInput(key, 'CryptoKey', 'KeyObject'));
|
| 18 |
+
}
|
| 19 |
+
if (!key.extractable) {
|
| 20 |
+
throw new TypeError('CryptoKey is not extractable');
|
| 21 |
+
}
|
| 22 |
+
if (key.type !== keyType) {
|
| 23 |
+
throw new TypeError(`key is not a ${keyType} key`);
|
| 24 |
+
}
|
| 25 |
+
return formatPEM(encodeBase64(new Uint8Array(await crypto.subtle.exportKey(keyFormat, key))), `${keyType.toUpperCase()} KEY`);
|
| 26 |
+
};
|
| 27 |
+
export const toSPKI = (key) => genericExport('public', 'spki', key);
|
| 28 |
+
export const toPKCS8 = (key) => genericExport('private', 'pkcs8', key);
|
| 29 |
+
const bytesEqual = (a, b) => {
|
| 30 |
+
if (a.byteLength !== b.length)
|
| 31 |
+
return false;
|
| 32 |
+
for (let i = 0; i < a.byteLength; i++) {
|
| 33 |
+
if (a[i] !== b[i])
|
| 34 |
+
return false;
|
| 35 |
+
}
|
| 36 |
+
return true;
|
| 37 |
+
};
|
| 38 |
+
const createASN1State = (data) => ({ data, pos: 0 });
|
| 39 |
+
const parseLength = (state) => {
|
| 40 |
+
const first = state.data[state.pos++];
|
| 41 |
+
if (first & 0x80) {
|
| 42 |
+
const lengthOfLen = first & 0x7f;
|
| 43 |
+
let length = 0;
|
| 44 |
+
for (let i = 0; i < lengthOfLen; i++) {
|
| 45 |
+
length = (length << 8) | state.data[state.pos++];
|
| 46 |
+
}
|
| 47 |
+
return length;
|
| 48 |
+
}
|
| 49 |
+
return first;
|
| 50 |
+
};
|
| 51 |
+
const skipElement = (state, count = 1) => {
|
| 52 |
+
if (count <= 0)
|
| 53 |
+
return;
|
| 54 |
+
state.pos++;
|
| 55 |
+
const length = parseLength(state);
|
| 56 |
+
state.pos += length;
|
| 57 |
+
if (count > 1) {
|
| 58 |
+
skipElement(state, count - 1);
|
| 59 |
+
}
|
| 60 |
+
};
|
| 61 |
+
const expectTag = (state, expectedTag, errorMessage) => {
|
| 62 |
+
if (state.data[state.pos++] !== expectedTag) {
|
| 63 |
+
throw new Error(errorMessage);
|
| 64 |
+
}
|
| 65 |
+
};
|
| 66 |
+
const getSubarray = (state, length) => {
|
| 67 |
+
const result = state.data.subarray(state.pos, state.pos + length);
|
| 68 |
+
state.pos += length;
|
| 69 |
+
return result;
|
| 70 |
+
};
|
| 71 |
+
const parseAlgorithmOID = (state) => {
|
| 72 |
+
expectTag(state, 0x06, 'Expected algorithm OID');
|
| 73 |
+
const oidLen = parseLength(state);
|
| 74 |
+
return getSubarray(state, oidLen);
|
| 75 |
+
};
|
| 76 |
+
function parsePKCS8Header(state) {
|
| 77 |
+
expectTag(state, 0x30, 'Invalid PKCS#8 structure');
|
| 78 |
+
parseLength(state);
|
| 79 |
+
expectTag(state, 0x02, 'Expected version field');
|
| 80 |
+
const verLen = parseLength(state);
|
| 81 |
+
state.pos += verLen;
|
| 82 |
+
expectTag(state, 0x30, 'Expected algorithm identifier');
|
| 83 |
+
const algIdLen = parseLength(state);
|
| 84 |
+
const algIdStart = state.pos;
|
| 85 |
+
return { algIdStart, algIdLength: algIdLen };
|
| 86 |
+
}
|
| 87 |
+
function parseSPKIHeader(state) {
|
| 88 |
+
expectTag(state, 0x30, 'Invalid SPKI structure');
|
| 89 |
+
parseLength(state);
|
| 90 |
+
expectTag(state, 0x30, 'Expected algorithm identifier');
|
| 91 |
+
const algIdLen = parseLength(state);
|
| 92 |
+
const algIdStart = state.pos;
|
| 93 |
+
return { algIdStart, algIdLength: algIdLen };
|
| 94 |
+
}
|
| 95 |
+
const parseECAlgorithmIdentifier = (state) => {
|
| 96 |
+
const algOid = parseAlgorithmOID(state);
|
| 97 |
+
if (bytesEqual(algOid, [0x2b, 0x65, 0x6e])) {
|
| 98 |
+
return 'X25519';
|
| 99 |
+
}
|
| 100 |
+
if (!bytesEqual(algOid, [0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01])) {
|
| 101 |
+
throw new Error('Unsupported key algorithm');
|
| 102 |
+
}
|
| 103 |
+
expectTag(state, 0x06, 'Expected curve OID');
|
| 104 |
+
const curveOidLen = parseLength(state);
|
| 105 |
+
const curveOid = getSubarray(state, curveOidLen);
|
| 106 |
+
for (const { name, oid } of [
|
| 107 |
+
{ name: 'P-256', oid: [0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07] },
|
| 108 |
+
{ name: 'P-384', oid: [0x2b, 0x81, 0x04, 0x00, 0x22] },
|
| 109 |
+
{ name: 'P-521', oid: [0x2b, 0x81, 0x04, 0x00, 0x23] },
|
| 110 |
+
]) {
|
| 111 |
+
if (bytesEqual(curveOid, oid)) {
|
| 112 |
+
return name;
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
throw new Error('Unsupported named curve');
|
| 116 |
+
};
|
| 117 |
+
const genericImport = async (keyFormat, keyData, alg, options) => {
|
| 118 |
+
let algorithm;
|
| 119 |
+
let keyUsages;
|
| 120 |
+
const isPublic = keyFormat === 'spki';
|
| 121 |
+
const getSigUsages = () => (isPublic ? ['verify'] : ['sign']);
|
| 122 |
+
const getEncUsages = () => isPublic ? ['encrypt', 'wrapKey'] : ['decrypt', 'unwrapKey'];
|
| 123 |
+
switch (alg) {
|
| 124 |
+
case 'PS256':
|
| 125 |
+
case 'PS384':
|
| 126 |
+
case 'PS512':
|
| 127 |
+
algorithm = { name: 'RSA-PSS', hash: `SHA-${alg.slice(-3)}` };
|
| 128 |
+
keyUsages = getSigUsages();
|
| 129 |
+
break;
|
| 130 |
+
case 'RS256':
|
| 131 |
+
case 'RS384':
|
| 132 |
+
case 'RS512':
|
| 133 |
+
algorithm = { name: 'RSASSA-PKCS1-v1_5', hash: `SHA-${alg.slice(-3)}` };
|
| 134 |
+
keyUsages = getSigUsages();
|
| 135 |
+
break;
|
| 136 |
+
case 'RSA-OAEP':
|
| 137 |
+
case 'RSA-OAEP-256':
|
| 138 |
+
case 'RSA-OAEP-384':
|
| 139 |
+
case 'RSA-OAEP-512':
|
| 140 |
+
algorithm = {
|
| 141 |
+
name: 'RSA-OAEP',
|
| 142 |
+
hash: `SHA-${parseInt(alg.slice(-3), 10) || 1}`,
|
| 143 |
+
};
|
| 144 |
+
keyUsages = getEncUsages();
|
| 145 |
+
break;
|
| 146 |
+
case 'ES256':
|
| 147 |
+
case 'ES384':
|
| 148 |
+
case 'ES512': {
|
| 149 |
+
const curveMap = { ES256: 'P-256', ES384: 'P-384', ES512: 'P-521' };
|
| 150 |
+
algorithm = { name: 'ECDSA', namedCurve: curveMap[alg] };
|
| 151 |
+
keyUsages = getSigUsages();
|
| 152 |
+
break;
|
| 153 |
+
}
|
| 154 |
+
case 'ECDH-ES':
|
| 155 |
+
case 'ECDH-ES+A128KW':
|
| 156 |
+
case 'ECDH-ES+A192KW':
|
| 157 |
+
case 'ECDH-ES+A256KW': {
|
| 158 |
+
try {
|
| 159 |
+
const namedCurve = options.getNamedCurve(keyData);
|
| 160 |
+
algorithm = namedCurve === 'X25519' ? { name: 'X25519' } : { name: 'ECDH', namedCurve };
|
| 161 |
+
}
|
| 162 |
+
catch (cause) {
|
| 163 |
+
throw new JOSENotSupported('Invalid or unsupported key format');
|
| 164 |
+
}
|
| 165 |
+
keyUsages = isPublic ? [] : ['deriveBits'];
|
| 166 |
+
break;
|
| 167 |
+
}
|
| 168 |
+
case 'Ed25519':
|
| 169 |
+
case 'EdDSA':
|
| 170 |
+
algorithm = { name: 'Ed25519' };
|
| 171 |
+
keyUsages = getSigUsages();
|
| 172 |
+
break;
|
| 173 |
+
case 'ML-DSA-44':
|
| 174 |
+
case 'ML-DSA-65':
|
| 175 |
+
case 'ML-DSA-87':
|
| 176 |
+
algorithm = { name: alg };
|
| 177 |
+
keyUsages = getSigUsages();
|
| 178 |
+
break;
|
| 179 |
+
default:
|
| 180 |
+
throw new JOSENotSupported('Invalid or unsupported "alg" (Algorithm) value');
|
| 181 |
+
}
|
| 182 |
+
return crypto.subtle.importKey(keyFormat, keyData, algorithm, options?.extractable ?? (isPublic ? true : false), keyUsages);
|
| 183 |
+
};
|
| 184 |
+
const processPEMData = (pem, pattern) => {
|
| 185 |
+
return decodeBase64(pem.replace(pattern, ''));
|
| 186 |
+
};
|
| 187 |
+
export const fromPKCS8 = (pem, alg, options) => {
|
| 188 |
+
const keyData = processPEMData(pem, /(?:-----(?:BEGIN|END) PRIVATE KEY-----|\s)/g);
|
| 189 |
+
let opts = options;
|
| 190 |
+
if (alg?.startsWith?.('ECDH-ES')) {
|
| 191 |
+
opts ||= {};
|
| 192 |
+
opts.getNamedCurve = (keyData) => {
|
| 193 |
+
const state = createASN1State(keyData);
|
| 194 |
+
parsePKCS8Header(state);
|
| 195 |
+
return parseECAlgorithmIdentifier(state);
|
| 196 |
+
};
|
| 197 |
+
}
|
| 198 |
+
return genericImport('pkcs8', keyData, alg, opts);
|
| 199 |
+
};
|
| 200 |
+
export const fromSPKI = (pem, alg, options) => {
|
| 201 |
+
const keyData = processPEMData(pem, /(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g);
|
| 202 |
+
let opts = options;
|
| 203 |
+
if (alg?.startsWith?.('ECDH-ES')) {
|
| 204 |
+
opts ||= {};
|
| 205 |
+
opts.getNamedCurve = (keyData) => {
|
| 206 |
+
const state = createASN1State(keyData);
|
| 207 |
+
parseSPKIHeader(state);
|
| 208 |
+
return parseECAlgorithmIdentifier(state);
|
| 209 |
+
};
|
| 210 |
+
}
|
| 211 |
+
return genericImport('spki', keyData, alg, opts);
|
| 212 |
+
};
|
| 213 |
+
function spkiFromX509(buf) {
|
| 214 |
+
const state = createASN1State(buf);
|
| 215 |
+
expectTag(state, 0x30, 'Invalid certificate structure');
|
| 216 |
+
parseLength(state);
|
| 217 |
+
expectTag(state, 0x30, 'Invalid tbsCertificate structure');
|
| 218 |
+
parseLength(state);
|
| 219 |
+
if (buf[state.pos] === 0xa0) {
|
| 220 |
+
skipElement(state, 6);
|
| 221 |
+
}
|
| 222 |
+
else {
|
| 223 |
+
skipElement(state, 5);
|
| 224 |
+
}
|
| 225 |
+
const spkiStart = state.pos;
|
| 226 |
+
expectTag(state, 0x30, 'Invalid SPKI structure');
|
| 227 |
+
const spkiContentLen = parseLength(state);
|
| 228 |
+
return buf.subarray(spkiStart, spkiStart + spkiContentLen + (state.pos - spkiStart));
|
| 229 |
+
}
|
| 230 |
+
function extractX509SPKI(x509) {
|
| 231 |
+
const derBytes = processPEMData(x509, /(?:-----(?:BEGIN|END) CERTIFICATE-----|\s)/g);
|
| 232 |
+
return spkiFromX509(derBytes);
|
| 233 |
+
}
|
| 234 |
+
export const fromX509 = (pem, alg, options) => {
|
| 235 |
+
let spki;
|
| 236 |
+
try {
|
| 237 |
+
spki = extractX509SPKI(pem);
|
| 238 |
+
}
|
| 239 |
+
catch (cause) {
|
| 240 |
+
throw new TypeError('Failed to parse the X.509 certificate', { cause });
|
| 241 |
+
}
|
| 242 |
+
return fromSPKI(formatPEM(encodeBase64(spki), 'PUBLIC KEY'), alg, options);
|
| 243 |
+
};
|
public/vendor/jose/lib/base64.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export function encodeBase64(input) {
|
| 2 |
+
if (Uint8Array.prototype.toBase64) {
|
| 3 |
+
return input.toBase64();
|
| 4 |
+
}
|
| 5 |
+
const CHUNK_SIZE = 0x8000;
|
| 6 |
+
const arr = [];
|
| 7 |
+
for (let i = 0; i < input.length; i += CHUNK_SIZE) {
|
| 8 |
+
arr.push(String.fromCharCode.apply(null, input.subarray(i, i + CHUNK_SIZE)));
|
| 9 |
+
}
|
| 10 |
+
return btoa(arr.join(''));
|
| 11 |
+
}
|
| 12 |
+
export function decodeBase64(encoded) {
|
| 13 |
+
if (Uint8Array.fromBase64) {
|
| 14 |
+
return Uint8Array.fromBase64(encoded);
|
| 15 |
+
}
|
| 16 |
+
const binary = atob(encoded);
|
| 17 |
+
const bytes = new Uint8Array(binary.length);
|
| 18 |
+
for (let i = 0; i < binary.length; i++) {
|
| 19 |
+
bytes[i] = binary.charCodeAt(i);
|
| 20 |
+
}
|
| 21 |
+
return bytes;
|
| 22 |
+
}
|
public/vendor/jose/lib/buffer_utils.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export const encoder = new TextEncoder();
|
| 2 |
+
export const decoder = new TextDecoder();
|
| 3 |
+
const MAX_INT32 = 2 ** 32;
|
| 4 |
+
export function concat(...buffers) {
|
| 5 |
+
const size = buffers.reduce((acc, { length }) => acc + length, 0);
|
| 6 |
+
const buf = new Uint8Array(size);
|
| 7 |
+
let i = 0;
|
| 8 |
+
for (const buffer of buffers) {
|
| 9 |
+
buf.set(buffer, i);
|
| 10 |
+
i += buffer.length;
|
| 11 |
+
}
|
| 12 |
+
return buf;
|
| 13 |
+
}
|
| 14 |
+
function writeUInt32BE(buf, value, offset) {
|
| 15 |
+
if (value < 0 || value >= MAX_INT32) {
|
| 16 |
+
throw new RangeError(`value must be >= 0 and <= ${MAX_INT32 - 1}. Received ${value}`);
|
| 17 |
+
}
|
| 18 |
+
buf.set([value >>> 24, value >>> 16, value >>> 8, value & 0xff], offset);
|
| 19 |
+
}
|
| 20 |
+
export function uint64be(value) {
|
| 21 |
+
const high = Math.floor(value / MAX_INT32);
|
| 22 |
+
const low = value % MAX_INT32;
|
| 23 |
+
const buf = new Uint8Array(8);
|
| 24 |
+
writeUInt32BE(buf, high, 0);
|
| 25 |
+
writeUInt32BE(buf, low, 4);
|
| 26 |
+
return buf;
|
| 27 |
+
}
|
| 28 |
+
export function uint32be(value) {
|
| 29 |
+
const buf = new Uint8Array(4);
|
| 30 |
+
writeUInt32BE(buf, value);
|
| 31 |
+
return buf;
|
| 32 |
+
}
|
| 33 |
+
export function encode(string) {
|
| 34 |
+
const bytes = new Uint8Array(string.length);
|
| 35 |
+
for (let i = 0; i < string.length; i++) {
|
| 36 |
+
const code = string.charCodeAt(i);
|
| 37 |
+
if (code > 127) {
|
| 38 |
+
throw new TypeError('non-ASCII string encountered in encode()');
|
| 39 |
+
}
|
| 40 |
+
bytes[i] = code;
|
| 41 |
+
}
|
| 42 |
+
return bytes;
|
| 43 |
+
}
|
public/vendor/jose/lib/cek.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { JOSENotSupported } from '../util/errors.js';
|
| 2 |
+
export function cekLength(alg) {
|
| 3 |
+
switch (alg) {
|
| 4 |
+
case 'A128GCM':
|
| 5 |
+
return 128;
|
| 6 |
+
case 'A192GCM':
|
| 7 |
+
return 192;
|
| 8 |
+
case 'A256GCM':
|
| 9 |
+
case 'A128CBC-HS256':
|
| 10 |
+
return 256;
|
| 11 |
+
case 'A192CBC-HS384':
|
| 12 |
+
return 384;
|
| 13 |
+
case 'A256CBC-HS512':
|
| 14 |
+
return 512;
|
| 15 |
+
default:
|
| 16 |
+
throw new JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
export const generateCek = (alg) => crypto.getRandomValues(new Uint8Array(cekLength(alg) >> 3));
|
public/vendor/jose/lib/check_cek_length.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { JWEInvalid } from '../util/errors.js';
|
| 2 |
+
export function checkCekLength(cek, expected) {
|
| 3 |
+
const actual = cek.byteLength << 3;
|
| 4 |
+
if (actual !== expected) {
|
| 5 |
+
throw new JWEInvalid(`Invalid Content Encryption Key length. Expected ${expected} bits, got ${actual} bits`);
|
| 6 |
+
}
|
| 7 |
+
}
|
public/vendor/jose/lib/check_iv_length.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { JWEInvalid } from '../util/errors.js';
|
| 2 |
+
import { bitLength } from './iv.js';
|
| 3 |
+
export function checkIvLength(enc, iv) {
|
| 4 |
+
if (iv.length << 3 !== bitLength(enc)) {
|
| 5 |
+
throw new JWEInvalid('Invalid Initialization Vector length');
|
| 6 |
+
}
|
| 7 |
+
}
|
public/vendor/jose/lib/check_key_length.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export function checkKeyLength(alg, key) {
|
| 2 |
+
if (alg.startsWith('RS') || alg.startsWith('PS')) {
|
| 3 |
+
const { modulusLength } = key.algorithm;
|
| 4 |
+
if (typeof modulusLength !== 'number' || modulusLength < 2048) {
|
| 5 |
+
throw new TypeError(`${alg} requires key modulusLength to be 2048 bits or larger`);
|
| 6 |
+
}
|
| 7 |
+
}
|
| 8 |
+
}
|
public/vendor/jose/lib/check_key_type.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { withAlg as invalidKeyInput } from './invalid_key_input.js';
|
| 2 |
+
import { isKeyLike } from './is_key_like.js';
|
| 3 |
+
import * as jwk from './is_jwk.js';
|
| 4 |
+
const tag = (key) => key?.[Symbol.toStringTag];
|
| 5 |
+
const jwkMatchesOp = (alg, key, usage) => {
|
| 6 |
+
if (key.use !== undefined) {
|
| 7 |
+
let expected;
|
| 8 |
+
switch (usage) {
|
| 9 |
+
case 'sign':
|
| 10 |
+
case 'verify':
|
| 11 |
+
expected = 'sig';
|
| 12 |
+
break;
|
| 13 |
+
case 'encrypt':
|
| 14 |
+
case 'decrypt':
|
| 15 |
+
expected = 'enc';
|
| 16 |
+
break;
|
| 17 |
+
}
|
| 18 |
+
if (key.use !== expected) {
|
| 19 |
+
throw new TypeError(`Invalid key for this operation, its "use" must be "${expected}" when present`);
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
if (key.alg !== undefined && key.alg !== alg) {
|
| 23 |
+
throw new TypeError(`Invalid key for this operation, its "alg" must be "${alg}" when present`);
|
| 24 |
+
}
|
| 25 |
+
if (Array.isArray(key.key_ops)) {
|
| 26 |
+
let expectedKeyOp;
|
| 27 |
+
switch (true) {
|
| 28 |
+
case usage === 'sign' || usage === 'verify':
|
| 29 |
+
case alg === 'dir':
|
| 30 |
+
case alg.includes('CBC-HS'):
|
| 31 |
+
expectedKeyOp = usage;
|
| 32 |
+
break;
|
| 33 |
+
case alg.startsWith('PBES2'):
|
| 34 |
+
expectedKeyOp = 'deriveBits';
|
| 35 |
+
break;
|
| 36 |
+
case /^A\d{3}(?:GCM)?(?:KW)?$/.test(alg):
|
| 37 |
+
if (!alg.includes('GCM') && alg.endsWith('KW')) {
|
| 38 |
+
expectedKeyOp = usage === 'encrypt' ? 'wrapKey' : 'unwrapKey';
|
| 39 |
+
}
|
| 40 |
+
else {
|
| 41 |
+
expectedKeyOp = usage;
|
| 42 |
+
}
|
| 43 |
+
break;
|
| 44 |
+
case usage === 'encrypt' && alg.startsWith('RSA'):
|
| 45 |
+
expectedKeyOp = 'wrapKey';
|
| 46 |
+
break;
|
| 47 |
+
case usage === 'decrypt':
|
| 48 |
+
expectedKeyOp = alg.startsWith('RSA') ? 'unwrapKey' : 'deriveBits';
|
| 49 |
+
break;
|
| 50 |
+
}
|
| 51 |
+
if (expectedKeyOp && key.key_ops?.includes?.(expectedKeyOp) === false) {
|
| 52 |
+
throw new TypeError(`Invalid key for this operation, its "key_ops" must include "${expectedKeyOp}" when present`);
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
return true;
|
| 56 |
+
};
|
| 57 |
+
const symmetricTypeCheck = (alg, key, usage) => {
|
| 58 |
+
if (key instanceof Uint8Array)
|
| 59 |
+
return;
|
| 60 |
+
if (jwk.isJWK(key)) {
|
| 61 |
+
if (jwk.isSecretJWK(key) && jwkMatchesOp(alg, key, usage))
|
| 62 |
+
return;
|
| 63 |
+
throw new TypeError(`JSON Web Key for symmetric algorithms must have JWK "kty" (Key Type) equal to "oct" and the JWK "k" (Key Value) present`);
|
| 64 |
+
}
|
| 65 |
+
if (!isKeyLike(key)) {
|
| 66 |
+
throw new TypeError(invalidKeyInput(alg, key, 'CryptoKey', 'KeyObject', 'JSON Web Key', 'Uint8Array'));
|
| 67 |
+
}
|
| 68 |
+
if (key.type !== 'secret') {
|
| 69 |
+
throw new TypeError(`${tag(key)} instances for symmetric algorithms must be of type "secret"`);
|
| 70 |
+
}
|
| 71 |
+
};
|
| 72 |
+
const asymmetricTypeCheck = (alg, key, usage) => {
|
| 73 |
+
if (jwk.isJWK(key)) {
|
| 74 |
+
switch (usage) {
|
| 75 |
+
case 'decrypt':
|
| 76 |
+
case 'sign':
|
| 77 |
+
if (jwk.isPrivateJWK(key) && jwkMatchesOp(alg, key, usage))
|
| 78 |
+
return;
|
| 79 |
+
throw new TypeError(`JSON Web Key for this operation must be a private JWK`);
|
| 80 |
+
case 'encrypt':
|
| 81 |
+
case 'verify':
|
| 82 |
+
if (jwk.isPublicJWK(key) && jwkMatchesOp(alg, key, usage))
|
| 83 |
+
return;
|
| 84 |
+
throw new TypeError(`JSON Web Key for this operation must be a public JWK`);
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
if (!isKeyLike(key)) {
|
| 88 |
+
throw new TypeError(invalidKeyInput(alg, key, 'CryptoKey', 'KeyObject', 'JSON Web Key'));
|
| 89 |
+
}
|
| 90 |
+
if (key.type === 'secret') {
|
| 91 |
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithms must not be of type "secret"`);
|
| 92 |
+
}
|
| 93 |
+
if (key.type === 'public') {
|
| 94 |
+
switch (usage) {
|
| 95 |
+
case 'sign':
|
| 96 |
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm signing must be of type "private"`);
|
| 97 |
+
case 'decrypt':
|
| 98 |
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm decryption must be of type "private"`);
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
if (key.type === 'private') {
|
| 102 |
+
switch (usage) {
|
| 103 |
+
case 'verify':
|
| 104 |
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm verifying must be of type "public"`);
|
| 105 |
+
case 'encrypt':
|
| 106 |
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm encryption must be of type "public"`);
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
};
|
| 110 |
+
export function checkKeyType(alg, key, usage) {
|
| 111 |
+
switch (alg.substring(0, 2)) {
|
| 112 |
+
case 'A1':
|
| 113 |
+
case 'A2':
|
| 114 |
+
case 'di':
|
| 115 |
+
case 'HS':
|
| 116 |
+
case 'PB':
|
| 117 |
+
symmetricTypeCheck(alg, key, usage);
|
| 118 |
+
break;
|
| 119 |
+
default:
|
| 120 |
+
asymmetricTypeCheck(alg, key, usage);
|
| 121 |
+
}
|
| 122 |
+
}
|
public/vendor/jose/lib/crypto_key.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const unusable = (name, prop = 'algorithm.name') => new TypeError(`CryptoKey does not support this operation, its ${prop} must be ${name}`);
|
| 2 |
+
const isAlgorithm = (algorithm, name) => algorithm.name === name;
|
| 3 |
+
function getHashLength(hash) {
|
| 4 |
+
return parseInt(hash.name.slice(4), 10);
|
| 5 |
+
}
|
| 6 |
+
function getNamedCurve(alg) {
|
| 7 |
+
switch (alg) {
|
| 8 |
+
case 'ES256':
|
| 9 |
+
return 'P-256';
|
| 10 |
+
case 'ES384':
|
| 11 |
+
return 'P-384';
|
| 12 |
+
case 'ES512':
|
| 13 |
+
return 'P-521';
|
| 14 |
+
default:
|
| 15 |
+
throw new Error('unreachable');
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
function checkUsage(key, usage) {
|
| 19 |
+
if (usage && !key.usages.includes(usage)) {
|
| 20 |
+
throw new TypeError(`CryptoKey does not support this operation, its usages must include ${usage}.`);
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
export function checkSigCryptoKey(key, alg, usage) {
|
| 24 |
+
switch (alg) {
|
| 25 |
+
case 'HS256':
|
| 26 |
+
case 'HS384':
|
| 27 |
+
case 'HS512': {
|
| 28 |
+
if (!isAlgorithm(key.algorithm, 'HMAC'))
|
| 29 |
+
throw unusable('HMAC');
|
| 30 |
+
const expected = parseInt(alg.slice(2), 10);
|
| 31 |
+
const actual = getHashLength(key.algorithm.hash);
|
| 32 |
+
if (actual !== expected)
|
| 33 |
+
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
| 34 |
+
break;
|
| 35 |
+
}
|
| 36 |
+
case 'RS256':
|
| 37 |
+
case 'RS384':
|
| 38 |
+
case 'RS512': {
|
| 39 |
+
if (!isAlgorithm(key.algorithm, 'RSASSA-PKCS1-v1_5'))
|
| 40 |
+
throw unusable('RSASSA-PKCS1-v1_5');
|
| 41 |
+
const expected = parseInt(alg.slice(2), 10);
|
| 42 |
+
const actual = getHashLength(key.algorithm.hash);
|
| 43 |
+
if (actual !== expected)
|
| 44 |
+
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
| 45 |
+
break;
|
| 46 |
+
}
|
| 47 |
+
case 'PS256':
|
| 48 |
+
case 'PS384':
|
| 49 |
+
case 'PS512': {
|
| 50 |
+
if (!isAlgorithm(key.algorithm, 'RSA-PSS'))
|
| 51 |
+
throw unusable('RSA-PSS');
|
| 52 |
+
const expected = parseInt(alg.slice(2), 10);
|
| 53 |
+
const actual = getHashLength(key.algorithm.hash);
|
| 54 |
+
if (actual !== expected)
|
| 55 |
+
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
| 56 |
+
break;
|
| 57 |
+
}
|
| 58 |
+
case 'Ed25519':
|
| 59 |
+
case 'EdDSA': {
|
| 60 |
+
if (!isAlgorithm(key.algorithm, 'Ed25519'))
|
| 61 |
+
throw unusable('Ed25519');
|
| 62 |
+
break;
|
| 63 |
+
}
|
| 64 |
+
case 'ML-DSA-44':
|
| 65 |
+
case 'ML-DSA-65':
|
| 66 |
+
case 'ML-DSA-87': {
|
| 67 |
+
if (!isAlgorithm(key.algorithm, alg))
|
| 68 |
+
throw unusable(alg);
|
| 69 |
+
break;
|
| 70 |
+
}
|
| 71 |
+
case 'ES256':
|
| 72 |
+
case 'ES384':
|
| 73 |
+
case 'ES512': {
|
| 74 |
+
if (!isAlgorithm(key.algorithm, 'ECDSA'))
|
| 75 |
+
throw unusable('ECDSA');
|
| 76 |
+
const expected = getNamedCurve(alg);
|
| 77 |
+
const actual = key.algorithm.namedCurve;
|
| 78 |
+
if (actual !== expected)
|
| 79 |
+
throw unusable(expected, 'algorithm.namedCurve');
|
| 80 |
+
break;
|
| 81 |
+
}
|
| 82 |
+
default:
|
| 83 |
+
throw new TypeError('CryptoKey does not support this operation');
|
| 84 |
+
}
|
| 85 |
+
checkUsage(key, usage);
|
| 86 |
+
}
|
| 87 |
+
export function checkEncCryptoKey(key, alg, usage) {
|
| 88 |
+
switch (alg) {
|
| 89 |
+
case 'A128GCM':
|
| 90 |
+
case 'A192GCM':
|
| 91 |
+
case 'A256GCM': {
|
| 92 |
+
if (!isAlgorithm(key.algorithm, 'AES-GCM'))
|
| 93 |
+
throw unusable('AES-GCM');
|
| 94 |
+
const expected = parseInt(alg.slice(1, 4), 10);
|
| 95 |
+
const actual = key.algorithm.length;
|
| 96 |
+
if (actual !== expected)
|
| 97 |
+
throw unusable(expected, 'algorithm.length');
|
| 98 |
+
break;
|
| 99 |
+
}
|
| 100 |
+
case 'A128KW':
|
| 101 |
+
case 'A192KW':
|
| 102 |
+
case 'A256KW': {
|
| 103 |
+
if (!isAlgorithm(key.algorithm, 'AES-KW'))
|
| 104 |
+
throw unusable('AES-KW');
|
| 105 |
+
const expected = parseInt(alg.slice(1, 4), 10);
|
| 106 |
+
const actual = key.algorithm.length;
|
| 107 |
+
if (actual !== expected)
|
| 108 |
+
throw unusable(expected, 'algorithm.length');
|
| 109 |
+
break;
|
| 110 |
+
}
|
| 111 |
+
case 'ECDH': {
|
| 112 |
+
switch (key.algorithm.name) {
|
| 113 |
+
case 'ECDH':
|
| 114 |
+
case 'X25519':
|
| 115 |
+
break;
|
| 116 |
+
default:
|
| 117 |
+
throw unusable('ECDH or X25519');
|
| 118 |
+
}
|
| 119 |
+
break;
|
| 120 |
+
}
|
| 121 |
+
case 'PBES2-HS256+A128KW':
|
| 122 |
+
case 'PBES2-HS384+A192KW':
|
| 123 |
+
case 'PBES2-HS512+A256KW':
|
| 124 |
+
if (!isAlgorithm(key.algorithm, 'PBKDF2'))
|
| 125 |
+
throw unusable('PBKDF2');
|
| 126 |
+
break;
|
| 127 |
+
case 'RSA-OAEP':
|
| 128 |
+
case 'RSA-OAEP-256':
|
| 129 |
+
case 'RSA-OAEP-384':
|
| 130 |
+
case 'RSA-OAEP-512': {
|
| 131 |
+
if (!isAlgorithm(key.algorithm, 'RSA-OAEP'))
|
| 132 |
+
throw unusable('RSA-OAEP');
|
| 133 |
+
const expected = parseInt(alg.slice(9), 10) || 1;
|
| 134 |
+
const actual = getHashLength(key.algorithm.hash);
|
| 135 |
+
if (actual !== expected)
|
| 136 |
+
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
| 137 |
+
break;
|
| 138 |
+
}
|
| 139 |
+
default:
|
| 140 |
+
throw new TypeError('CryptoKey does not support this operation');
|
| 141 |
+
}
|
| 142 |
+
checkUsage(key, usage);
|
| 143 |
+
}
|
public/vendor/jose/lib/decrypt.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { concat, uint64be } from './buffer_utils.js';
|
| 2 |
+
import { checkIvLength } from './check_iv_length.js';
|
| 3 |
+
import { checkCekLength } from './check_cek_length.js';
|
| 4 |
+
import { JOSENotSupported, JWEDecryptionFailed, JWEInvalid } from '../util/errors.js';
|
| 5 |
+
import { checkEncCryptoKey } from './crypto_key.js';
|
| 6 |
+
import { invalidKeyInput } from './invalid_key_input.js';
|
| 7 |
+
import { isCryptoKey } from './is_key_like.js';
|
| 8 |
+
async function timingSafeEqual(a, b) {
|
| 9 |
+
if (!(a instanceof Uint8Array)) {
|
| 10 |
+
throw new TypeError('First argument must be a buffer');
|
| 11 |
+
}
|
| 12 |
+
if (!(b instanceof Uint8Array)) {
|
| 13 |
+
throw new TypeError('Second argument must be a buffer');
|
| 14 |
+
}
|
| 15 |
+
const algorithm = { name: 'HMAC', hash: 'SHA-256' };
|
| 16 |
+
const key = (await crypto.subtle.generateKey(algorithm, false, ['sign']));
|
| 17 |
+
const aHmac = new Uint8Array(await crypto.subtle.sign(algorithm, key, a));
|
| 18 |
+
const bHmac = new Uint8Array(await crypto.subtle.sign(algorithm, key, b));
|
| 19 |
+
let out = 0;
|
| 20 |
+
let i = -1;
|
| 21 |
+
while (++i < 32) {
|
| 22 |
+
out |= aHmac[i] ^ bHmac[i];
|
| 23 |
+
}
|
| 24 |
+
return out === 0;
|
| 25 |
+
}
|
| 26 |
+
async function cbcDecrypt(enc, cek, ciphertext, iv, tag, aad) {
|
| 27 |
+
if (!(cek instanceof Uint8Array)) {
|
| 28 |
+
throw new TypeError(invalidKeyInput(cek, 'Uint8Array'));
|
| 29 |
+
}
|
| 30 |
+
const keySize = parseInt(enc.slice(1, 4), 10);
|
| 31 |
+
const encKey = await crypto.subtle.importKey('raw', cek.subarray(keySize >> 3), 'AES-CBC', false, ['decrypt']);
|
| 32 |
+
const macKey = await crypto.subtle.importKey('raw', cek.subarray(0, keySize >> 3), {
|
| 33 |
+
hash: `SHA-${keySize << 1}`,
|
| 34 |
+
name: 'HMAC',
|
| 35 |
+
}, false, ['sign']);
|
| 36 |
+
const macData = concat(aad, iv, ciphertext, uint64be(aad.length << 3));
|
| 37 |
+
const expectedTag = new Uint8Array((await crypto.subtle.sign('HMAC', macKey, macData)).slice(0, keySize >> 3));
|
| 38 |
+
let macCheckPassed;
|
| 39 |
+
try {
|
| 40 |
+
macCheckPassed = await timingSafeEqual(tag, expectedTag);
|
| 41 |
+
}
|
| 42 |
+
catch {
|
| 43 |
+
}
|
| 44 |
+
if (!macCheckPassed) {
|
| 45 |
+
throw new JWEDecryptionFailed();
|
| 46 |
+
}
|
| 47 |
+
let plaintext;
|
| 48 |
+
try {
|
| 49 |
+
plaintext = new Uint8Array(await crypto.subtle.decrypt({ iv: iv, name: 'AES-CBC' }, encKey, ciphertext));
|
| 50 |
+
}
|
| 51 |
+
catch {
|
| 52 |
+
}
|
| 53 |
+
if (!plaintext) {
|
| 54 |
+
throw new JWEDecryptionFailed();
|
| 55 |
+
}
|
| 56 |
+
return plaintext;
|
| 57 |
+
}
|
| 58 |
+
async function gcmDecrypt(enc, cek, ciphertext, iv, tag, aad) {
|
| 59 |
+
let encKey;
|
| 60 |
+
if (cek instanceof Uint8Array) {
|
| 61 |
+
encKey = await crypto.subtle.importKey('raw', cek, 'AES-GCM', false, ['decrypt']);
|
| 62 |
+
}
|
| 63 |
+
else {
|
| 64 |
+
checkEncCryptoKey(cek, enc, 'decrypt');
|
| 65 |
+
encKey = cek;
|
| 66 |
+
}
|
| 67 |
+
try {
|
| 68 |
+
return new Uint8Array(await crypto.subtle.decrypt({
|
| 69 |
+
additionalData: aad,
|
| 70 |
+
iv: iv,
|
| 71 |
+
name: 'AES-GCM',
|
| 72 |
+
tagLength: 128,
|
| 73 |
+
}, encKey, concat(ciphertext, tag)));
|
| 74 |
+
}
|
| 75 |
+
catch {
|
| 76 |
+
throw new JWEDecryptionFailed();
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
export async function decrypt(enc, cek, ciphertext, iv, tag, aad) {
|
| 80 |
+
if (!isCryptoKey(cek) && !(cek instanceof Uint8Array)) {
|
| 81 |
+
throw new TypeError(invalidKeyInput(cek, 'CryptoKey', 'KeyObject', 'Uint8Array', 'JSON Web Key'));
|
| 82 |
+
}
|
| 83 |
+
if (!iv) {
|
| 84 |
+
throw new JWEInvalid('JWE Initialization Vector missing');
|
| 85 |
+
}
|
| 86 |
+
if (!tag) {
|
| 87 |
+
throw new JWEInvalid('JWE Authentication Tag missing');
|
| 88 |
+
}
|
| 89 |
+
checkIvLength(enc, iv);
|
| 90 |
+
switch (enc) {
|
| 91 |
+
case 'A128CBC-HS256':
|
| 92 |
+
case 'A192CBC-HS384':
|
| 93 |
+
case 'A256CBC-HS512':
|
| 94 |
+
if (cek instanceof Uint8Array)
|
| 95 |
+
checkCekLength(cek, parseInt(enc.slice(-3), 10));
|
| 96 |
+
return cbcDecrypt(enc, cek, ciphertext, iv, tag, aad);
|
| 97 |
+
case 'A128GCM':
|
| 98 |
+
case 'A192GCM':
|
| 99 |
+
case 'A256GCM':
|
| 100 |
+
if (cek instanceof Uint8Array)
|
| 101 |
+
checkCekLength(cek, parseInt(enc.slice(1, 4), 10));
|
| 102 |
+
return gcmDecrypt(enc, cek, ciphertext, iv, tag, aad);
|
| 103 |
+
default:
|
| 104 |
+
throw new JOSENotSupported('Unsupported JWE Content Encryption Algorithm');
|
| 105 |
+
}
|
| 106 |
+
}
|
public/vendor/jose/lib/decrypt_key_management.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import * as aeskw from './aeskw.js';
|
| 2 |
+
import * as ecdhes from './ecdhes.js';
|
| 3 |
+
import * as pbes2kw from './pbes2kw.js';
|
| 4 |
+
import * as rsaes from './rsaes.js';
|
| 5 |
+
import { decode as b64u } from '../util/base64url.js';
|
| 6 |
+
import { JOSENotSupported, JWEInvalid } from '../util/errors.js';
|
| 7 |
+
import { cekLength } from '../lib/cek.js';
|
| 8 |
+
import { importJWK } from '../key/import.js';
|
| 9 |
+
import { isObject } from './is_object.js';
|
| 10 |
+
import { unwrap as aesGcmKw } from './aesgcmkw.js';
|
| 11 |
+
import { assertCryptoKey } from './is_key_like.js';
|
| 12 |
+
export async function decryptKeyManagement(alg, key, encryptedKey, joseHeader, options) {
|
| 13 |
+
switch (alg) {
|
| 14 |
+
case 'dir': {
|
| 15 |
+
if (encryptedKey !== undefined)
|
| 16 |
+
throw new JWEInvalid('Encountered unexpected JWE Encrypted Key');
|
| 17 |
+
return key;
|
| 18 |
+
}
|
| 19 |
+
case 'ECDH-ES':
|
| 20 |
+
if (encryptedKey !== undefined)
|
| 21 |
+
throw new JWEInvalid('Encountered unexpected JWE Encrypted Key');
|
| 22 |
+
case 'ECDH-ES+A128KW':
|
| 23 |
+
case 'ECDH-ES+A192KW':
|
| 24 |
+
case 'ECDH-ES+A256KW': {
|
| 25 |
+
if (!isObject(joseHeader.epk))
|
| 26 |
+
throw new JWEInvalid(`JOSE Header "epk" (Ephemeral Public Key) missing or invalid`);
|
| 27 |
+
assertCryptoKey(key);
|
| 28 |
+
if (!ecdhes.allowed(key))
|
| 29 |
+
throw new JOSENotSupported('ECDH with the provided key is not allowed or not supported by your javascript runtime');
|
| 30 |
+
const epk = await importJWK(joseHeader.epk, alg);
|
| 31 |
+
assertCryptoKey(epk);
|
| 32 |
+
let partyUInfo;
|
| 33 |
+
let partyVInfo;
|
| 34 |
+
if (joseHeader.apu !== undefined) {
|
| 35 |
+
if (typeof joseHeader.apu !== 'string')
|
| 36 |
+
throw new JWEInvalid(`JOSE Header "apu" (Agreement PartyUInfo) invalid`);
|
| 37 |
+
try {
|
| 38 |
+
partyUInfo = b64u(joseHeader.apu);
|
| 39 |
+
}
|
| 40 |
+
catch {
|
| 41 |
+
throw new JWEInvalid('Failed to base64url decode the apu');
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
if (joseHeader.apv !== undefined) {
|
| 45 |
+
if (typeof joseHeader.apv !== 'string')
|
| 46 |
+
throw new JWEInvalid(`JOSE Header "apv" (Agreement PartyVInfo) invalid`);
|
| 47 |
+
try {
|
| 48 |
+
partyVInfo = b64u(joseHeader.apv);
|
| 49 |
+
}
|
| 50 |
+
catch {
|
| 51 |
+
throw new JWEInvalid('Failed to base64url decode the apv');
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
const sharedSecret = await ecdhes.deriveKey(epk, key, alg === 'ECDH-ES' ? joseHeader.enc : alg, alg === 'ECDH-ES' ? cekLength(joseHeader.enc) : parseInt(alg.slice(-5, -2), 10), partyUInfo, partyVInfo);
|
| 55 |
+
if (alg === 'ECDH-ES')
|
| 56 |
+
return sharedSecret;
|
| 57 |
+
if (encryptedKey === undefined)
|
| 58 |
+
throw new JWEInvalid('JWE Encrypted Key missing');
|
| 59 |
+
return aeskw.unwrap(alg.slice(-6), sharedSecret, encryptedKey);
|
| 60 |
+
}
|
| 61 |
+
case 'RSA-OAEP':
|
| 62 |
+
case 'RSA-OAEP-256':
|
| 63 |
+
case 'RSA-OAEP-384':
|
| 64 |
+
case 'RSA-OAEP-512': {
|
| 65 |
+
if (encryptedKey === undefined)
|
| 66 |
+
throw new JWEInvalid('JWE Encrypted Key missing');
|
| 67 |
+
assertCryptoKey(key);
|
| 68 |
+
return rsaes.decrypt(alg, key, encryptedKey);
|
| 69 |
+
}
|
| 70 |
+
case 'PBES2-HS256+A128KW':
|
| 71 |
+
case 'PBES2-HS384+A192KW':
|
| 72 |
+
case 'PBES2-HS512+A256KW': {
|
| 73 |
+
if (encryptedKey === undefined)
|
| 74 |
+
throw new JWEInvalid('JWE Encrypted Key missing');
|
| 75 |
+
if (typeof joseHeader.p2c !== 'number')
|
| 76 |
+
throw new JWEInvalid(`JOSE Header "p2c" (PBES2 Count) missing or invalid`);
|
| 77 |
+
const p2cLimit = options?.maxPBES2Count || 10_000;
|
| 78 |
+
if (joseHeader.p2c > p2cLimit)
|
| 79 |
+
throw new JWEInvalid(`JOSE Header "p2c" (PBES2 Count) out is of acceptable bounds`);
|
| 80 |
+
if (typeof joseHeader.p2s !== 'string')
|
| 81 |
+
throw new JWEInvalid(`JOSE Header "p2s" (PBES2 Salt) missing or invalid`);
|
| 82 |
+
let p2s;
|
| 83 |
+
try {
|
| 84 |
+
p2s = b64u(joseHeader.p2s);
|
| 85 |
+
}
|
| 86 |
+
catch {
|
| 87 |
+
throw new JWEInvalid('Failed to base64url decode the p2s');
|
| 88 |
+
}
|
| 89 |
+
return pbes2kw.unwrap(alg, key, encryptedKey, joseHeader.p2c, p2s);
|
| 90 |
+
}
|
| 91 |
+
case 'A128KW':
|
| 92 |
+
case 'A192KW':
|
| 93 |
+
case 'A256KW': {
|
| 94 |
+
if (encryptedKey === undefined)
|
| 95 |
+
throw new JWEInvalid('JWE Encrypted Key missing');
|
| 96 |
+
return aeskw.unwrap(alg, key, encryptedKey);
|
| 97 |
+
}
|
| 98 |
+
case 'A128GCMKW':
|
| 99 |
+
case 'A192GCMKW':
|
| 100 |
+
case 'A256GCMKW': {
|
| 101 |
+
if (encryptedKey === undefined)
|
| 102 |
+
throw new JWEInvalid('JWE Encrypted Key missing');
|
| 103 |
+
if (typeof joseHeader.iv !== 'string')
|
| 104 |
+
throw new JWEInvalid(`JOSE Header "iv" (Initialization Vector) missing or invalid`);
|
| 105 |
+
if (typeof joseHeader.tag !== 'string')
|
| 106 |
+
throw new JWEInvalid(`JOSE Header "tag" (Authentication Tag) missing or invalid`);
|
| 107 |
+
let iv;
|
| 108 |
+
try {
|
| 109 |
+
iv = b64u(joseHeader.iv);
|
| 110 |
+
}
|
| 111 |
+
catch {
|
| 112 |
+
throw new JWEInvalid('Failed to base64url decode the iv');
|
| 113 |
+
}
|
| 114 |
+
let tag;
|
| 115 |
+
try {
|
| 116 |
+
tag = b64u(joseHeader.tag);
|
| 117 |
+
}
|
| 118 |
+
catch {
|
| 119 |
+
throw new JWEInvalid('Failed to base64url decode the tag');
|
| 120 |
+
}
|
| 121 |
+
return aesGcmKw(alg, key, encryptedKey, iv, tag);
|
| 122 |
+
}
|
| 123 |
+
default: {
|
| 124 |
+
throw new JOSENotSupported('Invalid or unsupported "alg" (JWE Algorithm) header value');
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
}
|
public/vendor/jose/lib/digest.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export async function digest(algorithm, data) {
|
| 2 |
+
const subtleDigest = `SHA-${algorithm.slice(-3)}`;
|
| 3 |
+
return new Uint8Array(await crypto.subtle.digest(subtleDigest, data));
|
| 4 |
+
}
|
public/vendor/jose/lib/ecdhes.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { encode, concat, uint32be } from './buffer_utils.js';
|
| 2 |
+
import { checkEncCryptoKey } from './crypto_key.js';
|
| 3 |
+
import { digest } from './digest.js';
|
| 4 |
+
function lengthAndInput(input) {
|
| 5 |
+
return concat(uint32be(input.length), input);
|
| 6 |
+
}
|
| 7 |
+
async function concatKdf(Z, L, OtherInfo) {
|
| 8 |
+
const dkLen = L >> 3;
|
| 9 |
+
const hashLen = 32;
|
| 10 |
+
const reps = Math.ceil(dkLen / hashLen);
|
| 11 |
+
const dk = new Uint8Array(reps * hashLen);
|
| 12 |
+
for (let i = 1; i <= reps; i++) {
|
| 13 |
+
const hashInput = new Uint8Array(4 + Z.length + OtherInfo.length);
|
| 14 |
+
hashInput.set(uint32be(i), 0);
|
| 15 |
+
hashInput.set(Z, 4);
|
| 16 |
+
hashInput.set(OtherInfo, 4 + Z.length);
|
| 17 |
+
const hashResult = await digest('sha256', hashInput);
|
| 18 |
+
dk.set(hashResult, (i - 1) * hashLen);
|
| 19 |
+
}
|
| 20 |
+
return dk.slice(0, dkLen);
|
| 21 |
+
}
|
| 22 |
+
export async function deriveKey(publicKey, privateKey, algorithm, keyLength, apu = new Uint8Array(), apv = new Uint8Array()) {
|
| 23 |
+
checkEncCryptoKey(publicKey, 'ECDH');
|
| 24 |
+
checkEncCryptoKey(privateKey, 'ECDH', 'deriveBits');
|
| 25 |
+
const algorithmID = lengthAndInput(encode(algorithm));
|
| 26 |
+
const partyUInfo = lengthAndInput(apu);
|
| 27 |
+
const partyVInfo = lengthAndInput(apv);
|
| 28 |
+
const suppPubInfo = uint32be(keyLength);
|
| 29 |
+
const suppPrivInfo = new Uint8Array();
|
| 30 |
+
const otherInfo = concat(algorithmID, partyUInfo, partyVInfo, suppPubInfo, suppPrivInfo);
|
| 31 |
+
const Z = new Uint8Array(await crypto.subtle.deriveBits({
|
| 32 |
+
name: publicKey.algorithm.name,
|
| 33 |
+
public: publicKey,
|
| 34 |
+
}, privateKey, getEcdhBitLength(publicKey)));
|
| 35 |
+
return concatKdf(Z, keyLength, otherInfo);
|
| 36 |
+
}
|
| 37 |
+
function getEcdhBitLength(publicKey) {
|
| 38 |
+
if (publicKey.algorithm.name === 'X25519') {
|
| 39 |
+
return 256;
|
| 40 |
+
}
|
| 41 |
+
return (Math.ceil(parseInt(publicKey.algorithm.namedCurve.slice(-3), 10) / 8) << 3);
|
| 42 |
+
}
|
| 43 |
+
export function allowed(key) {
|
| 44 |
+
switch (key.algorithm.namedCurve) {
|
| 45 |
+
case 'P-256':
|
| 46 |
+
case 'P-384':
|
| 47 |
+
case 'P-521':
|
| 48 |
+
return true;
|
| 49 |
+
default:
|
| 50 |
+
return key.algorithm.name === 'X25519';
|
| 51 |
+
}
|
| 52 |
+
}
|