├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README-CloudHSM.md ├── README-KMS.md ├── README.md ├── images ├── cloudhsm-id.jpg ├── hsm.jpg ├── kms.jpg ├── proxy-cloudhsm-arch.png ├── proxy-cloudhsm.png ├── proxy-kms-arch.png └── proxy-kms-arch1.png └── proxy ├── cloudhsm ├── cavium │ └── pom.xml ├── pom.xml └── proxy │ ├── pom.xml │ └── src │ └── main │ ├── docker │ ├── Dockerfile │ └── wrapper_script.sh │ ├── java │ └── com │ │ └── amazon │ │ └── aws │ │ └── pix │ │ └── cloudhsm │ │ └── proxy │ │ ├── PixCloudHSMProxyRouteBuilder.java │ │ ├── camel │ │ └── netty │ │ │ ├── NettyHttpClientInitializerFactory.java │ │ │ └── NettySSLContextParameters.java │ │ └── processor │ │ ├── CaptureRequestProcessor.java │ │ ├── LogRequestResponseProcessor.java │ │ ├── SignRequestProcessor.java │ │ └── VerifyResponseProcessor.java │ └── resources │ └── application.properties ├── core ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── amazon │ │ └── aws │ │ └── pix │ │ └── core │ │ ├── audit │ │ └── AuditLog.java │ │ ├── util │ │ ├── KeyStoreUtil.java │ │ └── PixConstants.java │ │ └── xml │ │ ├── Iso20022URIDereferencer.java │ │ ├── Iso20022XmlSigner.java │ │ ├── X509IssuerSerialKeySelector.java │ │ └── XmlSigner.java │ └── test │ ├── java │ └── com │ │ └── amazon │ │ └── aws │ │ └── pix │ │ └── core │ │ └── test │ │ └── xml │ │ ├── Iso20022XmlSignerTest.java │ │ └── XmlSignerTest.java │ └── resources │ ├── security │ └── client.jks │ └── xml │ ├── pacs.008_CONTA_1_msg.xml │ └── test.xml ├── kms ├── pom.xml └── src │ ├── assembly │ └── zip.xml │ └── main │ ├── java │ └── com │ │ └── amazon │ │ └── aws │ │ └── pix │ │ └── kms │ │ └── proxy │ │ ├── config │ │ └── Config.java │ │ ├── service │ │ ├── Logger.java │ │ ├── Sender.java │ │ └── Signer.java │ │ └── sync │ │ └── ProxyHandler.java │ └── resources │ ├── application.properties │ ├── reflection-config.json │ └── resources-config.json ├── pom.xml └── test ├── pom.xml └── src └── main ├── docker ├── Dockerfile └── ssl │ ├── dict-response.xml │ ├── mtls.cer │ ├── mtls.jks │ ├── mtls.key │ ├── mtls.p12 │ ├── sig.cer │ ├── sig.jks │ ├── sig.key │ └── sig.p12 ├── java └── com │ └── amazon │ └── aws │ └── pix │ └── proxy │ └── test │ └── PixProxyTestRouteBuilder.java └── resources └── application.properties /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/ 2 | *.iml 3 | **/target/ 4 | /lsp/ 5 | *.DS_Store -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 3 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 4 | opensource-codeofconduct@amazon.com with any additional questions or comments. 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *master* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 49 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 50 | opensource-codeofconduct@amazon.com with any additional questions or comments. 51 | 52 | 53 | ## Security issue notifications 54 | If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. 55 | 56 | 57 | ## Licensing 58 | 59 | See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 60 | 61 | We may ask you to sign a [Contributor License Agreement (CLA)](http://en.wikipedia.org/wiki/Contributor_License_Agreement) for larger changes. 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 10 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 11 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 12 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 13 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 14 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | 16 | -------------------------------------------------------------------------------- /README-CloudHSM.md: -------------------------------------------------------------------------------- 1 | # AWS CloudHSM architecture to exemplify digital signature and secure message transmission to the Brazilian Instant Payment System 2 | 3 |

