(key, ctx, name = 'key')
| 550 | |
| 551 | |
| 552 | function prepareAsymmetricKey(key, ctx, name = 'key') { |
| 553 | if (isKeyObject(key)) { |
| 554 | // Best case: A key object, as simple as that. |
| 555 | const type = getKeyObjectType(key); |
| 556 | validateAsymmetricKeyType(type, ctx, key); |
| 557 | return { data: getKeyObjectHandle(key) }; |
| 558 | } |
| 559 | if (isCryptoKey(key)) { |
| 560 | emitDEP0203(); |
| 561 | validateAsymmetricKeyType(getCryptoKeyType(key), ctx, key); |
| 562 | return { data: getCryptoKeyHandle(key) }; |
| 563 | } |
| 564 | if (isStringOrBuffer(key)) { |
| 565 | // Expect PEM by default, mostly for backward compatibility. |
| 566 | return { format: kKeyFormatPEM, data: getArrayBufferOrView(key, name) }; |
| 567 | } |
| 568 | if (typeof key === 'object') { |
| 569 | const { key: data, encoding, format } = key; |
| 570 | |
| 571 | // The 'key' property can be a KeyObject as well to allow specifying |
| 572 | // additional options such as padding along with the key. |
| 573 | if (isKeyObject(data)) { |
| 574 | const type = getKeyObjectType(data); |
| 575 | validateAsymmetricKeyType(type, ctx, data); |
| 576 | return { data: getKeyObjectHandle(data) }; |
| 577 | } |
| 578 | if (isCryptoKey(data)) { |
| 579 | emitDEP0203(); |
| 580 | validateAsymmetricKeyType(getCryptoKeyType(data), ctx, data); |
| 581 | return { data: getCryptoKeyHandle(data) }; |
| 582 | } |
| 583 | if (format === 'jwk') { |
| 584 | validateObject(data, `${name}.key`); |
| 585 | return { data, format: kKeyFormatJWK }; |
| 586 | } else if (format === 'raw-public' || format === 'raw-private' || |
| 587 | format === 'raw-seed') { |
| 588 | if ((ctx === kConsumePrivate || ctx === kCreatePrivate) && |
| 589 | format === 'raw-public') { |
| 590 | throw new ERR_INVALID_ARG_VALUE(`${name}.format`, format); |
| 591 | } |
| 592 | if (!isArrayBufferView(data) && !isAnyArrayBuffer(data)) { |
| 593 | throw new ERR_INVALID_ARG_TYPE( |
| 594 | `${name}.key`, |
| 595 | ['ArrayBuffer', 'Buffer', 'TypedArray', 'DataView'], |
| 596 | data); |
| 597 | } |
| 598 | validateString(key.asymmetricKeyType, `${name}.asymmetricKeyType`); |
| 599 | if (key.asymmetricKeyType === 'ec') { |
| 600 | validateString(key.namedCurve, `${name}.namedCurve`); |
| 601 | } |
| 602 | const rawFormat = parseKeyFormat(format, undefined, `${name}.format`); |
| 603 | return { |
| 604 | data: getArrayBufferOrView(data, `${name}.key`), |
| 605 | format: rawFormat, |
| 606 | type: key.asymmetricKeyType, |
| 607 | namedCurve: key.namedCurve ?? null, |
| 608 | }; |
| 609 | } |
no test coverage detected
searching dependent graphs…