{"version":3,"file":"KmsOperation.mjs","names":[],"sources":["../../../../src/modules/kms/options/KmsOperation.ts"],"sourcesContent":["import { KeyManagementError } from '../error/KeyManagementError'\nimport { getJwkHumanDescription, type KmsJwkPrivate, type KnownJwaSignatureAlgorithm } from '../jwk'\nimport type { KmsCreateKeyType } from './KmsCreateKeyOptions'\nimport type { KmsDecryptDataDecryption } from './KmsDecryptOptions'\nimport type { KmsEncryptDataEncryption } from './KmsEncryptOptions'\nimport type { KmsKeyAgreementDecryptOptions } from './KmsKeyAgreementDecryptOptions'\nimport type { KmsKeyAgreementEncryptOptions } from './KmsKeyAgreementEncryptOptions'\n\nexport type KmsOperationCreateKey = {\n  operation: 'createKey'\n  type: KmsCreateKeyType\n}\n\nexport type KmsOperationImportKey = {\n  operation: 'importKey'\n  privateJwk: KmsJwkPrivate\n}\n\nexport type KmsOperationDeleteKey = {\n  operation: 'deleteKey'\n}\n\nexport type KmsOperationSign = {\n  operation: 'sign'\n  algorithm: KnownJwaSignatureAlgorithm\n}\n\nexport type KmsOperationVerify = {\n  operation: 'verify'\n  algorithm: KnownJwaSignatureAlgorithm\n}\n\n/**\n * The key id is not needed to determine whether an operation is supported, and is\n * often not known yet when checking for support (e.g. the ephemeral key for `ECDH-ES`\n * is only created once we know the key agreement is supported).\n */\ntype WithOptionalKeyId<Options> = Options extends { keyId: string }\n  ? Omit<Options, 'keyId'> & { keyId?: string }\n  : Options\n\nexport type KmsOperationEncrypt = {\n  operation: 'encrypt'\n  /**\n   * `HPKE` for the integrated-encryption HPKE algorithms, where the suite fixes the AEAD.\n   */\n  encryption: KmsEncryptDataEncryption\n  keyAgreement?: WithOptionalKeyId<KmsKeyAgreementEncryptOptions>\n}\n\nexport type KmsOperationDecrypt = {\n  operation: 'decrypt'\n  /**\n   * `HPKE` for the integrated-encryption HPKE algorithms, where the suite fixes the AEAD.\n   */\n  decryption: KmsDecryptDataDecryption\n  keyAgreement?: WithOptionalKeyId<KmsKeyAgreementDecryptOptions>\n}\n\nexport type KmsOperationRandomBytes = {\n  operation: 'randomBytes'\n}\n\nexport type KmsOperation =\n  | KmsOperationCreateKey\n  | KmsOperationImportKey\n  | KmsOperationDeleteKey\n  | KmsOperationSign\n  | KmsOperationVerify\n  | KmsOperationEncrypt\n  | KmsOperationDecrypt\n  | KmsOperationRandomBytes\n\nexport function getKmsOperationHumanDescription(operation: KmsOperation) {\n  if (operation.operation === 'deleteKey') {\n    return \"'deleteKey' operation\"\n  }\n\n  if (operation.operation === 'createKey') {\n    let base = `'createKey' operation with kty '${operation.type.kty}'`\n\n    if (operation.type.kty === 'EC' || operation.type.kty === 'OKP') {\n      base += ` and crv '${operation.type.crv}'`\n    } else if (operation.type.kty === 'RSA') {\n      base += ` and bit length '${operation.type.modulusLength}'`\n    } else if (operation.type.kty === 'oct') {\n      base += ` and algorithm '${operation.type.algorithm}'`\n\n      if (operation.type.algorithm === 'aes' || operation.type.algorithm === 'hmac') {\n        base += ` with key length '${operation.type.length}'`\n      }\n    }\n\n    return base\n  }\n\n  if (operation.operation === 'importKey') {\n    return `'importKey' operation with ${getJwkHumanDescription(operation.privateJwk)}`\n  }\n\n  if (operation.operation === 'sign' || operation.operation === 'verify') {\n    return `'${operation.operation}' operation with algorithm '${operation.algorithm}'`\n  }\n\n  if (operation.operation === 'encrypt' || operation.operation === 'decrypt') {\n    const encryption = operation.operation === 'encrypt' ? operation.encryption : operation.decryption\n\n    let message = `'${operation.operation}' operation with encryption algorithm '${encryption.algorithm}'`\n    if (operation.keyAgreement) {\n      message += ` and key agreement algorithm '${operation.keyAgreement.algorithm}'`\n    }\n    return message\n  }\n\n  if (operation.operation === 'randomBytes') {\n    return `'randomBytes' operation`\n  }\n\n  throw new KeyManagementError('Unsupported operation')\n}\n"],"mappings":";;;;AAyEA,SAAgB,gCAAgC,WAAyB;CACvE,IAAI,UAAU,cAAc,aAC1B,OAAO;CAGT,IAAI,UAAU,cAAc,aAAa;EACvC,IAAI,OAAO,mCAAmC,UAAU,KAAK,IAAI;EAEjE,IAAI,UAAU,KAAK,QAAQ,QAAQ,UAAU,KAAK,QAAQ,OACxD,QAAQ,aAAa,UAAU,KAAK,IAAI;OACnC,IAAI,UAAU,KAAK,QAAQ,OAChC,QAAQ,oBAAoB,UAAU,KAAK,cAAc;OACpD,IAAI,UAAU,KAAK,QAAQ,OAAO;GACvC,QAAQ,mBAAmB,UAAU,KAAK,UAAU;GAEpD,IAAI,UAAU,KAAK,cAAc,SAAS,UAAU,KAAK,cAAc,QACrE,QAAQ,qBAAqB,UAAU,KAAK,OAAO;EAEvD;EAEA,OAAO;CACT;CAEA,IAAI,UAAU,cAAc,aAC1B,OAAO,8BAA8B,uBAAuB,UAAU,UAAU;CAGlF,IAAI,UAAU,cAAc,UAAU,UAAU,cAAc,UAC5D,OAAO,IAAI,UAAU,UAAU,8BAA8B,UAAU,UAAU;CAGnF,IAAI,UAAU,cAAc,aAAa,UAAU,cAAc,WAAW;EAC1E,MAAM,aAAa,UAAU,cAAc,YAAY,UAAU,aAAa,UAAU;EAExF,IAAI,UAAU,IAAI,UAAU,UAAU,yCAAyC,WAAW,UAAU;EACpG,IAAI,UAAU,cACZ,WAAW,iCAAiC,UAAU,aAAa,UAAU;EAE/E,OAAO;CACT;CAEA,IAAI,UAAU,cAAc,eAC1B,OAAO;CAGT,MAAM,IAAI,mBAAmB,uBAAuB;AACtD"}