4 | 5 |

6 | 7 | This project contains source code and supporting files that includes the following folders: 8 | 9 | - `proxy/cloudhsm` - Proxy that uses AWS CloudHSM. 10 | - `proxy/core` - Sign XML messages. 11 | - `proxy/test` - BACEN simulator. 12 | 13 | The main code of application uses several AWS resources, including AWS CLoudHSM and an AWS Fargate. The audit part of solution use other AWS resources, including [Amazon Kinesis Firehose](https://aws.amazon.com/kinesis/data-firehose/?nc1=h_ls), [Amazon Athena](https://aws.amazon.com/athena/?nc1=h_ls&whats-new-cards.sort-by=item.additionalFields.postDateTime&whats-new-cards.sort-order=desc), [Amazon S3](https://aws.amazon.com/s3/?nc1=h_ls) and [AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/components-overview.html). 14 | 15 | 16 | ## Following is the proposed architecture 17 | 18 | The architecture presented here can be part of a more complete, [event-based solution](https://aws.amazon.com/en/event-driven-architecture/), which can cover the entire payment message transmission flow, from the banking core. For example, the complete solution of the Financial Institution (paying or receiving), could contain other complementary architectures such as **Authorization**, **Undo** (based on the [SAGA model](https://docs.aws.amazon.com/whitepapers/latest/microservices-on-aws/distributed-data-management.html)), **Effectiveness**, **Communication with on-premises** environment ([hybrid environment](https://aws.amazon.com/en/hybrid/)), etc., using other services such as [Amazon EventBridge](https://aws.amazon.com/en/eventbridge/), Amazon Simple Notification Service ([SNS](https://aws.amazon.com/en/sns/?whats-new-cards.sort-by=item.additionalFields.postDateTime&whats-new-cards.sort-order=desc)), Amazon Simple Queue Service ([SQS](https://aws.amazon.com/en/sqs/)), [AWS Step Functions](https://aws.amazon.com/en/step-functions/), [Amazon ElastiCache](https://aws.amazon.com/en/elasticache/), [Amazon DynamoDB](https://aws.amazon.com/en/dynamodb/). 19 | 20 |

21 | 22 |

23 | 24 | 25 | 1. Store login and password in [AWS Secrets Manager](https://aws.amazon.com/en/secrets-manager/), to communicate with AWS CloudHSM. 26 | 2. Store or import the private key on [AWS CloudHSM](https://aws.amazon.com/cloudhsm/?nc1=h_ls). 27 | 3. Store the three certificates in the [AWS Systems Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html): generated key certificate for signature, certificate generated for mTLS, CloudHSM certificate (customer CA). 28 | 4. Service/Application sends transaction request in XML format. 29 | 5. [ELB](https://aws.amazon.com/en/elasticloadbalancing/) balances requests among [AWS Fargate containers](https://aws.amazon.com/en/fargate/). 30 | 6. Application (AWS Fargate) uses AWS CloudHSM for digital signature of XML. 31 | 7. Application (AWS Fargate) uses AWS CloudHSM to establish mTLS and transmit XML to [BACEN](https://www.bcb.gov.br/en/financialstability/instantpayments). 32 | 8. Application (AWS Fargate) receives the response from BACEN and, if necessary, validates the digital signature of the received XML. 33 | 9. Application (AWS Fargate) logs the request log by sending it directly to [Amazon Kinesis Data Firehose](https://aws.amazon.com/en/kinesis/data-firehose/). 34 | 10. The reply message is sent to the ELB. 35 | 11. The reply message is received by the Service/Application. 36 | 12. Amazon Kinesis Data Firehose uses the [AWS Glue Data Catalog](https://aws.amazon.com/en/glue/?whats-new-cards.sort-by=item.additionalFields.postDateTime&whats-new-cards.sort-order=desc) to convert the logs to parquet format. 37 | 13. Amazon Kinesis Data Firehose sends the logs to [Amazon S3](https://aws.amazon.com/en/s3/), already partitioned into “folders” (/year/month/day/hour/). 38 | 14. [Amazon Athena](https://docs.aws.amazon.com/athena/latest/ug/glue-athena.html) uses the AWS Glue Data Catalog as a central place to store and retrieve table metadata. 39 | 15. [AWS Glue crawlers](https://docs.aws.amazon.com/glue/latest/dg/add-crawler.html) automatically update the metadata repository every hour. 40 | 16. You can immediately query the data directly on Amazon S3 using serverless analytics services, such as [Amazon Athena](https://aws.amazon.com/en/athena/?whats-new-cards.sort-by=item.additionalFields.postDateTime&whats-new-cards.sort-order=desc) (ad hoc with standard SQL) and optionally the [Amazon QuickSight](https://aws.amazon.com/en/quicksight/). 41 | 42 | 43 | ## How to deploy? 44 | 45 | ### AWS CloudHSM 46 | 47 | Here are the resources you’ll need in order to follow along with both architectures: 48 | 49 | - An Amazon Virtual Private Cloud (Amazon VPC) with the following components: 50 | 51 | Private subnet in Availability Zone to be used for the HSM’s elastic network interface (ENI). 52 | A public subnet that contains a network address translation (NAT) gateway. 53 | A private subnet with a route table that routes internet traffic (0.0.0.0/0) to the NAT gateway. You’ll use this subnet to run the AWS Fargate application. The NAT gateway allows you to connect to the AWS CloudHSM, [AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/setup-create-vpc.html), and [AWS Secrets Manager endpoints](https://docs.aws.amazon.com/secretsmanager/latest/userguide/vpc-endpoint-overview.html#vpc-endpoint). 54 | 55 | **Note**: For high availability, you can add multiple instances of the public and private subnets. For more information about how to create an Amazon VPC with public and private subnets as well as a NAT gateway, refer to the [Amazon VPC user guide](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Scenarios.html). 56 | 57 | - An **active AWS CloudHSM cluster** with at least one active HSM. The HSMs should be created in the private subnets. You can follow the Getting Started with [AWS CloudHSM guide](https://docs.aws.amazon.com/cloudhsm/latest/userguide/create-cluster.html) to create and initialize the CloudHSM cluster. 58 | 59 | - The **AWS CloudHSM client** installed and configured to connect to the CloudHSM cluster. Optionally, you can use an Amazon Linux 2 EC2 instance with the CloudHSM client installed and configured. The client instance should be launched in the public subnet. You can again refer to [Getting Started With AWS CloudHSM](https://docs.aws.amazon.com/cloudhsm/latest/userguide/getting-started.html) to configure and connect the client instance. Also, install the [AWS CloudHSM Dynamic Engine for OpenSSL](https://docs.aws.amazon.com/cloudhsm/latest/userguide/openssl-library-install.html). 60 | 61 | - The **CO** and **CU credentials** created: CO (crypto officer) and CU (crypto user) by following the steps in the [user guide](https://docs.aws.amazon.com/cloudhsm/latest/userguide/manage-hsm-users.html#create-user). 62 | 63 | #### Generate keys and certificate to digital signature 64 | 65 | You can generate or import a [private key using Open SSL](https://docs.aws.amazon.com/cloudhsm/latest/userguide/ssl-offload-import-or-generate-private-key-and-certificate.html). 66 | We recommend that private key should be non-extractable. 67 | 68 | - Generating a [NON-EXTRACTABLE](https://docs.aws.amazon.com/cloudhsm/latest/userguide/key_mgmt_util-genRSAKeyPair.html) private key: 69 | 70 | Launch the key management util: 71 | ``` 72 | $ /opt/cloudhsm/bin/key_mgmt_util 73 | ``` 74 | 75 | Login: 76 | ``` 77 | Command: loginHSM -u CU -s -p 78 | ``` 79 | 80 | Generate the key pair: 81 | ``` 82 | Command: genRSAKeyPair -m 2048 -e 65541 -l