| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ const { | |||
| 8 | 8 | ReflectApply, | |
| 9 | 9 | ReflectConstruct, | |
| 10 | 10 | SafeSet, | |
| 11 | + StringPrototypeRepeat, | ||
| 11 | 12 | SymbolToStringTag, | |
| 12 | 13 | } = primordials; | |
| 13 | 14 | ||
@@ -680,7 +681,16 @@ async function wrapKey(format, key, wrappingKey, algorithm) { | |||
| 680 | 681 | let keyData = await ReflectApply(exportKey, this, [format, key]); | |
| 681 | 682 | ||
| 682 | 683 | if (format === 'jwk') { | |
| 683 | - keyData = new TextEncoder().encode(JSONStringify(keyData)); | ||
| 684 | + const ec = new TextEncoder(); | ||
| 685 | + const raw = JSONStringify(keyData); | ||
| 686 | + // As per the NOTE in step 13 https://w3c.github.io/webcrypto/#SubtleCrypto-method-wrapKey | ||
| 687 | + // we're padding AES-KW wrapped JWK to make sure it is always a multiple of 8 bytes | ||
| 688 | + // in length | ||
| 689 | + if (algorithm.name === 'AES-KW' && raw.length % 8 !== 0) { | ||
| 690 | + keyData = ec.encode(raw + StringPrototypeRepeat(' ', 8 - (raw.length % 8))); | ||
| 691 | + } else { | ||
| 692 | + keyData = ec.encode(raw); | ||
| 693 | + } | ||
| 684 | 694 | } | |
| 685 | 695 | ||
| 686 | 696 | return cipherOrWrap( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -231,6 +231,10 @@ function getFormats(key) { | |||
| 231 | 231 | // material length must be a multiple of 8. | |
| 232 | 232 | // If the wrapping algorithm is RSA-OAEP, the exported key | |
| 233 | 233 | // material maximum length is a factor of the modulusLength | |
| 234 | + // | ||
| 235 | + // As per the NOTE in step 13 https://w3c.github.io/webcrypto/#SubtleCrypto-method-wrapKey | ||
| 236 | + // we're padding AES-KW wrapped JWK to make sure it is always a multiple of 8 bytes | ||
| 237 | + // in length | ||
| 234 | 238 | async function wrappingIsPossible(name, exported) { | |
| 235 | 239 | if ('byteLength' in exported) { | |
| 236 | 240 | switch (name) { | |
@@ -239,13 +243,8 @@ async function wrappingIsPossible(name, exported) { | |||
| 239 | 243 | case 'RSA-OAEP': | |
| 240 | 244 | return exported.byteLength <= 446; | |
| 241 | 245 | } | |
| 242 | - } else if ('kty' in exported) { | ||
| 243 | - switch (name) { | ||
| 244 | - case 'AES-KW': | ||
| 245 | - return JSON.stringify(exported).length % 8 === 0; | ||
| 246 | - case 'RSA-OAEP': | ||
| 247 | - return JSON.stringify(exported).length <= 478; | ||
| 248 | - } | ||
| 246 | + } else if ('kty' in exported && name === 'RSA-OAEP') { | ||
| 247 | + return JSON.stringify(exported).length <= 478; | ||
| 249 | 248 | } | |
| 250 | 249 | return true; | |
| 251 | 250 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments