2019-05-15 14:57:35 +02:00
|
|
|
// Package armor contains a set of helper methods for armoring and unarmoring
|
|
|
|
|
// data.
|
2018-09-11 11:09:28 +02:00
|
|
|
package armor
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2018-09-19 11:52:14 +02:00
|
|
|
"errors"
|
2019-06-03 17:00:01 +02:00
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
2019-05-13 14:07:18 +02:00
|
|
|
"github.com/ProtonMail/gopenpgp/constants"
|
|
|
|
|
"github.com/ProtonMail/gopenpgp/internal"
|
2019-06-03 17:00:01 +02:00
|
|
|
|
2018-09-11 11:09:28 +02:00
|
|
|
"golang.org/x/crypto/openpgp/armor"
|
|
|
|
|
"golang.org/x/crypto/openpgp/clearsign"
|
|
|
|
|
)
|
|
|
|
|
|
2019-05-15 14:57:35 +02:00
|
|
|
// ArmorKey armors input as a public key.
|
2018-09-11 11:09:28 +02:00
|
|
|
func ArmorKey(input []byte) (string, error) {
|
2019-03-07 16:56:12 +01:00
|
|
|
return ArmorWithType(input, constants.PublicKeyHeader)
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-15 14:57:35 +02:00
|
|
|
// ArmorWithTypeBuffered returns a io.WriteCloser which, when written to, writes
|
|
|
|
|
// armored data to w with the given armorType.
|
2018-11-05 22:55:45 +01:00
|
|
|
func ArmorWithTypeBuffered(w io.Writer, armorType string) (io.WriteCloser, error) {
|
|
|
|
|
return armor.Encode(w, armorType, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 14:57:35 +02:00
|
|
|
// ArmorWithType armors input with the given armorType.
|
2018-09-11 11:09:28 +02:00
|
|
|
func ArmorWithType(input []byte, armorType string) (string, error) {
|
|
|
|
|
var b bytes.Buffer
|
2018-11-05 22:55:45 +01:00
|
|
|
|
2018-09-11 11:09:28 +02:00
|
|
|
w, err := armor.Encode(&b, armorType, internal.ArmorHeaders)
|
2018-11-05 22:55:45 +01:00
|
|
|
|
2018-09-11 11:09:28 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
_, err = w.Write(input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
w.Close()
|
|
|
|
|
return b.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 14:57:35 +02:00
|
|
|
// Unarmor unarmors an armored key.
|
2018-09-19 11:52:14 +02:00
|
|
|
func Unarmor(input string) ([]byte, error) {
|
|
|
|
|
b, err := internal.Unarmor(input)
|
2018-09-11 11:09:28 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return ioutil.ReadAll(b.Body)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:00:01 +02:00
|
|
|
// ReadClearSignedMessage returns the message body and unarmored signature from a clearsigned message.
|
|
|
|
|
func ReadClearSignedMessage(signedMessage string) (string, []byte, error) {
|
2018-09-11 11:09:28 +02:00
|
|
|
modulusBlock, rest := clearsign.Decode([]byte(signedMessage))
|
|
|
|
|
if len(rest) != 0 {
|
2019-06-03 17:00:01 +02:00
|
|
|
return "", nil, errors.New("pmapi: extra data after modulus")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
signature, err := ioutil.ReadAll(modulusBlock.ArmoredSignature.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", nil, err
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|
2019-06-03 17:00:01 +02:00
|
|
|
|
|
|
|
|
return string(modulusBlock.Bytes), signature, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ArmorClearSignedMessage armors plaintext and signature with the PGP SIGNED MESSAGE armoring
|
|
|
|
|
func ArmorClearSignedMessage(plaintext []byte, signature []byte) (string, error) {
|
|
|
|
|
armSignature, err := ArmorWithType(signature, constants.PGPSignatureHeader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
str := "-----BEGIN PGP SIGNED MESSAGE-----\r\nHash:SHA512\r\n\r\n"
|
|
|
|
|
str += string(plaintext)
|
|
|
|
|
str += "\r\n"
|
|
|
|
|
str += armSignature
|
|
|
|
|
|
|
|
|
|
return str, nil
|
2018-09-11 11:09:28 +02:00
|
|
|
}
|