Skip to content

Commit 4c85374

Browse files
author
Jake Champion
committed
chore: add documentation for the new SubtleCrypto methods we support
1 parent 820dfcc commit 4c85374

File tree

21 files changed

+12131
-11415
lines changed

21 files changed

+12131
-11415
lines changed

documentation/app/fastly.toml

Lines changed: 11519 additions & 11375 deletions
Large diffs are not rendered by default.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# CryptoKey
8+
9+
The **`CryptoKey`** interface represents a cryptographic key obtained from one of the [`SubtleCrypto`](../SubtleCrypto/SubtleCrypto.mdx) method [`importKey()`](../SubtleCrypto/prototype/importKey.mdx).
10+
11+
## Instance properties
12+
13+
- [`type`](./prototype/type.mdx) _**readonly**_
14+
- : The type of key the object represents. It may take one of the following values: `"secret"`, `"private"` or `"public"`.
15+
16+
- [`extractable`](./prototype/extractable.mdx) _**readonly**_
17+
- : A boolean value indicating whether or not the key may be extracted.
18+
19+
- [`algorithm`](./prototype/algorithm.mdx) _**readonly**_
20+
- : An object describing the algorithm for which this key can be used and any associated extra parameters.
21+
22+
- [`usages`](./prototype/usages.mdx) _**readonly**_
23+
- : An `Array` of strings, indicating what can be done with the key. Possible values for array elements are `"encrypt"`, `"decrypt"`, `"sign"`, `"verify"`, `"deriveKey"`, `"deriveBits"`, `"wrapKey"`, and `"unwrapKey"`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# algorithm
9+
10+
The read-only **`algorithm`** property of the [`CryptoKey`](../CryptoKey.mdx) interface returns an object describing the algorithm for which this key can be used, and any associated extra parameters.
11+
12+
The object returned depends of the algorithm used to generate the key.
13+
14+
## Value
15+
16+
An object matching:
17+
18+
- [`RsaHashedImportParams`](../../RsaHashedImportParams/RsaHashedImportParams.mdx) if the algorithm is any of the RSA variants.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# extractable
9+
10+
The read-only **`extractable`** property indicates whether or not the key may be extracted.
11+
12+
If the key cannot be exported, an exception will be thrown if an attempt to extract the key is made.
13+
14+
## Value
15+
16+
A boolean value that is `true` if the key can be exported and `false` if not.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# type
9+
10+
The read-only **`type`** property indicates which kind of key is represented by the object. It can have the following values:
11+
12+
- `"secret"`: This key is a secret key for use with a symmetric algorithm.
13+
- `"private"`: This key is the private half of an asymmetric algorithm's key pair.
14+
- `"public"`: This key is the public half of an asymmetric algorithm's key pair.
15+
16+
## Value
17+
18+
One of the following strings: `"secret"`, `"private"`, or `"public"`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# usages
9+
10+
The read-only **`usages`** property indicates what can be done with the key.
11+
12+
## Value
13+
14+
An `Array` of strings from the following list:
15+
16+
- `"encrypt"`: The key may be used to encrypt messages.
17+
- `"decrypt"`: The key may be used to decrypt messages.
18+
- `"sign"`: The key may be used to sign messages.
19+
- `"verify"`: The key may be used to verify signatures.
20+
- `"deriveKey"`: The key may be used in deriving a new key.
21+
- `"deriveBits"`: The key may be used in deriving bits.
22+
- `"wrapKey"`: The key may be used to wrap a key.
23+
- `"unwrapKey"`: The key may be used to unwrap a key.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# RsaHashedImportParams
8+
9+
The **`RsaHashedImportParams`** dictionary represents the object that should be passed as the `algorithm` parameter into {{domxref("SubtleCrypto.importKey()")}}, when importing any RSA-based key pair.
10+
11+
## Instance properties
12+
13+
- `name`
14+
- : A string. This should be set to `RSASSA-PKCS1-v1_5`.
15+
16+
- `hash`
17+
- : A string representing the name of the digest function to use. This can be one of `SHA-256`, `SHA-384`, or `SHA-512`.
18+
> **Warning:** Although you can technically pass `SHA-1` here, this is strongly discouraged as it is considered vulnerable.
19+

