Skip to content

Commit ee4e19a

Browse files
Iceberrhatdan
authored andcommitted
remove github.com/pkg/errors
Signed-off-by: Iceber Gu <[email protected]>
1 parent e0cec6f commit ee4e19a

24 files changed

+123
-127
lines changed

blockcipher/blockcipher.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
package blockcipher
1818

1919
import (
20+
"errors"
21+
"fmt"
2022
"io"
2123

2224
"github.com/opencontainers/go-digest"
23-
"github.com/pkg/errors"
2425
)
2526

2627
// LayerCipherType is the ciphertype as specified in the layer metadata
@@ -129,7 +130,7 @@ func (h *LayerBlockCipherHandler) Encrypt(plainDataReader io.Reader, typ LayerCi
129130
}
130131
return encDataReader, fin, err
131132
}
132-
return nil, nil, errors.Errorf("unsupported cipher type: %s", typ)
133+
return nil, nil, fmt.Errorf("unsupported cipher type: %s", typ)
133134
}
134135

135136
// Decrypt is the handler for the layer decryption routine
@@ -141,7 +142,7 @@ func (h *LayerBlockCipherHandler) Decrypt(encDataReader io.Reader, opt LayerBloc
141142
if c, ok := h.cipherMap[typ]; ok {
142143
return c.Decrypt(encDataReader, opt)
143144
}
144-
return nil, LayerBlockCipherOptions{}, errors.Errorf("unsupported cipher type: %s", typ)
145+
return nil, LayerBlockCipherOptions{}, fmt.Errorf("unsupported cipher type: %s", typ)
145146
}
146147

147148
// NewLayerBlockCipherHandler returns a new default handler
@@ -153,7 +154,7 @@ func NewLayerBlockCipherHandler() (*LayerBlockCipherHandler, error) {
153154
var err error
154155
h.cipherMap[AES256CTR], err = NewAESCTRLayerBlockCipher(256)
155156
if err != nil {
156-
return nil, errors.Wrap(err, "unable to set up Cipher AES-256-CTR")
157+
return nil, fmt.Errorf("unable to set up Cipher AES-256-CTR: %w", err)
157158
}
158159

159160
return &h, nil

blockcipher/blockcipher_aes_ctr.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import (
2222
"crypto/hmac"
2323
"crypto/rand"
2424
"crypto/sha256"
25+
"errors"
2526
"fmt"
2627
"hash"
2728
"io"
2829

2930
"github.com/containers/ocicrypt/utils"
30-
"github.com/pkg/errors"
3131
)
3232

3333
// AESCTRLayerBlockCipher implements the AES CTR stream cipher
@@ -74,7 +74,7 @@ func (r *aesctrcryptor) Read(p []byte) (int, error) {
7474

7575
if !r.bc.encrypt {
7676
if _, err := r.bc.hmac.Write(p[:o]); err != nil {
77-
r.bc.err = errors.Wrapf(err, "could not write to hmac")
77+
r.bc.err = fmt.Errorf("could not write to hmac: %w", err)
7878
return 0, r.bc.err
7979
}
8080

@@ -92,7 +92,7 @@ func (r *aesctrcryptor) Read(p []byte) (int, error) {
9292

9393
if r.bc.encrypt {
9494
if _, err := r.bc.hmac.Write(p[:o]); err != nil {
95-
r.bc.err = errors.Wrapf(err, "could not write to hmac")
95+
r.bc.err = fmt.Errorf("could not write to hmac: %w", err)
9696
return 0, r.bc.err
9797
}
9898

@@ -120,13 +120,13 @@ func (bc *AESCTRLayerBlockCipher) init(encrypt bool, reader io.Reader, opts Laye
120120
if !ok {
121121
nonce = make([]byte, aes.BlockSize)
122122
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
123-
return LayerBlockCipherOptions{}, errors.Wrap(err, "unable to generate random nonce")
123+
return LayerBlockCipherOptions{}, fmt.Errorf("unable to generate random nonce: %w", err)
124124
}
125125
}
126126

127127
block, err := aes.NewCipher(key)
128128
if err != nil {
129-
return LayerBlockCipherOptions{}, errors.Wrap(err, "aes.NewCipher failed")
129+
return LayerBlockCipherOptions{}, fmt.Errorf("aes.NewCipher failed: %w", err)
130130
}
131131

132132
bc.reader = reader

config/constructors.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
package config
1818

1919
import (
20+
"errors"
21+
"fmt"
2022
"strings"
2123

2224
"github.com/containers/ocicrypt/crypto/pkcs11"
23-
"github.com/pkg/errors"
2425
"gopkg.in/yaml.v3"
2526
)
2627

@@ -85,7 +86,7 @@ func EncryptWithPkcs11(pkcs11Config *pkcs11.Pkcs11Config, pkcs11Pubkeys, pkcs11Y
8586
}
8687
p11confYaml, err := yaml.Marshal(pkcs11Config)
8788
if err != nil {
88-
return CryptoConfig{}, errors.Wrapf(err, "Could not marshal Pkcs11Config to Yaml")
89+
return CryptoConfig{}, fmt.Errorf("Could not marshal Pkcs11Config to Yaml: %w", err)
8990
}
9091

9192
dc = DecryptConfig{
@@ -223,7 +224,7 @@ func DecryptWithGpgPrivKeys(gpgPrivKeys, gpgPrivKeysPwds [][]byte) (CryptoConfig
223224
func DecryptWithPkcs11Yaml(pkcs11Config *pkcs11.Pkcs11Config, pkcs11Yamls [][]byte) (CryptoConfig, error) {
224225
p11confYaml, err := yaml.Marshal(pkcs11Config)
225226
if err != nil {
226-
return CryptoConfig{}, errors.Wrapf(err, "Could not marshal Pkcs11Config to Yaml")
227+
return CryptoConfig{}, fmt.Errorf("Could not marshal Pkcs11Config to Yaml: %w", err)
227228
}
228229

229230
dc := DecryptConfig{

config/keyprovider-config/config.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ package config
1818

1919
import (
2020
"encoding/json"
21+
"fmt"
2122
"os"
22-
23-
"github.com/pkg/errors"
2423
)
2524

2625
// Command describes the structure of command, it consist of path and args, where path defines the location of
@@ -72,7 +71,7 @@ func GetConfiguration() (*OcicryptConfig, error) {
7271
if len(filename) > 0 {
7372
ic, err = parseConfigFile(filename)
7473
if err != nil {
75-
return nil, errors.Wrap(err, "Error while parsing keyprovider config file")
74+
return nil, fmt.Errorf("Error while parsing keyprovider config file: %w", err)
7675
}
7776
} else {
7877
return nil, nil

config/pkcs11config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package pkcs11config
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"os"
2223
"path"
2324

2425
"github.com/containers/ocicrypt/crypto/pkcs11"
25-
"github.com/pkg/errors"
2626
"gopkg.in/yaml.v3"
2727
)
2828

crypto/pkcs11/common.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package pkcs11
1616
import (
1717
"fmt"
1818

19-
"github.com/pkg/errors"
2019
pkcs11uri "github.com/stefanberger/go-pkcs11uri"
2120
"gopkg.in/yaml.v3"
2221
)
@@ -43,7 +42,7 @@ func ParsePkcs11Uri(uri string) (*pkcs11uri.Pkcs11URI, error) {
4342
p11uri := pkcs11uri.New()
4443
err := p11uri.Parse(uri)
4544
if err != nil {
46-
return nil, errors.Wrapf(err, "Could not parse Pkcs11URI from file")
45+
return nil, fmt.Errorf("Could not parse Pkcs11URI from file: %w", err)
4746
}
4847
return p11uri, err
4948
}
@@ -58,7 +57,7 @@ func ParsePkcs11KeyFile(yamlstr []byte) (*Pkcs11KeyFileObject, error) {
5857

5958
err := yaml.Unmarshal(yamlstr, &p11keyfile)
6059
if err != nil {
61-
return nil, errors.Wrapf(err, "Could not unmarshal pkcs11 keyfile")
60+
return nil, fmt.Errorf("Could not unmarshal pkcs11 keyfile: %w", err)
6261
}
6362

6463
p11uri, err := ParsePkcs11Uri(p11keyfile.Pkcs11.Uri)
@@ -129,7 +128,7 @@ func ParsePkcs11ConfigFile(yamlstr []byte) (*Pkcs11Config, error) {
129128

130129
err := yaml.Unmarshal(yamlstr, &p11conf)
131130
if err != nil {
132-
return &p11conf, errors.Wrapf(err, "Could not parse Pkcs11Config")
131+
return &p11conf, fmt.Errorf("Could not parse Pkcs11Config: %w", err)
133132
}
134133
return &p11conf, nil
135134
}

crypto/pkcs11/pkcs11helpers.go

+25-25
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"crypto/sha256"
2727
"encoding/base64"
2828
"encoding/json"
29+
"errors"
2930
"fmt"
3031
"hash"
3132
"net/url"
@@ -34,7 +35,6 @@ import (
3435
"strings"
3536

3637
"github.com/miekg/pkcs11"
37-
"github.com/pkg/errors"
3838
pkcs11uri "github.com/stefanberger/go-pkcs11uri"
3939
)
4040

@@ -77,11 +77,11 @@ func rsaPublicEncryptOAEP(pubKey *rsa.PublicKey, plaintext []byte) ([]byte, stri
7777
hashfunc = sha256.New()
7878
hashalg = "sha256"
7979
default:
80-
return nil, "", errors.Errorf("Unsupported OAEP hash '%s'", oaephash)
80+
return nil, "", fmt.Errorf("Unsupported OAEP hash '%s'", oaephash)
8181
}
8282
ciphertext, err := rsa.EncryptOAEP(hashfunc, rand.Reader, pubKey, plaintext, OAEPLabel)
8383
if err != nil {
84-
return nil, "", errors.Wrapf(err, "rss.EncryptOAEP failed")
84+
return nil, "", fmt.Errorf("rss.EncryptOAEP failed: %w", err)
8585
}
8686

8787
return ciphertext, hashalg, nil
@@ -105,7 +105,7 @@ func pkcs11UriGetLoginParameters(p11uri *pkcs11uri.Pkcs11URI, privateKeyOperatio
105105

106106
module, err := p11uri.GetModule()
107107
if err != nil {
108-
return "", "", 0, errors.Wrap(err, "No module available in pkcs11 URI")
108+
return "", "", 0, fmt.Errorf("No module available in pkcs11 URI: %w", err)
109109
}
110110

111111
slotid := int64(-1)
@@ -114,7 +114,7 @@ func pkcs11UriGetLoginParameters(p11uri *pkcs11uri.Pkcs11URI, privateKeyOperatio
114114
if ok {
115115
slotid, err = strconv.ParseInt(slot, 10, 64)
116116
if err != nil {
117-
return "", "", 0, errors.Wrap(err, "slot-id is not a valid number")
117+
return "", "", 0, fmt.Errorf("slot-id is not a valid number: %w", err)
118118
}
119119
if slotid < 0 {
120120
return "", "", 0, fmt.Errorf("slot-id is a negative number")
@@ -141,13 +141,13 @@ func pkcs11UriGetKeyIdAndLabel(p11uri *pkcs11uri.Pkcs11URI) (string, string, err
141141
func pkcs11OpenSession(p11ctx *pkcs11.Ctx, slotid uint, pin string) (session pkcs11.SessionHandle, err error) {
142142
session, err = p11ctx.OpenSession(slotid, pkcs11.CKF_SERIAL_SESSION|pkcs11.CKF_RW_SESSION)
143143
if err != nil {
144-
return 0, errors.Wrapf(err, "OpenSession to slot %d failed", slotid)
144+
return 0, fmt.Errorf("OpenSession to slot %d failed: %w", slotid, err)
145145
}
146146
if len(pin) > 0 {
147147
err = p11ctx.Login(session, pkcs11.CKU_USER, pin)
148148
if err != nil {
149149
_ = p11ctx.CloseSession(session)
150-
return 0, errors.Wrap(err, "Could not login to device")
150+
return 0, fmt.Errorf("Could not login to device: %w", err)
151151
}
152152
}
153153
return session, nil
@@ -171,7 +171,7 @@ func pkcs11UriLogin(p11uri *pkcs11uri.Pkcs11URI, privateKeyOperation bool) (ctx
171171
if err != nil {
172172
p11Err := err.(pkcs11.Error)
173173
if p11Err != pkcs11.CKR_CRYPTOKI_ALREADY_INITIALIZED {
174-
return nil, 0, errors.Wrap(err, "Initialize failed")
174+
return nil, 0, fmt.Errorf("Initialize failed: %w", err)
175175
}
176176
}
177177

@@ -182,7 +182,7 @@ func pkcs11UriLogin(p11uri *pkcs11uri.Pkcs11URI, privateKeyOperation bool) (ctx
182182

183183
slots, err := p11ctx.GetSlotList(true)
184184
if err != nil {
185-
return nil, 0, errors.Wrap(err, "GetSlotList failed")
185+
return nil, 0, fmt.Errorf("GetSlotList failed: %w", err)
186186
}
187187

188188
tokenlabel, ok := p11uri.GetPathAttribute("token", false)
@@ -234,24 +234,24 @@ func findObject(p11ctx *pkcs11.Ctx, session pkcs11.SessionHandle, class uint, ke
234234
}
235235

236236
if err := p11ctx.FindObjectsInit(session, template); err != nil {
237-
return 0, errors.Wrap(err, "FindObjectsInit failed")
237+
return 0, fmt.Errorf("FindObjectsInit failed: %w", err)
238238
}
239239

240240
obj, _, err := p11ctx.FindObjects(session, 100)
241241
if err != nil {
242-
return 0, errors.Wrap(err, "FindObjects failed")
242+
return 0, fmt.Errorf("FindObjects failed: %w", err)
243243
}
244244

245245
if err := p11ctx.FindObjectsFinal(session); err != nil {
246-
return 0, errors.Wrap(err, "FindObjectsFinal failed")
246+
return 0, fmt.Errorf("FindObjectsFinal failed: %w", err)
247247
}
248248
if len(obj) > 1 {
249-
return 0, errors.Errorf("There are too many (=%d) keys with %s", len(obj), msg)
249+
return 0, fmt.Errorf("There are too many (=%d) keys with %s", len(obj), msg)
250250
} else if len(obj) == 1 {
251251
return obj[0], nil
252252
}
253253

254-
return 0, errors.Errorf("Could not find any object with %s", msg)
254+
return 0, fmt.Errorf("Could not find any object with %s", msg)
255255
}
256256

257257
// publicEncryptOAEP uses a public key described by a pkcs11 URI to OAEP encrypt the given plaintext
@@ -291,17 +291,17 @@ func publicEncryptOAEP(pubKey *Pkcs11KeyFileObject, plaintext []byte) ([]byte, s
291291
oaep = OAEPSha256Params
292292
hashalg = "sha256"
293293
default:
294-
return nil, "", errors.Errorf("Unsupported OAEP hash '%s'", oaephash)
294+
return nil, "", fmt.Errorf("Unsupported OAEP hash '%s'", oaephash)
295295
}
296296

297297
err = p11ctx.EncryptInit(session, []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_RSA_PKCS_OAEP, oaep)}, p11PubKey)
298298
if err != nil {
299-
return nil, "", errors.Wrap(err, "EncryptInit error")
299+
return nil, "", fmt.Errorf("EncryptInit error: %w", err)
300300
}
301301

302302
ciphertext, err := p11ctx.Encrypt(session, plaintext)
303303
if err != nil {
304-
return nil, "", errors.Wrap(err, "Encrypt failed")
304+
return nil, "", fmt.Errorf("Encrypt failed: %w", err)
305305
}
306306
return ciphertext, hashalg, nil
307307
}
@@ -339,16 +339,16 @@ func privateDecryptOAEP(privKeyObj *Pkcs11KeyFileObject, ciphertext []byte, hash
339339
case "sha256":
340340
oaep = OAEPSha256Params
341341
default:
342-
return nil, errors.Errorf("Unsupported hash algorithm '%s' for decryption", hashalg)
342+
return nil, fmt.Errorf("Unsupported hash algorithm '%s' for decryption", hashalg)
343343
}
344344

345345
err = p11ctx.DecryptInit(session, []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_RSA_PKCS_OAEP, oaep)}, p11PrivKey)
346346
if err != nil {
347-
return nil, errors.Wrapf(err, "DecryptInit failed")
347+
return nil, fmt.Errorf("DecryptInit failed: %w", err)
348348
}
349349
plaintext, err := p11ctx.Decrypt(session, ciphertext)
350350
if err != nil {
351-
return nil, errors.Wrapf(err, "Decrypt failed")
351+
return nil, fmt.Errorf("Decrypt failed: %w", err)
352352
}
353353
return plaintext, err
354354
}
@@ -403,7 +403,7 @@ func EncryptMultiple(pubKeys []interface{}, data []byte) ([]byte, error) {
403403
case *Pkcs11KeyFileObject:
404404
ciphertext, hashalg, err = publicEncryptOAEP(pkey, data)
405405
default:
406-
err = errors.Errorf("Unsupported key object type for pkcs11 public key")
406+
err = fmt.Errorf("Unsupported key object type for pkcs11 public key")
407407
}
408408
if err != nil {
409409
return nil, err
@@ -442,13 +442,13 @@ func Decrypt(privKeyObjs []*Pkcs11KeyFileObject, pkcs11blobstr []byte) ([]byte,
442442
pkcs11blob := Pkcs11Blob{}
443443
err := json.Unmarshal(pkcs11blobstr, &pkcs11blob)
444444
if err != nil {
445-
return nil, errors.Wrapf(err, "Could not parse Pkcs11Blob")
445+
return nil, fmt.Errorf("Could not parse Pkcs11Blob: %w", err)
446446
}
447447
switch pkcs11blob.Version {
448448
case 0:
449449
// latest supported version
450450
default:
451-
return nil, errors.Errorf("found Pkcs11Blob with version %d but maximum supported version is 0", pkcs11blob.Version)
451+
return nil, fmt.Errorf("found Pkcs11Blob with version %d but maximum supported version is 0", pkcs11blob.Version)
452452
}
453453
// since we do trial and error, collect all encountered errors
454454
errs := ""
@@ -458,7 +458,7 @@ func Decrypt(privKeyObjs []*Pkcs11KeyFileObject, pkcs11blobstr []byte) ([]byte,
458458
case 0:
459459
// last supported version
460460
default:
461-
return nil, errors.Errorf("found Pkcs11Recipient with version %d but maximum supported version is 0", recipient.Version)
461+
return nil, fmt.Errorf("found Pkcs11Recipient with version %d but maximum supported version is 0", recipient.Version)
462462
}
463463

464464
ciphertext, err := base64.StdEncoding.DecodeString(recipient.Blob)
@@ -481,5 +481,5 @@ func Decrypt(privKeyObjs []*Pkcs11KeyFileObject, pkcs11blobstr []byte) ([]byte,
481481
}
482482
}
483483

484-
return nil, errors.Errorf("Could not find a pkcs11 key for decryption:\n%s", errs)
484+
return nil, fmt.Errorf("Could not find a pkcs11 key for decryption:\n%s", errs)
485485
}

0 commit comments

Comments
 (0)