-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for PGP Encryption/Decryption #555
Changes from 9 commits
1897673
07115d4
fd56930
9110389
04f59c4
a3bfb6d
6fc145a
d89771b
dc1d31e
247f52a
78e3fb4
557f72b
f24bd9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,74 @@ | ||||||
// Copyright (c) 2024 WSO2 LLC. (https://www.wso2.com). | ||||||
// | ||||||
// WSO2 LLC. licenses this file to you under the Apache License, | ||||||
// Version 2.0 (the "License"); you may not use this file except | ||||||
// in compliance with the License. | ||||||
// You may obtain a copy of the License at | ||||||
// | ||||||
// http://www.apache.org/licenses/LICENSE-2.0 | ||||||
// | ||||||
// Unless required by applicable law or agreed to in writing, | ||||||
// software distributed under the License is distributed on an | ||||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||
// KIND, either express or implied. See the License for the | ||||||
// specific language governing permissions and limitations | ||||||
// under the License. | ||||||
|
||||||
# Represents the PGP encryption options | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# | ||||||
# + compressionAlgorithm - Specifies the compression algorithm used for PGP encryption | ||||||
# + symmetricKeyAlgorithm - Specifies the symmetric key algorithm used for encryption | ||||||
# + armor - Indicates whether ASCII armor is enabled for the encrypted output | ||||||
# + withIntegrityCheck - Indicates whether integrity check is included in the encryption | ||||||
public type PgpEncryptionOptions record {| | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add API doc comments to all public types There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we just rename this to public isolated function encryptPgp(byte[] plainText, byte[] publicKey, *Options options)
returns byte[]|Error = @java:Method { IMO, the words WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we can reduce the redundancy by renaming it to |
||||||
CompressionAlgorithmTags compressionAlgorithm = ZIP; | ||||||
SymmetricKeyAlgorithmTags symmetricKeyAlgorithm = AES_256; | ||||||
boolean armor = true; | ||||||
boolean withIntegrityCheck = true; | ||||||
|}; | ||||||
|
||||||
# Represents the compressions algorithms available in PGP | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# | ||||||
# + UNCOMPRESSED - No compression | ||||||
# + ZIP - Uses (RFC 1951) compression | ||||||
# + ZLIB - Uses (RFC 1950) compression | ||||||
# + BZIP2 - Uses Burrows–Wheeler algorithm | ||||||
public enum CompressionAlgorithmTags { | ||||||
UNCOMPRESSED = "0", | ||||||
ZIP = "1", | ||||||
ZLIB = "2", | ||||||
BZIP2= "3" | ||||||
} | ||||||
|
||||||
# Represent the symmetric key algorithms available in PGP | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# | ||||||
# + NULL - No encryption | ||||||
# + IDEA - IDEA symmetric key algorithm | ||||||
# + TRIPLE_DES - Triple DES symmetric key algorithm | ||||||
# + CAST5 - CAST5 symmetric key algorithm | ||||||
# + BLOWFISH - Blowfish symmetric key algorithm | ||||||
# + SAFER - SAFER symmetric key algorithm | ||||||
# + DES - DES symmetric key algorithm | ||||||
# + AES_128 - AES 128-bit symmetric key algorithm | ||||||
# + AES_192 - AES 192-bit symmetric key algorithm | ||||||
# + AES_256 - AES 256-bit symmetric key algorithm | ||||||
# + TWOFISH - Twofish symmetric key algorithm | ||||||
# + CAMELLIA_128 - Camellia 128-bit symmetric key algorithm | ||||||
# + CAMELLIA_192 - Camellia 192-bit symmetric key algorithm | ||||||
# + CAMMELIA_256 - Camellia 256-bit symmetric key algorithm | ||||||
public enum SymmetricKeyAlgorithmTags { | ||||||
NULL = "0", | ||||||
IDEA = "1", | ||||||
TRIPLE_DES = "2", | ||||||
CAST5 = "3", | ||||||
BLOWFISH = "4", | ||||||
SAFER = "5", | ||||||
DES = "6", | ||||||
AES_128 = "7", | ||||||
AES_192 = "8", | ||||||
AES_256 = "9", | ||||||
TWOFISH = "10", | ||||||
CAMELLIA_128 = "11", | ||||||
CAMELLIA_192 = "12", | ||||||
CAMELLIA_256 = "13" | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2024 WSO2 LLC. (https://www.wso2.com). | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/test; | ||
import ballerina/io; | ||
|
||
@test:Config {} | ||
isolated function testEncryptAndDecryptWithPgp() returns error? { | ||
byte[] message = "Ballerina crypto test ".toBytes(); | ||
byte[] passphrase = check io:fileReadBytes(PGP_PRIVATE_KEY_PASSPHRASE_PATH); | ||
byte[] publicKey = check io:fileReadBytes(PGP_PUBLIC_KEY_PATH); | ||
byte[] privateKey = check io:fileReadBytes(PGP_PRIVATE_KEY_PATH); | ||
byte[] cipherText = check encryptPgp(message, publicKey); | ||
byte[] plainText = check decryptPgp(cipherText, privateKey, passphrase); | ||
test:assertEquals(plainText.toBase16(), message.toBase16()); | ||
} | ||
|
||
@test:Config {} | ||
isolated function testEncryptAndDecryptWithPgpWithOptions() returns error? { | ||
byte[] message = "Ballerina crypto test ".toBytes(); | ||
byte[] passphrase = check io:fileReadBytes(PGP_PRIVATE_KEY_PASSPHRASE_PATH); | ||
byte[] publicKey = check io:fileReadBytes(PGP_PUBLIC_KEY_PATH); | ||
byte[] privateKey = check io:fileReadBytes(PGP_PRIVATE_KEY_PATH); | ||
byte[] cipherText = check encryptPgp(message, publicKey, symmetricKeyAlgorithm = AES_128, armor = false); | ||
byte[] plainText = check decryptPgp(cipherText, privateKey, passphrase); | ||
test:assertEquals(plainText.toBase16(), message.toBase16()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we also add a negative test case by creating another PGP key pair, like encrypting with a public key and trying to decrypt using a wrong private key? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added two more test cases for following in here 78e3fb4
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
qCr3bv@5mj5n4eY |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
-----BEGIN PGP PRIVATE KEY BLOCK----- | ||
Version: Keybase OpenPGP v2.0.76 | ||
Comment: https://keybase.io/crypto | ||
|
||
xcMFBGY7OqUBCADQO4C2dbXISbB9EZKX8hEysDKk89o+5C2SvCUBChBOwV036dDQ | ||
R+4gCf2UE+/uER702r2QzDtpeCr/7uraNFf7x05Teng0ACk5aBTMunYF4KnmuicP | ||
G/qQW0vW04Tv7l/qAGjGAaf8wZkFlQRIb0zMASyxLnxYSuRn09CeNLy+3fNBQxc0 | ||
cYUByZKb3IG0tMBUkr63RvCWwRGesQD3aqKrsvmKkTVWnosHt1Il6zFMGrY0M6Pr | ||
S/lps9T677nu3C9ySiviem/sC4UORllfCtBO22eWKlC9KicbhR084vmrtT/s2cPd | ||
vBIOxdm1Dczi2VrcIOsWOZiXfoIwHYsGL/+xABEBAAH+CQMIsIoEwtjUyBJge7cP | ||
Mg/okGCw9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hJtl5sJ04EnM9yPOruREmTlcxeJvyJ5lnlDRGK8 | ||
jN/C3R/zSpAjSpA/vY4EeUOQuMni5PG5SxfFZupQSus3r0qZx5BJdnyY276yDKSi | ||
s/CP5qP5zE8YmJjLMdkwAfJuFi5EkigjOnOgGYymivYiURbpCxmn83rXBjoH/RPw | ||
0Q5Wj0+5+Obc7yw8uUSor1m11kyI8/sv7G8dSDQLMpE5V7VKaj4OQ1fHMzpBPXP4 | ||
H8WqQx0XPUEScOJ8zDIS2p1VRMnMr2EerEw/QV/A3B5AwMDZkhd3tK0liP95xZ9v | ||
y+pWPGAFIQrDYcmRMRBL2WB+jxm49VOgcesKl1OP3NO2DpHu/p4RS6N6+2w6H/De | ||
x39rIv+7B6mdK54nHdomM0AdDc8qkks42Qr3kXbpLtnefZirWPs2QcNr9kXPtpfp | ||
IyMpGoeudZWlaBMLcGlkEEO0MJKyTe2iKhIl+UAQwBoJEMbPc8L3m6oNtQotRzJv | ||
4WbzDTObZg6ttIYG6+s= | ||
=4NKk | ||
-----END PGP PRIVATE KEY BLOCK----- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's take in the
publicKey
andprivateKey
as file paths for the moment.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same should applied for the params
privateKey
andpassphrase
indecryptPgp
as well right? Should we keep the return type tobyte []
as it is for those two methodsencryptPgp
anddecryptPgp
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's have
privateKey
as a string file path indecryptPgp
function as well. The passphrase should be a byte[] and what we return from the APIs also should be byte[] s.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have addressed this in 78e3fb4