The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 2 | import * as PaymentCryptography from "@alchemy.run/aws/PaymentCryptography"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import path from "pathe"; |
| 10 | |
| 11 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 12 | |
| 13 | // Decoded distilled Sensitive* outputs are Redacted at runtime. |
| 14 | const unwrap = (value: string | Redacted.Redacted<string>): string => |
| 15 | Redacted.isRedacted(value) ? Redacted.value(value) : value; |
| 16 | |
| 17 | // Deterministic IV for the CBC round-trip routes (test-only). |
| 18 | const IV = "00000000000000000000000000000000"; |
| 19 | |
| 20 | export class PaymentCryptographyTestFunction extends Lambda.Function<Lambda.Function>()( |
| 21 | "PaymentCryptographyTestFunction", |
| 22 | ) {} |
| 23 | |
| 24 | export default PaymentCryptographyTestFunction.make( |
| 25 | { |
| 26 | main, |
| 27 | functionUrl: true, |
| 28 | timeout: Duration.seconds(30), |
| 29 | }, |
| 30 | Effect.gen(function* () { |
| 31 | const dataKey = yield* PaymentCryptography.Key("DataKey", { |
| 32 | keyAttributes: { |
| 33 | keyAlgorithm: "AES_128", |
| 34 | keyClass: "SYMMETRIC_KEY", |
| 35 | keyUsage: "TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY", |
| 36 | keyModesOfUse: { |
| 37 | encrypt: true, |
| 38 | decrypt: true, |
| 39 | wrap: true, |
| 40 | unwrap: true, |
| 41 | }, |
| 42 | }, |
| 43 | }); |
| 44 | // DUKPT Base Derivation Key — the ReEncryptData incoming side (the |
| 45 | // service rejects symmetric->symmetric re-encryption; the operation |
| 46 | // translates DUKPT terminal ciphertext to a working key). |
| 47 | const bdk = yield* PaymentCryptography.Key("Bdk", { |
| 48 | keyAttributes: { |
| 49 | keyAlgorithm: "TDES_2KEY", |
| 50 | keyClass: "SYMMETRIC_KEY", |
| 51 | keyUsage: "TR31_B0_BASE_DERIVATION_KEY", |
| 52 | keyModesOfUse: { deriveKey: true }, |
| 53 | }, |
| 54 | }); |
| 55 | const macKey = yield* PaymentCryptography.Key("MacKey", { |
| 56 | keyAttributes: { |
| 57 | keyAlgorithm: "HMAC_SHA256", |
| 58 | keyClass: "SYMMETRIC_KEY", |
| 59 | keyUsage: "TR31_M7_HMAC_KEY", |
| 60 | keyModesOfUse: { generate: true, verify: true }, |
| 61 | }, |
| 62 | }); |
| 63 | // Card Verification Key for CVV2 generation/verification. |
| 64 | const cvk = yield* PaymentCryptography.Key("Cvk", { |
| 65 | keyAttributes: { |
| 66 | keyAlgorithm: "TDES_2KEY", |
| 67 | keyClass: "SYMMETRIC_KEY", |
| 68 | keyUsage: "TR31_C0_CARD_VERIFICATION_KEY", |
| 69 | keyModesOfUse: { generate: true, verify: true }, |
| 70 | }, |
| 71 | }); |
| 72 | // PIN Encryption Keys — PIN blocks are encrypted/translated under these. |
| 73 | const pek = yield* PaymentCryptography.Key("Pek", { |
| 74 | keyAttributes: { |
| 75 | keyAlgorithm: "TDES_2KEY", |
| 76 | keyClass: "SYMMETRIC_KEY", |
| 77 | keyUsage: "TR31_P0_PIN_ENCRYPTION_KEY", |
| 78 | keyModesOfUse: { |
| 79 | encrypt: true, |
| 80 | decrypt: true, |
| 81 | wrap: true, |
| 82 | unwrap: true, |
| 83 | }, |
| 84 | }, |
| 85 | }); |
| 86 | const pek2 = yield* PaymentCryptography.Key("Pek2", { |
| 87 | keyAttributes: { |
| 88 | keyAlgorithm: "TDES_2KEY", |
| 89 | keyClass: "SYMMETRIC_KEY", |
| 90 | keyUsage: "TR31_P0_PIN_ENCRYPTION_KEY", |
| 91 | keyModesOfUse: { |
| 92 | encrypt: true, |
| 93 | decrypt: true, |
| 94 | wrap: true, |
| 95 | unwrap: true, |
| 96 | }, |
| 97 | }, |
| 98 | }); |
| 99 | // Visa PIN Verification Key — generates/verifies PVVs. |
| 100 | const pvk = yield* PaymentCryptography.Key("Pvk", { |
| 101 | keyAttributes: { |
| 102 | keyAlgorithm: "TDES_2KEY", |
| 103 | keyClass: "SYMMETRIC_KEY", |
| 104 | keyUsage: "TR31_V2_VISA_PIN_VERIFICATION_KEY", |
| 105 | keyModesOfUse: { generate: true, verify: true }, |
| 106 | }, |
| 107 | }); |
| 108 | // Asymmetric signing key pair — GetPublicKeyCertificate target. |
| 109 | const signKey = yield* PaymentCryptography.Key("SignKey", { |
| 110 | keyAttributes: { |
| 111 | keyAlgorithm: "ECC_NIST_P256", |
| 112 | keyClass: "ASYMMETRIC_KEY_PAIR", |
| 113 | keyUsage: "TR31_S0_ASYMMETRIC_KEY_FOR_DIGITAL_SIGNATURE", |
| 114 | // S0 key pairs generated in-service sign with the private half; the |
| 115 | // service rejects sign+verify combined on this usage. |
| 116 | keyModesOfUse: { sign: true }, |
| 117 | }, |
| 118 | }); |
| 119 | |
| 120 | const encryptData = yield* PaymentCryptography.EncryptData(dataKey); |
| 121 | const encryptDukpt = yield* PaymentCryptography.EncryptData(bdk); |
| 122 | const decryptData = yield* PaymentCryptography.DecryptData(dataKey); |
| 123 | const generateMac = yield* PaymentCryptography.GenerateMac(macKey); |
| 124 | const verifyMac = yield* PaymentCryptography.VerifyMac(macKey); |
| 125 | const reEncryptData = yield* PaymentCryptography.ReEncryptData(bdk, dataKey); |
| 126 | const generateCardValidationData = yield* PaymentCryptography.GenerateCardValidationData(cvk); |
| 127 | const verifyCardValidationData = yield* PaymentCryptography.VerifyCardValidationData(cvk); |
| 128 | const generatePinData = yield* PaymentCryptography.GeneratePinData(pvk, pek); |
| 129 | const verifyPinData = yield* PaymentCryptography.VerifyPinData(pvk, pek); |
| 130 | const translatePinData = yield* PaymentCryptography.TranslatePinData(pek, pek2); |
| 131 | const getPublicKeyCertificate = yield* PaymentCryptography.GetPublicKeyCertificate(signKey); |
| 132 | |
| 133 | return { |
| 134 | fetch: Effect.gen(function* () { |
| 135 | const request = yield* HttpServerRequest; |
| 136 | const pathname = new URL(request.originalUrl).pathname; |
| 137 | |
| 138 | if (request.method === "GET" && pathname === "/health") { |
| 139 | return yield* HttpServerResponse.json({ ok: true }); |
| 140 | } |
| 141 | |
| 142 | if (request.method === "POST" && pathname === "/encrypt-decrypt") { |
| 143 | const body = (yield* request.json) as unknown as { |
| 144 | plainTextHex: string; |
| 145 | }; |
| 146 | const encrypted = yield* encryptData({ |
| 147 | PlainText: body.plainTextHex, |
| 148 | EncryptionAttributes: { |
| 149 | Symmetric: { Mode: "CBC", InitializationVector: IV }, |
| 150 | }, |
| 151 | }); |
| 152 | const cipherText = unwrap(encrypted.CipherText); |
| 153 | const decrypted = yield* decryptData({ |
| 154 | CipherText: cipherText, |
| 155 | DecryptionAttributes: { |
| 156 | Symmetric: { Mode: "CBC", InitializationVector: IV }, |
| 157 | }, |
| 158 | }); |
| 159 | return yield* HttpServerResponse.json({ |
| 160 | keyArn: encrypted.KeyArn, |
| 161 | cipherText, |
| 162 | plainText: unwrap(decrypted.PlainText), |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | if (request.method === "POST" && pathname === "/re-encrypt") { |
| 167 | const body = (yield* request.json) as unknown as { |
| 168 | plainTextHex: string; |
| 169 | }; |
| 170 | // Simulate a DUKPT terminal: encrypt under a key derived from the |
| 171 | // BDK at a fixed Key Serial Number. |
| 172 | const KSN = "FFFF9876543210E00001"; |
| 173 | const encrypted = yield* encryptDukpt({ |
| 174 | PlainText: body.plainTextHex, |
| 175 | EncryptionAttributes: { |
| 176 | Dukpt: { KeySerialNumber: KSN, Mode: "CBC" }, |
| 177 | }, |
| 178 | }); |
| 179 | // Translate the DUKPT ciphertext to the symmetric working key |
| 180 | // inside the service — the plaintext never leaves Payment |
| 181 | // Cryptography. |
| 182 | const reEncrypted = yield* reEncryptData({ |
| 183 | CipherText: unwrap(encrypted.CipherText), |
| 184 | IncomingEncryptionAttributes: { |
| 185 | Dukpt: { KeySerialNumber: KSN, Mode: "CBC" }, |
| 186 | }, |
| 187 | OutgoingEncryptionAttributes: { |
| 188 | Symmetric: { Mode: "CBC", InitializationVector: IV }, |
| 189 | }, |
| 190 | }); |
| 191 | const decrypted = yield* decryptData({ |
| 192 | CipherText: unwrap(reEncrypted.CipherText), |
| 193 | DecryptionAttributes: { |
| 194 | Symmetric: { Mode: "CBC", InitializationVector: IV }, |
| 195 | }, |
| 196 | }); |
| 197 | return yield* HttpServerResponse.json({ |
| 198 | outgoingKeyArn: reEncrypted.KeyArn, |
| 199 | plainText: unwrap(decrypted.PlainText), |
| 200 | }); |
| 201 | } |
| 202 | |
| 203 | if (request.method === "POST" && pathname === "/card") { |
| 204 | const body = (yield* request.json) as unknown as { |
| 205 | pan: string; |
| 206 | expiry: string; |
| 207 | }; |
| 208 | const generated = yield* generateCardValidationData({ |
| 209 | PrimaryAccountNumber: body.pan, |
| 210 | GenerationAttributes: { |
| 211 | CardVerificationValue2: { CardExpiryDate: body.expiry }, |
| 212 | }, |
| 213 | }); |
| 214 | const cvv2 = unwrap(generated.ValidationData); |
| 215 | const verified = yield* verifyCardValidationData({ |
| 216 | PrimaryAccountNumber: body.pan, |
| 217 | VerificationAttributes: { |
| 218 | CardVerificationValue2: { CardExpiryDate: body.expiry }, |
| 219 | }, |
| 220 | ValidationData: cvv2, |
| 221 | }); |
| 222 | // A wrong CVV2 must fail with the typed |
| 223 | // VerificationFailedException. |
| 224 | const tamperedCvv2 = cvv2 |
| 225 | .split("") |
| 226 | .map((d) => String((Number(d) + 1) % 10)) |
| 227 | .join(""); |
| 228 | const tampered = yield* verifyCardValidationData({ |
| 229 | PrimaryAccountNumber: body.pan, |
| 230 | VerificationAttributes: { |
| 231 | CardVerificationValue2: { CardExpiryDate: body.expiry }, |
| 232 | }, |
| 233 | ValidationData: tamperedCvv2, |
| 234 | }).pipe( |
| 235 | Effect.map(() => "verified"), |
| 236 | Effect.catchTag("VerificationFailedException", () => |
| 237 | Effect.succeed("verification-failed"), |
| 238 | ), |
| 239 | ); |
| 240 | return yield* HttpServerResponse.json({ |
| 241 | cvv2, |
| 242 | verifiedKeyArn: verified.KeyArn, |
| 243 | tampered, |
| 244 | }); |
| 245 | } |
| 246 | |
| 247 | if (request.method === "POST" && pathname === "/pin") { |
| 248 | const body = (yield* request.json) as unknown as { pan: string }; |
| 249 | // Issue a Visa PIN: the PVK generates the PVV, the PEK encrypts |
| 250 | // the PIN block. |
| 251 | const generated = yield* generatePinData({ |
| 252 | GenerationAttributes: { VisaPin: { PinVerificationKeyIndex: 1 } }, |
| 253 | PrimaryAccountNumber: body.pan, |
| 254 | PinBlockFormat: "ISO_FORMAT_0", |
| 255 | }); |
| 256 | const encryptedPinBlock = unwrap(generated.EncryptedPinBlock); |
| 257 | const pvv = |
| 258 | generated.PinData.VerificationValue === undefined |
| 259 | ? "" |
| 260 | : unwrap(generated.PinData.VerificationValue); |
| 261 | const verified = yield* verifyPinData({ |
| 262 | VerificationAttributes: { |
| 263 | VisaPin: { PinVerificationKeyIndex: 1, VerificationValue: pvv }, |
| 264 | }, |
| 265 | EncryptedPinBlock: encryptedPinBlock, |
| 266 | PrimaryAccountNumber: body.pan, |
| 267 | PinBlockFormat: "ISO_FORMAT_0", |
| 268 | }); |
| 269 | // Translate the PIN block from pek to pek2 (acquirer forwarding). |
| 270 | const translated = yield* translatePinData({ |
| 271 | IncomingTranslationAttributes: { |
| 272 | IsoFormat0: { PrimaryAccountNumber: body.pan }, |
| 273 | }, |
| 274 | OutgoingTranslationAttributes: { |
| 275 | IsoFormat0: { PrimaryAccountNumber: body.pan }, |
| 276 | }, |
| 277 | EncryptedPinBlock: encryptedPinBlock, |
| 278 | }); |
| 279 | return yield* HttpServerResponse.json({ |
| 280 | pvv, |
| 281 | verificationKeyArn: verified.VerificationKeyArn, |
| 282 | translatedKeyArn: translated.KeyArn, |
| 283 | translatedPinBlock: unwrap(translated.PinBlock), |
| 284 | }); |
| 285 | } |
| 286 | |
| 287 | if (request.method === "GET" && pathname === "/public-key-cert") { |
| 288 | const certificate = yield* getPublicKeyCertificate(); |
| 289 | return yield* HttpServerResponse.json({ |
| 290 | keyCertificate: certificate.KeyCertificate, |
| 291 | keyCertificateChain: certificate.KeyCertificateChain, |
| 292 | }); |
| 293 | } |
| 294 | |
| 295 | if (request.method === "POST" && pathname === "/mac") { |
| 296 | const body = (yield* request.json) as unknown as { |
| 297 | messageDataHex: string; |
| 298 | }; |
| 299 | // The hash function comes from the key's HMAC_SHA256 algorithm; |
| 300 | // the MAC attribute is plain "HMAC". |
| 301 | const generated = yield* generateMac({ |
| 302 | MessageData: body.messageDataHex, |
| 303 | GenerationAttributes: { Algorithm: "HMAC" }, |
| 304 | }); |
| 305 | const mac = unwrap(generated.Mac); |
| 306 | const verified = yield* verifyMac({ |
| 307 | MessageData: body.messageDataHex, |
| 308 | Mac: mac, |
| 309 | VerificationAttributes: { Algorithm: "HMAC" }, |
| 310 | }); |
| 311 | // A tampered MAC must fail with the typed |
| 312 | // VerificationFailedException. |
| 313 | const tamperedMac = (mac.startsWith("0") ? "1" : "0") + mac.slice(1); |
| 314 | const tampered = yield* verifyMac({ |
| 315 | MessageData: body.messageDataHex, |
| 316 | Mac: tamperedMac, |
| 317 | VerificationAttributes: { Algorithm: "HMAC" }, |
| 318 | }).pipe( |
| 319 | Effect.map(() => "verified"), |
| 320 | Effect.catchTag("VerificationFailedException", () => |
| 321 | Effect.succeed("verification-failed"), |
| 322 | ), |
| 323 | ); |
| 324 | return yield* HttpServerResponse.json({ |
| 325 | mac, |
| 326 | verifiedKeyArn: verified.KeyArn, |
| 327 | tampered, |
| 328 | }); |
| 329 | } |
| 330 | |
| 331 | return yield* HttpServerResponse.json({ error: "Not found" }, { status: 404 }); |
| 332 | }).pipe(Effect.orDie), |
| 333 | }; |
| 334 | }).pipe( |
| 335 | Effect.provide( |
| 336 | Layer.mergeAll( |
| 337 | PaymentCryptography.EncryptDataHttp, |
| 338 | PaymentCryptography.DecryptDataHttp, |
| 339 | PaymentCryptography.GenerateMacHttp, |
| 340 | PaymentCryptography.VerifyMacHttp, |
| 341 | PaymentCryptography.ReEncryptDataHttp, |
| 342 | PaymentCryptography.GenerateCardValidationDataHttp, |
| 343 | PaymentCryptography.VerifyCardValidationDataHttp, |
| 344 | PaymentCryptography.GeneratePinDataHttp, |
| 345 | PaymentCryptography.VerifyPinDataHttp, |
| 346 | PaymentCryptography.TranslatePinDataHttp, |
| 347 | PaymentCryptography.GetPublicKeyCertificateHttp, |
| 348 | ), |
| 349 | ), |
| 350 | ), |
| 351 | ); |