documentation/docs/globals/SubtleCrypto/SubtleCrypto.mdx

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,3 @@ The **`SubtleCrypto`** interface provides a number of low-level cryptographic fu
1616
>
1717
> Please learn and experiment, but don't guarantee or imply the security of your work before an individual knowledgeable in this subject matter thoroughly reviews it. The [Crypto 101 Course](https://www.crypto101.io/) can be a great place to start learning about the design and implementation of secure systems.
1818
19-
## Instance properties
20-
21-
_This interface doesn't inherit any properties, as it has no parent interface._
22-
23-
## Instance methods
24-
25-
_This interface doesn't inherit any methods, as it has no parent interface._
26-
27-
- [`SubtleCrypto.digest()`](./prototype/digest.mdx)
28-
- : Returns a [`Promise`](../Promise/Promise.mdx) that fulfills with a digest generated from the algorithm and text given as parameters.
29-
30-
## Using SubtleCrypto
31-
32-
We can split the functions implemented by this API into two groups: cryptography functions and key management functions.
33-
34-
### Cryptography functions
35-
36-
These are the functions you can use to implement security features such as privacy and authentication in a system. The `SubtleCrypto` API provides the following cryptography functions:
37-
38-
- `SubtleCrypto.digest()`: create a fixed-length, collision-resistant digest of some data.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# importKey()
8+
9+
The **`importKey()`** method of the [`SubtleCrypto`](../SubtleCrypto.mdx)
10+
interface imports a key: that is, it takes as input a key in an external, portable
11+
format and gives you a [`CryptoKey`](../../CryptoKey/CryptoKey.mdx) object that you can use.
12+
13+
The function accepts several import formats: see [Supported formats](#supported-formats) for details.
14+
15+
## Syntax
16+
17+
```js
18+
importKey(format, keyData, algorithm, extractable, keyUsages)
19+
```
20+
21+
### Parameters
22+
23+
- `format`
24+
- : A string describing the data format of the key to import. It can be one of the following:
25+
- `jwk`: [JSON Web Key](#json_web_key) format.
26+
- `keyData`
27+
- : An `ArrayBuffer`, a TypedArray, a `DataView`, or a `JSONWebKey` object containing the key in
28+
the given format.
29+
- `algorithm`
30+
- : An object defining the type of key to import and providing extra algorithm-specific parameters.
31+
- For RSASSA-PKCS1-v1_5:
32+
Pass an [`RsaHashedImportParams`](../../RsaHashedImportParams/RsaHashedImportParams.mdx) object.
33+
- `extractable`
34+
- : A boolean value indicating whether it will be possible to export the key.
35+
- `keyUsages`
36+
- : An `Array` indicating what can be done with the key. Possible array values are:
37+
- `encrypt`: The key may be used to encrypt messages.
38+
- `decrypt`: The key may be used to decrypt messages.
39+
- `sign`: The key may be used to sign messages.
40+
- `verify`: The key may be used to verify signatures.
41+
- `deriveKey`: The key may be used in deriving a new key.
42+
- `deriveBits`: The key may be used in deriving bits.
43+
- `wrapKey`: The key may be used to wrap a key.
44+
- `unwrapKey`: The key may be used to unwrap a key.
45+
46+
### Return value
47+
48+
A [`Promise`](../../Promise/Promise.mdx)
49+
that fulfills with the imported key as a [`CryptoKey`](../../CryptoKey/CryptoKey.mdx) object.
50+
51+
### Exceptions
52+
53+
The promise is rejected when one of the following exceptions is encountered:
54+
55+
- `SyntaxError`
56+
- : Raised when `keyUsages` is empty but the unwrapped key is of
57+
type `secret` or `private`.
58+
- `TypeError`
59+
- : Raised when trying to use an invalid format or if the `keyData`
60+
is not suited for that format.
61+
62+
## Supported formats
63+
64+
This API currently supports one key import/export format: JSON Web Key.
65+
66+
67+
### JSON Web Key
68+
69+
You can use JSON Web Key format to import or export RSA or Elliptic Curve public or
70+
private keys, as well as AES and HMAC secret keys.
71+
72+
JSON Web Key format is defined in [RFC 7517](https://datatracker.ietf.org/doc/html/rfc7517).
73+
It describes a way to represent public, private, and secret keys as JSON objects.
74+
75+
A JSON Web Key looks something like this (this is an EC private key):
76+
77+
```json
78+
{
79+
"crv": "P-384",
80+
"d": "wouCtU7Nw4E8_7n5C1-xBjB4xqSb_liZhYMsy8MGgxUny6Q8NCoH9xSiviwLFfK_",
81+
"ext": true,
82+
"key_ops": ["sign"],
83+
"kty": "EC",
84+
"x": "SzrRXmyI8VWFJg1dPUNbFcc9jZvjZEfH7ulKI1UkXAltd7RGWrcfFxqyGPcwu6AQ",
85+
"y": "hHUag3OvDzEr0uUQND4PXHQTXP5IDGdYhJhL-WLKjnGjQAw0rNGy5V29-aV-yseW"
86+
};
87+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# sign()
8+
9+
The **`sign()`** method of the [`SubtleCrypto`](../SubtleCrypto.mdx) interface generates a digital signature.
10+
11+
It takes as its arguments a key to sign with, some algorithm-specific
12+
parameters, and the data to sign. It returns a `Promise` which will be
13+
fulfilled with the signature.
14+
15+
You can use the corresponding [`verify()`](./verify.mdx) method to verify the signature.
16+
17+
## Syntax
18+
19+
```js
20+
sign(algorithm, key, data)
21+
```
22+
23+
### Parameters
24+
25+
- `algorithm`
26+
- : A string or object that specifies the signature algorithm to use and its parameters:
27+
- To use [RSASSA-PKCS1-v1_5](#rsassa-pkcs1-v1_5), pass the string `"RSASSA-PKCS1-v1_5"` or an object of the form `{ "name": "RSASSA-PKCS1-v1_5" }`.
28+
- `key`
29+
- : A [`CryptoKey`](../../CryptoKey/CryptoKey.mdx) object containing the key to be used for signing.
30+
If `algorithm` identifies a public-key cryptosystem, this is the private key.
31+
- `data`
32+
- : An `ArrayBuffer`, a TypedArray or a `DataView` object containing the data to be signed.
33+
34+
### Return value
35+
36+
A `Promise` that fulfills with an `ArrayBuffer` containing the signature.
37+
38+
### Exceptions
39+
40+
The promise is rejected when the following exception is encountered:
41+
42+
- `InvalidAccessError`
43+
- : Raised when the signing key is not a key for the request signing algorithm or when
44+
trying to use an algorithm that is either unknown or isn't suitable for signing.
45+
46+
## Supported algorithms
47+
48+
### RSASSA-PKCS1-v1_5
49+
50+
The RSASSA-PKCS1-v1_5 algorithm is specified in [RFC 3447](https://datatracker.ietf.org/doc/html/rfc3447).
51+

0 commit comments

Comments
 (0)