diff --git a/armor/armor.go b/armor/armor.go index 2ad88350..b01d8e7e 100644 --- a/armor/armor.go +++ b/armor/armor.go @@ -4,13 +4,13 @@ package armor import ( "bytes" + "fmt" "io" "io/ioutil" "github.com/ProtonMail/go-crypto/openpgp/armor" "github.com/ProtonMail/gopenpgp/v2/constants" "github.com/ProtonMail/gopenpgp/v2/internal" - "github.com/pkg/errors" ) // ArmorKey armors input as a public key. @@ -46,7 +46,7 @@ func ArmorWithTypeAndCustomHeaders(input []byte, armorType, version, comment str func Unarmor(input string) ([]byte, error) { b, err := internal.Unarmor(input) if err != nil { - return nil, errors.Wrap(err, "gopengp: unable to unarmor") + return nil, fmt.Errorf("gopengp: unable to unarmor: %w", err) } return ioutil.ReadAll(b.Body) } @@ -57,13 +57,13 @@ func armorWithTypeAndHeaders(input []byte, armorType string, headers map[string] w, err := armor.Encode(&b, armorType, headers) if err != nil { - return "", errors.Wrap(err, "gopengp: unable to encode armoring") + return "", fmt.Errorf("gopengp: unable to encode armoring: %w", err) } if _, err = w.Write(input); err != nil { - return "", errors.Wrap(err, "gopengp: unable to write armored to buffer") + return "", fmt.Errorf("gopengp: unable to write armored to buffer: %w", err) } if err := w.Close(); err != nil { - return "", errors.Wrap(err, "gopengp: unable to close armor buffer") + return "", fmt.Errorf("gopengp: unable to close armor buffer: %w", err) } return b.String(), nil } diff --git a/crypto/attachment.go b/crypto/attachment.go index b078ce1c..b33331f9 100644 --- a/crypto/attachment.go +++ b/crypto/attachment.go @@ -2,6 +2,7 @@ package crypto import ( "bytes" + "fmt" "io" "io/ioutil" "runtime" @@ -10,7 +11,6 @@ import ( "github.com/ProtonMail/go-crypto/openpgp" "github.com/ProtonMail/go-crypto/openpgp/packet" - "github.com/pkg/errors" ) // AttachmentProcessor keeps track of the progress of encrypting an attachment @@ -41,7 +41,7 @@ func (ap *AttachmentProcessor) Finish() (*PGPSplitMessage, error) { } if err := (*ap.w).Close(); err != nil { - return nil, errors.Wrap(err, "gopengpp: unable to close writer") + return nil, fmt.Errorf("gopengpp: unable to close writer: %w", err) } if ap.garbageCollector > 0 { @@ -50,7 +50,7 @@ func (ap *AttachmentProcessor) Finish() (*PGPSplitMessage, error) { } if err := (*ap.pipe).Close(); err != nil { - return nil, errors.Wrap(err, "gopengpp: unable to close pipe") + return nil, fmt.Errorf("gopengpp: unable to close pipe: %w", err) } ap.done.Wait() @@ -107,7 +107,7 @@ func (keyRing *KeyRing) newAttachmentProcessor( var encryptErr error ew, encryptErr = openpgp.Encrypt(writer, keyRing.entities, nil, hints, config) if encryptErr != nil { - return nil, errors.Wrap(encryptErr, "gopengpp: unable to encrypt attachment") + return nil, fmt.Errorf("gopengpp: unable to encrypt attachment: %w", encryptErr) } attachmentProc.w = &ew attachmentProc.pipe = writer @@ -167,13 +167,13 @@ func (keyRing *KeyRing) DecryptAttachment(message *PGPSplitMessage) (*PlainMessa md, err := openpgp.ReadMessage(encryptedReader, privKeyEntries, nil, config) if err != nil { - return nil, errors.Wrap(err, "gopengpp: unable to read attachment") + return nil, fmt.Errorf("gopengpp: unable to read attachment: %w", err) } decrypted := md.UnverifiedBody b, err := ioutil.ReadAll(decrypted) if err != nil { - return nil, errors.Wrap(err, "gopengpp: unable to read attachment body") + return nil, fmt.Errorf("gopengpp: unable to read attachment body: %w", err) } return &PlainMessage{ diff --git a/crypto/attachment_manual.go b/crypto/attachment_manual.go index 39a2957a..94d75d7d 100644 --- a/crypto/attachment_manual.go +++ b/crypto/attachment_manual.go @@ -1,6 +1,8 @@ package crypto import ( + "errors" + "fmt" "io" "io/ioutil" "runtime" @@ -10,7 +12,6 @@ import ( "github.com/ProtonMail/go-crypto/openpgp" "github.com/ProtonMail/go-crypto/openpgp/packet" - "github.com/pkg/errors" ) // ManualAttachmentProcessor keeps track of the progress of encrypting an attachment @@ -42,7 +43,10 @@ func (ap *ManualAttachmentProcessor) GetDataLength() int { func (ap *ManualAttachmentProcessor) Process(plainData []byte) error { defer runtime.GC() _, err := ap.plaintextWriter.Write(plainData) - return errors.Wrap(err, "gopenpgp: couldn't write attachment data") + if err != nil { + err = fmt.Errorf("gopenpgp: couldn't write attachment data: %w", err) + } + return err } // Finish tells the processor to finalize encryption. @@ -52,10 +56,10 @@ func (ap *ManualAttachmentProcessor) Finish() error { return ap.err } if err := ap.plaintextWriter.Close(); err != nil { - return errors.Wrap(err, "gopengpp: unable to close the plaintext writer") + return fmt.Errorf("gopengpp: unable to close the plaintext writer: %w", err) } if err := ap.ciphertextWriter.Close(); err != nil { - return errors.Wrap(err, "gopengpp: unable to close the dataPacket writer") + return fmt.Errorf("gopengpp: unable to close the dataPacket writer: %w", err) } ap.done.Wait() if ap.err != nil { @@ -130,7 +134,7 @@ func (keyRing *KeyRing) NewManualAttachmentProcessor( var encryptErr error ew, encryptErr = openpgp.EncryptSplit(keyWriter, dataWriter, keyRing.entities, nil, hints, config) if encryptErr != nil { - return nil, errors.Wrap(encryptErr, "gopengpp: unable to encrypt attachment") + return nil, fmt.Errorf("gopengpp: unable to encrypt attachment: %w", encryptErr) } attachmentProc.plaintextWriter = ew @@ -138,7 +142,7 @@ func (keyRing *KeyRing) NewManualAttachmentProcessor( // The key packet should have been already written, so we can close if err := keyWriter.Close(); err != nil { - return nil, errors.Wrap(err, "gopenpgp: couldn't close the keyPacket writer") + return nil, fmt.Errorf("gopenpgp: couldn't close the keyPacket writer: %w", err) } // Check if the goroutines encountered errors @@ -171,7 +175,7 @@ func readAll(buffer []byte, reader io.Reader) (int, error) { if errors.Is(err, io.EOF) { break } - return 0, errors.Wrap(err, "gopenpgp: couldn't read data from the encrypted reader") + return 0, fmt.Errorf("gopenpgp: couldn't read data from the encrypted reader: %w", err) } if offset == bufferLen { // Here we've reached the end of the buffer diff --git a/crypto/attachment_manual_test.go b/crypto/attachment_manual_test.go index 4a9673df..59d4aeba 100644 --- a/crypto/attachment_manual_test.go +++ b/crypto/attachment_manual_test.go @@ -2,10 +2,9 @@ package crypto import ( "bytes" + "errors" "io" "testing" - - "github.com/pkg/errors" ) func TestManualAttachmentProcessor(t *testing.T) { diff --git a/crypto/key.go b/crypto/key.go index 16c50d98..bbbd6a90 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -5,6 +5,7 @@ import ( "crypto" "crypto/sha256" "encoding/hex" + "errors" "fmt" "io" "math/big" @@ -13,7 +14,6 @@ import ( "github.com/ProtonMail/gopenpgp/v2/armor" "github.com/ProtonMail/gopenpgp/v2/constants" - "github.com/pkg/errors" openpgp "github.com/ProtonMail/go-crypto/openpgp" packet "github.com/ProtonMail/go-crypto/openpgp/packet" @@ -117,14 +117,14 @@ func (key *Key) Lock(passphrase []byte) (*Key, error) { if lockedKey.entity.PrivateKey != nil && !lockedKey.entity.PrivateKey.Dummy() { err = lockedKey.entity.PrivateKey.Encrypt(passphrase) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in locking key") + return nil, fmt.Errorf("gopenpgp: error in locking key: %w", err) } } for _, sub := range lockedKey.entity.Subkeys { if sub.PrivateKey != nil && !sub.PrivateKey.Dummy() { if err := sub.PrivateKey.Encrypt(passphrase); err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in locking sub key") + return nil, fmt.Errorf("gopenpgp: error in locking sub key: %w", err) } } } @@ -162,14 +162,14 @@ func (key *Key) Unlock(passphrase []byte) (*Key, error) { if unlockedKey.entity.PrivateKey != nil && !unlockedKey.entity.PrivateKey.Dummy() { err = unlockedKey.entity.PrivateKey.Decrypt(passphrase) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in unlocking key") + return nil, fmt.Errorf("gopenpgp: error in unlocking key: %w", err) } } for _, sub := range unlockedKey.entity.Subkeys { if sub.PrivateKey != nil && !sub.PrivateKey.Dummy() { if err := sub.PrivateKey.Decrypt(passphrase); err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in unlocking sub key") + return nil, fmt.Errorf("gopenpgp: error in unlocking sub key: %w", err) } } } @@ -198,7 +198,7 @@ func (key *Key) Serialize() ([]byte, error) { } if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in serializing key") + return nil, fmt.Errorf("gopenpgp: error in serializing key: %w", err) } return buffer.Bytes(), nil @@ -254,7 +254,7 @@ func (key *Key) GetArmoredPublicKeyWithCustomHeaders(comment, version string) (s func (key *Key) GetPublicKey() (b []byte, err error) { var outBuf bytes.Buffer if err = key.entity.Serialize(&outBuf); err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in serializing public key") + return nil, fmt.Errorf("gopenpgp: error in serializing public key: %w", err) } return outBuf.Bytes(), nil @@ -417,7 +417,7 @@ func (key *Key) readFrom(r io.Reader, armored bool) error { entities, err = openpgp.ReadKeyRing(r) } if err != nil { - return errors.Wrap(err, "gopenpgp: error in reading key ring") + return fmt.Errorf("gopenpgp: error in reading key ring: %w", err) } if len(entities) > 1 { @@ -439,7 +439,7 @@ func generateKey( prime1, prime2, prime3, prime4 []byte, ) (*Key, error) { if len(email) == 0 && len(name) == 0 { - return nil, errors.New("gopenpgp: neither name nor email set.") + return nil, errors.New("gopenpgp: neither name nor email set") } comments := "" @@ -473,7 +473,7 @@ func generateKey( newEntity, err := openpgp.NewEntity(name, comments, email, cfg) if err != nil { - return nil, errors.Wrap(err, "gopengpp: error in encoding new entity") + return nil, fmt.Errorf("gopengpp: error in encoding new entity: %w", err) } if newEntity.PrivateKey == nil { diff --git a/crypto/keyring.go b/crypto/keyring.go index 201f5eec..8f4bcd00 100644 --- a/crypto/keyring.go +++ b/crypto/keyring.go @@ -2,11 +2,12 @@ package crypto import ( "bytes" + "errors" + "fmt" "time" "github.com/ProtonMail/go-crypto/openpgp" "github.com/ProtonMail/go-crypto/openpgp/packet" - "github.com/pkg/errors" ) // KeyRing contains multiple private and public keys. @@ -220,14 +221,14 @@ func (keyRing *KeyRing) Copy() (*KeyRing, error) { } if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to copy key: error in serializing entity") + return nil, fmt.Errorf("gopenpgp: unable to copy key: error in serializing entity: %w", err) } bt := buffer.Bytes() entities[id], err = openpgp.ReadEntity(packet.NewReader(bytes.NewReader(bt))) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to copy key: error in reading entity") + return nil, fmt.Errorf("gopenpgp: unable to copy key: error in reading entity: %w", err) } } newKeyRing.entities = entities diff --git a/crypto/keyring_message.go b/crypto/keyring_message.go index f0fc16e0..dc0e2806 100644 --- a/crypto/keyring_message.go +++ b/crypto/keyring_message.go @@ -2,6 +2,8 @@ package crypto import ( "bytes" + "errors" + "fmt" "io" "io/ioutil" "time" @@ -9,7 +11,6 @@ import ( "github.com/ProtonMail/go-crypto/openpgp" "github.com/ProtonMail/go-crypto/openpgp/packet" "github.com/ProtonMail/gopenpgp/v2/constants" - "github.com/pkg/errors" ) // Encrypt encrypts a PlainMessage, outputs a PGPMessage. @@ -219,12 +220,12 @@ func asymmetricEncrypt( _, err = encryptWriter.Write(plainMessage.GetBinary()) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in writing to message") + return nil, fmt.Errorf("gopenpgp: error in writing to message: %w", err) } err = encryptWriter.Close() if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in closing message") + return nil, fmt.Errorf("gopenpgp: error in closing message: %w", err) } return &PGPMessage{outBuf.Bytes()}, nil @@ -268,7 +269,7 @@ func asymmetricEncryptStream( encryptWriter, err = openpgp.EncryptTextSplit(keyPacketWriter, dataPacketWriter, publicKey.entities, signEntity, hints, config) } if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in encrypting asymmetrically") + return nil, fmt.Errorf("gopenpgp: error in encrypting asymmetrically: %w", err) } return encryptWriter, nil } @@ -294,7 +295,7 @@ func asymmetricDecrypt( body, err := ioutil.ReadAll(messageDetails.UnverifiedBody) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in reading message body") + return nil, fmt.Errorf("gopenpgp: error in reading message body: %w", err) } if verifyKey != nil { @@ -349,7 +350,7 @@ func asymmetricDecryptStream( messageDetails, err = openpgp.ReadMessage(encryptedIO, privKeyEntries, nil, config) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in reading message") + return nil, fmt.Errorf("gopenpgp: error in reading message: %w", err) } return messageDetails, err } diff --git a/crypto/keyring_session.go b/crypto/keyring_session.go index ca2a94f9..e7de844a 100644 --- a/crypto/keyring_session.go +++ b/crypto/keyring_session.go @@ -2,10 +2,10 @@ package crypto import ( "bytes" + "errors" + "fmt" "strconv" - "github.com/pkg/errors" - "github.com/ProtonMail/go-crypto/openpgp/packet" ) @@ -55,11 +55,11 @@ Loop: } if !hasPacket { - return nil, errors.Wrap(err, "gopenpgp: couldn't find a session key packet") + return nil, fmt.Errorf("gopenpgp: couldn't find a session key packet: %w", err) } if decryptErr != nil { - return nil, errors.Wrap(decryptErr, "gopenpgp: error in decrypting") + return nil, fmt.Errorf("gopenpgp: error in decrypting: %w", decryptErr) } if ek == nil || ek.Key == nil { @@ -75,7 +75,7 @@ func (keyRing *KeyRing) EncryptSessionKey(sk *SessionKey) ([]byte, error) { outbuf := &bytes.Buffer{} cf, err := sk.GetCipherFunc() if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to encrypt session key") + return nil, fmt.Errorf("gopenpgp: unable to encrypt session key: %w", err) } pubKeys := make([]*packet.PublicKey, 0, len(keyRing.entities)) @@ -92,7 +92,7 @@ func (keyRing *KeyRing) EncryptSessionKey(sk *SessionKey) ([]byte, error) { for _, pub := range pubKeys { if err := packet.SerializeEncryptedKey(outbuf, pub, cf, sk.Key, nil); err != nil { - return nil, errors.Wrap(err, "gopenpgp: cannot set key") + return nil, fmt.Errorf("gopenpgp: cannot set key: %w", err) } } return outbuf.Bytes(), nil diff --git a/crypto/keyring_streaming.go b/crypto/keyring_streaming.go index 6d99402d..b0be89fd 100644 --- a/crypto/keyring_streaming.go +++ b/crypto/keyring_streaming.go @@ -2,11 +2,11 @@ package crypto import ( "bytes" + "errors" "io" "time" "github.com/ProtonMail/go-crypto/openpgp" - "github.com/pkg/errors" ) type Reader interface { diff --git a/crypto/keyring_streaming_test.go b/crypto/keyring_streaming_test.go index 1cd6e44f..89ee2408 100644 --- a/crypto/keyring_streaming_test.go +++ b/crypto/keyring_streaming_test.go @@ -2,12 +2,11 @@ package crypto import ( "bytes" + "errors" "io" "io/ioutil" "reflect" "testing" - - "github.com/pkg/errors" ) const testContext = "test-context" diff --git a/crypto/message.go b/crypto/message.go index 4f34be4d..dc281ff0 100644 --- a/crypto/message.go +++ b/crypto/message.go @@ -3,7 +3,8 @@ package crypto import ( "bytes" "encoding/base64" - goerrors "errors" + "errors" + "fmt" "io" "io/ioutil" "regexp" @@ -15,7 +16,6 @@ import ( "github.com/ProtonMail/gopenpgp/v2/armor" "github.com/ProtonMail/gopenpgp/v2/constants" "github.com/ProtonMail/gopenpgp/v2/internal" - "github.com/pkg/errors" ) // ---- MODELS ----- @@ -110,12 +110,12 @@ func NewPGPMessage(data []byte) *PGPMessage { func NewPGPMessageFromArmored(armored string) (*PGPMessage, error) { encryptedIO, err := internal.Unarmor(armored) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in unarmoring message") + return nil, fmt.Errorf("gopenpgp: error in unarmoring message: %w", err) } message, err := ioutil.ReadAll(encryptedIO.Body) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in reading armored message") + return nil, fmt.Errorf("gopenpgp: error in reading armored message: %w", err) } return &PGPMessage{ @@ -155,12 +155,12 @@ func NewPGPSignature(data []byte) *PGPSignature { func NewPGPSignatureFromArmored(armored string) (*PGPSignature, error) { encryptedIO, err := internal.Unarmor(armored) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in unarmoring signature") + return nil, fmt.Errorf("gopenpgp: error in unarmoring signature: %w", err) } signature, err := ioutil.ReadAll(encryptedIO.Body) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in reading armored signature") + return nil, fmt.Errorf("gopenpgp: error in reading armored signature: %w", err) } return &PGPSignature{ @@ -187,7 +187,7 @@ func NewClearTextMessageFromArmored(signedMessage string) (*ClearTextMessage, er signature, err := ioutil.ReadAll(modulusBlock.ArmoredSignature.Body) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in reading cleartext message") + return nil, fmt.Errorf("gopenpgp: error in reading cleartext message: %w", err) } return NewClearTextMessage(modulusBlock.Bytes, signature), nil @@ -262,7 +262,7 @@ func (msg *PGPMessage) GetEncryptionKeyIDs() ([]uint64, bool) { Loop: for { var p packet.Packet - if p, err = packets.Next(); goerrors.Is(err, io.EOF) { + if p, err = packets.Next(); errors.Is(err, io.EOF) { break } switch p := p.(type) { @@ -333,7 +333,7 @@ func (msg *PGPMessage) SplitMessage() (*PGPSplitMessage, error) { Loop: for { p, err := packets.Next() - if goerrors.Is(err, io.EOF) { + if errors.Is(err, io.EOF) { break } if err != nil { @@ -399,7 +399,7 @@ func (msg *ClearTextMessage) GetBinarySignature() []byte { func (msg *ClearTextMessage) GetArmored() (string, error) { armSignature, err := armor.ArmorWithType(msg.GetBinarySignature(), constants.PGPSignatureHeader) if err != nil { - return "", errors.Wrap(err, "gopenpgp: error in armoring cleartext message") + return "", fmt.Errorf("gopenpgp: error in armoring cleartext message: %w", err) } str := "-----BEGIN PGP SIGNED MESSAGE-----\r\nHash: SHA512\r\n\r\n" @@ -429,7 +429,7 @@ func getSignatureKeyIDs(data []byte) ([]uint64, bool) { Loop: for { var p packet.Packet - if p, err = packets.Next(); goerrors.Is(err, io.EOF) { + if p, err = packets.Next(); errors.Is(err, io.EOF) { break } switch p := p.(type) { diff --git a/crypto/mime.go b/crypto/mime.go index d756dfcc..c392cc86 100644 --- a/crypto/mime.go +++ b/crypto/mime.go @@ -2,6 +2,8 @@ package crypto import ( "bytes" + "errors" + "fmt" "io/ioutil" "net/mail" "net/textproto" @@ -11,7 +13,6 @@ import ( "github.com/ProtonMail/go-crypto/openpgp/packet" gomime "github.com/ProtonMail/go-mime" "github.com/ProtonMail/gopenpgp/v2/constants" - "github.com/pkg/errors" ) // MIMECallbacks defines callback methods to process a MIME message. @@ -84,14 +85,14 @@ func parseMIME( ) (*gomime.BodyCollector, []string, []string, error) { mm, err := mail.ReadMessage(strings.NewReader(mimeBody)) if err != nil { - return nil, nil, nil, errors.Wrap(err, "gopenpgp: error in reading message") + return nil, nil, nil, fmt.Errorf("gopenpgp: error in reading message: %w", err) } config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: getTimeGenerator()} h := textproto.MIMEHeader(mm.Header) mmBodyData, err := ioutil.ReadAll(mm.Body) if err != nil { - return nil, nil, nil, errors.Wrap(err, "gopenpgp: error in reading message body data") + return nil, nil, nil, fmt.Errorf("gopenpgp: error in reading message body data: %w", err) } printAccepter := gomime.NewMIMEPrinter() diff --git a/crypto/password.go b/crypto/password.go index 64ee0264..565ad67d 100644 --- a/crypto/password.go +++ b/crypto/password.go @@ -2,12 +2,13 @@ package crypto import ( "bytes" + "errors" + "fmt" "io" "github.com/ProtonMail/go-crypto/openpgp" pgpErrors "github.com/ProtonMail/go-crypto/openpgp/errors" "github.com/ProtonMail/go-crypto/openpgp/packet" - "github.com/pkg/errors" ) // EncryptMessageWithPassword encrypts a PlainMessage to PGPMessage with a @@ -62,7 +63,7 @@ func DecryptSessionKeyWithPassword(keyPacket, password []byte) (*SessionKey, err } if err = sk.checkSize(); err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to decrypt session key with password") + return nil, fmt.Errorf("gopenpgp: unable to decrypt session key with password: %w", err) } return sk, nil @@ -80,7 +81,7 @@ func EncryptSessionKeyWithPassword(sk *SessionKey, password []byte) ([]byte, err cf, err := sk.GetCipherFunc() if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to encrypt session key with password") + return nil, fmt.Errorf("gopenpgp: unable to encrypt session key with password: %w", err) } if len(password) == 0 { @@ -88,7 +89,7 @@ func EncryptSessionKeyWithPassword(sk *SessionKey, password []byte) ([]byte, err } if err = sk.checkSize(); err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to encrypt session key with password") + return nil, fmt.Errorf("gopenpgp: unable to encrypt session key with password: %w", err) } config := &packet.Config{ @@ -97,7 +98,7 @@ func EncryptSessionKeyWithPassword(sk *SessionKey, password []byte) ([]byte, err err = packet.SerializeSymmetricKeyEncryptedReuseKey(outbuf, sk.Key, password, config) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to encrypt session key with password") + return nil, fmt.Errorf("gopenpgp: unable to encrypt session key with password: %w", err) } return outbuf.Bytes(), nil } @@ -120,16 +121,16 @@ func passwordEncrypt(message *PlainMessage, password []byte) ([]byte, error) { encryptWriter, err := openpgp.SymmetricallyEncrypt(&outBuf, password, hints, config) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in encrypting message symmetrically") + return nil, fmt.Errorf("gopenpgp: error in encrypting message symmetrically: %w", err) } _, err = encryptWriter.Write(message.GetBinary()) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in writing data to message") + return nil, fmt.Errorf("gopenpgp: error in writing data to message: %w", err) } err = encryptWriter.Close() if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in closing writer") + return nil, fmt.Errorf("gopenpgp: error in closing writer: %w", err) } return outBuf.Bytes(), nil diff --git a/crypto/sessionkey.go b/crypto/sessionkey.go index 1e48209d..cecb9fcc 100644 --- a/crypto/sessionkey.go +++ b/crypto/sessionkey.go @@ -3,12 +3,12 @@ package crypto import ( "bytes" "encoding/base64" + "errors" "fmt" "io" "time" "github.com/ProtonMail/gopenpgp/v2/constants" - "github.com/pkg/errors" "github.com/ProtonMail/go-crypto/openpgp" pgpErrors "github.com/ProtonMail/go-crypto/openpgp/errors" @@ -74,7 +74,7 @@ func RandomToken(size int) ([]byte, error) { config := &packet.Config{DefaultCipher: packet.CipherAES256} symKey := make([]byte, size) if _, err := io.ReadFull(config.Random(), symKey); err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in generating random token") + return nil, fmt.Errorf("gopenpgp: error in generating random token: %w", err) } return symKey, nil } @@ -128,7 +128,7 @@ func newSessionKeyFromEncrypted(ek *packet.EncryptedKey) (*SessionKey, error) { } if err := sk.checkSize(); err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to decrypt session key") + return nil, fmt.Errorf("gopenpgp: unable to decrypt session key: %w", err) } return sk, nil @@ -192,21 +192,21 @@ func encryptWithSessionKey( if signKeyRing != nil { _, err = signWriter.Write(message.GetBinary()) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in writing signed message") + return nil, fmt.Errorf("gopenpgp: error in writing signed message: %w", err) } err = signWriter.Close() if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in closing signing writer") + return nil, fmt.Errorf("gopenpgp: error in closing signing writer: %w", err) } } else { _, err = encryptWriter.Write(message.GetBinary()) } if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in writing message") + return nil, fmt.Errorf("gopenpgp: error in writing message: %w", err) } err = encryptWriter.Close() if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in closing encryption writer") + return nil, fmt.Errorf("gopenpgp: error in closing encryption writer: %w", err) } return encBuf.Bytes(), nil } @@ -221,7 +221,7 @@ func encryptStreamWithSessionKey( ) (encryptWriter, signWriter io.WriteCloser, err error) { dc, err := sk.GetCipherFunc() if err != nil { - return nil, nil, errors.Wrap(err, "gopenpgp: unable to encrypt with session key") + return nil, nil, fmt.Errorf("gopenpgp: unable to encrypt with session key: %w", err) } config := &packet.Config{ @@ -233,7 +233,7 @@ func encryptStreamWithSessionKey( if signKeyRing != nil { signEntity, err = signKeyRing.getSigningEntity() if err != nil { - return nil, nil, errors.Wrap(err, "gopenpgp: unable to sign") + return nil, nil, fmt.Errorf("gopenpgp: unable to sign: %w", err) } } @@ -285,13 +285,13 @@ func encryptStreamWithSessionKeyAndConfig( ) if err != nil { - return nil, nil, errors.Wrap(err, "gopenpgp: unable to encrypt") + return nil, nil, fmt.Errorf("gopenpgp: unable to encrypt: %w", err) } if algo := config.Compression(); algo != packet.CompressionNone { encryptWriter, err = packet.SerializeCompressed(encryptWriter, algo, config.CompressionConfig) if err != nil { - return nil, nil, errors.Wrap(err, "gopenpgp: error in compression") + return nil, nil, fmt.Errorf("gopenpgp: error in compression: %w", err) } } @@ -304,7 +304,7 @@ func encryptStreamWithSessionKeyAndConfig( signWriter, err = openpgp.Sign(encryptWriter, signEntity, hints, config) if err != nil { - return nil, nil, errors.Wrap(err, "gopenpgp: unable to sign") + return nil, nil, fmt.Errorf("gopenpgp: unable to sign: %w", err) } } else { encryptWriter, err = packet.SerializeLiteral( @@ -314,7 +314,7 @@ func encryptStreamWithSessionKeyAndConfig( modTime, ) if err != nil { - return nil, nil, errors.Wrap(err, "gopenpgp: unable to serialize") + return nil, nil, fmt.Errorf("gopenpgp: unable to serialize: %w", err) } } return encryptWriter, signWriter, nil @@ -374,7 +374,7 @@ func decryptWithSessionKeyAndContext( messageBuf := new(bytes.Buffer) _, err = messageBuf.ReadFrom(md.UnverifiedBody) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in reading message body") + return nil, fmt.Errorf("gopenpgp: error in reading message body: %w", err) } if verifyKeyRing != nil { @@ -403,7 +403,7 @@ func decryptStreamWithSessionKey( packets := packet.NewReader(messageReader) p, err := packets.Next() if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to read symmetric packet") + return nil, fmt.Errorf("gopenpgp: unable to read symmetric packet: %w", err) } // Decrypt data packet @@ -411,15 +411,15 @@ func decryptStreamWithSessionKey( case *packet.SymmetricallyEncrypted, *packet.AEADEncrypted: dc, err := sk.GetCipherFunc() if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to decrypt with session key") + return nil, fmt.Errorf("gopenpgp: unable to decrypt with session key: %w", err) } encryptedDataPacket, isDataPacket := p.(packet.EncryptedDataPacket) if !isDataPacket { - return nil, errors.Wrap(err, "gopenpgp: unknown data packet") + return nil, fmt.Errorf("gopenpgp: unknown data packet: %w", err) } decrypted, err = encryptedDataPacket.Decrypt(dc, sk.Key) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to decrypt symmetric packet") + return nil, fmt.Errorf("gopenpgp: unable to decrypt symmetric packet: %w", err) } default: return nil, errors.New("gopenpgp: invalid packet type") @@ -442,7 +442,7 @@ func decryptStreamWithSessionKey( md, err := openpgp.ReadMessage(decrypted, keyring, nil, config) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to decode symmetric packet") + return nil, fmt.Errorf("gopenpgp: unable to decode symmetric packet: %w", err) } md.UnverifiedBody = checkReader{decrypted, md.UnverifiedBody} diff --git a/crypto/sessionkey_streaming.go b/crypto/sessionkey_streaming.go index 200ca639..10a1b817 100644 --- a/crypto/sessionkey_streaming.go +++ b/crypto/sessionkey_streaming.go @@ -1,8 +1,6 @@ package crypto -import ( - "github.com/pkg/errors" -) +import "fmt" type signAndEncryptWriteCloser struct { signWriter WriteCloser @@ -176,7 +174,7 @@ func decryptStreamWithSessionKeyAndContext( verificationContext, ) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in reading message") + return nil, fmt.Errorf("gopenpgp: error in reading message: %w", err) } return &PlainMessageReader{ diff --git a/crypto/sessionkey_streaming_test.go b/crypto/sessionkey_streaming_test.go index 0ea9f093..40fd05cd 100644 --- a/crypto/sessionkey_streaming_test.go +++ b/crypto/sessionkey_streaming_test.go @@ -2,12 +2,11 @@ package crypto import ( "bytes" + "errors" "io" "io/ioutil" "reflect" "testing" - - "github.com/pkg/errors" ) func TestSessionKey_EncryptDecryptStream(t *testing.T) { diff --git a/crypto/signature.go b/crypto/signature.go index f5c58ab3..532056de 100644 --- a/crypto/signature.go +++ b/crypto/signature.go @@ -3,6 +3,7 @@ package crypto import ( "bytes" "crypto" + "errors" "fmt" "io" "math" @@ -11,7 +12,6 @@ import ( "github.com/ProtonMail/go-crypto/openpgp" pgpErrors "github.com/ProtonMail/go-crypto/openpgp/errors" "github.com/ProtonMail/go-crypto/openpgp/packet" - "github.com/pkg/errors" "github.com/ProtonMail/gopenpgp/v2/constants" "github.com/ProtonMail/gopenpgp/v2/internal" @@ -263,12 +263,12 @@ func verifySignature( seeker, ok := origText.(io.ReadSeeker) if !ok { - return nil, errors.Wrap(err, "gopenpgp: message reader do not support seeking, cannot retry signature verification") + return nil, fmt.Errorf("gopenpgp: message reader do not support seeking, cannot retry signature verification: %w", err) } _, err = seeker.Seek(0, io.SeekStart) if err != nil { - return nil, newSignatureFailed(errors.Wrap(err, "gopenpgp: could not rewind the data reader.")) + return nil, newSignatureFailed(fmt.Errorf("gopenpgp: could not rewind the data reader.: %w", err)) } _, err = signatureReader.Seek(0, io.SeekStart) @@ -325,7 +325,7 @@ func signMessageDetached( err = openpgp.DetachSignText(&outBuf, signEntity, messageReader, config) } if err != nil { - return nil, errors.Wrap(err, "gopenpgp: error in signing") + return nil, fmt.Errorf("gopenpgp: error in signing: %w", err) } return NewPGPSignature(outBuf.Bytes()), nil diff --git a/crypto/signature_collector.go b/crypto/signature_collector.go index fb769a91..9d63891a 100644 --- a/crypto/signature_collector.go +++ b/crypto/signature_collector.go @@ -2,6 +2,8 @@ package crypto import ( "bytes" + "errors" + "fmt" "io" "io/ioutil" "mime" @@ -13,7 +15,6 @@ import ( "github.com/ProtonMail/go-crypto/openpgp" "github.com/ProtonMail/go-crypto/openpgp/packet" gomime "github.com/ProtonMail/go-mime" - "github.com/pkg/errors" ) // SignatureCollector structure. @@ -62,7 +63,7 @@ func (sc *SignatureCollector) Accept( sc.verified = newSignatureNotSigned() // Invalid multipart/signed format just pass along if _, err = ioutil.ReadAll(rawBody); err != nil { - return errors.Wrap(err, "gopenpgp: error in reading raw message body") + return fmt.Errorf("gopenpgp: error in reading raw message body: %w", err) } for i, p := range multiparts { @@ -76,12 +77,12 @@ func (sc *SignatureCollector) Accept( // actual multipart/signed format err = sc.target.Accept(multiparts[0], multipartHeaders[0], hasPlainChild, true, true) if err != nil { - return errors.Wrap(err, "gopenpgp: error in parsing body") + return fmt.Errorf("gopenpgp: error in parsing body: %w", err) } partData, err := ioutil.ReadAll(multiparts[1]) if err != nil { - return errors.Wrap(err, "gopenpgp: error in ready part data") + return fmt.Errorf("gopenpgp: error in ready part data: %w", err) } decodedPart := gomime.DecodeContentEncoding( @@ -90,12 +91,12 @@ func (sc *SignatureCollector) Accept( buffer, err := ioutil.ReadAll(decodedPart) if err != nil { - return errors.Wrap(err, "gopenpgp: error in reading decoded data") + return fmt.Errorf("gopenpgp: error in reading decoded data: %w", err) } mediaType, _, _ := mime.ParseMediaType(header.Get("Content-Type")) buffer, err = gomime.DecodeCharset(buffer, mediaType, params) if err != nil { - return errors.Wrap(err, "gopenpgp: error in decoding charset") + return fmt.Errorf("gopenpgp: error in decoding charset: %w", err) } sc.signature = string(buffer) str, _ := ioutil.ReadAll(rawBody) diff --git a/go.mod b/go.mod index 8acf46aa..c68e6502 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/ProtonMail/go-crypto v0.0.0-20230321155629-9a39f2531310 github.com/ProtonMail/go-mime v0.0.0-20230322103455-7d82a3887f2f github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.7.0 golang.org/x/crypto v0.7.0 ) diff --git a/go.sum b/go.sum index 761a4e91..1605f447 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,6 @@ github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtM github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/helper/cleartext.go b/helper/cleartext.go index d5517b16..f683e1ad 100644 --- a/helper/cleartext.go +++ b/helper/cleartext.go @@ -1,9 +1,10 @@ package helper import ( + "fmt" + "github.com/ProtonMail/gopenpgp/v2/crypto" "github.com/ProtonMail/gopenpgp/v2/internal" - "github.com/pkg/errors" ) // SignCleartextMessageArmored signs text given a private key and its @@ -12,18 +13,18 @@ import ( func SignCleartextMessageArmored(privateKey string, passphrase []byte, text string) (string, error) { signingKey, err := crypto.NewKeyFromArmored(privateKey) if err != nil { - return "", errors.Wrap(err, "gopenpgp: error in creating key object") + return "", fmt.Errorf("gopenpgp: error in creating key object: %w", err) } unlockedKey, err := signingKey.Unlock(passphrase) if err != nil { - return "", errors.Wrap(err, "gopenpgp: error in unlocking key") + return "", fmt.Errorf("gopenpgp: error in unlocking key: %w", err) } defer unlockedKey.ClearPrivateParams() keyRing, err := crypto.NewKeyRing(unlockedKey) if err != nil { - return "", errors.Wrap(err, "gopenpgp: error in creating keyring") + return "", fmt.Errorf("gopenpgp: error in creating keyring: %w", err) } return SignCleartextMessage(keyRing, text) @@ -35,12 +36,12 @@ func SignCleartextMessageArmored(privateKey string, passphrase []byte, text stri func VerifyCleartextMessageArmored(publicKey, armored string, verifyTime int64) (string, error) { signingKey, err := crypto.NewKeyFromArmored(publicKey) if err != nil { - return "", errors.Wrap(err, "gopenpgp: error in creating key object") + return "", fmt.Errorf("gopenpgp: error in creating key object: %w", err) } verifyKeyRing, err := crypto.NewKeyRing(signingKey) if err != nil { - return "", errors.Wrap(err, "gopenpgp: error in creating key ring") + return "", fmt.Errorf("gopenpgp: error in creating key ring: %w", err) } return VerifyCleartextMessage(verifyKeyRing, armored, verifyTime) @@ -53,7 +54,7 @@ func SignCleartextMessage(keyRing *crypto.KeyRing, text string) (string, error) signature, err := keyRing.SignDetached(message) if err != nil { - return "", errors.Wrap(err, "gopenpgp: error in signing cleartext message") + return "", fmt.Errorf("gopenpgp: error in signing cleartext message: %w", err) } return crypto.NewClearTextMessage(message.GetBinary(), signature.GetBinary()).GetArmored() @@ -65,14 +66,14 @@ func SignCleartextMessage(keyRing *crypto.KeyRing, text string) (string, error) func VerifyCleartextMessage(keyRing *crypto.KeyRing, armored string, verifyTime int64) (string, error) { clearTextMessage, err := crypto.NewClearTextMessageFromArmored(armored) if err != nil { - return "", errors.Wrap(err, "gopengpp: unable to unarmor cleartext message") + return "", fmt.Errorf("gopengpp: unable to unarmor cleartext message: %w", err) } message := crypto.NewPlainMessageFromString(internal.TrimEachLine(clearTextMessage.GetString())) signature := crypto.NewPGPSignature(clearTextMessage.GetBinarySignature()) err = keyRing.VerifyDetached(message, signature, verifyTime) if err != nil { - return "", errors.Wrap(err, "gopengpp: unable to verify cleartext message") + return "", fmt.Errorf("gopengpp: unable to verify cleartext message: %w", err) } return message.GetString(), nil diff --git a/helper/helper.go b/helper/helper.go index 76c1f123..7b8004fe 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -2,8 +2,10 @@ package helper import ( + "errors" + "fmt" + "github.com/ProtonMail/gopenpgp/v2/crypto" - "github.com/pkg/errors" ) // EncryptMessageWithPassword encrypts a string with a passphrase using AES256. @@ -13,11 +15,11 @@ func EncryptMessageWithPassword(password []byte, plaintext string) (ciphertext s var message = crypto.NewPlainMessageFromString(plaintext) if pgpMessage, err = crypto.EncryptMessageWithPassword(message, password); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to encrypt message with password") + return "", fmt.Errorf("gopenpgp: unable to encrypt message with password: %w", err) } if ciphertext, err = pgpMessage.GetArmored(); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to armor ciphertext") + return "", fmt.Errorf("gopenpgp: unable to armor ciphertext: %w", err) } return ciphertext, nil @@ -30,11 +32,11 @@ func DecryptMessageWithPassword(password []byte, ciphertext string) (plaintext s var pgpMessage *crypto.PGPMessage if pgpMessage, err = crypto.NewPGPMessageFromArmored(ciphertext); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to unarmor ciphertext") + return "", fmt.Errorf("gopenpgp: unable to unarmor ciphertext: %w", err) } if message, err = crypto.DecryptMessageWithPassword(pgpMessage, password); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to decrypt message with password") + return "", fmt.Errorf("gopenpgp: unable to decrypt message with password: %w", err) } return message.GetString(), nil @@ -62,24 +64,24 @@ func EncryptSignMessageArmored( } if privateKeyObj, err = crypto.NewKeyFromArmored(privateKey); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to read key") + return "", fmt.Errorf("gopenpgp: unable to read key: %w", err) } if unlockedKeyObj, err = privateKeyObj.Unlock(passphrase); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to unlock key") + return "", fmt.Errorf("gopenpgp: unable to unlock key: %w", err) } defer unlockedKeyObj.ClearPrivateParams() if privateKeyRing, err = crypto.NewKeyRing(unlockedKeyObj); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to create new keyring") + return "", fmt.Errorf("gopenpgp: unable to create new keyring: %w", err) } if pgpMessage, err = publicKeyRing.Encrypt(message, privateKeyRing); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to encrypt message") + return "", fmt.Errorf("gopenpgp: unable to encrypt message: %w", err) } if ciphertext, err = pgpMessage.GetArmored(); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to armor ciphertext") + return "", fmt.Errorf("gopenpgp: unable to armor ciphertext: %w", err) } return ciphertext, nil @@ -114,24 +116,24 @@ func DecryptVerifyMessageArmored( } if privateKeyObj, err = crypto.NewKeyFromArmored(privateKey); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to unarmor private key") + return "", fmt.Errorf("gopenpgp: unable to unarmor private key: %w", err) } if unlockedKeyObj, err = privateKeyObj.Unlock(passphrase); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to unlock private key") + return "", fmt.Errorf("gopenpgp: unable to unlock private key: %w", err) } defer unlockedKeyObj.ClearPrivateParams() if privateKeyRing, err = crypto.NewKeyRing(unlockedKeyObj); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to create new keyring") + return "", fmt.Errorf("gopenpgp: unable to create new keyring: %w", err) } if pgpMessage, err = crypto.NewPGPMessageFromArmored(ciphertext); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to unarmor ciphertext") + return "", fmt.Errorf("gopenpgp: unable to unarmor ciphertext: %w", err) } if message, err = privateKeyRing.Decrypt(pgpMessage, publicKeyRing, crypto.GetUnixTime()); err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to decrypt message") + return "", fmt.Errorf("gopenpgp: unable to decrypt message: %w", err) } return message.GetString(), nil @@ -199,7 +201,7 @@ func encryptSignArmoredDetached( // We armor the ciphertext and signature ciphertextArmored, err = ciphertext.GetArmored() if err != nil { - return "", "", errors.Wrap(err, "gopenpgp: unable to armor the ciphertext") + return "", "", fmt.Errorf("gopenpgp: unable to armor the ciphertext: %w", err) } return ciphertextArmored, encryptedSignatureArmored, nil @@ -219,7 +221,7 @@ func DecryptVerifyArmoredDetached( // Some type casting ciphertext, err := crypto.NewPGPMessageFromArmored(ciphertextArmored) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to unarmor ciphertext") + return nil, fmt.Errorf("gopenpgp: unable to unarmor ciphertext: %w", err) } // We decrypt and verify the encrypted signature @@ -265,7 +267,7 @@ func DecryptVerifyBinaryDetached( // Some type casting ciphertext := crypto.NewPGPMessage(encryptedData) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to parse ciphertext") + return nil, fmt.Errorf("gopenpgp: unable to parse ciphertext: %w", err) } // We decrypt and verify the encrypted signature @@ -315,7 +317,7 @@ func EncryptSessionKey( } encryptedSessionKey, err = publicKeyRing.EncryptSessionKey(sessionKey) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to encrypt sessionKey") + return nil, fmt.Errorf("gopenpgp: unable to encrypt sessionKey: %w", err) } return encryptedSessionKey, nil } @@ -329,24 +331,24 @@ func DecryptSessionKey( ) (sessionKey *crypto.SessionKey, err error) { privateKeyObj, err := crypto.NewKeyFromArmored(privateKey) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to read armored key") + return nil, fmt.Errorf("gopenpgp: unable to read armored key: %w", err) } privateKeyUnlocked, err := privateKeyObj.Unlock(passphrase) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to unlock private key") + return nil, fmt.Errorf("gopenpgp: unable to unlock private key: %w", err) } defer privateKeyUnlocked.ClearPrivateParams() privateKeyRing, err := crypto.NewKeyRing(privateKeyUnlocked) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to create new keyring") + return nil, fmt.Errorf("gopenpgp: unable to create new keyring: %w", err) } sessionKey, err = privateKeyRing.DecryptSessionKey(encryptedSessionKey) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to decrypt session key") + return nil, fmt.Errorf("gopenpgp: unable to decrypt session key: %w", err) } return sessionKey, nil @@ -360,7 +362,7 @@ func encryptMessageArmored(key string, message *crypto.PlainMessage) (string, er ciphertextArmored, err := ciphertext.GetArmored() if err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to armor ciphertext") + return "", fmt.Errorf("gopenpgp: unable to armor ciphertext: %w", err) } return ciphertextArmored, nil @@ -369,7 +371,7 @@ func encryptMessageArmored(key string, message *crypto.PlainMessage) (string, er func decryptMessageArmored(privateKey string, passphrase []byte, ciphertextArmored string) (*crypto.PlainMessage, error) { ciphertext, err := crypto.NewPGPMessageFromArmored(ciphertextArmored) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to parse ciphertext") + return nil, fmt.Errorf("gopenpgp: unable to parse ciphertext: %w", err) } return decryptMessage(privateKey, passphrase, ciphertext) @@ -383,7 +385,7 @@ func encryptMessage(key string, message *crypto.PlainMessage) (*crypto.PGPMessag ciphertext, err := publicKeyRing.Encrypt(message, nil) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to encrypt message") + return nil, fmt.Errorf("gopenpgp: unable to encrypt message: %w", err) } return ciphertext, nil @@ -392,24 +394,24 @@ func encryptMessage(key string, message *crypto.PlainMessage) (*crypto.PGPMessag func decryptMessage(privateKey string, passphrase []byte, ciphertext *crypto.PGPMessage) (*crypto.PlainMessage, error) { privateKeyObj, err := crypto.NewKeyFromArmored(privateKey) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to parse the private key") + return nil, fmt.Errorf("gopenpgp: unable to parse the private key: %w", err) } privateKeyUnlocked, err := privateKeyObj.Unlock(passphrase) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to unlock key") + return nil, fmt.Errorf("gopenpgp: unable to unlock key: %w", err) } defer privateKeyUnlocked.ClearPrivateParams() privateKeyRing, err := crypto.NewKeyRing(privateKeyUnlocked) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to create the private key ring") + return nil, fmt.Errorf("gopenpgp: unable to create the private key ring: %w", err) } message, err := privateKeyRing.Decrypt(ciphertext, nil, 0) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to decrypt message") + return nil, fmt.Errorf("gopenpgp: unable to decrypt message: %w", err) } return message, nil @@ -418,24 +420,24 @@ func decryptMessage(privateKey string, passphrase []byte, ciphertext *crypto.PGP func signDetached(privateKey string, passphrase []byte, message *crypto.PlainMessage) (detachedSignature *crypto.PGPSignature, err error) { privateKeyObj, err := crypto.NewKeyFromArmored(privateKey) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to parse private key") + return nil, fmt.Errorf("gopenpgp: unable to parse private key: %w", err) } privateKeyUnlocked, err := privateKeyObj.Unlock(passphrase) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to unlock key") + return nil, fmt.Errorf("gopenpgp: unable to unlock key: %w", err) } defer privateKeyUnlocked.ClearPrivateParams() privateKeyRing, err := crypto.NewKeyRing(privateKeyUnlocked) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to create new keyring") + return nil, fmt.Errorf("gopenpgp: unable to create new keyring: %w", err) } detachedSignature, err = privateKeyRing.SignDetached(message) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to sign message") + return nil, fmt.Errorf("gopenpgp: unable to sign message: %w", err) } return detachedSignature, nil @@ -446,7 +448,7 @@ func verifyDetachedArmored(publicKey string, message *crypto.PlainMessage, armor // We unarmor the signature if detachedSignature, err = crypto.NewPGPSignatureFromArmored(armoredSignature); err != nil { - return false, errors.Wrap(err, "gopenpgp: unable to unarmor signature") + return false, fmt.Errorf("gopenpgp: unable to unarmor signature: %w", err) } // we verify the signature return verifyDetached(publicKey, message, detachedSignature) @@ -479,19 +481,19 @@ func decryptAttachment( // prepare the private key for decryption if privateKeyObj, err = crypto.NewKeyFromArmored(privateKey); err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to parse private key") + return nil, fmt.Errorf("gopenpgp: unable to parse private key: %w", err) } if unlockedKeyObj, err = privateKeyObj.Unlock(passphrase); err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to unlock private key") + return nil, fmt.Errorf("gopenpgp: unable to unlock private key: %w", err) } defer unlockedKeyObj.ClearPrivateParams() if privateKeyRing, err = crypto.NewKeyRing(unlockedKeyObj); err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to create new keyring") + return nil, fmt.Errorf("gopenpgp: unable to create new keyring: %w", err) } if message, err = privateKeyRing.DecryptAttachment(packets); err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to decrypt attachment") + return nil, fmt.Errorf("gopenpgp: unable to decrypt attachment: %w", err) } return message, nil @@ -500,19 +502,19 @@ func decryptAttachment( func createPublicKeyRing(publicKey string) (*crypto.KeyRing, error) { publicKeyObj, err := crypto.NewKeyFromArmored(publicKey) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to parse public key") + return nil, fmt.Errorf("gopenpgp: unable to parse public key: %w", err) } if publicKeyObj.IsPrivate() { publicKeyObj, err = publicKeyObj.ToPublic() if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to extract public key from private key") + return nil, fmt.Errorf("gopenpgp: unable to extract public key from private key: %w", err) } } publicKeyRing, err := crypto.NewKeyRing(publicKeyObj) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to create new keyring") + return nil, fmt.Errorf("gopenpgp: unable to create new keyring: %w", err) } return publicKeyRing, nil @@ -526,32 +528,32 @@ func encryptSignObjDetached( // We generate the session key sessionKey, err := crypto.GenerateSessionKey() if err != nil { - return nil, "", errors.Wrap(err, "gopenpgp: unable to create new session key") + return nil, "", fmt.Errorf("gopenpgp: unable to create new session key: %w", err) } // We encrypt the message with the session key messageDataPacket, err := sessionKey.Encrypt(message) if err != nil { - return nil, "", errors.Wrap(err, "gopenpgp: unable to encrypt message") + return nil, "", fmt.Errorf("gopenpgp: unable to encrypt message: %w", err) } // We sign the message detachedSignature, err := signDetached(privateKey, passphrase, message) if err != nil { - return nil, "", errors.Wrap(err, "gopenpgp: unable to sign message") + return nil, "", fmt.Errorf("gopenpgp: unable to sign message: %w", err) } // We encrypt the signature with the session key signaturePlaintext := crypto.NewPlainMessage(detachedSignature.GetBinary()) signatureDataPacket, err := sessionKey.Encrypt(signaturePlaintext) if err != nil { - return nil, "", errors.Wrap(err, "gopenpgp: unable to encrypt detached signature") + return nil, "", fmt.Errorf("gopenpgp: unable to encrypt detached signature: %w", err) } // We encrypt the session key keyPacket, err := EncryptSessionKey(publicKey, sessionKey) if err != nil { - return nil, "", errors.Wrap(err, "gopenpgp: unable to encrypt the session key") + return nil, "", fmt.Errorf("gopenpgp: unable to encrypt the session key: %w", err) } // We join the key packets and datapackets @@ -559,7 +561,7 @@ func encryptSignObjDetached( encryptedSignature := crypto.NewPGPSplitMessage(keyPacket, signatureDataPacket) encryptedSignatureArmored, err = encryptedSignature.GetArmored() if err != nil { - return nil, "", errors.Wrap(err, "gopenpgp: unable to armor encrypted signature") + return nil, "", fmt.Errorf("gopenpgp: unable to armor encrypted signature: %w", err) } return ciphertext, encryptedSignatureArmored, nil } @@ -577,7 +579,7 @@ func decryptVerifyObjDetached( encryptedSignature, err := crypto.NewPGPMessageFromArmored(encryptedSignatureArmored) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to parse encrypted signature") + return nil, fmt.Errorf("gopenpgp: unable to parse encrypted signature: %w", err) } // We decrypt the signature diff --git a/helper/key.go b/helper/key.go index 6352545b..d8549ff2 100644 --- a/helper/key.go +++ b/helper/key.go @@ -1,8 +1,9 @@ package helper import ( + "fmt" + "github.com/ProtonMail/gopenpgp/v2/crypto" - "github.com/pkg/errors" ) // UpdatePrivateKeyPassphrase decrypts the given armored privateKey with oldPassphrase, @@ -13,23 +14,23 @@ func UpdatePrivateKeyPassphrase( ) (string, error) { key, err := crypto.NewKeyFromArmored(privateKey) if err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to parse key") + return "", fmt.Errorf("gopenpgp: unable to parse key: %w", err) } unlocked, err := key.Unlock(oldPassphrase) if err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to unlock old key") + return "", fmt.Errorf("gopenpgp: unable to unlock old key: %w", err) } defer unlocked.ClearPrivateParams() locked, err := unlocked.Lock(newPassphrase) if err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to lock new key") + return "", fmt.Errorf("gopenpgp: unable to lock new key: %w", err) } armored, err := locked.Armor() if err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to armor new key") + return "", fmt.Errorf("gopenpgp: unable to armor new key: %w", err) } return armored, nil @@ -41,13 +42,13 @@ func UpdatePrivateKeyPassphrase( func GenerateKey(name, email string, passphrase []byte, keyType string, bits int) (string, error) { key, err := crypto.GenerateKey(name, email, keyType, bits) if err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to generate new key") + return "", fmt.Errorf("gopenpgp: unable to generate new key: %w", err) } defer key.ClearPrivateParams() locked, err := key.Lock(passphrase) if err != nil { - return "", errors.Wrap(err, "gopenpgp: unable to lock new key") + return "", fmt.Errorf("gopenpgp: unable to lock new key: %w", err) } return locked.Armor() @@ -56,7 +57,7 @@ func GenerateKey(name, email string, passphrase []byte, keyType string, bits int func GetSHA256Fingerprints(publicKey string) ([]string, error) { key, err := crypto.NewKeyFromArmored(publicKey) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to parse key") + return nil, fmt.Errorf("gopenpgp: unable to parse key: %w", err) } return key.GetSHA256Fingerprints(), nil diff --git a/helper/mobile.go b/helper/mobile.go index a08d81d9..1af86590 100644 --- a/helper/mobile.go +++ b/helper/mobile.go @@ -2,11 +2,11 @@ package helper import ( "encoding/json" - goerrors "errors" + "errors" + "fmt" "runtime/debug" "github.com/ProtonMail/gopenpgp/v2/crypto" - "github.com/pkg/errors" ) type ExplicitVerifyMessage struct { @@ -72,9 +72,9 @@ func newExplicitVerifyMessage(message *crypto.PlainMessage, err error) (*Explici var explicitVerify *ExplicitVerifyMessage if err != nil { castedErr := &crypto.SignatureVerificationError{} - isType := goerrors.As(err, castedErr) + isType := errors.As(err, castedErr) if !isType { - return nil, errors.Wrap(err, "gopenpgp: unable to decrypt message") + return nil, fmt.Errorf("gopenpgp: unable to decrypt message: %w", err) } explicitVerify = &ExplicitVerifyMessage{ @@ -99,7 +99,7 @@ func DecryptAttachment(keyPacket []byte, dataPacket []byte, keyRing *crypto.KeyR decrypted, err := keyRing.DecryptAttachment(splitMessage) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to decrypt attachment") + return nil, fmt.Errorf("gopenpgp: unable to decrypt attachment: %w", err) } return decrypted, nil } @@ -112,7 +112,7 @@ func EncryptAttachment(plainData []byte, filename string, keyRing *crypto.KeyRin plainMessage := crypto.NewPlainMessageFromFile(plainData, filename, uint32(crypto.GetUnixTime())) decrypted, err := keyRing.EncryptAttachment(plainMessage, "") if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to encrypt attachment") + return nil, fmt.Errorf("gopenpgp: unable to encrypt attachment: %w", err) } return decrypted, nil } @@ -122,7 +122,7 @@ func EncryptAttachment(plainData []byte, filename string, keyRing *crypto.KeyRin func GetJsonSHA256Fingerprints(publicKey string) ([]byte, error) { key, err := crypto.NewKeyFromArmored(publicKey) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to parse key") + return nil, fmt.Errorf("gopenpgp: unable to parse key: %w", err) } return json.Marshal(key.GetSHA256Fingerprints()) diff --git a/helper/mobile_stream.go b/helper/mobile_stream.go index 9fa927c4..9e0a7808 100644 --- a/helper/mobile_stream.go +++ b/helper/mobile_stream.go @@ -2,11 +2,12 @@ package helper import ( "crypto/sha256" + "errors" + "fmt" "hash" "io" "github.com/ProtonMail/gopenpgp/v2/crypto" - "github.com/pkg/errors" ) // Mobile2GoWriter is used to wrap a writer in the mobile app runtime, @@ -52,7 +53,7 @@ func (w *Mobile2GoWriterWithSHA256) Write(b []byte) (n int, err error) { for hashedTotal < n { hashed, err := w.sha256.Write(bufferCopy[hashedTotal:n]) if err != nil { - return 0, errors.Wrap(err, "gopenpgp: couldn't hash encrypted data") + return 0, fmt.Errorf("gopenpgp: couldn't hash encrypted data: %w", err) } hashedTotal += hashed } @@ -108,7 +109,7 @@ func NewMobile2GoReader(reader MobileReader) *Mobile2GoReader { func (r *Mobile2GoReader) Read(b []byte) (n int, err error) { result, err := r.reader.Read(len(b)) if err != nil { - return 0, errors.Wrap(err, "gopenpgp: couldn't read from mobile reader") + return 0, fmt.Errorf("gopenpgp: couldn't read from mobile reader: %w", err) } n = result.N if n > 0 { diff --git a/helper/sign_detached.go b/helper/sign_detached.go index 76c8ff9a..05bf0ee2 100644 --- a/helper/sign_detached.go +++ b/helper/sign_detached.go @@ -4,8 +4,9 @@ package helper import ( + "fmt" + "github.com/ProtonMail/gopenpgp/v2/crypto" - "github.com/pkg/errors" ) // EncryptSignAttachment encrypts an attachment using a detached signature, given a publicKey, a privateKey @@ -26,24 +27,24 @@ func EncryptSignAttachment( } if privateKeyObj, err = crypto.NewKeyFromArmored(privateKey); err != nil { - return nil, nil, nil, errors.Wrap(err, "gopenpgp: unable to parse private key") + return nil, nil, nil, fmt.Errorf("gopenpgp: unable to parse private key: %w", err) } if unlockedKeyObj, err = privateKeyObj.Unlock(passphrase); err != nil { - return nil, nil, nil, errors.Wrap(err, "gopenpgp: unable to unlock key") + return nil, nil, nil, fmt.Errorf("gopenpgp: unable to unlock key: %w", err) } defer unlockedKeyObj.ClearPrivateParams() if privateKeyRing, err = crypto.NewKeyRing(unlockedKeyObj); err != nil { - return nil, nil, nil, errors.Wrap(err, "gopenpgp: unable to create private keyring") + return nil, nil, nil, fmt.Errorf("gopenpgp: unable to create private keyring: %w", err) } if packets, err = publicKeyRing.EncryptAttachment(binMessage, ""); err != nil { - return nil, nil, nil, errors.Wrap(err, "gopenpgp: unable to encrypt attachment") + return nil, nil, nil, fmt.Errorf("gopenpgp: unable to encrypt attachment: %w", err) } if signatureObj, err = privateKeyRing.SignDetached(binMessage); err != nil { - return nil, nil, nil, errors.Wrap(err, "gopenpgp: unable to sign attachment") + return nil, nil, nil, fmt.Errorf("gopenpgp: unable to sign attachment: %w", err) } return packets.GetBinaryKeyPacket(), packets.GetBinaryDataPacket(), signatureObj.GetBinary(), nil diff --git a/internal/armor.go b/internal/armor.go index 0cf8f7db..99ac4aba 100644 --- a/internal/armor.go +++ b/internal/armor.go @@ -1,10 +1,10 @@ package internal import ( + "fmt" "strings" "github.com/ProtonMail/go-crypto/openpgp/armor" - "github.com/pkg/errors" ) // Unarmor unarmors an armored string. @@ -12,7 +12,7 @@ func Unarmor(input string) (*armor.Block, error) { io := strings.NewReader(input) b, err := armor.Decode(io) if err != nil { - return nil, errors.Wrap(err, "gopenpgp: unable to unarmor") + return nil, fmt.Errorf("gopenpgp: unable to unarmor: %w", err) } return b, nil }