-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The implementation of basic packets and project layout
- Loading branch information
Showing
44 changed files
with
3,799 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter,.credo}.exs", "{config,lib,test}/**/*.{ex,exs}"], | ||
line_length: 120, | ||
rename_deprecated_at: "1.7.0" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# The directory Mix will write compiled artifacts to. | ||
/_build/ | ||
|
||
# If you run "mix test --cover", coverage assets end up here. | ||
/cover/ | ||
|
||
# The directory Mix downloads your dependencies sources to. | ||
/deps/ | ||
|
||
# Where third-party dependencies like ExDoc output generated docs. | ||
/doc/ | ||
|
||
# Ignore .fetch files in case you like to edit your project deps locally. | ||
/.fetch | ||
|
||
# If the VM crashes, it generates a dump, let's ignore it too. | ||
erl_crash.dump | ||
|
||
# Also ignore archive artifacts (built via "mix archive.build"). | ||
*.ez | ||
|
||
# Ignore package tarball (built via "mix hex.build"). | ||
open_pgp-*.tar | ||
|
||
# Temporary files, for example, from tests. | ||
/tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[ | ||
verbose: false, | ||
private: false, | ||
compact: true, | ||
skip: true, | ||
exit: "low", | ||
format: "txt", | ||
threshold: "low", | ||
ignore: [], | ||
ignore_files: [] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Changelog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Code of Conduct | ||
|
||
## Our Pledge | ||
|
||
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. | ||
|
||
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. | ||
|
||
## Our Standards | ||
|
||
Examples of behavior that contributes to a positive environment for our community include: | ||
|
||
- Demonstrating empathy and kindness toward other people | ||
- Being respectful of differing opinions, viewpoints, and experiences | ||
- Giving and gracefully accepting constructive feedback | ||
- Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience | ||
- Focusing on what is best not just for us as individuals, but for the overall community | ||
|
||
Examples of unacceptable behavior include: | ||
|
||
- The use of sexualized language or imagery, and sexual attention or | ||
advances of any kind | ||
- Trolling, insulting or derogatory comments, and personal or political attacks | ||
- Public or private harassment | ||
- Publishing others' private information, such as a physical or email | ||
address, without their explicit permission | ||
- Other conduct which could reasonably be considered inappropriate in a | ||
professional setting | ||
|
||
## Enforcement Responsibilities | ||
|
||
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. | ||
|
||
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. | ||
|
||
## Scope | ||
|
||
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. | ||
|
||
## Enforcement | ||
|
||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at [INSERT CONTACT METHOD]. All complaints will be reviewed and investigated promptly and fairly. | ||
|
||
All community leaders are obligated to respect the privacy and security of the reporter of any incident. | ||
|
||
## Enforcement Guidelines | ||
|
||
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: | ||
|
||
### 1. Correction | ||
|
||
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. | ||
|
||
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. | ||
|
||
### 2. Warning | ||
|
||
**Community Impact**: A violation through a single incident or series of actions. | ||
|
||
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. | ||
|
||
### 3. Temporary Ban | ||
|
||
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. | ||
|
||
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. | ||
|
||
### 4. Permanent Ban | ||
|
||
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. | ||
|
||
**Consequence**: A permanent ban from any sort of public interaction within the community. | ||
|
||
## Attribution | ||
|
||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, | ||
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. | ||
|
||
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). | ||
|
||
[homepage]: https://www.contributor-covenant.org | ||
|
||
For answers to common questions about this code of conduct, see the FAQ at | ||
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# License | ||
|
||
MIT License | ||
|
||
Copyright (c) 2024 BILL Operations, LLC. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
# OpenPGP | ||
|
||
OpenPGP lib allows to inspect, decode and decrypt OpenPGP Message Format as per [RFC4880](https://www.ietf.org/rfc/rfc4880.html) | ||
|
||
## Installation | ||
|
||
Add `:open_pgp` to the list of dependencies in `mix.exs`: | ||
|
||
```elixir | ||
def deps() do | ||
[ | ||
{:open_pgp, "~> 0.5"} | ||
] | ||
end | ||
``` | ||
|
||
## OpenPGP Packet | ||
|
||
The `OpenPGP.Packet` is a generic packet type. It has an essential purpose: split OpenPGP message in packets and decode packet tags. | ||
|
||
An OpenPGP message is constructed from a number of records that are traditionally called packets. A packet is a chunk of data that has a tag specifying its meaning. An OpenPGP message, keyring, certificate, and so forth consists of a number of packets. Some of those packets may contain other OpenPGP packets (for example, a compressed data packet, when uncompressed, contains OpenPGP packets). Each packet consists of a packet header, followed by the packet body. For more details refer to [Packet Syntax chapter in RFC4880](https://www.ietf.org/rfc/rfc4880.html#section-4) | ||
|
||
Once OpenPGP message split into generic packets, the higher order tag-specific packet decoders can be applied on its' data. Example: | ||
|
||
``` | ||
{packet, _rest} = OpenPGP.Packet.decode("...") | ||
{compressed_data_packet, <<>>} = | ||
packet |> OpenPGP.Util.concat_body() |> OpenPGP.CompressedDataPacket.decode() | ||
``` | ||
|
||
More details can be found in `OpenPGP.Packet` and `OpenPGP.Packet.Behaviour` | ||
|
||
## Examples | ||
|
||
### List and cast packets | ||
|
||
List packets in a message and then cast to specific packet types. | ||
|
||
``` | ||
iex> message = <<160, 24, 2, 120, 156, 243, 72, 205, 201, 201, 215, 81, 8, 207, 47, 202, 73, 81, 84, 84, 4, 0, 40, 213, 4, 172>> | ||
...> | ||
iex> packets = OpenPGP.list_packets(message) | ||
[ | ||
%OpenPGP.Packet{ | ||
body: [ | ||
%OpenPGP.Packet.BodyChunk{ | ||
chunk_length: {:fixed, 24}, | ||
data: <<2, 120, 156, 243, 72, 205, 201, 201, 215, 81, 8, 207, 47, 202, 73, 81, 84, 84, 4, 0, 40, 213, 4, 172>>, | ||
header_length: 1 | ||
} | ||
], | ||
tag: %OpenPGP.Packet.PacketTag{ | ||
format: :old, | ||
length_type: {0, "one-octet"}, | ||
tag: {8, "Compressed Data Packet"} | ||
} | ||
} | ||
] | ||
iex> OpenPGP.cast_packets(packets) | ||
[ | ||
%OpenPGP.CompressedDataPacket{ | ||
algo: {2, "ZLIB [RFC1950]"}, | ||
data_deflated: <<120, 156, 243, 72, 205, 201, 201, 215, 81, 8, 207, 47, 202, 73, 81, 84, 84, 4, 0, 40, 213, 4, 172>>, | ||
data_inflated: "Hello, World!!!" | ||
} | ||
] | ||
``` | ||
|
||
### Decode Generic OpenPGP packet | ||
|
||
In this example the packet tag specifies a Signature Packet with body length of 7 bytes. The remaining binary will be return as a second element in a two element tuple. More details in `OpenPGP.Packet.Behaviour`. | ||
|
||
``` | ||
iex> alias OpenPGP.Packet | ||
iex> alias OpenPGP.Packet.PacketTag | ||
iex> alias OpenPGP.Packet.BodyChunk | ||
iex> Packet.decode(<<1::1, 0::1, 2::4, 0::2, 7::8, "Hello, World!!!">>) | ||
{ | ||
%Packet{ | ||
tag: %PacketTag{format: :old, length_type: {0, "one-octet"}, tag: {2, "Signature Packet"}}, | ||
body: [%BodyChunk{chunk_length: {:fixed, 7}, data: "Hello, ", header_length: 1}] | ||
}, | ||
"World!!!" | ||
} | ||
``` | ||
|
||
### CompressedDataPacket | ||
|
||
The `OpenPGP.CompressedDataPacket` will inflate data implicitly when decoded (also, data inflated implicitly when `OpenPGP.cast_packets/1` used). | ||
|
||
``` | ||
iex> alias OpenPGP.CompressedDataPacket | ||
iex> deflated = <<120, 156, 243, 72, 205, 201, 201, 215, 81, 8, 207, 47, 202, 73, 81, 84, 84, 4, 0, 40, 213, 4, 172>> | ||
iex> CompressedDataPacket.decode(<<2, deflated::binary>>) | ||
{ | ||
%CompressedDataPacket{ | ||
algo: {2, "ZLIB [RFC1950]"}, | ||
data_deflated: deflated, | ||
data_inflated: "Hello, World!!!"}, | ||
<<>> | ||
} | ||
``` | ||
|
||
### IntegrityProtectedDataPacket | ||
|
||
The `OpenPGP.IntegrityProtectedDataPacket` does not decrypt its' data implicitly. The `OpenPGP.IntegrityProtectedDataPacket.decrypt/2` should be used to get plaintext. Please note that some packets have packet speicifc functions, such as `OpenPGP.IntegrityProtectedDataPacket.decrypt/2`. | ||
|
||
``` | ||
iex> alias OpenPGP.IntegrityProtectedDataPacket | ||
iex> alias OpenPGP.PublicKeyEncryptedSessionKeyPacket | ||
...> | ||
iex> key = <<38, 165, 130, 172, 168, 51, 184, 238, 96, 204, 88, | ||
...> 134, 93, 25, 162, 22, 83, 211, 140, 176, 115, 113, 37, 201, | ||
...> 171, 249, 115, 64, 94, 59, 35, 60>> | ||
...> | ||
iex> iv = <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>> | ||
iex> <<prefix::14*8, chsum::2*8>> = :crypto.strong_rand_bytes(16) | ||
iex> plaintext = <<prefix::14*8, chsum::2*8, chsum::2*8, "Hello">> | ||
...> | ||
iex> ciphertext = | ||
...> :crypto.crypto_one_time( | ||
...> :aes_256_cfb128, | ||
...> key, | ||
...> iv, | ||
...> plaintext, | ||
...> true) | ||
...> | ||
iex> payload = <<1::8, ciphertext::binary>> | ||
iex> {packet_decoded, <<>>} = IntegrityProtectedDataPacket.decode(payload) | ||
{ | ||
%IntegrityProtectedDataPacket{ | ||
ciphertext: ciphertext, | ||
plaintext: nil, | ||
version: 1 | ||
}, | ||
<<>> | ||
} | ||
iex> pkesk = %PublicKeyEncryptedSessionKeyPacket{ | ||
...> version: 3, | ||
...> session_key_algo: {9, "AES with 256-bit key"}, | ||
...> session_key_material: {key} | ||
...> } | ||
...> | ||
iex> IntegrityProtectedDataPacket.decrypt(packet_decoded, pkesk) | ||
%IntegrityProtectedDataPacket{ | ||
version: 1, | ||
plaintext: "Hello", | ||
ciphertext: ciphertext | ||
} | ||
``` | ||
|
||
## Notes | ||
|
||
As of **v0.5.x**: | ||
|
||
1. Any valid OpenPGP message can be decoded via generic `OpenPGP.Packet` decoder. This abstraction layer provide Packet Tags and Body Chunks for packet envelope level evaluation. | ||
1. Some Packet Tag specific decoders implemented with limited feature support: | ||
1. `OpenPGP.LiteralDataPacket` | ||
1. `OpenPGP.PublicKeyEncryptedSessionKeyPacket` | ||
1. `OpenPGP.PublicKeyPacket` - support only V4 packets | ||
1. `OpenPGP.SecretKeyPacket` - support only V4 packets; Iterated and Salted String-to-Key (S2K) specifier (ID: 3); S2K usage convention octet of 254 only; S2K hashing algo SHA1; AES128 symmetric encryption of secret key material | ||
1. `OpenPGP.CompressedDataPacket` - support only ZLIB- and ZIP-style blocks | ||
1. `OpenPGP.IntegrityProtectedDataPacket` - support Session Key algo 9 (AES with 256-bit key) in CFB mode; Modification Detection Code system is not supported | ||
|
||
At a high level `OpenPGP.list_packets/1` and `OpenPGP.cast_packets/1` serve as an entrypoint to OpenPGP Message decoding and extracting generic data. | ||
|
||
Packet specific decoders implement `OpenPGP.Packet.Behaviour`, which exposes `.decode/1` interface (including genric `OpenPGP.Packet`). Additionaly some of the packet specific decoders may provide interface for further packet processing, such as `OpenPGP.SecretKeyPacket.decrypt/2`. | ||
|
||
Usage example of a comon use case can be found in `test/open_pgp/open_pgp_test.exs` in the test **"full integration: load private key and decrypt encrypted file"** | ||
|
||
## Refs, Snippets, Misc | ||
|
||
```console | ||
# GPG commands | ||
~$ gpg --list-keys | ||
~$ gpg --list-secret-keys | ||
~$ gpg --export-secret-key --armor john.doe@example.com > ./private.pgp | ||
~$ gpg --list-packets --verbose example.txt.pgp | ||
~$ gpg --encrypt --recipient F89B64F782254B03624FCF5C052E8381B5C335DA /usr/share/dict/words | ||
~$ gpg --batch --passphrase "passphrase" --quick-generate-key "John Doe (RSA2048) <john.doe@example.com>" rsa2048 default never | ||
~$ gpg --edit-key F89B64F782254B03624FCF5C052E8381B5C335DA | ||
|
||
# Handy tools | ||
~$ hexdump -vx ./words.pgp | ||
~$ xxd -b ./words.pgp | ||
~$ xxd -g 1 | ||
``` |
Oops, something went wrong.