Refactor: Moved relevant parts of Key and KeyRing objs from go-pmapi
This commit is contained in:
parent
d1c55119c0
commit
b3e6e67cac
11 changed files with 1113 additions and 32 deletions
|
|
@ -5,12 +5,12 @@ import (
|
|||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
armorUtils "gitlab.com/ProtonMail/go-pm-crypto/armor"
|
||||
"gitlab.com/ProtonMail/go-pm-crypto/internal"
|
||||
"gitlab.com/ProtonMail/go-pm-crypto/models"
|
||||
"golang.org/x/crypto/openpgp"
|
||||
"golang.org/x/crypto/openpgp/armor"
|
||||
"golang.org/x/crypto/openpgp/packet"
|
||||
armorUtils "proton/pmcrypto/armor"
|
||||
"proton/pmcrypto/internal"
|
||||
"proton/pmcrypto/models"
|
||||
)
|
||||
|
||||
//EncryptAttachmentBinKey ...
|
||||
|
|
|
|||
|
|
@ -8,10 +8,10 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"gitlab.com/ProtonMail/go-pm-crypto/armor"
|
||||
"golang.org/x/crypto/openpgp"
|
||||
"golang.org/x/crypto/openpgp/packet"
|
||||
"math/big"
|
||||
"proton/pmcrypto/armor"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
|
|||
|
|
@ -8,14 +8,14 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
armorUtils "gitlab.com/ProtonMail/go-pm-crypto/armor"
|
||||
"gitlab.com/ProtonMail/go-pm-crypto/internal"
|
||||
"gitlab.com/ProtonMail/go-pm-crypto/models"
|
||||
"golang.org/x/crypto/openpgp"
|
||||
"golang.org/x/crypto/openpgp/armor"
|
||||
errors2 "golang.org/x/crypto/openpgp/errors"
|
||||
"golang.org/x/crypto/openpgp/packet"
|
||||
"math"
|
||||
armorUtils "proton/pmcrypto/armor"
|
||||
"proton/pmcrypto/internal"
|
||||
"proton/pmcrypto/models"
|
||||
)
|
||||
|
||||
// DecryptMessage decrypt encrypted message use private key (string )
|
||||
|
|
|
|||
|
|
@ -1,22 +1,21 @@
|
|||
package crypto
|
||||
|
||||
import (
|
||||
"proton/pmmime"
|
||||
"net/mail"
|
||||
"strings"
|
||||
"golang.org/x/crypto/openpgp/packet"
|
||||
"net/textproto"
|
||||
"io/ioutil"
|
||||
"bytes"
|
||||
"github.com/ProtonMail/go-pm-mime"
|
||||
"gitlab.com/ProtonMail/go-pm-crypto/armor"
|
||||
"golang.org/x/crypto/openpgp"
|
||||
"proton/pmcrypto/armor"
|
||||
"golang.org/x/crypto/openpgp/packet"
|
||||
"io/ioutil"
|
||||
"net/mail"
|
||||
"net/textproto"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ======================== Attachments Collector ==============
|
||||
// Collect contents of all attachment parts and return
|
||||
// them as a string
|
||||
|
||||
|
||||
func (pm PmCrypto) parseMIME(mimeBody string, verifierKey []byte) (*pmmime.BodyCollector, int, []string, []string, error) {
|
||||
pubKey := bytes.NewReader(verifierKey)
|
||||
pubKeyEntries, err := openpgp.ReadKeyRing(pubKey)
|
||||
|
|
@ -34,7 +33,10 @@ func (pm PmCrypto) parseMIME(mimeBody string, verifierKey []byte) (*pmmime.BodyC
|
|||
bodyCollector := pmmime.NewBodyCollector(printAccepter)
|
||||
attachmentsCollector := pmmime.NewAttachmentsCollector(bodyCollector)
|
||||
mimeVisitor := pmmime.NewMimeVisitor(attachmentsCollector)
|
||||
str, err := armor.ArmorKey(verifierKey)
|
||||
// TODO: build was failing on this unused 'str' variable. This code looks like WIP
|
||||
//str, err := armor.ArmorKey(verifierKey)
|
||||
_, err = armor.ArmorKey(verifierKey)
|
||||
|
||||
signatureCollector := newSignatureCollector(mimeVisitor, pubKeyEntries, config)
|
||||
err = pmmime.VisitAll(bytes.NewReader(mmBodyData), h, signatureCollector)
|
||||
|
||||
|
|
@ -80,4 +82,4 @@ func (pm *PmCrypto) DecryptMIMEMessage(encryptedText string, verifierKey []byte,
|
|||
} else {
|
||||
callbacks.OnVerified(decsignverify.Verify)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,10 +7,10 @@ import (
|
|||
"io"
|
||||
"strings"
|
||||
|
||||
"gitlab.com/ProtonMail/go-pm-crypto/armor"
|
||||
"gitlab.com/ProtonMail/go-pm-crypto/models"
|
||||
"golang.org/x/crypto/openpgp"
|
||||
"golang.org/x/crypto/openpgp/packet"
|
||||
"proton/pmcrypto/armor"
|
||||
"proton/pmcrypto/models"
|
||||
)
|
||||
|
||||
//RandomToken ...
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"gitlab.com/ProtonMail/go-pm-crypto/internal"
|
||||
"golang.org/x/crypto/openpgp"
|
||||
"golang.org/x/crypto/openpgp/packet"
|
||||
errors2 "golang.org/x/crypto/openpgp/errors"
|
||||
"golang.org/x/crypto/openpgp/packet"
|
||||
"io"
|
||||
"proton/pmcrypto/internal"
|
||||
)
|
||||
|
||||
// SignTextDetached sign detached text type
|
||||
|
|
@ -45,7 +45,7 @@ func (pm *PmCrypto) SignTextDetached(plainText string, privateKey string, passph
|
|||
return "", errors.New("cannot sign message, signer key is not unlocked")
|
||||
}
|
||||
|
||||
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pm.getTimeGenerator() }
|
||||
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pm.getTimeGenerator()}
|
||||
|
||||
att := strings.NewReader(plainText)
|
||||
|
||||
|
|
@ -90,7 +90,7 @@ func (pm *PmCrypto) SignTextDetachedBinKey(plainText string, privateKey []byte,
|
|||
return "", errors.New("cannot sign message, singer key is not unlocked")
|
||||
}
|
||||
|
||||
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pm.getTimeGenerator() }
|
||||
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pm.getTimeGenerator()}
|
||||
|
||||
att := strings.NewReader(plainText)
|
||||
|
||||
|
|
@ -131,7 +131,7 @@ func (pm *PmCrypto) SignBinDetached(plainData []byte, privateKey string, passphr
|
|||
return "", errors.New("cannot sign message, singer key is not unlocked")
|
||||
}
|
||||
|
||||
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pm.getTimeGenerator() }
|
||||
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pm.getTimeGenerator()}
|
||||
|
||||
att := bytes.NewReader(plainData)
|
||||
|
||||
|
|
@ -172,7 +172,7 @@ func (pm *PmCrypto) SignBinDetachedBinKey(plainData []byte, privateKey []byte, p
|
|||
return "", errors.New("cannot sign message, singer key is not unlocked")
|
||||
}
|
||||
|
||||
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pm.getTimeGenerator() }
|
||||
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pm.getTimeGenerator()}
|
||||
|
||||
att := bytes.NewReader(plainData)
|
||||
|
||||
|
|
@ -226,7 +226,7 @@ func verifySignature(pubKeyEntries openpgp.EntityList, origText *bytes.Reader, s
|
|||
}
|
||||
} else {
|
||||
config.Time = func() time.Time {
|
||||
return time.Unix(verifyTime + internal.CreationTimeOffset, 0)
|
||||
return time.Unix(verifyTime+internal.CreationTimeOffset, 0)
|
||||
}
|
||||
}
|
||||
signatureReader := strings.NewReader(signature)
|
||||
|
|
@ -261,7 +261,6 @@ func verifySignature(pubKeyEntries openpgp.EntityList, origText *bytes.Reader, s
|
|||
return true, nil
|
||||
}
|
||||
|
||||
|
||||
// VerifyBinSignDetached ...
|
||||
func (pm *PmCrypto) VerifyBinSignDetached(signature string, plainData []byte, publicKey string, verifyTime int64) (bool, error) {
|
||||
|
||||
|
|
|
|||
|
|
@ -1,16 +1,14 @@
|
|||
package crypto
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"github.com/ProtonMail/go-pm-mime"
|
||||
"golang.org/x/crypto/openpgp"
|
||||
"golang.org/x/crypto/openpgp/packet"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
"net/textproto"
|
||||
"proton/pmmime"
|
||||
)
|
||||
|
||||
type SignatureCollector struct {
|
||||
|
|
|
|||
|
|
@ -11,15 +11,20 @@ func (pm *PmCrypto) UpdateTime(newTime int64) {
|
|||
}
|
||||
|
||||
//GetTime get latest cached time
|
||||
func (pm *PmCrypto) GetTime() int64 {
|
||||
func (pm *PmCrypto) GetTimeUnix() int64 {
|
||||
return pm.getNow().Unix()
|
||||
}
|
||||
|
||||
//GetTime get latest cached time
|
||||
func (pm *PmCrypto) GetTime() time.Time {
|
||||
return pm.getNow()
|
||||
}
|
||||
|
||||
func (pm *PmCrypto) getNow() time.Time {
|
||||
if pm.latestServerTime > 0 && !pm.latestClientTime.IsZero() {
|
||||
// Sub is monotome, it uses a monotime time clock in this case instead of the wall clock
|
||||
extrapolate := int64(pm.latestClientTime.Sub(time.Now()).Seconds())
|
||||
return time.Unix(pm.latestServerTime + extrapolate, 0)
|
||||
return time.Unix(pm.latestServerTime+extrapolate, 0)
|
||||
}
|
||||
|
||||
return time.Now()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue