diff --git a/crypto/pb/Makefile b/crypto/pb/Makefile index df34e54..8af2dd8 100644 --- a/crypto/pb/Makefile +++ b/crypto/pb/Makefile @@ -4,7 +4,7 @@ GO = $(PB:.proto=.pb.go) all: $(GO) %.pb.go: %.proto - protoc --proto_path=$(GOPATH)/src:. --gogofaster_out=. $< + protoc --proto_path=$(PWD)/../..:. --gogofaster_out=. $< clean: rm -f *.pb.go diff --git a/crypto/pb/crypto.proto b/crypto/pb/crypto.proto index cb5cee8..182b0d4 100644 --- a/crypto/pb/crypto.proto +++ b/crypto/pb/crypto.proto @@ -2,6 +2,8 @@ syntax = "proto2"; package crypto.pb; +option go_package = "github.com/libp2p/go-libp2p-core/crypto/pb"; + enum KeyType { RSA = 0; Ed25519 = 1; diff --git a/record/envelope.go b/record/envelope.go index a26c001..4290c68 100644 --- a/record/envelope.go +++ b/record/envelope.go @@ -7,7 +7,7 @@ import ( pool "github.com/libp2p/go-buffer-pool" "github.com/libp2p/go-libp2p-core/crypto" - pb "github.com/libp2p/go-libp2p-core/crypto/pb" + pb "github.com/libp2p/go-libp2p-core/record/pb" "github.com/gogo/protobuf/proto" "github.com/multiformats/go-varint" diff --git a/record/envelope_test.go b/record/envelope_test.go index f2b44ad..b6e8aaf 100644 --- a/record/envelope_test.go +++ b/record/envelope_test.go @@ -1,13 +1,15 @@ -package record +package record_test import ( "bytes" - "github.com/gogo/protobuf/proto" - - . "github.com/libp2p/go-libp2p-core/crypto" - pb "github.com/libp2p/go-libp2p-core/crypto/pb" - "github.com/libp2p/go-libp2p-core/test" "testing" + + crypto "github.com/libp2p/go-libp2p-core/crypto" + . "github.com/libp2p/go-libp2p-core/record" + pb "github.com/libp2p/go-libp2p-core/record/pb" + "github.com/libp2p/go-libp2p-core/test" + + "github.com/gogo/protobuf/proto" ) // Make an envelope, verify & open it, marshal & unmarshal it @@ -16,7 +18,7 @@ func TestEnvelopeHappyPath(t *testing.T) { payload = []byte("happy hacking") domain = "libp2p-testing" payloadType = []byte("/libp2p/testdata") - priv, pub, err = test.RandTestKeyPair(Ed25519, 256) + priv, pub, err = test.RandTestKeyPair(crypto.Ed25519, 256) ) test.AssertNilError(t, err) @@ -51,7 +53,7 @@ func TestMakeEnvelopeFailsWithEmptyDomain(t *testing.T) { var ( payload = []byte("happy hacking") payloadType = []byte("/libp2p/testdata") - priv, _, err = test.RandTestKeyPair(Ed25519, 256) + priv, _, err = test.RandTestKeyPair(crypto.Ed25519, 256) ) if err != nil { @@ -67,7 +69,7 @@ func TestEnvelopeValidateFailsForDifferentDomain(t *testing.T) { payload = []byte("happy hacking") domain = "libp2p-testing" payloadType = []byte("/libp2p/testdata") - priv, _, err = test.RandTestKeyPair(Ed25519, 256) + priv, _, err = test.RandTestKeyPair(crypto.Ed25519, 256) ) test.AssertNilError(t, err) @@ -87,7 +89,7 @@ func TestEnvelopeValidateFailsIfTypeHintIsAltered(t *testing.T) { payload = []byte("happy hacking") domain = "libp2p-testing" payloadType = []byte("/libp2p/testdata") - priv, _, err = test.RandTestKeyPair(Ed25519, 256) + priv, _, err = test.RandTestKeyPair(crypto.Ed25519, 256) ) test.AssertNilError(t, err) @@ -109,7 +111,7 @@ func TestEnvelopeValidateFailsIfContentsAreAltered(t *testing.T) { payload = []byte("happy hacking") domain = "libp2p-testing" payloadType = []byte("/libp2p/testdata") - priv, _, err = test.RandTestKeyPair(Ed25519, 256) + priv, _, err = test.RandTestKeyPair(crypto.Ed25519, 256) ) test.AssertNilError(t, err) diff --git a/record/pb/Makefile b/record/pb/Makefile new file mode 100644 index 0000000..7cf8222 --- /dev/null +++ b/record/pb/Makefile @@ -0,0 +1,11 @@ +PB = $(wildcard *.proto) +GO = $(PB:.proto=.pb.go) + +all: $(GO) + +%.pb.go: %.proto + protoc --proto_path=$(PWD):$(PWD)/../.. --gogofaster_out=. $< + +clean: + rm -f *.pb.go + rm -f *.go diff --git a/crypto/pb/envelope.pb.go b/record/pb/envelope.pb.go similarity index 73% rename from crypto/pb/envelope.pb.go rename to record/pb/envelope.pb.go index d18436f..10548a5 100644 --- a/crypto/pb/envelope.pb.go +++ b/record/pb/envelope.pb.go @@ -1,14 +1,14 @@ // Code generated by protoc-gen-gogo. DO NOT EDIT. // source: envelope.proto -package crypto_pb +package record_pb import ( fmt "fmt" proto "github.com/gogo/protobuf/proto" + pb "github.com/libp2p/go-libp2p-core/crypto/pb" io "io" math "math" - math_bits "math/bits" ) // Reference imports to suppress errors if they are not otherwise used. @@ -20,13 +20,13 @@ var _ = math.Inf // is compatible with the proto package it is being compiled against. // A compilation error at this line likely means your copy of the // proto package needs to be updated. -const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package +const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package type SignedEnvelope struct { - PublicKey *PublicKey `protobuf:"bytes,1,opt,name=publicKey,proto3" json:"publicKey,omitempty"` - PayloadType []byte `protobuf:"bytes,2,opt,name=payloadType,proto3" json:"payloadType,omitempty"` - Payload []byte `protobuf:"bytes,3,opt,name=payload,proto3" json:"payload,omitempty"` - Signature []byte `protobuf:"bytes,4,opt,name=signature,proto3" json:"signature,omitempty"` + PublicKey *pb.PublicKey `protobuf:"bytes,1,opt,name=publicKey,proto3" json:"publicKey,omitempty"` + PayloadType []byte `protobuf:"bytes,2,opt,name=payloadType,proto3" json:"payloadType,omitempty"` + Payload []byte `protobuf:"bytes,3,opt,name=payload,proto3" json:"payload,omitempty"` + Signature []byte `protobuf:"bytes,4,opt,name=signature,proto3" json:"signature,omitempty"` } func (m *SignedEnvelope) Reset() { *m = SignedEnvelope{} } @@ -43,7 +43,7 @@ func (m *SignedEnvelope) XXX_Marshal(b []byte, deterministic bool) ([]byte, erro return xxx_messageInfo_SignedEnvelope.Marshal(b, m, deterministic) } else { b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) + n, err := m.MarshalTo(b) if err != nil { return nil, err } @@ -62,7 +62,7 @@ func (m *SignedEnvelope) XXX_DiscardUnknown() { var xxx_messageInfo_SignedEnvelope proto.InternalMessageInfo -func (m *SignedEnvelope) GetPublicKey() *PublicKey { +func (m *SignedEnvelope) GetPublicKey() *pb.PublicKey { if m != nil { return m.PublicKey } @@ -91,31 +91,32 @@ func (m *SignedEnvelope) GetSignature() []byte { } func init() { - proto.RegisterType((*SignedEnvelope)(nil), "crypto.pb.SignedEnvelope") + proto.RegisterType((*SignedEnvelope)(nil), "record.pb.SignedEnvelope") } func init() { proto.RegisterFile("envelope.proto", fileDescriptor_ee266e8c558e9dc5) } var fileDescriptor_ee266e8c558e9dc5 = []byte{ - // 189 bytes of a gzipped FileDescriptorProto + // 200 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x4b, 0xcd, 0x2b, 0x4b, - 0xcd, 0xc9, 0x2f, 0x48, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x4c, 0x2e, 0xaa, 0x2c, - 0x28, 0xc9, 0xd7, 0x2b, 0x48, 0x92, 0xe2, 0x81, 0x31, 0x41, 0x12, 0x4a, 0xf3, 0x18, 0xb9, 0xf8, - 0x82, 0x33, 0xd3, 0xf3, 0x52, 0x53, 0x5c, 0xa1, 0x3a, 0x84, 0x8c, 0xb8, 0x38, 0x0b, 0x4a, 0x93, - 0x72, 0x32, 0x93, 0xbd, 0x53, 0x2b, 0x25, 0x18, 0x15, 0x18, 0x35, 0xb8, 0x8d, 0x44, 0xf4, 0xe0, - 0xfa, 0xf5, 0x02, 0x60, 0x72, 0x41, 0x08, 0x65, 0x42, 0x0a, 0x5c, 0xdc, 0x05, 0x89, 0x95, 0x39, - 0xf9, 0x89, 0x29, 0x21, 0x95, 0x05, 0xa9, 0x12, 0x4c, 0x0a, 0x8c, 0x1a, 0x3c, 0x41, 0xc8, 0x42, - 0x42, 0x12, 0x5c, 0xec, 0x50, 0xae, 0x04, 0x33, 0x58, 0x16, 0xc6, 0x15, 0x92, 0xe1, 0xe2, 0x2c, - 0xce, 0x4c, 0xcf, 0x4b, 0x2c, 0x29, 0x2d, 0x4a, 0x95, 0x60, 0x01, 0xcb, 0x21, 0x04, 0x9c, 0x24, - 0x4e, 0x3c, 0x92, 0x63, 0xbc, 0xf0, 0x48, 0x8e, 0xf1, 0xc1, 0x23, 0x39, 0xc6, 0x09, 0x8f, 0xe5, - 0x18, 0x2e, 0x3c, 0x96, 0x63, 0xb8, 0xf1, 0x58, 0x8e, 0x21, 0x89, 0x0d, 0xec, 0x03, 0x63, 0x40, - 0x00, 0x00, 0x00, 0xff, 0xff, 0x25, 0xd3, 0xde, 0xe5, 0xec, 0x00, 0x00, 0x00, + 0xcd, 0xc9, 0x2f, 0x48, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x2c, 0x4a, 0x4d, 0xce, + 0x2f, 0x4a, 0xd1, 0x2b, 0x48, 0x92, 0x12, 0x4b, 0x2e, 0xaa, 0x2c, 0x28, 0xc9, 0xd7, 0x2f, 0x48, + 0xd2, 0x87, 0xb0, 0x20, 0x4a, 0x94, 0xe6, 0x31, 0x72, 0xf1, 0x05, 0x67, 0xa6, 0xe7, 0xa5, 0xa6, + 0xb8, 0x42, 0xf5, 0x0a, 0x19, 0x71, 0x71, 0x16, 0x94, 0x26, 0xe5, 0x64, 0x26, 0x7b, 0xa7, 0x56, + 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0x70, 0x1b, 0x89, 0xe8, 0xc1, 0x34, 0x25, 0xe9, 0x05, 0xc0, 0xe4, + 0x82, 0x10, 0xca, 0x84, 0x14, 0xb8, 0xb8, 0x0b, 0x12, 0x2b, 0x73, 0xf2, 0x13, 0x53, 0x42, 0x2a, + 0x0b, 0x52, 0x25, 0x98, 0x14, 0x18, 0x35, 0x78, 0x82, 0x90, 0x85, 0x84, 0x24, 0xb8, 0xd8, 0xa1, + 0x5c, 0x09, 0x66, 0xb0, 0x2c, 0x8c, 0x2b, 0x24, 0xc3, 0xc5, 0x59, 0x9c, 0x99, 0x9e, 0x97, 0x58, + 0x52, 0x5a, 0x94, 0x2a, 0xc1, 0x02, 0x96, 0x43, 0x08, 0x38, 0x49, 0x9c, 0x78, 0x24, 0xc7, 0x78, + 0xe1, 0x91, 0x1c, 0xe3, 0x83, 0x47, 0x72, 0x8c, 0x13, 0x1e, 0xcb, 0x31, 0x5c, 0x78, 0x2c, 0xc7, + 0x70, 0xe3, 0xb1, 0x1c, 0x43, 0x12, 0x1b, 0xd8, 0x07, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, + 0xfb, 0xd0, 0x77, 0x21, 0xf6, 0x00, 0x00, 0x00, } func (m *SignedEnvelope) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) + n, err := m.MarshalTo(dAtA) if err != nil { return nil, err } @@ -123,61 +124,49 @@ func (m *SignedEnvelope) Marshal() (dAtA []byte, err error) { } func (m *SignedEnvelope) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *SignedEnvelope) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) + var i int _ = i var l int _ = l - if len(m.Signature) > 0 { - i -= len(m.Signature) - copy(dAtA[i:], m.Signature) - i = encodeVarintEnvelope(dAtA, i, uint64(len(m.Signature))) - i-- - dAtA[i] = 0x22 - } - if len(m.Payload) > 0 { - i -= len(m.Payload) - copy(dAtA[i:], m.Payload) - i = encodeVarintEnvelope(dAtA, i, uint64(len(m.Payload))) - i-- - dAtA[i] = 0x1a + if m.PublicKey != nil { + dAtA[i] = 0xa + i++ + i = encodeVarintEnvelope(dAtA, i, uint64(m.PublicKey.Size())) + n1, err := m.PublicKey.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err + } + i += n1 } if len(m.PayloadType) > 0 { - i -= len(m.PayloadType) - copy(dAtA[i:], m.PayloadType) - i = encodeVarintEnvelope(dAtA, i, uint64(len(m.PayloadType))) - i-- dAtA[i] = 0x12 + i++ + i = encodeVarintEnvelope(dAtA, i, uint64(len(m.PayloadType))) + i += copy(dAtA[i:], m.PayloadType) } - if m.PublicKey != nil { - { - size, err := m.PublicKey.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintEnvelope(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0xa + if len(m.Payload) > 0 { + dAtA[i] = 0x1a + i++ + i = encodeVarintEnvelope(dAtA, i, uint64(len(m.Payload))) + i += copy(dAtA[i:], m.Payload) } - return len(dAtA) - i, nil + if len(m.Signature) > 0 { + dAtA[i] = 0x22 + i++ + i = encodeVarintEnvelope(dAtA, i, uint64(len(m.Signature))) + i += copy(dAtA[i:], m.Signature) + } + return i, nil } func encodeVarintEnvelope(dAtA []byte, offset int, v uint64) int { - offset -= sovEnvelope(v) - base := offset for v >= 1<<7 { dAtA[offset] = uint8(v&0x7f | 0x80) v >>= 7 offset++ } dAtA[offset] = uint8(v) - return base + return offset + 1 } func (m *SignedEnvelope) Size() (n int) { if m == nil { @@ -205,7 +194,14 @@ func (m *SignedEnvelope) Size() (n int) { } func sovEnvelope(x uint64) (n int) { - return (math_bits.Len64(x|1) + 6) / 7 + for { + n++ + x >>= 7 + if x == 0 { + break + } + } + return n } func sozEnvelope(x uint64) (n int) { return sovEnvelope(uint64((x << 1) ^ uint64((int64(x) >> 63)))) @@ -269,7 +265,7 @@ func (m *SignedEnvelope) Unmarshal(dAtA []byte) error { return io.ErrUnexpectedEOF } if m.PublicKey == nil { - m.PublicKey = &PublicKey{} + m.PublicKey = &pb.PublicKey{} } if err := m.PublicKey.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err @@ -404,7 +400,6 @@ func (m *SignedEnvelope) Unmarshal(dAtA []byte) error { func skipEnvelope(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 - depth := 0 for iNdEx < l { var wire uint64 for shift := uint(0); ; shift += 7 { @@ -436,8 +431,10 @@ func skipEnvelope(dAtA []byte) (n int, err error) { break } } + return iNdEx, nil case 1: iNdEx += 8 + return iNdEx, nil case 2: var length int for shift := uint(0); ; shift += 7 { @@ -458,30 +455,55 @@ func skipEnvelope(dAtA []byte) (n int, err error) { return 0, ErrInvalidLengthEnvelope } iNdEx += length - case 3: - depth++ - case 4: - if depth == 0 { - return 0, ErrUnexpectedEndOfGroupEnvelope + if iNdEx < 0 { + return 0, ErrInvalidLengthEnvelope } - depth-- + return iNdEx, nil + case 3: + for { + var innerWire uint64 + var start int = iNdEx + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowEnvelope + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + innerWire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + innerWireType := int(innerWire & 0x7) + if innerWireType == 4 { + break + } + next, err := skipEnvelope(dAtA[start:]) + if err != nil { + return 0, err + } + iNdEx = start + next + if iNdEx < 0 { + return 0, ErrInvalidLengthEnvelope + } + } + return iNdEx, nil + case 4: + return iNdEx, nil case 5: iNdEx += 4 + return iNdEx, nil default: return 0, fmt.Errorf("proto: illegal wireType %d", wireType) } - if iNdEx < 0 { - return 0, ErrInvalidLengthEnvelope - } - if depth == 0 { - return iNdEx, nil - } } - return 0, io.ErrUnexpectedEOF + panic("unreachable") } var ( - ErrInvalidLengthEnvelope = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowEnvelope = fmt.Errorf("proto: integer overflow") - ErrUnexpectedEndOfGroupEnvelope = fmt.Errorf("proto: unexpected end of group") + ErrInvalidLengthEnvelope = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowEnvelope = fmt.Errorf("proto: integer overflow") ) diff --git a/crypto/pb/envelope.proto b/record/pb/envelope.proto similarity index 57% rename from crypto/pb/envelope.proto rename to record/pb/envelope.proto index 1b96a3a..a0fb2b8 100644 --- a/crypto/pb/envelope.proto +++ b/record/pb/envelope.proto @@ -1,11 +1,11 @@ syntax = "proto3"; -package crypto.pb; +package record.pb; -import "crypto.proto"; +import "crypto/pb/crypto.proto"; message SignedEnvelope { - PublicKey publicKey = 1; + crypto.pb.PublicKey publicKey = 1; bytes payloadType = 2; bytes payload = 3; bytes signature = 4; diff --git a/routing/state.go b/routing/state.go index c626685..28d06db 100644 --- a/routing/state.go +++ b/routing/state.go @@ -2,12 +2,14 @@ package routing import ( "errors" + "time" + "github.com/gogo/protobuf/proto" "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/peer" + "github.com/libp2p/go-libp2p-core/record" pb "github.com/libp2p/go-libp2p-core/routing/pb" ma "github.com/multiformats/go-multiaddr" - "time" ) // The domain string used for routing state records contained in a SignedEnvelope. @@ -29,7 +31,7 @@ type SignedRoutingState struct { // Envelope contains the signature and serialized RoutingStateRecord protobuf. // Although it uses a bit - Envelope *crypto.SignedEnvelope + Envelope *record.SignedEnvelope } // MakeSignedRoutingState returns a SignedRoutingState record containing the given multiaddrs, @@ -54,7 +56,7 @@ func MakeSignedRoutingState(privKey crypto.PrivKey, addrs []ma.Multiaddr) (*Sign if err != nil { return nil, err } - envelope, err := crypto.MakeEnvelope(privKey, StateEnvelopeDomain, StateEnvelopePayloadType, payload) + envelope, err := record.MakeEnvelope(privKey, StateEnvelopeDomain, StateEnvelopePayloadType, payload) if err != nil { return nil, err } @@ -71,7 +73,7 @@ func MakeSignedRoutingState(privKey crypto.PrivKey, addrs []ma.Multiaddr) (*Sign // Fails if the signature is invalid, if the envelope has an unexpected payload type, // if deserialization of the envelope or its inner payload fails. func UnmarshalSignedRoutingState(envelopeBytes []byte) (*SignedRoutingState, error) { - envelope, err := crypto.OpenEnvelope(envelopeBytes, StateEnvelopeDomain) + envelope, err := record.ConsumeEnvelope(envelopeBytes, StateEnvelopeDomain) if err != nil { return nil, err } @@ -82,7 +84,7 @@ func UnmarshalSignedRoutingState(envelopeBytes []byte) (*SignedRoutingState, err // a RoutingStateRecord protobuf and returns a SignedRoutingState record. // Fails if the signature is invalid, if the envelope has an unexpected payload type, // or if deserialization of the envelope payload fails. -func SignedRoutingStateFromEnvelope(envelope *crypto.SignedEnvelope) (*SignedRoutingState, error) { +func SignedRoutingStateFromEnvelope(envelope *record.SignedEnvelope) (*SignedRoutingState, error) { var msg pb.RoutingStateRecord err := proto.Unmarshal(envelope.Payload, &msg) if err != nil {