passforios-gopenpgp/crypto/sign_detached.go

127 lines
3.7 KiB
Go
Raw Normal View History

package crypto
2018-06-04 16:05:14 -07:00
import (
"bytes"
"errors"
"strings"
"time"
2018-11-01 17:03:43 +01:00
"github.com/ProtonMail/go-pm-crypto/internal"
2018-06-04 16:05:14 -07:00
"golang.org/x/crypto/openpgp"
errors2 "golang.org/x/crypto/openpgp/errors"
"golang.org/x/crypto/openpgp/packet"
2018-07-31 01:13:00 +02:00
"io"
2018-06-04 16:05:14 -07:00
)
2019-01-11 00:23:00 +01:00
// Use: ios/android only
2018-06-04 16:05:14 -07:00
// SignTextDetached sign detached text type
func (pm *PmCrypto) SignTextDetached(plainText string, privateKey *KeyRing, passphrase string, trim bool) (string, error) {
2018-06-04 16:05:14 -07:00
//sign with 0x01 text
2018-06-05 15:26:55 -07:00
if trim {
plainText = internal.TrimNewlines(plainText)
2018-06-05 15:26:55 -07:00
}
signEntity := privateKey.GetSigningEntity(passphrase)
2018-06-04 16:05:14 -07:00
if signEntity == nil {
2018-06-04 17:50:26 -07:00
return "", errors.New("cannot sign message, signer key is not unlocked")
2018-06-04 16:05:14 -07:00
}
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pm.getTimeGenerator()}
2018-06-04 16:05:14 -07:00
att := strings.NewReader(plainText)
var outBuf bytes.Buffer
//SignText
if err := openpgp.ArmoredDetachSignText(&outBuf, signEntity, att, config); err != nil {
2018-06-04 16:05:14 -07:00
return "", err
}
return outBuf.String(), nil
}
2019-01-11 00:23:00 +01:00
// Use: ios/android only
// Sign detached bin data using string key
func (pm *PmCrypto) SignBinDetached(plainData []byte, privateKey *KeyRing, passphrase string) (string, error) {
2018-06-04 16:05:14 -07:00
//sign with 0x00
signEntity := privateKey.GetSigningEntity(passphrase)
2018-06-04 16:05:14 -07:00
if signEntity == nil {
return "", errors.New("cannot sign message, singer key is not unlocked")
}
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pm.getTimeGenerator()}
2018-06-04 16:05:14 -07:00
att := bytes.NewReader(plainData)
var outBuf bytes.Buffer
//sign bin
if err := openpgp.ArmoredDetachSign(&outBuf, signEntity, att, config); err != nil {
2018-06-04 16:05:14 -07:00
return "", err
}
return outBuf.String(), nil
}
2019-01-11 00:23:00 +01:00
// Use: ios/android only
// Verify detached text - check if signature is valid using a given publicKey in binary format
func (pm *PmCrypto) VerifyTextSignDetachedBinKey(signature string, plainText string, publicKey *KeyRing, verifyTime int64) (bool, error) {
2018-06-04 16:05:14 -07:00
plainText = internal.TrimNewlines(plainText)
2018-06-04 16:05:14 -07:00
origText := bytes.NewReader(bytes.NewBufferString(plainText).Bytes())
return verifySignature(publicKey.entities, origText, signature, verifyTime)
}
func verifySignature(pubKeyEntries openpgp.EntityList, origText *bytes.Reader, signature string, verifyTime int64) (bool, error) {
config := &packet.Config{}
if verifyTime == 0 {
config.Time = func() time.Time {
return time.Unix(0, 0)
}
} else {
2018-06-04 16:05:14 -07:00
config.Time = func() time.Time {
return time.Unix(verifyTime+internal.CreationTimeOffset, 0)
2018-06-04 16:05:14 -07:00
}
}
signatureReader := strings.NewReader(signature)
2018-06-04 16:05:14 -07:00
signer, err := openpgp.CheckArmoredDetachedSignature(pubKeyEntries, origText, signatureReader, config)
if err == errors2.ErrSignatureExpired && signer != nil {
if verifyTime > 0 {
// Maybe the creation time offset pushed it over the edge
// Retry with the actual verification time
config.Time = func() time.Time {
return time.Unix(verifyTime, 0)
}
2018-07-31 01:13:00 +02:00
signatureReader.Seek(0, io.SeekStart)
signer, err = openpgp.CheckArmoredDetachedSignature(pubKeyEntries, origText, signatureReader, config)
} else {
// verifyTime = 0: time check disabled, everything is okay
err = nil
}
}
2018-06-04 16:05:14 -07:00
if err != nil {
return false, err
}
if signer == nil {
return false, errors.New("signer is empty")
}
// if signer.PrimaryKey.KeyId != signed.PrimaryKey.KeyId {
// // t.Errorf("wrong signer got:%x want:%x", signer.PrimaryKey.KeyId, 0)
// return false, errors.New("signer is nil")
// }
return true, nil
}
2019-01-11 00:23:00 +01:00
// Use: ios/android only
// Verify detached text in binary format - check if signature is valid using a given publicKey in binary format
func (pm *PmCrypto) VerifyBinSignDetachedBinKey(signature string, plainData []byte, publicKey *KeyRing, verifyTime int64) (bool, error) {
2018-06-04 16:05:14 -07:00
origText := bytes.NewReader(plainData)
return verifySignature(publicKey.entities, origText, signature, verifyTime)
2018-06-04 16:05:14 -07:00
}