From 7fae22fea3a9909a106dfcd8b6cd87c4a741c98b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maido=20K=C3=A4=C3=A4ra?= Date: Wed, 11 May 2022 22:07:38 +0300 Subject: [PATCH] Armor encode input --- bridge/call.go | 13 +++++ bridge/model/ArmorDecodeRequest.go | 52 +++++++++++++++++++ bridge/model/ArmorEncodeRequest.go | 81 ++++++++++++++++++++++++++++++ flatbuffers/bridge.fbs | 6 ++- openpgp/armor.go | 23 +++++++++ openpgp/armor_test.go | 27 ++++++++++ 6 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 bridge/model/ArmorDecodeRequest.go create mode 100644 bridge/model/ArmorEncodeRequest.go create mode 100644 openpgp/armor.go create mode 100644 openpgp/armor_test.go diff --git a/bridge/call.go b/bridge/call.go index f7b91d6..ece4832 100644 --- a/bridge/call.go +++ b/bridge/call.go @@ -53,6 +53,8 @@ func Call(name string, payload []byte) ([]byte, error) { output = instance.encryptSymmetricBytes(payload) case "generate": output = instance.generate(payload) + case "armorEncode": + output = instance.armorEncode(payload) default: return nil, fmt.Errorf("not implemented: %s", name) } @@ -374,6 +376,17 @@ func (m instance) parseCompression(input model.Compression) string { } } +func (m instance) armorEncode(payload []byte) []byte { + response := flatbuffers.NewBuilder(0) + request := model.GetRootAsArmorEncodeRequest(payload, 0) + + output, err := m.instance.ArmorEncode(request.PacketBytes()) + if err != nil { + return m._stringResponse(response, output, err) + } + return m._stringResponse(response, output, nil) +} + func (m instance) _keyPairResponse(response *flatbuffers.Builder, output *openpgp.KeyPair, err error) []byte { if err != nil { outputOffset := response.CreateString(err.Error()) diff --git a/bridge/model/ArmorDecodeRequest.go b/bridge/model/ArmorDecodeRequest.go new file mode 100644 index 0000000..4a86f21 --- /dev/null +++ b/bridge/model/ArmorDecodeRequest.go @@ -0,0 +1,52 @@ +// Code generated by the FlatBuffers compiler. DO NOT EDIT. + +package model + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) + +type ArmorDecodeRequest struct { + _tab flatbuffers.Table +} + +func GetRootAsArmorDecodeRequest(buf []byte, offset flatbuffers.UOffsetT) *ArmorDecodeRequest { + n := flatbuffers.GetUOffsetT(buf[offset:]) + x := &ArmorDecodeRequest{} + x.Init(buf, n+offset) + return x +} + +func GetSizePrefixedRootAsArmorDecodeRequest(buf []byte, offset flatbuffers.UOffsetT) *ArmorDecodeRequest { + n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:]) + x := &ArmorDecodeRequest{} + x.Init(buf, n+offset+flatbuffers.SizeUint32) + return x +} + +func (rcv *ArmorDecodeRequest) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *ArmorDecodeRequest) Table() flatbuffers.Table { + return rcv._tab +} + +func (rcv *ArmorDecodeRequest) Packet() []byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + return rcv._tab.ByteVector(o + rcv._tab.Pos) + } + return nil +} + +func ArmorDecodeRequestStart(builder *flatbuffers.Builder) { + builder.StartObject(1) +} +func ArmorDecodeRequestAddPacket(builder *flatbuffers.Builder, packet flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(packet), 0) +} +func ArmorDecodeRequestEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/bridge/model/ArmorEncodeRequest.go b/bridge/model/ArmorEncodeRequest.go new file mode 100644 index 0000000..9d7cb48 --- /dev/null +++ b/bridge/model/ArmorEncodeRequest.go @@ -0,0 +1,81 @@ +// Code generated by the FlatBuffers compiler. DO NOT EDIT. + +package model + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) + +type ArmorEncodeRequest struct { + _tab flatbuffers.Table +} + +func GetRootAsArmorEncodeRequest(buf []byte, offset flatbuffers.UOffsetT) *ArmorEncodeRequest { + n := flatbuffers.GetUOffsetT(buf[offset:]) + x := &ArmorEncodeRequest{} + x.Init(buf, n+offset) + return x +} + +func GetSizePrefixedRootAsArmorEncodeRequest(buf []byte, offset flatbuffers.UOffsetT) *ArmorEncodeRequest { + n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:]) + x := &ArmorEncodeRequest{} + x.Init(buf, n+offset+flatbuffers.SizeUint32) + return x +} + +func (rcv *ArmorEncodeRequest) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *ArmorEncodeRequest) Table() flatbuffers.Table { + return rcv._tab +} + +func (rcv *ArmorEncodeRequest) Packet(j int) byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + a := rcv._tab.Vector(o) + return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j*1)) + } + return 0 +} + +func (rcv *ArmorEncodeRequest) PacketLength() int { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + return rcv._tab.VectorLen(o) + } + return 0 +} + +func (rcv *ArmorEncodeRequest) PacketBytes() []byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + return rcv._tab.ByteVector(o + rcv._tab.Pos) + } + return nil +} + +func (rcv *ArmorEncodeRequest) MutatePacket(j int, n byte) bool { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + a := rcv._tab.Vector(o) + return rcv._tab.MutateByte(a+flatbuffers.UOffsetT(j*1), n) + } + return false +} + +func ArmorEncodeRequestStart(builder *flatbuffers.Builder) { + builder.StartObject(1) +} +func ArmorEncodeRequestAddPacket(builder *flatbuffers.Builder, packet flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(packet), 0) +} +func ArmorEncodeRequestStartPacketVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { + return builder.StartVector(1, numElems, 1) +} +func ArmorEncodeRequestEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/flatbuffers/bridge.fbs b/flatbuffers/bridge.fbs index 5da3404..19fc2c8 100644 --- a/flatbuffers/bridge.fbs +++ b/flatbuffers/bridge.fbs @@ -153,6 +153,10 @@ table GenerateRequest { options:model.Options; } +table ArmorEncodeRequest { + packet:[ubyte]; +} + /// KeyOptions collects a number of parameters along with sensible defaults. table KeyOptions { /// Hash is the default hash function to be used. @@ -255,4 +259,4 @@ table PrivateKeyMetadata { key_id_numeric:string; is_sub_key:bool; encrypted:bool; -} \ No newline at end of file +} diff --git a/openpgp/armor.go b/openpgp/armor.go new file mode 100644 index 0000000..7f1f000 --- /dev/null +++ b/openpgp/armor.go @@ -0,0 +1,23 @@ +package openpgp + +import ( + "bytes" + "github.com/ProtonMail/go-crypto/openpgp/armor" +) + +func (o FastOpenPGP) ArmorEncode(packet []byte) (string, error) { + buf := bytes.NewBuffer(nil) + writer, err := armor.Encode(buf, messageType, headers) + if err != nil { + return "", err + } + _, err = writer.Write(packet) + if err != nil { + return "", err + } + err = writer.Close() + if err != nil { + return "", err + } + return buf.String(), nil +} diff --git a/openpgp/armor_test.go b/openpgp/armor_test.go new file mode 100644 index 0000000..539e234 --- /dev/null +++ b/openpgp/armor_test.go @@ -0,0 +1,27 @@ +package openpgp + +import ( + "testing" +) + +var encoded = `-----BEGIN PGP MESSAGE----- +Version: openpgp-mobile + +cmFuZG9tIHN0cmluZw== +=zR7q +-----END PGP MESSAGE-----` + +func TestFastOpenPGP_ArmorEncode(t *testing.T) { + + openPGP := NewFastOpenPGP() + output, err := openPGP.ArmorEncode([]byte("random string")) + if err != nil { + t.Fatal(err) + } + + t.Log("output:", output) + + if string(output) != string(encoded) { + t.Fatal("not same input") + } +}