more cleanup, fixes

This commit is contained in:
Sanjana Rajan 2018-06-04 17:50:26 -07:00
parent c254bd5d44
commit 04ebe6d459
9 changed files with 33 additions and 53 deletions

View file

@ -8,24 +8,12 @@ import (
"golang.org/x/crypto/openpgp/armor" "golang.org/x/crypto/openpgp/armor"
) )
// ...Armor Type
type ArmorType string
func (at ArmorType) string() string {
return string(at)
}
const ( const (
pgpMessageType ArmorType = "PGP MESSAGE" pgpMessageType string = "PGP MESSAGE"
pgpPublicBlockType ArmorType = "PGP PUBLIC KEY BLOCK" pgpPublicBlockType string = "PGP PUBLIC KEY BLOCK"
pgpPrivateBlockType ArmorType = "PGP PRIVATE KEY BLOCK" pgpPrivateBlockType string = "PGP PRIVATE KEY BLOCK"
) )
// ArmorKey make bytes input key to armor format
func ArmorKey(input []byte) (string, error) {
return ArmorWithType(input, pgpPublicBlockType.string())
}
// ArmorWithType make bytes input to armor format // ArmorWithType make bytes input to armor format
func ArmorWithType(input []byte, armorType string) (string, error) { func ArmorWithType(input []byte, armorType string) (string, error) {
var b bytes.Buffer var b bytes.Buffer

View file

@ -14,7 +14,7 @@ import (
func (o *OpenPGP) EncryptAttachmentBinKey(plainData []byte, fileName string, publicKey []byte) (*EncryptedSplit, error) { func (o *OpenPGP) EncryptAttachmentBinKey(plainData []byte, fileName string, publicKey []byte) (*EncryptedSplit, error) {
var outBuf bytes.Buffer var outBuf bytes.Buffer
w, err := armor.Encode(&outBuf, pgpMessageType.string(), armorHeader) w, err := armor.Encode(&outBuf, pgpMessageType, armorHeader)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -35,12 +35,12 @@ func (o *OpenPGP) EncryptAttachmentBinKey(plainData []byte, fileName string, pub
ew.Close() ew.Close()
w.Close() w.Close()
splited, err := SeparateKeyAndData(outBuf.String()) split, err := SeparateKeyAndData(outBuf.String())
if err != nil { if err != nil {
return nil, err return nil, err
} }
splited.Algo = "aes256" split.Algo = "aes256"
return splited, nil return split, nil
} }
//EncryptAttachment ... //EncryptAttachment ...
@ -109,7 +109,7 @@ func (o *OpenPGP) DecryptAttachment(keyPacket []byte, dataPacket []byte, private
func (o *OpenPGP) EncryptAttachmentWithPassword(plainData []byte, password string) (string, error) { func (o *OpenPGP) EncryptAttachmentWithPassword(plainData []byte, password string) (string, error) {
var outBuf bytes.Buffer var outBuf bytes.Buffer
w, err := armor.Encode(&outBuf, pgpMessageType.string(), armorHeader) w, err := armor.Encode(&outBuf, pgpMessageType, armorHeader)
if err != nil { if err != nil {
return "", err return "", err
} }

View file

@ -1,7 +1,7 @@
package pm package pm
var armorHeader = map[string]string{ var armorHeader = map[string]string{
"Version": "OpenPGP Mobile 0.0.1 (" + Version() + ")", "Version": "OpenPGP Golang 0.0.1 (" + Version() + ")",
"Comment": "https://protonmail.com", "Comment": "https://protonmail.com",
} }

6
key.go
View file

@ -190,7 +190,7 @@ func (o *OpenPGP) GenerateKey(userName string, domain string, passphrase string,
return "", err return "", err
} }
serialized := w.Bytes() serialized := w.Bytes()
return ArmorWithType(serialized, pgpPrivateBlockType.string()) return ArmorWithType(serialized, pgpPrivateBlockType)
} }
// UpdatePrivateKeyPassphrase ... // UpdatePrivateKeyPassphrase ...
@ -235,7 +235,7 @@ func (o *OpenPGP) UpdatePrivateKeyPassphrase(privateKey string, oldPassphrase st
} }
serialized := w.Bytes() serialized := w.Bytes()
return ArmorWithType(serialized, pgpPrivateBlockType.string()) return ArmorWithType(serialized, pgpPrivateBlockType)
} }
// PublicKey get a public key from a private key // PublicKey get a public key from a private key
@ -251,7 +251,7 @@ func PublicKey(privateKey string) (string, error) {
e.Serialize(&outBuf) e.Serialize(&outBuf)
} }
outString, err := ArmorKey(outBuf.Bytes()) outString, err := ArmorWithType(outBuf.Bytes(), pgpPublicBlockType)
if err != nil { if err != nil {
return "", nil return "", nil
} }

View file

