diff --git a/armor/armor.go b/armor/armor.go index f337b68..73ace71 100644 --- a/armor/armor.go +++ b/armor/armor.go @@ -2,13 +2,13 @@ package armor import ( "bytes" - "io/ioutil" - "golang.org/x/crypto/openpgp/armor" - "proton/pmcrypto/internal" - "golang.org/x/crypto/openpgp/clearsign" "errors" + "golang.org/x/crypto/openpgp/armor" + "golang.org/x/crypto/openpgp/clearsign" "golang.org/x/crypto/openpgp/packet" "io" + "io/ioutil" + "proton/pmcrypto/internal" "proton/pmcrypto/models" ) @@ -32,9 +32,9 @@ func ArmorWithType(input []byte, armorType string) (string, error) { return b.String(), nil } -// UnArmor an armored key to bytes key -func UnArmor(input string) ([]byte, error) { - b, err := internal.UnArmor(input) +// Unarmor an armored key to bytes key +func Unarmor(input string) ([]byte, error) { + b, err := internal.Unarmor(input) if err != nil { return nil, err } @@ -50,14 +50,12 @@ func ReadClearSignedMessage(signedMessage string) (string, error) { return string(modulusBlock.Bytes), nil } - - //SeparateKeyAndData ... func SplitArmor(encrypted string) (*models.EncryptedSplit, error) { var err error - encryptedRaw, err := UnArmor(encrypted) + encryptedRaw, err := Unarmor(encrypted) if err != nil { return nil, err } @@ -112,8 +110,6 @@ func SplitArmor(encrypted string) (*models.EncryptedSplit, error) { return outSplit, err } - - //encode length based on 4.2.2. in the RFC func encodedLength(length int) (b []byte) { if length < 192 { diff --git a/crypto/attachment.go b/crypto/attachment.go index 134a6c0..f5c8019 100644 --- a/crypto/attachment.go +++ b/crypto/attachment.go @@ -6,10 +6,10 @@ import ( "io/ioutil" "golang.org/x/crypto/openpgp" - armorUtils "proton/pmcrypto/armor" - "golang.org/x/crypto/openpgp/packet" - "proton/pmcrypto/internal" "golang.org/x/crypto/openpgp/armor" + "golang.org/x/crypto/openpgp/packet" + armorUtils "proton/pmcrypto/armor" + "proton/pmcrypto/internal" "proton/pmcrypto/models" ) @@ -52,7 +52,7 @@ func (pm *PmCrypto) EncryptAttachmentBinKey(plainData []byte, fileName string, p //EncryptAttachment ... func (pm *PmCrypto) EncryptAttachment(plainData []byte, fileName string, publicKey string) (*models.EncryptedSplit, error) { - rawPubKey, err := armorUtils.UnArmor(publicKey) + rawPubKey, err := armorUtils.Unarmor(publicKey) if err != nil { return nil, err } @@ -89,7 +89,7 @@ func (pm *PmCrypto) DecryptAttachmentBinKey(keyPacket []byte, dataPacket []byte, encryptedReader := io.MultiReader(keyReader, dataReader) - config := &packet.Config{ Time: pm.getTimeGenerator() } + config := &packet.Config{Time: pm.getTimeGenerator()} md, err := openpgp.ReadMessage(encryptedReader, privKeyEntries, nil, config) if err != nil { @@ -107,7 +107,7 @@ func (pm *PmCrypto) DecryptAttachmentBinKey(keyPacket []byte, dataPacket []byte, //DecryptAttachment ... func (pm *PmCrypto) DecryptAttachment(keyPacket []byte, dataPacket []byte, privateKey string, passphrase string) ([]byte, error) { - rawPrivKey, err := armorUtils.UnArmor(privateKey) + rawPrivKey, err := armorUtils.Unarmor(privateKey) if err != nil { return nil, err } @@ -123,7 +123,7 @@ func (pm *PmCrypto) EncryptAttachmentWithPassword(plainData []byte, password str return "", err } - config := &packet.Config{ Time: pm.getTimeGenerator() } + config := &packet.Config{Time: pm.getTimeGenerator()} plaintext, err := openpgp.SymmetricallyEncrypt(w, []byte(password), nil, config) if err != nil { @@ -154,7 +154,7 @@ func (pm *PmCrypto) DecryptAttachmentWithPassword(keyPacket []byte, dataPacket [ return []byte(password), nil } - config := &packet.Config{ Time: pm.getTimeGenerator() } + config := &packet.Config{Time: pm.getTimeGenerator()} md, err := openpgp.ReadMessage(encryptedReader, nil, prompt, config) if err != nil { diff --git a/crypto/key.go b/crypto/key.go index a52d67e..a418ec2 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -5,7 +5,7 @@ import ( "crypto" "encoding/hex" "errors" - "strings" + "strings" "time" "golang.org/x/crypto/openpgp" @@ -21,7 +21,6 @@ const ( failed = 3 ) - //IsKeyExpiredBin ... func (pm *PmCrypto) IsKeyExpiredBin(publicKey []byte) (bool, error) { now := pm.getNow() @@ -77,7 +76,7 @@ func (pm *PmCrypto) IsKeyExpiredBin(publicKey []byte) (bool, error) { //IsKeyExpired .... // will user the cached time to check func (pm *PmCrypto) IsKeyExpired(publicKey string) (bool, error) { - rawPubKey, err := armor.UnArmor(publicKey) + rawPubKey, err := armor.Unarmor(publicKey) if err != nil { return false, err } @@ -133,7 +132,6 @@ func (pm *PmCrypto) generateKey(userName string, domain string, passphrase strin return "", err } - rawPwd := []byte(passphrase) if newEntity.PrivateKey != nil && !newEntity.PrivateKey.Encrypted { if err := newEntity.PrivateKey.Encrypt(rawPwd); err != nil { @@ -158,7 +156,7 @@ func (pm *PmCrypto) generateKey(userName string, domain string, passphrase strin } func (pm *PmCrypto) GenerateRSAKeyWithPrimes(userName string, domain string, passphrase string, bits int, - primeone []byte, primetwo []byte, primethree []byte, primefour []byte) (string, error) { + primeone []byte, primetwo []byte, primethree []byte, primefour []byte) (string, error) { return pm.generateKey(userName, domain, passphrase, "rsa", bits, primeone, primetwo, primethree, primefour) } @@ -171,6 +169,7 @@ func (pm *PmCrypto) GenerateRSAKeyWithPrimes(userName string, domain string, pas func (pm *PmCrypto) GenerateKey(userName string, domain string, passphrase string, keyType string, bits int) (string, error) { return pm.generateKey(userName, domain, passphrase, keyType, bits, nil, nil, nil, nil) } + // UpdatePrivateKeyPassphrase ... func (pm *PmCrypto) UpdatePrivateKeyPassphrase(privateKey string, oldPassphrase string, newPassphrase string) (string, error) { @@ -217,7 +216,7 @@ func (pm *PmCrypto) UpdatePrivateKeyPassphrase(privateKey string, oldPassphrase } // CheckKey print out the key and subkey fingerprint -func (pm *PmCrypto) CheckKey(pubKey string) (string, error) { +func (pm *PmCrypto) CheckKey(pubKey string) (string, error) { pubKeyReader := strings.NewReader(pubKey) entries, err := openpgp.ReadArmoredKeyRing(pubKeyReader) if err != nil { diff --git a/crypto/message.go b/crypto/message.go index 729ee32..e2bd6fb 100644 --- a/crypto/message.go +++ b/crypto/message.go @@ -10,8 +10,8 @@ import ( "golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp/armor" - "golang.org/x/crypto/openpgp/packet" errors2 "golang.org/x/crypto/openpgp/errors" + "golang.org/x/crypto/openpgp/packet" "math" armorUtils "proton/pmcrypto/armor" "proton/pmcrypto/internal" @@ -23,7 +23,7 @@ import ( // privateKey : armored private use to decrypt message // passphrase : match with private key to decrypt message func (pm *PmCrypto) DecryptMessage(encryptedText string, privateKey string, passphrase string) (string, error) { - privKeyRaw, err := armorUtils.UnArmor(privateKey) + privKeyRaw, err := armorUtils.Unarmor(privateKey) if err != nil { return "", err } @@ -41,7 +41,7 @@ func (pm *PmCrypto) DecryptMessageBinKey(encryptedText string, privateKey []byte return "", err } - encryptedio, err := internal.UnArmor(encryptedText) + encryptedio, err := internal.Unarmor(encryptedText) if err != nil { return "", err } @@ -59,7 +59,7 @@ func (pm *PmCrypto) DecryptMessageBinKey(encryptedText string, privateKey []byte } } - config := &packet.Config{ Time: pm.getTimeGenerator() } + config := &packet.Config{Time: pm.getTimeGenerator()} md, err := openpgp.ReadMessage(encryptedio.Body, privKeyEntries, nil, config) if err != nil { @@ -76,13 +76,13 @@ func (pm *PmCrypto) DecryptMessageBinKey(encryptedText string, privateKey []byte return string(b), nil } -// DecryptMessageVerifyPrivbinkeys decrypt message and verify the signature -// veriferKey string: armored verifier keys +// DecryptMessageVerifyPrivBinKeys decrypt message and verify the signature +// verifierKey string: armored verifier keys // privateKey []byte: unarmored private key to decrypt. could be mutiple -func (pm *PmCrypto) DecryptMessageVerifyPrivbinkeys(encryptedText string, veriferKey string, privateKeys []byte, passphrase string, verifyTime int64) (*models.DecryptSignedVerify, error) { +func (pm *PmCrypto) DecryptMessageVerifyPrivBinKeys(encryptedText string, verifierKey string, privateKeys []byte, passphrase string, verifyTime int64) (*models.DecryptSignedVerify, error) { - if len(veriferKey) > 0 { - verifierRaw, err := armorUtils.UnArmor(veriferKey) + if len(verifierKey) > 0 { + verifierRaw, err := armorUtils.Unarmor(verifierKey) if err != nil { return nil, err } @@ -91,19 +91,19 @@ func (pm *PmCrypto) DecryptMessageVerifyPrivbinkeys(encryptedText string, verife return pm.decryptMessageVerifyAllBin(encryptedText, nil, privateKeys, passphrase, verifyTime) } -// DecryptMessageVerifyBinKeyPrivbinkeys decrypt message and verify the signature -// veriferKey []byte: unarmored verifier keys +// DecryptMessageVerifyBinKeyPrivBinKeys decrypt message and verify the signature +// verifierKey []byte: unarmored verifier keys // privateKey []byte: unarmored private key to decrypt. could be mutiple -func (pm *PmCrypto) DecryptMessageVerifyBinKeyPrivbinkeys(encryptedText string, veriferKey []byte, privateKeys []byte, passphrase string, verifyTime int64) (*models.DecryptSignedVerify, error) { - return pm.decryptMessageVerifyAllBin(encryptedText, veriferKey, privateKeys, passphrase, verifyTime) +func (pm *PmCrypto) DecryptMessageVerifyBinKeyPrivBinKeys(encryptedText string, verifierKey []byte, privateKeys []byte, passphrase string, verifyTime int64) (*models.DecryptSignedVerify, error) { + return pm.decryptMessageVerifyAllBin(encryptedText, verifierKey, privateKeys, passphrase, verifyTime) } // DecryptMessageVerify decrypt message and verify the signature -// veriferKey string: armored verifier keys +// verifierKey string: armored verifier keys // privateKey string: private to decrypt -func (pm *PmCrypto) DecryptMessageVerify(encryptedText string, veriferKey string, privateKey string, passphrase string, verifyTime int64) (*models.DecryptSignedVerify, error) { - if len(veriferKey) > 0 { - verifierRaw, err := armorUtils.UnArmor(veriferKey) +func (pm *PmCrypto) DecryptMessageVerify(encryptedText string, verifierKey string, privateKey string, passphrase string, verifyTime int64) (*models.DecryptSignedVerify, error) { + if len(verifierKey) > 0 { + verifierRaw, err := armorUtils.Unarmor(verifierKey) if err != nil { return nil, err } @@ -113,20 +113,20 @@ func (pm *PmCrypto) DecryptMessageVerify(encryptedText string, veriferKey string } // DecryptMessageVerifyBinKey decrypt message and verify the signature -// veriferKey []byte: unarmored verifier keys +// verifierKey []byte: unarmored verifier keys // privateKey string: private to decrypt -func (pm *PmCrypto) DecryptMessageVerifyBinKey(encryptedText string, veriferKey []byte, privateKey string, passphrase string, verifyTime int64) (*models.DecryptSignedVerify, error) { - privateKeyRaw, err := armorUtils.UnArmor(privateKey) +func (pm *PmCrypto) DecryptMessageVerifyBinKey(encryptedText string, verifierKey []byte, privateKey string, passphrase string, verifyTime int64) (*models.DecryptSignedVerify, error) { + privateKeyRaw, err := armorUtils.Unarmor(privateKey) if err != nil { return nil, err } - return pm.decryptMessageVerifyAllBin(encryptedText, veriferKey, privateKeyRaw, passphrase, verifyTime) + return pm.decryptMessageVerifyAllBin(encryptedText, verifierKey, privateKeyRaw, passphrase, verifyTime) } // decryptMessageVerifyAllBin // decrypt_message_verify_single_key(private_key: string, passphras: string, encrypted : string, signature : string) : decrypt_sign_verify; // decrypt_message_verify(passphras: string, encrypted : string, signature : string) : decrypt_sign_verify; -func (pm *PmCrypto) decryptMessageVerifyAllBin(encryptedText string, veriferKey []byte, privateKey []byte, passphrase string, verifyTime int64) (*models.DecryptSignedVerify, error) { +func (pm *PmCrypto) decryptMessageVerifyAllBin(encryptedText string, verifierKey []byte, privateKey []byte, passphrase string, verifyTime int64) (*models.DecryptSignedVerify, error) { privKey := bytes.NewReader(privateKey) privKeyEntries, err := openpgp.ReadKeyRing(privKey) if err != nil { @@ -151,8 +151,8 @@ func (pm *PmCrypto) decryptMessageVerifyAllBin(encryptedText string, veriferKey out.Verify = failed var verifierEntries openpgp.EntityList - if len(veriferKey) > 0 { - verifierReader := bytes.NewReader(veriferKey) + if len(verifierKey) > 0 { + verifierReader := bytes.NewReader(verifierKey) verifierEntries, err = openpgp.ReadKeyRing(verifierReader) if err != nil { return nil, err @@ -165,7 +165,7 @@ func (pm *PmCrypto) decryptMessageVerifyAllBin(encryptedText string, veriferKey out.Verify = noVerifier } - encryptedio, err := internal.UnArmor(encryptedText) + encryptedio, err := internal.Unarmor(encryptedText) if err != nil { return nil, err } @@ -222,7 +222,7 @@ func processSignatureExpiration(md *openpgp.MessageDetails, verifyTime int64) { if md.Signature.KeyLifetimeSecs != nil { expires = int64(*md.Signature.KeyLifetimeSecs) + created } - if created - internal.CreationTimeOffset <= verifyTime && verifyTime <= expires { + if created-internal.CreationTimeOffset <= verifyTime && verifyTime <= expires { md.SignatureError = nil } } else { @@ -238,7 +238,7 @@ func processSignatureExpiration(md *openpgp.MessageDetails, verifyTime int64) { // privateKey : optional required when you want to sign // passphrase : optional required when you pass the private key and this passphrase must could decrypt the private key func (pm *PmCrypto) EncryptMessage(plainText string, publicKey string, privateKey string, passphrase string, trim bool) (string, error) { - rawPubKey, err := armorUtils.UnArmor(publicKey) + rawPubKey, err := armorUtils.Unarmor(publicKey) if err != nil { return "", err } @@ -290,11 +290,11 @@ func (pm *PmCrypto) EncryptMessageBinKey(plainText string, publicKey []byte, pri } if signEntity == nil { - return "", errors.New("cannot sign message, singer key is not unlocked") + return "", errors.New("cannot sign message, signer key is not unlocked") } } - config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pm.getTimeGenerator() } + config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pm.getTimeGenerator()} ew, err := openpgp.Encrypt(w, pubKeyEntries, signEntity, nil, config) @@ -315,7 +315,7 @@ func (pm *PmCrypto) EncryptMessageWithPassword(plainText string, password string return "", err } - config := &packet.Config{ Time: pm.getTimeGenerator() } + config := &packet.Config{Time: pm.getTimeGenerator()} plaintext, err := openpgp.SymmetricallyEncrypt(w, []byte(password), nil, config) if err != nil { return "", err @@ -338,7 +338,7 @@ func (pm *PmCrypto) EncryptMessageWithPassword(plainText string, password string //encrypted string : armored pgp message //output string : clear text func (pm *PmCrypto) DecryptMessageWithPassword(encrypted string, password string) (string, error) { - encryptedio, err := internal.UnArmor(encrypted) + encryptedio, err := internal.Unarmor(encrypted) if err != nil { return "", err } @@ -347,7 +347,7 @@ func (pm *PmCrypto) DecryptMessageWithPassword(encrypted string, password string return []byte(password), nil } - config := &packet.Config{ Time: pm.getTimeGenerator() } + config := &packet.Config{Time: pm.getTimeGenerator()} md, err := openpgp.ReadMessage(encryptedio.Body, nil, prompt, config) if err != nil { return "", err diff --git a/crypto/mime_test.go b/crypto/mime_test.go index 313f040..d2718ae 100644 --- a/crypto/mime_test.go +++ b/crypto/mime_test.go @@ -1,10 +1,10 @@ package crypto import ( - "testing" "fmt" - "io/ioutil" - "proton/pmcrypto/internal" + "io/ioutil" + "proton/pmcrypto/internal" + "testing" ) const publicKey = `-----BEGIN PGP PUBLIC KEY BLOCK----- @@ -235,7 +235,6 @@ z9GxJikRwscymWmXx2QsvhUiWeOJ05WwK+WAnKR1uVtkEJ9QJVe2chyuMORY -----END PGP PRIVATE KEY BLOCK----- ` - // define call back interface type Callbacks struct { } @@ -259,9 +258,9 @@ func (t Callbacks) OnError(err error) { func TestDecrypt(t *testing.T) { callbacks := Callbacks{} o := PmCrypto{} - block, _ := internal.UnArmor(publicKey) + block, _ := internal.Unarmor(publicKey) publicKeyUnarmored, _ := ioutil.ReadAll(block.Body) - block, _ = internal.UnArmor(privatekey) + block, _ = internal.Unarmor(privatekey) privateKeyUnarmored, _ := ioutil.ReadAll(block.Body) o.DecryptMIMEMessage(testMessage, publicKeyUnarmored, privateKeyUnarmored, privatekeypassword, &callbacks, o.GetTime()) @@ -410,4 +409,3 @@ RIzX2CG47PuGl/uvImFW/Iw= fmt.Println(attachment) } } - diff --git a/crypto/session.go b/crypto/session.go index e926c14..f1188b5 100644 --- a/crypto/session.go +++ b/crypto/session.go @@ -5,7 +5,7 @@ import ( "errors" "fmt" "io" - "strings" + "strings" "golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp/packet" @@ -118,7 +118,7 @@ func (pm *PmCrypto) GetSessionFromKeyPacket(keyPackage []byte, privateKey string //KeyPacketWithPublicKey ... func (pm *PmCrypto) KeyPacketWithPublicKey(sessionSplit *models.SessionSplit, publicKey string) ([]byte, error) { - pubkeyRaw, err := armor.UnArmor(publicKey) + pubkeyRaw, err := armor.Unarmor(publicKey) if err != nil { return nil, err } @@ -282,4 +282,4 @@ func getAlgo(cipher packet.CipherFunction) string { } return algo -} \ No newline at end of file +} diff --git a/internal/armor.go b/internal/armor.go index 3cd7652..93b105b 100644 --- a/internal/armor.go +++ b/internal/armor.go @@ -5,7 +5,7 @@ import ( "strings" ) -func UnArmor(input string) (*armor.Block, error) { +func Unarmor(input string) (*armor.Block, error) { io := strings.NewReader(input) b, err := armor.Decode(io) if err != nil { @@ -13,4 +13,4 @@ func UnArmor(input string) (*armor.Block, error) { } return b, nil -} \ No newline at end of file +} diff --git a/key/fingerprint.go b/key/fingerprint.go index c9e563a..08394c3 100644 --- a/key/fingerprint.go +++ b/key/fingerprint.go @@ -11,7 +11,7 @@ import ( // GetFingerprint get a armored public key fingerprint func GetFingerprint(publicKey string) (string, error) { - rawPubKey, err := armor.UnArmor(publicKey) + rawPubKey, err := armor.Unarmor(publicKey) if err != nil { return "", err } diff --git a/models/models.go b/models/models.go index 04a2791..9f6ea50 100644 --- a/models/models.go +++ b/models/models.go @@ -1,6 +1,5 @@ package models - //EncryptedSplit when encrypt attachemt type EncryptedSplit struct { DataPacket []byte @@ -8,7 +7,7 @@ type EncryptedSplit struct { Algo string } -//SessionSplit splited session +//SessionSplit split session type SessionSplit struct { Session []byte Algo string @@ -29,4 +28,3 @@ type DecryptSignedVerify struct { //error message if verify failed Message string } -