2018-09-11 11:09:28 +02:00
|
|
|
package crypto
|
2018-06-04 16:05:14 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"errors"
|
2019-05-14 14:42:38 +00:00
|
|
|
"io"
|
2018-06-04 16:05:14 -07:00
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2019-05-13 14:07:18 +02:00
|
|
|
"github.com/ProtonMail/gopenpgp/internal"
|
2019-05-14 14:42:38 +00:00
|
|
|
|
2018-06-04 16:05:14 -07:00
|
|
|
"golang.org/x/crypto/openpgp"
|
2019-05-14 14:42:38 +00:00
|
|
|
errorsPGP "golang.org/x/crypto/openpgp/errors"
|
2018-09-20 15:20:45 +02:00
|
|
|
"golang.org/x/crypto/openpgp/packet"
|
2018-06-04 16:05:14 -07:00
|
|
|
)
|
|
|
|
|
|
2019-05-15 14:36:04 +02:00
|
|
|
// SignTextDetached creates an armored detached signature of a given string.
|
2019-05-15 13:48:47 +02:00
|
|
|
func (kr *KeyRing) SignTextDetached(plainText string, passphrase string, trimNewlines bool) (string, error) {
|
|
|
|
|
signEntity, err := kr.GetSigningEntity(passphrase)
|
2019-05-14 14:42:38 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
2018-06-04 16:05:14 -07:00
|
|
|
}
|
|
|
|
|
|
2019-05-14 18:05:01 +02:00
|
|
|
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pgp.getTimeGenerator()}
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-15 13:48:47 +02:00
|
|
|
if trimNewlines {
|
|
|
|
|
plainText = internal.TrimNewlines(plainText)
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-04 16:05:14 -07:00
|
|
|
att := strings.NewReader(plainText)
|
|
|
|
|
|
|
|
|
|
var outBuf bytes.Buffer
|
|
|
|
|
//SignText
|
2018-11-09 13:03:46 +01:00
|
|
|
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-05-15 14:36:04 +02:00
|
|
|
// SignBinDetached creates an armored detached signature of binary data.
|
2019-05-15 13:48:47 +02:00
|
|
|
func (kr *KeyRing) SignBinDetached(plainData []byte, passphrase string) (string, error) {
|
2018-06-04 16:05:14 -07:00
|
|
|
//sign with 0x00
|
2019-05-15 13:48:47 +02:00
|
|
|
signEntity, err := kr.GetSigningEntity(passphrase)
|
2019-05-14 14:42:38 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
2018-06-04 16:05:14 -07:00
|
|
|
}
|
|
|
|
|
|
2019-05-14 18:05:01 +02:00
|
|
|
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pgp.getTimeGenerator()}
|
2018-06-04 16:05:14 -07:00
|
|
|
|
|
|
|
|
att := bytes.NewReader(plainData)
|
|
|
|
|
|
|
|
|
|
var outBuf bytes.Buffer
|
|
|
|
|
//sign bin
|
2018-11-09 13:03:46 +01:00
|
|
|
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-05-15 14:36:04 +02:00
|
|
|
// VerifyTextDetachedSig verifies an armored detached signature given the plaintext as a string.
|
2019-05-15 13:48:47 +02:00
|
|
|
func (kr *KeyRing) VerifyTextDetachedSig(
|
|
|
|
|
signature string, plainText string, verifyTime int64, trimNewlines bool,
|
2019-05-14 14:42:38 +00:00
|
|
|
) (bool, error) {
|
2019-05-15 13:48:47 +02:00
|
|
|
if trimNewlines {
|
|
|
|
|
plainText = internal.TrimNewlines(plainText)
|
|
|
|
|
}
|
2018-06-04 16:05:14 -07:00
|
|
|
origText := bytes.NewReader(bytes.NewBufferString(plainText).Bytes())
|
2018-07-31 01:00:45 +02:00
|
|
|
|
2019-05-15 13:48:47 +02:00
|
|
|
return verifySignature(kr.GetEntities(), origText, signature, verifyTime)
|
2018-07-31 01:00:45 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-15 14:36:04 +02:00
|
|
|
// VerifyBinDetachedSig verifies an armored detached signature given the plaintext as binary data.
|
2019-05-15 13:48:47 +02:00
|
|
|
func (kr *KeyRing) VerifyBinDetachedSig(signature string, plainData []byte, verifyTime int64) (bool, error) {
|
|
|
|
|
origText := bytes.NewReader(plainData)
|
|
|
|
|
|
|
|
|
|
return verifySignature(kr.GetEntities(), origText, signature, verifyTime)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Internal
|
2019-05-14 14:42:38 +00:00
|
|
|
func verifySignature(
|
|
|
|
|
pubKeyEntries openpgp.EntityList, origText *bytes.Reader,
|
|
|
|
|
signature string, verifyTime int64,
|
|
|
|
|
) (bool, error) {
|
2018-07-31 01:00:45 +02:00
|
|
|
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 {
|
2019-05-14 08:07:49 +00:00
|
|
|
return time.Unix(verifyTime+internal.CreationTimeOffset, 0)
|
2018-06-04 16:05:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
2018-07-31 01:00:45 +02:00
|
|
|
signatureReader := strings.NewReader(signature)
|
|
|
|
|
|
2018-06-04 16:05:14 -07:00
|
|
|
signer, err := openpgp.CheckArmoredDetachedSignature(pubKeyEntries, origText, signatureReader, config)
|
2018-07-31 01:00:45 +02:00
|
|
|
|
2019-05-14 14:42:38 +00:00
|
|
|
if err == errorsPGP.ErrSignatureExpired && signer != nil {
|
|
|
|
|
if verifyTime > 0 { // if verifyTime = 0: time check disabled, everything is okay
|
2018-07-31 01:00:45 +02:00
|
|
|
// 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
|
|
|
|
2019-05-14 14:42:38 +00:00
|
|
|
_, err = signatureReader.Seek(0, io.SeekStart)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-31 01:00:45 +02:00
|
|
|
signer, err = openpgp.CheckArmoredDetachedSignature(pubKeyEntries, origText, signatureReader, config)
|
2019-05-14 14:42:38 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
2018-07-31 01:00:45 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-14 14:42:38 +00:00
|
|
|
|
2018-06-04 16:05:14 -07:00
|
|
|
if signer == nil {
|
2019-05-14 18:05:01 +02:00
|
|
|
return false, errors.New("gopenpgp: signer is empty")
|
2018-06-04 16:05:14 -07:00
|
|
|
}
|
|
|
|
|
// 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
|
|
|
|
|
}
|