@ -220,7 +220,7 @@ func (o *OpenPGP) EncryptMessage(plainText string, publicKey string, privateKey
func (o *OpenPGP) EncryptMessageBinKey(plainText string, publicKey []byte, privateKey string, passphrase string, trim bool) (string, error) { func (o *OpenPGP) EncryptMessageBinKey(plainText string, publicKey []byte, privateKey string, passphrase string, trim bool) (string, error) {
var outBuf bytes.Buffer var outBuf bytes.Buffer
w, err := armor.Encode(&outBuf, pgpMessageType.string(), armorHeader) w, err := armor.Encode(&outBuf, pgpMessageType, armorHeader)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -272,7 +272,7 @@ func (o *OpenPGP) EncryptMessageBinKey(plainText string, publicKey []byte, priva
func (o *OpenPGP) EncryptMessageWithPassword(plainText string, password string) (string, error) { func (o *OpenPGP) EncryptMessageWithPassword(plainText string, password string) (string, error) {
var outBuf bytes.Buffer var outBuf bytes.Buffer
w, err := armor.Encode(&outBuf, pgpMessageType.string(), armorHeader) w, err := armor.Encode(&outBuf, pgpMessageType, armorHeader)
if err != nil { if err != nil {
return "", err return "", err
} }

View file

@ -1,12 +1,11 @@
package pm package pm
// OpenPGP strutature to manager mutiple address keys and user keys // OpenPGP structure to manage mutiple address keys and user keys
type OpenPGP struct { type OpenPGP struct {
// key ring not in used
addresses []*Address addresses []*Address
//lastestServerTime unix time cache //latestServerTime unix time cache
lastestServerTime int64 latestServerTime int64
} }
// //AddAddress add a new address to key ring // //AddAddress add a new address to key ring

View file

@ -198,7 +198,7 @@ func GetSessionFromSymmetricPacket(keyPackage []byte, password string) (*Session
if err == nil { if err == nil {
return &SessionSplit{ return &SessionSplit{
Session: key, Session: key,
Algo: getAlog(cipherFunc), Algo: getAlgo(cipherFunc),
}, nil }, nil
} }
@ -240,7 +240,7 @@ var symKeyAlgos = map[string]packet.CipherFunction{
"aes256": packet.CipherAES256, "aes256": packet.CipherAES256,
} }
// Get this's cipher function. // Get cipher function.
func cipherFunc(algo string) packet.CipherFunction { func cipherFunc(algo string) packet.CipherFunction {
cf, ok := symKeyAlgos[algo] cf, ok := symKeyAlgos[algo]
if ok { if ok {
@ -253,16 +253,13 @@ func getSessionSplit(ek *packet.EncryptedKey) (*SessionSplit, error) {
if ek == nil { if ek == nil {
return nil, errors.New("can't decrypt key packet") return nil, errors.New("can't decrypt key packet")
} }
var algo string algo := "aes256"
for k, v := range symKeyAlgos { for k, v := range symKeyAlgos {
if v == ek.CipherFunc { if v == ek.CipherFunc {
algo = k algo = k
break break
} }
} }
if algo == "" {
algo = "aes256"
}
return &SessionSplit{ return &SessionSplit{
Session: ek.Key, Session: ek.Key,
@ -270,17 +267,14 @@ func getSessionSplit(ek *packet.EncryptedKey) (*SessionSplit, error) {
}, nil }, nil
} }
func getAlog(cipher packet.CipherFunction) string { func getAlgo(cipher packet.CipherFunction) string {
var algo string algo := "aes256"
for k, v := range symKeyAlgos { for k, v := range symKeyAlgos {
if v == cipher { if v == cipher {
algo = k algo = k
break break
} }
} }
if algo == "" {
algo = "aes256"
}
return algo return algo
} }
@ -318,7 +312,7 @@ func SeparateKeyAndData(encrypted string) (*EncryptedSplit, error) {
//kr *KeyRing, r io.Reader) (key *SymmetricKey, symEncryptedData []byte, //kr *KeyRing, r io.Reader) (key *SymmetricKey, symEncryptedData []byte,
packets := packet.NewReader(encryptedReader) packets := packet.NewReader(encryptedReader)
outSplt := &EncryptedSplit{} outSplit := &EncryptedSplit{}
// Save encrypted key and signature apart // Save encrypted key and signature apart
var ek *packet.EncryptedKey var ek *packet.EncryptedKey
@ -350,15 +344,15 @@ func SeparateKeyAndData(encrypted string) (*EncryptedSplit, error) {
symEncryptedData = append(symEncryptedData, byte(1)) symEncryptedData = append(symEncryptedData, byte(1))
symEncryptedData = append(symEncryptedData, packetContents...) symEncryptedData = append(symEncryptedData, packetContents...)
outSplt.DataPacket = symEncryptedData outSplit.DataPacket = symEncryptedData
break break
} }
} }
var buff bytes.Buffer var buf bytes.Buffer
ek.Serialize(&buff) ek.Serialize(&buf)
outSplt.KeyPacket = buff.Bytes() outSplit.KeyPacket = buf.Bytes()
return outSplt, err return outSplit, err
} }

View file

@ -45,7 +45,7 @@ func (o *OpenPGP) SignTextDetached(plainText string, privateKey string, passphra
} }
if signEntity == nil { if signEntity == nil {
return "", errors.New("cannot sign message, singer key is not unlocked") return "", errors.New("cannot sign message, signer key is not unlocked")
} }
config := &packet.Config{DefaultCipher: packet.CipherAES256} config := &packet.Config{DefaultCipher: packet.CipherAES256}

View file

@ -6,19 +6,18 @@ import (
// UpdateTime update cached time // UpdateTime update cached time
func (o *OpenPGP) UpdateTime(newTime int64) { func (o *OpenPGP) UpdateTime(newTime int64) {
o.lastestServerTime = newTime o.latestServerTime = newTime
} }
//GetTime get latest cached time //GetTime get latest cached time
func (o *OpenPGP) GetTime() int64 { func (o *OpenPGP) GetTime() int64 {
return o.lastestServerTime return o.latestServerTime
} }
func (o *OpenPGP) getNow() time.Time { func (o *OpenPGP) getNow() time.Time {
if o.lastestServerTime > 0 { if o.latestServerTime > 0 {
tm := time.Unix(o.lastestServerTime, 0) return time.Unix(o.latestServerTime, 0)
return tm
} }
return time.Now() return time.Now()