Skip to content

Commit 75b8def

Browse files
author
Jacob H. Haven
committed
Merge pull request #35 from cloudflare/jacob/PrivateKey-DecrypterOpts
Update client.PrivateKey#Decrypt to allow nil DecrypterOpts
2 parents 8301df9 + ea23fd7 commit 75b8def

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

client/keys.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,21 @@ func (key *PrivateKey) Sign(r io.Reader, msg []byte, opts crypto.SignerOpts) ([]
122122

123123
// Decrypt implements the crypto.Decrypter operation for the given key.
124124
func (key *PrivateKey) Decrypt(rand io.Reader, msg []byte, opts crypto.DecrypterOpts) ([]byte, error) {
125-
switch opts := opts.(type) {
126-
case *rsa.PKCS1v15DecryptOptions:
127-
ptxt, decyptErr := key.execute(gokeyless.OpRSADecrypt, msg)
125+
opts1v15, ok := opts.(*rsa.PKCS1v15DecryptOptions)
126+
if opts != nil && !ok {
127+
return nil, errors.New("invalid options for Decrypt")
128+
}
129+
130+
ptxt, err := key.execute(gokeyless.OpRSADecrypt, msg)
131+
if err != nil {
132+
return nil, err
133+
}
128134

135+
if ok {
129136
// If opts.SessionKeyLen is set, we must perform a variation of
130137
// rsa.DecryptPKCS1v15SessionKey to ensure the entire operation
131138
// is performed in constant time regardless of padding errors.
132-
if l := opts.SessionKeyLen; l > 0 {
139+
if l := opts1v15.SessionKeyLen; l > 0 {
133140
plaintext := make([]byte, l)
134141
if _, err := io.ReadFull(rand, plaintext); err != nil {
135142
return nil, err
@@ -140,9 +147,6 @@ func (key *PrivateKey) Decrypt(rand io.Reader, msg []byte, opts crypto.Decrypter
140147
subtle.ConstantTimeCopy(valid, plaintext[:l2], ptxt[:l2])
141148
return plaintext, nil
142149
}
143-
// Otherwise, we can just return the error like rsa.DecryptPKCS1v15.
144-
return ptxt, decyptErr
145-
default:
146-
return nil, errors.New("invalid options for Decrypt")
147150
}
151+
return ptxt, nil
148152
}

0 commit comments

Comments
 (0)