2018-09-11 11:09:28 +02:00
|
|
|
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"
|
2018-07-31 01:00:45 +02:00
|
|
|
errors2 "golang.org/x/crypto/openpgp/errors"
|
2018-09-20 15:20:45 +02:00
|
|
|
"golang.org/x/crypto/openpgp/packet"
|
2018-07-31 01:13:00 +02:00
|
|
|
"io"
|
2018-06-04 16:05:14 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// SignTextDetached sign detached text type
|
2018-09-11 11:09:28 +02:00
|
|
|
func (pm *PmCrypto) SignTextDetached(plainText string, privateKey string, passphrase string, trim bool) (string, error) {
|
2018-06-04 16:05:14 -07:00
|
|
|
//sign with 0x01 text
|
|
|
|
|
var signEntity *openpgp.Entity
|
|
|
|
|
|
2018-06-05 15:26:55 -07:00
|
|
|
if trim {
|
2018-09-11 11:09:28 +02:00
|
|
|
plainText = internal.TrimNewlines(plainText)
|
2018-06-05 15:26:55 -07:00
|
|
|
}
|
|
|
|
|
|
2018-06-04 16:05:14 -07:00
|
|
|
signerReader := strings.NewReader(privateKey)
|
|
|
|
|
signerEntries, err := openpgp.ReadArmoredKeyRing(signerReader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, e := range signerEntries {
|
|
|
|
|
// Entity.PrivateKey must be a signing key
|
|
|
|
|
if e.PrivateKey != nil {
|
|
|
|
|
if e.PrivateKey.Encrypted {
|
|
|
|
|
e.PrivateKey.Decrypt([]byte(passphrase))
|
|
|
|
|
}
|
|
|
|
|
if !e.PrivateKey.Encrypted {
|
|
|
|
|
signEntity = e
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2018-09-20 15:20:45 +02: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 {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return outBuf.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SignTextDetachedBinKey ...
|
2018-09-11 11:09:28 +02:00
|
|
|
func (pm *PmCrypto) SignTextDetachedBinKey(plainText string, privateKey []byte, passphrase string, trim bool) (string, error) {
|
2018-06-04 16:05:14 -07:00
|
|
|
//sign with 0x01
|
|
|
|
|
var signEntity *openpgp.Entity
|
|
|
|
|
|
2018-06-05 15:26:55 -07:00
|
|
|
if trim {
|
2018-09-11 11:09:28 +02:00
|
|
|
plainText = internal.TrimNewlines(plainText)
|
2018-06-05 15:26:55 -07:00
|
|
|
}
|
|
|
|
|
|
2018-06-04 16:05:14 -07:00
|
|
|
signerReader := bytes.NewReader(privateKey)
|
|
|
|
|
signerEntries, err := openpgp.ReadKeyRing(signerReader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, e := range signerEntries {
|
|
|
|
|
// Entity.PrivateKey must be a signing key
|
|
|
|
|
if e.PrivateKey != nil {
|
|
|
|
|
if e.PrivateKey.Encrypted {
|
|
|
|
|
e.PrivateKey.Decrypt([]byte(passphrase))
|
|
|
|
|
}
|
|
|
|
|
if !e.PrivateKey.Encrypted {
|
|
|
|
|
signEntity = e
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if signEntity == nil {
|
|
|
|
|
return "", errors.New("cannot sign message, singer key is not unlocked")
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-20 15:20:45 +02: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
|
|
|
|
|
//sign text
|
|
|
|
|
if err = openpgp.ArmoredDetachSignText(&outBuf, signEntity, att, config); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return outBuf.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SignBinDetached sign bin data
|
2018-09-11 11:09:28 +02:00
|
|
|
func (pm *PmCrypto) SignBinDetached(plainData []byte, privateKey string, passphrase string) (string, error) {
|
2018-06-04 16:05:14 -07:00
|
|
|
//sign with 0x00
|
|
|
|
|
var signEntity *openpgp.Entity
|
|
|
|
|
|
|
|
|
|
signerReader := strings.NewReader(privateKey)
|
|
|
|
|
signerEntries, err := openpgp.ReadArmoredKeyRing(signerReader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, e := range signerEntries {
|
|
|
|
|
// Entity.PrivateKey must be a signing key
|
|
|
|
|
if e.PrivateKey != nil {
|
|
|
|
|
if e.PrivateKey.Encrypted {
|
|
|
|
|
e.PrivateKey.Decrypt([]byte(passphrase))
|
|
|
|
|
}
|
|
|
|
|
if !e.PrivateKey.Encrypted {
|
|
|
|
|
signEntity = e
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if signEntity == nil {
|
|
|
|
|
return "", errors.New("cannot sign message, singer key is not unlocked")
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-20 15:20:45 +02:00
|
|
|
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 {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return outBuf.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SignBinDetachedBinKey ...
|
2018-09-11 11:09:28 +02:00
|
|
|
func (pm *PmCrypto) SignBinDetachedBinKey(plainData []byte, privateKey []byte, passphrase string) (string, error) {
|
2018-06-04 16:05:14 -07:00
|
|
|
//sign with 0x00
|
|
|
|
|
var signEntity *openpgp.Entity
|
|
|
|
|
|
|
|
|
|
signerReader := bytes.NewReader(privateKey)
|
|
|
|
|
signerEntries, err := openpgp.ReadKeyRing(signerReader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, e := range signerEntries {
|
|
|
|
|
// Entity.PrivateKey must be a signing key
|
|
|
|
|
if e.PrivateKey != nil {
|
|
|
|
|
if e.PrivateKey.Encrypted {
|
|
|
|
|
e.PrivateKey.Decrypt([]byte(passphrase))
|
|
|
|
|
}
|
|
|
|
|
if !e.PrivateKey.Encrypted {
|
|
|
|
|
signEntity = e
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if signEntity == nil {
|
|
|
|
|
return "", errors.New("cannot sign message, singer key is not unlocked")
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-20 15:20:45 +02:00
|
|
|
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 {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return outBuf.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VerifyTextSignDetached ...
|
2018-09-11 11:09:28 +02:00
|
|
|
func (pm *PmCrypto) VerifyTextSignDetached(signature string, plainText string, publicKey string, verifyTime int64) (bool, error) {
|
2018-06-04 16:05:14 -07:00
|
|
|
|
|
|
|
|
pubKeyReader := strings.NewReader(publicKey)
|
|
|
|
|
|
|
|
|
|
pubKeyEntries, err := openpgp.ReadArmoredKeyRing(pubKeyReader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-11 11:09:28 +02:00
|
|
|
plainText = internal.TrimNewlines(plainText)
|
2018-06-05 15:26:55 -07:00
|
|
|
|
2018-06-04 16:05:14 -07:00
|
|
|
origText := bytes.NewReader(bytes.NewBufferString(plainText).Bytes())
|
|
|
|
|
|
2018-07-31 01:00:45 +02:00
|
|
|
return verifySignature(pubKeyEntries, origText, signature, verifyTime)
|
2018-06-04 16:05:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VerifyTextSignDetachedBinKey ...
|
2018-09-11 11:09:28 +02:00
|
|
|
func (pm *PmCrypto) VerifyTextSignDetachedBinKey(signature string, plainText string, publicKey []byte, verifyTime int64) (bool, error) {
|
2018-06-04 16:05:14 -07:00
|
|
|
|
|
|
|
|
pubKeyReader := bytes.NewReader(publicKey)
|
|
|
|
|
|
|
|
|
|
pubKeyEntries, err := openpgp.ReadKeyRing(pubKeyReader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-11 11:09:28 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
return verifySignature(pubKeyEntries, 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 {
|
2018-09-20 15:20:45 +02: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
|
|
|
|
|
|
|
|
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)
|
2018-07-31 01:00:45 +02:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VerifyBinSignDetached ...
|
2018-09-11 11:09:28 +02:00
|
|
|
func (pm *PmCrypto) VerifyBinSignDetached(signature string, plainData []byte, publicKey string, verifyTime int64) (bool, error) {
|
2018-06-04 16:05:14 -07:00
|
|
|
|
|
|
|
|
pubKeyReader := strings.NewReader(publicKey)
|
|
|
|
|
|
|
|
|
|
pubKeyEntries, err := openpgp.ReadArmoredKeyRing(pubKeyReader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
origText := bytes.NewReader(plainData)
|
2018-07-31 01:00:45 +02:00
|
|
|
return verifySignature(pubKeyEntries, origText, signature, verifyTime)
|
2018-06-04 16:05:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VerifyBinSignDetachedBinKey ...
|
2018-09-11 11:09:28 +02:00
|
|
|
func (pm *PmCrypto) VerifyBinSignDetachedBinKey(signature string, plainData []byte, publicKey []byte, verifyTime int64) (bool, error) {
|
2018-06-04 16:05:14 -07:00
|
|
|
pubKeyReader := bytes.NewReader(publicKey)
|
|
|
|
|
|
|
|
|
|
pubKeyEntries, err := openpgp.ReadKeyRing(pubKeyReader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
origText := bytes.NewReader(plainData)
|
|
|
|
|
|
2018-07-31 01:00:45 +02:00
|
|
|
return verifySignature(pubKeyEntries, origText, signature, verifyTime)
|
2018-06-04 16:05:14 -07:00
|
|
|
}
|