From d7bab37119de932e2b5b1dce56b3e3250145dba6 Mon Sep 17 00:00:00 2001 From: Giovanni Petrantoni Date: Wed, 15 Jul 2020 12:51:33 +0900 Subject: [PATCH 1/3] Fix gossip messages seqno according to spec (#253) * Fix gossip messages seqno according to spec * Add peers back to gossipsub table, slow down heartbeat * Revert "Add peers back to gossipsub table, slow down heartbeat" This reverts commit 01e2e62172a7793bb17f0eb8314e2faeb2682173. * make seqno a threadvar, remove from peerinfo * seqno refactor, into pubsub --- libp2p/protocols/pubsub/floodsub.nim | 3 ++- libp2p/protocols/pubsub/gossipsub.nim | 3 ++- libp2p/protocols/pubsub/pubsub.nim | 1 + libp2p/protocols/pubsub/pubsubpeer.nim | 3 ++- libp2p/protocols/pubsub/rpc/message.nim | 8 +++----- tests/pubsub/testgossipinternal.nim | 16 ++++++++++++---- tests/pubsub/testmessage.nim | 3 ++- 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/libp2p/protocols/pubsub/floodsub.nim b/libp2p/protocols/pubsub/floodsub.nim index a0fa44c64..3d8f70bc4 100644 --- a/libp2p/protocols/pubsub/floodsub.nim +++ b/libp2p/protocols/pubsub/floodsub.nim @@ -136,7 +136,8 @@ method publish*(f: FloodSub, return trace "publishing on topic", name = topic - let msg = Message.init(f.peerInfo, data, topic, f.sign) + inc f.msgSeqno + let msg = Message.init(f.peerInfo, data, topic, f.msgSeqno, f.sign) # start the future but do not wait yet let (published, failed) = await f.sendHelper(f.floodsub.getOrDefault(topic), @[msg]) for p in failed: diff --git a/libp2p/protocols/pubsub/gossipsub.nim b/libp2p/protocols/pubsub/gossipsub.nim index 708a19b4e..5fc2c4b50 100644 --- a/libp2p/protocols/pubsub/gossipsub.nim +++ b/libp2p/protocols/pubsub/gossipsub.nim @@ -506,8 +506,9 @@ method publish*(g: GossipSub, # time g.lastFanoutPubSub[topic] = Moment.fromNow(GossipSubFanoutTTL) + inc g.msgSeqno let - msg = Message.init(g.peerInfo, data, topic, g.sign) + msg = Message.init(g.peerInfo, data, topic, g.msgSeqno, g.sign) msgId = g.msgIdProvider(msg) trace "publishing on topic", diff --git a/libp2p/protocols/pubsub/pubsub.nim b/libp2p/protocols/pubsub/pubsub.nim index d9679fa85..66027e2d2 100644 --- a/libp2p/protocols/pubsub/pubsub.nim +++ b/libp2p/protocols/pubsub/pubsub.nim @@ -60,6 +60,7 @@ type validators*: Table[string, HashSet[ValidatorHandler]] observers: ref seq[PubSubObserver] # ref as in smart_ptr msgIdProvider*: MsgIdProvider # Turn message into message id (not nil) + msgSeqno*: uint64 proc hasPeerID*(t: PeerTable, topic, peerId: string): bool = # unefficient but used only in tests! diff --git a/libp2p/protocols/pubsub/pubsubpeer.nim b/libp2p/protocols/pubsub/pubsubpeer.nim index f5fcd1719..ca72a3224 100644 --- a/libp2p/protocols/pubsub/pubsubpeer.nim +++ b/libp2p/protocols/pubsub/pubsubpeer.nim @@ -187,8 +187,9 @@ proc sendMsg*(p: PubSubPeer, peerId: PeerID, topic: string, data: seq[byte], + seqno: uint64, sign: bool): Future[void] {.gcsafe.} = - p.send(@[RPCMsg(messages: @[Message.init(p.peerInfo, data, topic, sign)])]) + p.send(@[RPCMsg(messages: @[Message.init(p.peerInfo, data, topic, seqno, sign)])]) proc sendGraft*(p: PubSubPeer, topics: seq[string]) {.async.} = try: diff --git a/libp2p/protocols/pubsub/rpc/message.nim b/libp2p/protocols/pubsub/rpc/message.nim index 9ff941853..0eecd548c 100644 --- a/libp2p/protocols/pubsub/rpc/message.nim +++ b/libp2p/protocols/pubsub/rpc/message.nim @@ -19,6 +19,7 @@ import messages, protobuf, ../../../peerinfo, ../../../crypto/crypto, ../../../protobuf/minprotobuf +import stew/endians2 logScope: topics = "pubsubmessage" @@ -56,15 +57,12 @@ proc init*( p: PeerInfo, data: seq[byte], topic: string, + seqno: uint64, sign: bool = true): Message {.gcsafe, raises: [CatchableError, Defect].} = - var seqno: seq[byte] = newSeq[byte](8) - if randomBytes(addr seqno[0], 8) <= 0: - raise (ref CatchableError)(msg: "Cannot get randomness for message") - result = Message( fromPeer: p.peerId, data: data, - seqno: seqno, + seqno: @(seqno.toBytesBE), # unefficient, fine for now topicIDs: @[topic]) if sign and p.publicKey.isSome: diff --git a/tests/pubsub/testgossipinternal.nim b/tests/pubsub/testgossipinternal.nim index be0127873..7ba6cfac8 100644 --- a/tests/pubsub/testgossipinternal.nim +++ b/tests/pubsub/testgossipinternal.nim @@ -227,12 +227,14 @@ suite "GossipSub internal": gossipSub.gossipsub[topic].incl(peer) # generate messages + var seqno = 0'u64 for i in 0..5: let conn = newBufferStream(noop) conns &= conn let peerInfo = randomPeerInfo() conn.peerInfo = peerInfo - let msg = Message.init(peerInfo, ("HELLO" & $i).toBytes(), topic, false) + inc seqno + let msg = Message.init(peerInfo, ("HELLO" & $i).toBytes(), topic, seqno, false) gossipSub.mcache.put(gossipSub.msgIdProvider(msg), msg) check gossipSub.fanout[topic].len == 15 @@ -276,12 +278,14 @@ suite "GossipSub internal": gossipSub.gossipsub[topic].incl(peer) # generate messages + var seqno = 0'u64 for i in 0..5: let conn = newBufferStream(noop) conns &= conn let peerInfo = randomPeerInfo() conn.peerInfo = peerInfo - let msg = Message.init(peerInfo, ("HELLO" & $i).toBytes(), topic, false) + inc seqno + let msg = Message.init(peerInfo, ("HELLO" & $i).toBytes(), topic, seqno, false) gossipSub.mcache.put(gossipSub.msgIdProvider(msg), msg) let peers = gossipSub.getGossipPeers() @@ -318,12 +322,14 @@ suite "GossipSub internal": gossipSub.gossipsub[topic].incl(peer) # generate messages + var seqno = 0'u64 for i in 0..5: let conn = newBufferStream(noop) conns &= conn let peerInfo = randomPeerInfo() conn.peerInfo = peerInfo - let msg = Message.init(peerInfo, ("HELLO" & $i).toBytes(), topic, false) + inc seqno + let msg = Message.init(peerInfo, ("HELLO" & $i).toBytes(), topic, seqno, false) gossipSub.mcache.put(gossipSub.msgIdProvider(msg), msg) let peers = gossipSub.getGossipPeers() @@ -360,12 +366,14 @@ suite "GossipSub internal": gossipSub.fanout[topic].incl(peer) # generate messages + var seqno = 0'u64 for i in 0..5: let conn = newBufferStream(noop) conns &= conn let peerInfo = randomPeerInfo() conn.peerInfo = peerInfo - let msg = Message.init(peerInfo, ("bar" & $i).toBytes(), topic, false) + inc seqno + let msg = Message.init(peerInfo, ("bar" & $i).toBytes(), topic, seqno, false) gossipSub.mcache.put(gossipSub.msgIdProvider(msg), msg) let peers = gossipSub.getGossipPeers() diff --git a/tests/pubsub/testmessage.nim b/tests/pubsub/testmessage.nim index 48acffcef..571a0566c 100644 --- a/tests/pubsub/testmessage.nim +++ b/tests/pubsub/testmessage.nim @@ -11,8 +11,9 @@ let rng = newRng() suite "Message": test "signature": + var seqno = 11'u64 let peer = PeerInfo.init(PrivateKey.random(ECDSA, rng[]).get()) - msg = Message.init(peer, @[], "topic", sign = true) + msg = Message.init(peer, @[], "topic", seqno, sign = true) check verify(msg, peer) From 9eb5828a420f98d2c5895f8bffc108d9770313ef Mon Sep 17 00:00:00 2001 From: Eugene Kabanov Date: Wed, 15 Jul 2020 11:24:04 +0300 Subject: [PATCH 2/3] Fix #266. (#270) * Fix security issue #266. * Add more tests. * Fix PeerID tests should not use RSA-512 keys. * Fix crypto tests to use vectors with 2048+ bits. * Disable 4096bit RSA key generation for CI debug runs. --- libp2p/crypto/rsa.nim | 16 +- tests/testcrypto.nim | 117 ++++++++------ tests/testpeer.nim | 155 ++++++++++++++----- tests/testrsa.nim | 350 +++++++++++++++++++++++------------------- 4 files changed, 398 insertions(+), 240 deletions(-) diff --git a/libp2p/crypto/rsa.nim b/libp2p/crypto/rsa.nim index 5f7c5a50b..f325bc45f 100644 --- a/libp2p/crypto/rsa.nim +++ b/libp2p/crypto/rsa.nim @@ -23,11 +23,13 @@ import stew/results export results const - DefaultPublicExponent* = 3'u32 + DefaultPublicExponent* = 65537'u32 ## Default value for RSA public exponent. - MinKeySize* = 512 + ## https://golang.org/src/crypto/rsa/rsa.go#226 + MinKeySize* = 2048 ## Minimal allowed RSA key size in bits. - DefaultKeySize* = 2048 + ## https://github.com/libp2p/go-libp2p-core/blob/master/crypto/rsa_common.go#L13 + DefaultKeySize* = 3072 ## Default RSA key size in bits. RsaOidSha1* = [ @@ -78,7 +80,8 @@ type RsaError* = enum RsaGenError, RsaKeyIncorrectError, - RsaSignatureError + RsaSignatureError, + RsaLowSecurityError RsaResult*[T] = Result[T, RsaError] @@ -118,9 +121,12 @@ proc random*[T: RsaKP](t: typedesc[T], rng: var BrHmacDrbgContext, ## algorithm. ## ## ``bits`` number of bits in RSA key, must be in - ## range [512, 4096] (default = 2048). + ## range [2048, 4096] (default = 3072). ## ## ``pubexp`` is RSA public exponent, which must be prime (default = 3). + if bits < MinKeySize: + return err(RsaLowSecurityError) + let sko = 0 pko = brRsaPrivateKeyBufferSize(bits) diff --git a/tests/testcrypto.nim b/tests/testcrypto.nim index 79ac62d7e..287e6bd04 100644 --- a/tests/testcrypto.nim +++ b/tests/testcrypto.nim @@ -17,37 +17,6 @@ when defined(nimHasUsed): {.used.} const PrivateKeys = [ - """080012BE023082013A020100024100AD8A7D5B0EB7C852C1464E4567651F412C - 692534E1600FDC5BDA9EDBFA9927AF0FFA7C52599BE62999E085C345D21C8D43 - 627CCC0E16D695C770E26D220AE709020301000102403B58BCFDC2CEBEC6EE29 - A8E2BB352DB71004F5205C62898A062F815C211AF722AADD1087C68E24015417 - E36632EEA2D2B1A20FF3D283A4C3881C724B3919FCF1022100D4BD1D7D15B328 - 4EFBBB5B2C6A11CFB7BCD9000C1010D69C808F370DFA751D47022100D0D4CB1B - BAB9F8C11376744F0D96D6E4BC2B12F4DF768AC9EEA79DAB24C8C12F02210089 - A3FA0A4E19E64083FA8A58C81FD2070CF651637C9988612584839855ADA44D02 - 20137DD91B5479693B743A992E8BC1297B9E08933361EC2996217D699D00C8F0 - 2702201A8D0497E8962E0AF5B15CA03085070F735B39876C54BA8908EDC87C42 - D9DADC""", - """080012E0043082025C02010002818100B91DCD5578C2FA3EA7D1DF62F59DEEDA - 834ECE568844554163E1803CDF4C2988BE182B8F957D3405BC745A33DA1E714A - BA13C316683767735ED72AE8B35648FD6E33A49D696FADB6499C63A09204F0BF - 77B44D3917DC746FB7B52040725746A3140E96E8682A02A4767C280DEFBE58C6 - ACB6DD1EE63F3F589F4C7941B27957F70203010001028180364CDFA66ABE06D9 - CA306DEE814DCA7A9E79C75CEAABE0B645AE2807B3188C36684C7EBAA7870A73 - 844C3D0968B9F5518E33ADCE2CD3D3ACABED41D0F08A26EE705E4277AF0D5816 - 491C626F6D2D5396A741B83D0730401B061A8C2DE19E18B07CE8798F387D4C79 - 78C92DA6F4080A45F93D0ADE50CF7E139DB7288EBA72D341024100DE9B394C60 - 6C396B65756CE4A2E3D77F66C3AFBDA0C09C3CD87EB60A35018BDA7D82FF119F - F6EBAAEC868E8ED1AD215C9EBF4073F26A1B187CDAFC862540B1C3024100D4E2 - D92E126D92B6795EBD7991A2D8A35147FAB73CFA4BBE74644CE8D1AEDE10A2FF - 33CEDF0B4961A89B1121C58D233309589BFBDE72D374ECB6A6859B0CC9BD0241 - 008B0ABB9C6831D11FB2CCC8CA7AB003BF9109B3B0A7430793BDCA4F9C4A857F - BC665F3740E400D02CFFF5FFFF571A63D73D54CA4661E942A965FB4675E7B8E4 - 0F02404A175D90DC57085DBBA60E79B6072030CF04272D08EAEFDBAC349053E3 - 4E61F916518D9D6D21477BD6AB896DE08C7B78B2C3051EC6CFDDEA6ECC2EEE87 - 050F5D02407BC18B68F253353D7DB89AA85A7CEC4069DAEB894828F83931ADC4 - 9A3255DFD0C9AAF9D09EBFAE831588A360EB0EEB89E45C9D136DCEEC8EAEBD3E - 65FBA9EE01""", """080012A809308204A40201000282010100C8B014EC01E135D635F7E246BA7D42 3E2B36F45B052F0B644DA6B1B6DD964696F279BB9BB5F5C41EE3204F74EF70C2 28C8CDD97E4F19094274EB01C292EC25055CB3CECD1A41E8919A0C111602B1B8 @@ -86,6 +55,62 @@ const 8C0E4AEA1453952818FF9AD2FACCED7CE3A037D8545C151F57D35DED691CA53F 8A5336590F7B080805A46701B01C9F9919E3890CA1A0373D909373638B57FD0C 87627491C41F1BF1E9643DE7B4""", + """080012E80D308206E40201000282018100F50BABF75636386F1C606950F495F5 + 3149B648408E5D61B7EB0C9F96681CC685F35358A3B91C2AD848C819902FFCD2 + D3B1E46F806DC464109CEE43BDA4BDD01EE8A3F035B654C965255B5834A34C6F + 3543E66A0D61545B42F2BF819E76B602F6F6ADECB6EC81FBCFB0EBC9CAEFC7AA + D95DB7E91B52C4CF3BA5B25CCADB117FFE2488A906FFD56318A39B3054E3D221 + D95334A385A6AF9A00D51D119D953E2ADCDFF57708DA27602A2BF4C4B9D62059 + 35A22313086C8531017EF749B407B76A25280DD079CB7A0FACE87BA6EDA0C945 + 39E8CBC91D7E709E3BD32F266D4949B829A01303513AC3BF9777A8A6657AE1F2 + CDF18FF026C23321B2BFFAAD68250D054F7B24B98A18AF270EF7A5FF918BCABA + 3BDF1F44C0345CFD4B4CDDA9D51486D281F9560D69C0C30718118AD76C6BAB4A + E585A188A16A1148131D558555B36E8348065C7441D6D823906E13EF6F287310 + 9F3AF729637379CF07028FBFC34411DCDC765C38BBFC632556B29415EDC70DF8 + 352EFEF1BED37656565ECB8598EF9D7B8502030100010282018100B34C5C02B9 + 392927DBBA8BB4E99203FB9174E7FF1C6C25811E62792EE37512A9967648A85B + 5CC9D8C1C0703BF970E38E745EB25B6575CAC6011DB1C14496827EEB38711DCC + 10D3DFC76AC3EA9805E865300E9B50B903B15716299C1D27BD5C03FA511EA066 + 03620E00E5361CE03FD7B9FE81FFF5B7253C80E767CFCFBA655EA2C5769D3EFA + D8D70E833F9D28B01DE418917235A82863DAAA7B452ECB801521E5C5956508BC + 1F0D952DDE864777EEE9777DF06E0FBEDFE210B72717EDBBABAFE49B872FB653 + 5C35D539775E7B06241E7AE2C6C6B16E7A62334B60D4E034C514DD4EDB306AC1 + 151981114A555E69EB2C3CD5E71FB825151381506E7A574FB3448990CB3E11A8 + 832269C005E9C26601580492B1CC9A7C0FCB5008F1019781A6E06EAC47F270B5 + 333B9BA54F5BDBF73DC36B650AE320A269F2E723DD811A9B557EE001EC7EA037 + 3B88DD277FFA31C82FAC1C80657BCF175E81BD13C55A6F14F8D3FC8F2BBBC57D + CEFC0A697A57B27CBD82CF5F2C41DB0E9308A3D7C8374B970D786D0281C100F5 + D4DE76DC843D98DCF3D49787421D00E7CED08325863FA72793B0CBC339A29D0C + 1762BBD382EEE3CFD77F99343E8A9D1AB561E10286819A892E0C638C8F95904C + 2BA43BD83270817401158DB986F4DB41AAAFF769799319BBC958299AEA1809CA + A76A13FDE12D35830946DDCA39C1F828EE997F5F6E11365BC0A8417D2BB72CE8 + A9D31838D6EE6988E2BE8ACB7C50A64EBA9E78CB2519F9A0466411D250EB1F80 + 122071CD6D104D4845F5669A8DE63DECE598549827492306C898655BA6F49702 + 81C100FF2E7B01C2318C22AFE6D3B210542FC2BCC35EF128209C672AACCD997C + 56FB7263795ECA9EEB4C8E34E5A1D0D56209159311FB929D4839AAB873E5E194 + 6F5768C1798BB5CBA763D3BAE0F54456CFD812CFFA407731C5870E06CCBC9BF0 + 43D8A3DBD050AE0EC1116AD702531398FE76C9323C0B20277671859F15EB09E7 + FC84F4CC0122AED4C0C3ED9579DF2D0D0B0E625B4E2A04C893F3DCD7D00B2B57 + C6D7F7825C6C26EB4D9A74BA6D78046B6218B8420816ED46683CDD27E3AB3BE2 + 0748430281C100E6851183662F7F4F6BCA1DCD65E1822F0A306E68BC7452161D + CFA996B95FBF7403159ED94072E42EAE04873B93B91DE44A5A52095C83E2DE95 + CA9F856E166A8AA052C033F9FCFA70F9E5D65F4ADD1998CCD011283138F6CEC7 + BFFBB32FF85A483C4796FA5E349C5F349C2B5EE9361604EDE527FD3FF087A570 + 8E8D78B3B69D93382D9FAC7408601288710F7A396C3E7865152C6B5B1B546B37 + 772A7967BDC76D877BF7FE9F42A969BEDF134A7E9D978CF42B2C180F81AF5C70 + 0D0492EF49F4CB0281C0139CD1ED6FAC7935CD49336A1321354E854D8AC5842D + 0578546A559EFF547C1CED924D7BCB3D10743992909E495830948CEA2BE8B7D7 + EE3EB5D08A918CE405D6D24FF231F5C616C11632E606EB992BE53E40E0A58898 + C2E98C123E48C9ED9CFFF30C0EB76200EAC2C865B056E8425E84D62C913A566E + 1F0983119F2FF2B990CA73C77E8FEB60296EE0372EB4FAB4A7D55D6923879C96 + EA7E45C402A7C1265240FEAEF5CD03F6C1F148C6D31B9BD93D4724241507058B + 4B6A3FFF9A908EADBA9B0281C07259AACD3FD4D7A3B62814F9F3ADD57972CF52 + AD319505589E3E838F6A97D45F9965BD9B7A534C007A1B5BEA17D79971F4BCAE + ECCAF0283CEFBB81DCED9AB547221520E3D574746A7390303AD0C64F77657575 + 9D0D220B3269B739D2AD84501EAF84F819669AA926BA9F058C40BA1FCE327C38 + 1957F275AD7C6A832086438063A88F8502EC112D06726B9BF550B6E0720B8FDC + 3DE36D24F6E1A123779CE59D2340A36E025D47E4B1F8B49BBEF1E0FEEB70AE42 + 8B86865554F05081A22BF3F399""", """080012AD12308209290201000282020100E35735127777C52E66252B014E9650 01F3A515317B90DDAC8671F4F820AE67308DE2AA4162E99522CD6DA7EB9D7DB6 06489FEB77341A8FA058FAC832EE6EE5E978D512FF79461FC419A23B27C39C81 @@ -170,16 +195,6 @@ const ] PublicKeys = [ - """0800125E305C300D06092A864886F70D0101010500034B003048024100AD8A7D - 5B0EB7C852C1464E4567651F412C692534E1600FDC5BDA9EDBFA9927AF0FFA7C - 52599BE62999E085C345D21C8D43627CCC0E16D695C770E26D220AE709020301 - 0001""", - """080012A20130819F300D06092A864886F70D010101050003818D003081890281 - 8100B91DCD5578C2FA3EA7D1DF62F59DEEDA834ECE568844554163E1803CDF4C - 2988BE182B8F957D3405BC745A33DA1E714ABA13C316683767735ED72AE8B356 - 48FD6E33A49D696FADB6499C63A09204F0BF77B44D3917DC746FB7B520407257 - 46A3140E96E8682A02A4767C280DEFBE58C6ACB6DD1EE63F3F589F4C7941B279 - 57F70203010001""", """080012A60230820122300D06092A864886F70D01010105000382010F00308201 0A0282010100C8B014EC01E135D635F7E246BA7D423E2B36F45B052F0B644DA6 B1B6DD964696F279BB9BB5F5C41EE3204F74EF70C228C8CDD97E4F19094274EB @@ -190,6 +205,20 @@ const 4E2745A05E4D31F364C2C50C0F5C9A6889378051BF05669EA905C2370548D6EF 2CF66A4A5DC23F10B7614BC43CF0BFCC8711B6FF9F645FC2E5713ED1CB597539 2B8C865202690203010001""", + """080012A603308201A2300D06092A864886F70D01010105000382018F00308201 + 8A0282018100F50BABF75636386F1C606950F495F53149B648408E5D61B7EB0C + 9F96681CC685F35358A3B91C2AD848C819902FFCD2D3B1E46F806DC464109CEE + 43BDA4BDD01EE8A3F035B654C965255B5834A34C6F3543E66A0D61545B42F2BF + 819E76B602F6F6ADECB6EC81FBCFB0EBC9CAEFC7AAD95DB7E91B52C4CF3BA5B2 + 5CCADB117FFE2488A906FFD56318A39B3054E3D221D95334A385A6AF9A00D51D + 119D953E2ADCDFF57708DA27602A2BF4C4B9D6205935A22313086C8531017EF7 + 49B407B76A25280DD079CB7A0FACE87BA6EDA0C94539E8CBC91D7E709E3BD32F + 266D4949B829A01303513AC3BF9777A8A6657AE1F2CDF18FF026C23321B2BFFA + AD68250D054F7B24B98A18AF270EF7A5FF918BCABA3BDF1F44C0345CFD4B4CDD + A9D51486D281F9560D69C0C30718118AD76C6BAB4AE585A188A16A1148131D55 + 8555B36E8348065C7441D6D823906E13EF6F2873109F3AF729637379CF07028F + BFC34411DCDC765C38BBFC632556B29415EDC70DF8352EFEF1BED37656565ECB + 8598EF9D7B850203010001""", """080012A60430820222300D06092A864886F70D01010105000382020F00308202 0A0282020100E35735127777C52E66252B014E965001F3A515317B90DDAC8671 F4F820AE67308DE2AA4162E99522CD6DA7EB9D7DB606489FEB77341A8FA058FA @@ -396,10 +425,10 @@ suite "Key interface test suite": sig2.verify(bmsg, pubkey) == true recsig2.verify(bmsg, recpub2) == true - for i in 0..<5: - var seckey = PrivateKey.random(RSA, rng[], 512).get() + for i in 0 ..< 2: + var seckey = PrivateKey.random(RSA, rng[], 2048).get() var pubkey = seckey.getKey().get() - var pair = KeyPair.random(RSA, rng[], 512).get() + var pair = KeyPair.random(RSA, rng[], 2048).get() var sig1 = pair.seckey.sign(bmsg).get() var sig2 = seckey.sign(bmsg).get() var sersig1 = sig1.getBytes() diff --git a/tests/testpeer.nim b/tests/testpeer.nim index 2f497951b..c9425f0cf 100644 --- a/tests/testpeer.nim +++ b/tests/testpeer.nim @@ -20,40 +20,121 @@ const # https://github.com/libp2p/go-libp2p-crypto/blob/master/key.go # https://github.com/libp2p/go-libp2p-peer/blob/master/peer.go PrivateKeys = [ - # RSA512 keys - """080012BE023082013A020100024100CCE59D6260E7D511DB3DD2C8A3CD5B0F1 - D16C8A6FD6A331A211305B677EADF48A7B06F0A74088E96458F23B453EC03A6 - 63985D32105017A26F7EEE8CF43FAB110203010001024056D5609396B9E9447 - E037F56E4845CF8D4BD19201BF96BDAC0F58CD03D95DE35EBB015A88FD8B8CF - 731F5604C3895E05A1268D218E64E0C077422AAB3762E801022100F5C1FAAC2 - 59F7A765C2F7B5639EF537CFAB102989924552C0EB3608936623DC1022100D5 - 6FAFBD6ED32E7C02D310CC5355EA4B85704D1F73AECCCAB28BDD3BBEC461510 - 220056286B796EB73A77C459E12399AC0DD9030A8B9E741208CA295D39BAE11 - 6101022016D390070170427B6ECDE5F104449EFF1EB148FE412003B4F5ED716 - 6AF5B4F51022100B9F2F54717615B6B34C08571098BAE081B5DDA58A35440BC - BD0222EB250682BB""", - """080012BD0230820139020100024100A0C957A3EC20A90D511F732ECC650E4EA - EC501EC8349E8891BAF786E6AB424B5665AC229BCB241217FFC51FB7A443C85 - 688CE0D12A4C9C267B511488B537D2BB0203010001024068AD8CA448F79E77F - 007A4AAA4216AA6293C539B52824CA4EEE45768B3896297BABA5CFDAE4D1345 - 3BF3B7CEC8DF4623693B7CA7F046982A6B0CFEC722338D19022100CEC09F191 - 08A63E059DE14B057D08D81D6F8583D9CDCB89300A8D11DED084C4D022100C7 - 15CDBEDAF98D9F0A73DF4880A511B70E17835DA94C32D765B57E88AC827F270 - 220040684127CDBF4EDDD52119D0D58A628AD02D0D50244D9E322D4269E2A8B - 4AD102201316D81F90C794F8C6FBCD511A64B98A1FE3C948D5CEE4A11AB3886 - A7A8018C9022075677C2486F633F74E5718681D0C75477B62F0CD14A5C2226A - 11DEC5CFA96299""", - """080012C0023082013C020100024100D6E4788FBF4FCD607D6F61E01E910CE58 - 14B8BBA5A0F878EDF1C5ACC46A7C3FDECDEEE990D67C3DE5AE4A0301A1621BA - 1903938D08451F475393E2C863A8FE230203010001024100980168DE95BF475 - 197A8F01CEEC23FD3205065F85DB85A754F90E91152715D8AB693A37504D700 - 703C09F3B171BB5441CD6B5BF462FADDAAB896F2E4047D8681022100DFEE0F7 - 3DC6C9DC269E44A722007D937238299EEB7D07CE7B527AD9EE40A30E1022100 - F5AB120334B11F27C475385B2769448A53D40B47AA48D40AF623C7E4CB525B8 - 3022100B340507451754CCFAC8E471D9610524BB60C3C867FF3CF987745DCD0 - 7D00BF61022100DBCAC400CE41B18818C26F3B4E9565D7048CEFD29C4D3038E - 662B8D2BFFDDD1502206BC3E38AA82EBA2B99D2FF56AB19B2CE118B9A8A2AD1 - AEAF45F3ED4FE957619F""", + # RSA2048 keys + """080012A709308204A30201000282010100C277609AE7F5A06157D57A6EAACFA2 + 1CC01049AF18B9DE167B8B3B933487E9403E915FF3E7896932F4DD66A8B24061 + CC88F8650ED50E3C28026A83A018D994912491580B8FD70313EAB2D03AB582EA + 3B3DEB60133743CA0F15D9F844C1333D64DADD961CFF9E780A6D7F2245A838A0 + FF991955E2958D9B6781D6FD15E3C350D702377EB01823E64927A7CF7098801C + ACF60F5DDEDB64FBA27143F54878668657945D878981839EDE691393388F75E0 + F5948FE2EC86CAF2FD8882A57566E004A647721F47F2A82FBFBCD9F481D8DC74 + 0ED8A2FD9164367958FC55C4ABEC99D4ADCD8D841C616285D3076DE688045359 + 3E3E2B9811100A4B8E2C7E6DC5D5B3C93702030100010282010100913BB8B158 + 0550A7028313B1ECEDAEC3CC091E0E9FE7C85E801C06FD346140A953511D193A + 559C748AFD82FB004D26FD2B5A5F9709355D66000FEE87B5A761D6583A184862 + 3F9133E1B773DE34CB3605D283A43815B11209DC26F2CCDE81571BA87D8EEBCF + 58598C682467C3201452E1314503A87A4ACCC22BED150CB112A09EF5E9168FAD + E3AB135DD41B455B2754251E557D6AEC20B7EC9B38B512F1A93F40CD31D59071 + 67555D17A72A00E4BED5857D3CF9965016018F4DD0567A4C9440D8AB1C7A82CA + E3C2A34EF8CCDB81E3D070ACE7681CBDF5A438E15F6F2821D51F654467CBD509 + 576D5EBA6F3FF7B15F38FA92AF7F9CA92C0A41E10F038647920A4102818100E0 + 17CB68FAD59E92C5CA8F65C3C95900789D092755343A667ACA8C5BAC626EE30F + 789155F0E07D7D00B8132C502CF8D02CF7D7699C174193BCC5B821F3792AF060 + E838DB757929686AFF95BF9966A7B11951E16FEC07B09C16C2C27A3B1690D02E + 4D24E89C0BEE63A786CA70AA9DD7BF78E59CF364A194A858949ABA32CB296102 + 818100DE27B098887C0916D1F6CF7782A6B5F7C8A699665B49A5557317582329 + A61E411E36BEB2050390531E43466356D957825ECF49F889F2F4C1FCFD2B8855 + ACBB2663DF7F362F6FCCCE883B850F215A3E37D752E20549B429A0A90149B761 + A8D566013893D0B7A5E14D78F2237205ADB099D43A01CF573FB61E6DD9431407 + D501970281800BC7946CB74AC56427BB87202D5387372C1FED1C413156A48E6D + D944F461D43C6152D028F959839F2F8B7D8E85C4676BE20141348EFCF5F88322 + CD94134D5A417A869A7E86C550B4E972F7F733641F0A832F37AF7F73C407E076 + 6A6CAC707A3A4744CCDCE15F0B2FF7DD7104CD2330F522B0C7385020E2449B2B + 6C9ABF12C8E10281803ED36EA7D4816F789AAC7803CE5923410DE7BF9E28D6ED + 00FB6970AD910BBFA69ECAD0A73BAFC4531D4DDD4C1EB59C7FEC1C2749388A13 + 1A0157FFC5B0A506D0569250419CACBB78F52210013567760D08C211D5790090 + 7D350E7E307457F0F1C60AA012F8BCDAB8B42996489ABE82211ED9C0C5486166 + 39B8CB26A3358BD1C5028180412A1CE4CE9DDCDFFBB2AC90DD466C21D75F13F1 + 415967EADFBD9E87AF3DB520B4D87B1B619DCB26F32B500D439A0A4E7FF44A2B + 6EAFE424672F5A249BEB74D72A4A04A2EE2B5546571A68C79BC290677039E9C4 + 84B192DA4B7C7F72C21695403D421238539216861160351B99FC0BBD0F9D1A76 + 6A900436BAD373F31AAFBEB7""", + """080012A709308204A302010002820101009C338EFF095FB3F9989288D3B0D1EF + 4A425E887A109DD9E212311EACB8AA6737B53F352FC4B948E9B5BE9943C98516 + E71A85DD7D4A49325133294CE82D4E262FCA647444907BE36727DE527892DDC0 + BEA272F767EC596429E1DD4545D7910DF82ABA72C71EBD8D4E34C6C0424BD6BD + 66265DAD0D85A8BF912CFECCEB9B63B6C09A6AD026D70FAD445BAEE39ACF6DF1 + AAF81CFF3D6207053DEE94569C24274B9307CEF9770385C1FE65D8B502526903 + D834678F17BABEA1F9850B58C54B72D5E52E13B5C3E796BC3B989CD9FD616598 + 4BEA473B3157A1A61C072ECECFB12E09EAB2EDE57A7B5F1BE9D49C8B5242F7DA + 90BBC967BE92C761134E69D2F9AB0451CD02030100010282010030F794CF6CDF + DCAAD562B294326D4DA7A8F0BBB610797BB17C647BAA47E5DB9FC22903826B18 + 6EBC1D6697E3814C40A6C850C8E39B23212C056EE0163505B7C0E9A0DE361459 + 522BA77AF1BDACC4E9C49966931AB82439DB4B5C337836A0D9913FCBDD6980C0 + 8988C7D0BABACDCD8EE874048FD89A5B115AF0911C2A8C37113608804DCB3D04 + CA34EA7E184A3011C42525D8B2B00B12D45CCD533E32D7014E5119CF51954591 + 0B41E6845019104B5D63907616DB0CF0EEB82C449DAB0D1073D118660972D337 + 7786A662CE219F5012F4DCB0F0E2257F3558EDF372321F740458B13DF762D8C0 + D51AF28024198EE6E134CE2C9CCEB3940FFF04D8A5A38980D42102818100C170 + DF5FB4F412E18FAEA20C7C8C768B59933B72B12986877763706CFB8DD0062781 + DBD2102CD419DDFF20C39FA3B19AF9A10D7A38F9C1D426666521E4BAC7B7847E + 9838BF9CF28E4B1D12B7FD54166FCA81095E5557486F98641C7BAB6A6EA55C9B + CE2F4ED6B56BA259F93D4D95B32505CA3D38F7680310D03C05D36F6F9E490281 + 8100CEB798EFF7EDC87E46A2FAD6E061DBBB0051366E5FD6234B857B8B0F5501 + BC3708D7A23DB9F029FA4A17DFA9D0E1E271E3AB3065732A6525C4C2A881831C + 2D35E56749AAF26AC51A3EDD6989C7E161B3421AEE565CBFBB9C779BEE4182BA + 6CF8AF844FADF56C9A54C85EB949447A16D3CD0188A81E191BA824E4376286EF + E7650281810080A59158B41E5264424B30A83F38A729FBB38828B99BD5454868 + 107FDA3830EDB6DE8D13C2001E3AE9C1DFC759E6E29A1F843553608AF19626C7 + 9860971E84EE0EA6A693AF1330D8E3297DA6714AB7F536E6E415218A7FB8FFCF + 2C862EB504CAE2B117C9AC93EC699DBA5AA0A375788399BB2B46347BAEF64296 + 78856A2A004902818009089CF65FAF5483B0328F23B4CA91FFAA13E27D10531B + 57C18D949626F0DC78CB3A212CF1633D7262AA886BB3652BB02E72DCBA923DD8 + 0DBE7B8341A5A92590A565CE225A55B0276577E794CDD75B9DA9D5E37482B91E + 49C8305849249D6105BD25DB158F141FEA74972F21B48C6153003280F657DA61 + 0B0811B119ED2BAEC102818077F48BB3C3D417DC4A3D1F871FCD21A6434F1F55 + B73DF98524011427524695562A63DAE828427D22897EF60CC5E6A8591E8305AC + 202749D7459EBB85E4D65BC9CC44E251F92BDFEA5279C2F06647A2A29C62E90E + 37C7A3A72EC0FD20D9073D397EA1D6723473368B28ADA577FA8C2E840EB769FE + CC8B5863789948D6A1550344""", + """080012A809308204A40201000282010100EF0EBAC68BA931EC25491A9E062F11 + 485E4BE8CE5A3C7ACB9B058DB956517B76AC2227AD24CFE3CCB75DD4B3C1059E + 5FF472FD07715E2DACC97D809AC8CA3C37FBFB6352B956C0F6B9B28CB9ED291C + 85FD2814D954DECFB366CC59F3B1C74D477CE9CC2177C9A70041C5C7848627FE + 4599FA1AFEFB7E2064E917C56123C717E8670E35ADBE7E0BF981A7104CCE17B3 + 3758D75F09B42FAEEB6A5ED2B458B798DA2F7C5BF020EF83F4F947C583CE5797 + D3E6CCF815040A13CCBA898EB3EDFB57C77A1C6A6D58122BA87BF7C8E581E0A4 + F6E96DD783025C9B7C1DDF8F0ABE286B2CEADB884FE1B3081F9648448D5A9319 + 9A355C7FCFEDF72925A6224BA1E64749290203010001028201003E0ECFD3BCEB + B646FC42E70300CDF5299939117D88154D374FCDF84595AFDF62263B539B7203 + F9B9EA1C149C794E3117EDC870011F9FF83173B8FDCAB0B7373CBD503C2F7C67 + 95FE36A4B03D8FE06D44273CE174ADC08DA09FDE0AB7E57DBC85035B5A1F920E + 2A2939E9D2EA873D18980EE9CB5A48052CF9A53D03833D1B710BE64B2D7FEE62 + 66BF2C2A002C9589D8B9D7710244078FE2F10D18F45962B86F4FF834B3A5C8D0 + 080C9F36CE556E7BB5D9AF79D91263A179457575C84009C5A219D23A99586762 + 16319E1D5D85723771625FF80BC7D6A6665515F70EED682BD8209057DF1F6326 + A1734D73370F54A388F08C7379F07929DF1CCEA3D813772F126502818100F939 + 9034445E74434F6E7794874866D793AC180E9C2DB1F37327CC9C085789279F23 + AF5BB5323379565AADF865FC2376D8DCEB0C79580525B7FD3CDBE8C8F6CDC295 + D852FF40462B0D7AA05B4265ECDA4C4A2C13364A772AD57524D17F8B38A65784 + 03DF83C2702CD98D1A03778181A738FD05FF2D80D3082D99D6253DBB8F970281 + 8100F58E689B42904F62BAE4AC2EA87D8EAD61B93CF19622B939FE22F95C1DBA + 8A53BA5555F9A321A6E7A83B3D4F4BDEDB95068A9BD42E7CD61B075FEB5FD26E + 9D79502CA3A9C080EB0C00126B0D5064EBEFFE437036533133259895EB18CD14 + 095D7968A63382F0DED2727AD8A495E0C5774E59B1199BFF568A914E616C9957 + 053F02818100C73F911D68478775C4CC5147EABC249457187007462F7624673D + 3B64C77A8C49A3FE18951E10EB7C2760C9D35C5DAF50B5E230FCC10A70DB0DF4 + A4B23FB263366F49F32FAFA80831254E9363427B5057DA44366689B21101AAB4 + 43E245B0DD72876720DD926E61D6497B787FAB0C5BC6805631742841E4F595D7 + 76904181713302818100DC6E154B62CA86E8FB1AB1D244A049872D158B36D76E + 9E5607E3BF7348A09EF2206FE078F34F0F341F99E6249BD817C7C49282E64B40 + F736563D6DDE9BD97E755B5A6724C851322E9895F011889CC00BB57536731DE5 + 29A3D340A9459F3B20590FD6B873BF5498C1D3D0A14FC5D77B8BE902ACEF6F51 + 1C8FD176CA1021EAB32302818011C9B0591DF6F20172B6DBE725C14CAEB91A6B + 8C112CC765A5CDAC993ADFA031465BEB01C55212D8D30402B2C479D072642C48 + 7998D80899881202CDAFA7C3CCD2B87493C4F8A5E40E84A8E63D93B95F370C0C + 15A353C1BF0E625DB36257A2A499AE5611ACED18EBF0AA669D45AA1DC0583268 + C41737A8ECEC1978543EDE3D96""", # Ed25519 keys """08011240B6F99B4E4422C516F1BD135B4D2B02AE62C48388CE31AFBA16496D2 42FABE09BF3848ADABAA9F1E1230A3B94EDD3247C2395397EAFB59790B86595 @@ -84,9 +165,9 @@ const ] PeerIDs = [ - "Qmdxy8GAu1pvi35xBAie9sMpMN4G9p6GK6WCNbSCDCDgyp", - "QmczLMwRH4cNaLZx1t7PTe5b7k6xKkd3RCJZSxmG3azXJK", - "QmS9Sg4ZA5Fd1hHjNNkqfuMUXX9QnEXxgvT5hfSRuKHp8p", + "QmeuZJbXrszW2jdT7GdduSjQskPU3S7vvGWKtKgDfkDvWs", + "QmeasUkAi1BhVUmopWzYJ5G1PGys9T5MZ2sPn87XTyaUAM", + "Qmc3PxhMhQja8N4t7mRDyGm2vHkvcxe5Kabp2iAig1DXHb", "12D3KooWSCxTfVvMBJbpF75PQmnFdxdBfC1ZxAGYbFc3U9MjALXz", "12D3KooWMdZbdEudjgnCvQLoSoiqhQ4ET2gaA1d4JpC1CBkUnfzn", "12D3KooWDFCm93uCnm8tVdk3DYxNeMFxMGBaywVSt8a8ULtdLoeX", diff --git a/tests/testrsa.nim b/tests/testrsa.nim index 18b7eb3a0..fb37036f0 100644 --- a/tests/testrsa.nim +++ b/tests/testrsa.nim @@ -13,9 +13,7 @@ import ../libp2p/crypto/[crypto, rsa] when defined(nimHasUsed): {.used.} const - TestsCount = 20 # number of random tests - - PrivateKeys = [ + NotAllowedPrivateKeys = [ """3082013902010002410093405660EDBF5DADAF93DD91E1B38F1EF086A3A0C6EA 38011E58E08D27B163A9506AB837F4AF5CA23338AB3BFBC0AC7A0FAF6B9EE3B1 27BD66083C7B272C1D370203010001024017EE5DDB74E823F6655197B1ECC77C @@ -46,6 +44,21 @@ const 00C2200411163DFAD0D56501AB1076A2CD7CC2FDB066CEAACD8212EC1F292C22 F1840B8C1E23D941436F2EB38873FB76DC649D4DB85FF026D1D5DF405481A2F7 F5""", + ] + + NotAllowedPublicKeys = [ + """305C300D06092A864886F70D0101010500034B00304802410093405660EDBF5D + ADAF93DD91E1B38F1EF086A3A0C6EA38011E58E08D27B163A9506AB837F4AF5C + A23338AB3BFBC0AC7A0FAF6B9EE3B127BD66083C7B272C1D370203010001""", + """30819F300D06092A864886F70D010101050003818D0030818902818100BDB0E9 + A7AF9865E318C8890A883D185F6D9F88868A5F586244CA6A07F298349EE52E38 + EA322D7E453AD2AA8B5019C25C50AEAAA59AFA57173F2B3B44501EF45BCC6C1F + 75BEBAC5C11D7861A8A9DAED68964DEFA6ABD0D9FE232E6BA9E97572B6F68C57 + 030CCBAE19E972D16338201B7C11E00364FE7D613CB3367BB6CF2C3BE5020301 + 0001""", + ] + + PrivateKeys = [ """308204A40201000282010100B7362C6653ED53C35C3AE663DA496C9B834FF34D 72DA98FA6DBF4AABA39FCA0901F58A1B1D205076D20010151DBCA8FC2693E14D 3502320D61E796E9C102C4EBB8F50B90616DE7FB8EA0A4BFC89BE542CC86DB7F @@ -84,6 +97,62 @@ const 8A97B09FF1DC131E7136B557002205E2ADE5DB114499F15AA1E3C6C7E6FE6381 04B6A67697C60C4113F613130403C554688C7046D6AC5F5364A658917E23D40F 1FDDD3DE4C9F51A9""", + """308206E402010002820181009E4E6CCBE33B69A9FB6A3CAEADDC47E26AE15E1F + F8C56B3126C4C66E549F86F0002A1CC3D0169F034825A3683AA0A579D844B415 + 698F6F2AA5907246D911CFCD69A5151A78BA5AD803A8FAF0184DAD4BF471469D + B91BE951FD12EE5C8047B536984873A4F79BC054BF0E0A4730A1B20C3CF92440 + B1ECC5FA608213AD823C10D9994B3F532FE5FDEF25D6DAE7CDAF1EE7238E70F1 + 59BA05684261A49050B8E3F37141D13A328A4418168629A5CE8845FBDB70EAF8 + 988617D5200F1D3695E07CF72978B220AF30E9832BBCFC02273716D2851D43A0 + 154E16DED7B4975C1881411B63ACEEC3EA7E438DB441128BEB77BC7D452C931D + C8C03E8A882F85BF6E211EB79DC649A2CEF2F1A197EA69C022C6929AA79D9BAB + 6BF56DC21F7E3A70192F2523F8393AC95ADD0C07889F31C4BB2F936C35D2C25F + 9BDB1DCF131FCC87038C5F53898616956CC92FF904ED9DE01202630100E99D76 + 31556C3A335B4E44D74C2145F7769E071576CBBFB120850D85C524F0E613705E + 0EF3F8D2386F4EBF47520FB50203010001028201800C32CADB63251EDD444C49 + 64C46CE6F5217B403F8271E3F2A3A4220E3A631888C1DA7CE0F1D5EC66DA565C + 2319F16B0EDA8560C30DA149D5A5705DFDEB981DC51C50E63166002623E31450 + 51D3ED985EF3F50E95F4BF9BD8FF8147F0C4C9C1C2F535100434384237C58915 + 6DAAE7AACA9AA03014F420E498887B3D7CEF3E25A63ED3B78B7773677FD81098 + 49A865C821D371946E64959AF90FB46A73DC6482DC2D6BFEED571BA4679EC4B3 + CCDCFF4F353B11966995872FAAF28F7796CF31BF2F457270466CF15971A2F0F1 + 89EA7F9B68EA76290F06FF4FCE9D3A4526D9CB111B5B606CD6AB143B48D5444E + 1CA10F24AE42C243C9CDE87CFEF846D229D76707EB3ADA819C073FD4D601CB2D + 4FF7E58F61363B76826394327D1460A334C5C09BA5CEB51ED865ECC5D0BE75E7 + 19939A4C029F0AA49B5E958EC8303FF431D251CA7B4EA13158C3C61758675390 + A97D2BFE31CC592E249E78B55D434D837DD0F471C5782244C8ACCC2FDFEFDAB7 + 0FA53B8DC8C60933B94284853199C7EB6F9B7070F90281C100C9707D75B3C445 + 93C6295D6FF65FB41F38C74A8B358A3D6AF188B99971ABCA0B3374FFC15DC154 + 3DB4B4B502331E5FB075987C68FC42844855D45303DD3B78FA0581631149E31B + 7739E13C7CE2D2727AC75ECECAFAD3C2DF63A147134A9476F8DFAF72C74DB184 + 875434F94BD8C617B60B4CDE3C5EBEA372CFAB5FBBC56ADFAC2A5C25CDB157C8 + 56579AEB36338791AEFCC46B62481F3EABC753AD1ACDCAF2C1856B0E5F061941 + 4C1B5B133E25B70CD92AE6CDEC776A8D064BECBF11DBE3794F0281C100C92F26 + EFDCEB269267FB024A9C3BC33187CB80FAEA989E3F3A2CF657226D7FA8D9F32E + D3EA93730F7D1BA7CE807F3155E9CF931A812579CDF194C3608B12E66967B7F3 + 50E4AD150878F1CD4B18BDB7C250F3D69E078F0F4B6BFC32DC3369D7B7318896 + 5A6BCE2A73EC4BEDACF6165DC191C57A642AB69DF5B5B5B1F35D9488035FBF2E + A0BE3D0868A66E2C93E06EAEF6080CBC94D7CEABC895751618AD6ECB43C4146F + C5FD7A4CE761BA24F0EB4C7F159C74A18201174661037294F9364D9DBB0281C1 + 009C2D2DE822AF0A6EAED5854EF80A6D4143A927BF548C505FE8D1E36BF73884 + 963897FE6E71FD210E125B84772720E6798E42E23A17528EF2F23083085CEEEE + 35922D259CD2D754851487EF78B7F707B0EF802EFE2A8821EF46745501BCF1EC + 67BA2D71E4D9F4C6D6914FDCF49425C95E67D679FAFE4DF9B55B12F84F419941 + BF5EBC40C7003719E8EF54FA05F4DFA7F0AEA2AECAED35E446146D68A97E6259 + E1F649F143751C01873B325A71F595BD4D6638B9F11B08AE3BF283A403F9A29C + 7B0281C077FAB2D1E1822B62ACB839499D9AD671B77659D94A06F278EA8CDDEC + 610FC44E428C90A4B9046E5E125267E4F324E79B40F115DD7C9F88E094EE0F06 + 886A2117434FA4BFDD608B669E1A36404EE4F5ADE0F14A50BC5948D9C5F085B4 + 64C4FE6CB611AAF909C9CDEF8C404BE2167088416459CCFFE7A938D2CC272B94 + E37E2D0F360EA21422DFEB1FDE015E7C6220201F81F576919D9217486C39838F + FBFE53227AD16547423FA99563E6CB19127A705FC70A97371CC770A57294E6DB + 28D368F90281C1009D7403C764BBF9CC1A23F600E542B3069F1FC6C8B44D1C21 + BF2260E65EE86E4116E2CDB721257BB2D27BA8648DC47A3F583D2234263B24EE + 49B5549C8732D14493F72BB4A1290D70A181086A2ADFCA250FDAE11245353032 + 5A6E065A8C96E59E11BCADB3C4810AC2B1899AB986F62282B430EB317B771E6D + B7D2FC2F3F1A8C73CEE0095C67D102263143A518B6622EC47A39B52E276A82D1 + CFBFCF6011D9ED12206458512E807B310D9643FAB61DE3F517441116D7270E9E + B626451B5007DBCC""", """30820928020100028202010099EBDD4A8DBFD112966F0242CD0D0DFEE9A48572 D49ED4F1E8BD52A08924691A6CE53A47140EC84D046DB142E0607733204FD461 D8CB58BDBF05E51FFB77854660ED814861429AE54BD682A06F0B3C51FBD7A27D @@ -161,15 +230,6 @@ const ] PublicKeys = [ - """305C300D06092A864886F70D0101010500034B00304802410093405660EDBF5D - ADAF93DD91E1B38F1EF086A3A0C6EA38011E58E08D27B163A9506AB837F4AF5C - A23338AB3BFBC0AC7A0FAF6B9EE3B127BD66083C7B272C1D370203010001""", - """30819F300D06092A864886F70D010101050003818D0030818902818100BDB0E9 - A7AF9865E318C8890A883D185F6D9F88868A5F586244CA6A07F298349EE52E38 - EA322D7E453AD2AA8B5019C25C50AEAAA59AFA57173F2B3B44501EF45BCC6C1F - 75BEBAC5C11D7861A8A9DAED68964DEFA6ABD0D9FE232E6BA9E97572B6F68C57 - 030CCBAE19E972D16338201B7C11E00364FE7D613CB3367BB6CF2C3BE5020301 - 0001""", """30820122300D06092A864886F70D01010105000382010F003082010A02820101 00B7362C6653ED53C35C3AE663DA496C9B834FF34D72DA98FA6DBF4AABA39FCA 0901F58A1B1D205076D20010151DBCA8FC2693E14D3502320D61E796E9C102C4 @@ -180,6 +240,20 @@ const 7847CA4806722BC74ED9954F409D42CAA3BE028E251FA0218414F2ED4469ED78 C601C71EF7C68D06BBA24DCDFDC8DCCB1F93A72896756B9CCA840E4188D8D02F 890203010001""", + """308201A2300D06092A864886F70D01010105000382018F003082018A02820181 + 009E4E6CCBE33B69A9FB6A3CAEADDC47E26AE15E1FF8C56B3126C4C66E549F86 + F0002A1CC3D0169F034825A3683AA0A579D844B415698F6F2AA5907246D911CF + CD69A5151A78BA5AD803A8FAF0184DAD4BF471469DB91BE951FD12EE5C8047B5 + 36984873A4F79BC054BF0E0A4730A1B20C3CF92440B1ECC5FA608213AD823C10 + D9994B3F532FE5FDEF25D6DAE7CDAF1EE7238E70F159BA05684261A49050B8E3 + F37141D13A328A4418168629A5CE8845FBDB70EAF8988617D5200F1D3695E07C + F72978B220AF30E9832BBCFC02273716D2851D43A0154E16DED7B4975C188141 + 1B63ACEEC3EA7E438DB441128BEB77BC7D452C931DC8C03E8A882F85BF6E211E + B79DC649A2CEF2F1A197EA69C022C6929AA79D9BAB6BF56DC21F7E3A70192F25 + 23F8393AC95ADD0C07889F31C4BB2F936C35D2C25F9BDB1DCF131FCC87038C5F + 53898616956CC92FF904ED9DE01202630100E99D7631556C3A335B4E44D74C21 + 45F7769E071576CBBFB120850D85C524F0E613705E0EF3F8D2386F4EBF47520F + B50203010001""", """30820222300D06092A864886F70D01010105000382020F003082020A02820201 0099EBDD4A8DBFD112966F0242CD0D0DFEE9A48572D49ED4F1E8BD52A0892469 1A6CE53A47140EC84D046DB142E0607733204FD461D8CB58BDBF05E51FFB7785 @@ -201,22 +275,10 @@ const ] Messages = [ - "sample", "test", "sample", "test", "sample", "test", "sample", "test" + "sample", "test", "sample", "test", "sample", "test" ] Signatures = [ - """2E597258C445FC802E502E23E9ECC711F22A2264F661C2A738032A62408FA74B - C170A8C32F5283355898B2A547BB8C63E0A5CC80CBB326EE52C46094B9D1C517""", - """8CA474B07CFB229D530E9A1955D30058D3589DFCF44B442DA9F3D5362F1D36CD - 03184CFAA1F9153E034FCB2DA2226BD99BCD51612F909016707B22585A43DA96""", - """7B5311FED58881F5684343E030011A376563A7264C72553C66A5B2EA5D203127 - 8701B248C8D5008415AB7AB302F53A2C44064C84720C5F96A179B0DBC3AF7447 - 3E5A040513B0F8212509EE168E7AB90C0E59E157182471202D189F5C0AA2C30B - F172994581ECEC82F0D59DA7E35611B7E6938E8642F9F13DEC8E257FC5F1D70F""", - """8C8D7F3E30F8E8D9171F38BFFE2D80A8CF1F9CFC40BED7E530B650149BD395EF - FE1FFF5683B7E75215453E7ECFB5EF40BBF35B194FB85B718C80B3190C2851B1 - 36C84EBB8A8E45D55AB610B7E78B38F0D6A4E7032959CA1EF94E3B1C26702366 - F1524A9FFBA5243DF07876B7B77C4262C064841FEDCF82D1D7A1A0A411ED6E8D""", """30768DB2A850D5F3F9044D2C7545C3E3D30B03B6F8159D305F19B1CF939C1336 DDA83B406D86ECE5170CE922671FCB178FC03DEF87DBA73C002DC7D302FD122F 1A23BB9F39F21DEBFE42E69A06A0871691B02AEE59772B485ABBF92B99B8E3C8 @@ -233,6 +295,30 @@ const 7CAEBE2EACA1067E9B76DB8A74AF8416A3C1E0711F92A520AFE4A258ACDB6128 47B06DF8B6705618D284C4272EDC1C11D1EDDF4D8174DE4F4706034421B0CB82 70771C40A2020E022886C44F7811AB06A3838B055F8D652104E653A1312B7506""", + """89D01B4043322ED57F20D6756EAE5295A86AA3503FA66A6AC35E2AABAD78F199 + EC5E85DBD6D43DE03C662CEFA003413434EC890DD58DF8ED6C3167DA9B4DBFD5 + 703ADED2544C72E6300A76F0126720F7843A0B0C82CBE220619B3925F94BE8D1 + E6B9455EC9DFE6A133868E6E6893C8868B9DAD5D6F1A49056BD2784B6677A76D + B0B90152C398890305FC3A37BAAB5253CBC358FD33BC86FBFAC88E136C3EF393 + 8C922575C20878FEF8AB0CBD8570654275F9B96801410E13A8BF9F3ED8AAA72F + 5C7A9500A80761EB4462FBD3A0011C660C54301460B63E79F0693352E62DDD7C + 2EFF6522A39EA366A1CE0386EAE546EB373AD8BFE8371DF6D76241FB67215DE0 + 3D0A0EC109B2CD7D74BDCBBBD1700F6BA04CD737FB11A2BFDEA5CDC5AA162C73 + 26DBFE23D7EEBBCFD2462381BFEFDDC6929E0338CA552181022834F8C869D37B + 4EC5FD00A6AA7D8FDBB77AF226E2CB1D5A01056B3050409F9EE6C04188106E21 + E6EAE8991FB4355BA1037E6792F80F35A99598E493F910439DB7AC6BB211BE3B""", + """16A0A6913C5FFB44105C25B5C217249BC8A72D9AF0D7C669B66CB3520D131497 + A60EAD1EAE629C3102812FBC17FE681489F4B88705BD9E3DF6587897FF86AE4F + 4C23753BFDB59FCB6723910B087C2BEBA379AB418CE54C2FD98DBD8A64798F48 + 9989DE0C3980B2DA1EFF03A98945947DFFAE0053FD7E62C9C1525F5608693FF7 + A359CCA7970143878ED13E510D83B872FDE3E74E869B650728C93146A926D81F + B56A938E8F76F5D617A814926E54A43819815286B3A929A61A64C1D4D6236FFE + 3A3D6E5D1D77787A80B1EF30FE06F02DABC908C635E9D16CE15D3DD5F3DDEBEE + F30C2CD930A0835B75889EFCE46868A5CB6D484ABB1178E3867DEDAA81921414 + AA34200E2066C50AC5A9E926E4EB2ABFAD7B1D068E88D7A4EC748420DAE63926 + CF47E8A911B99FA9C9A4016A378B5AEB8B9AA2C92C1836994A9069D10985B394 + 7AC24DD27432DE0B4D3D62D9D2EC24CC1F1D9F44B6BFF74085CE3D8E2B23BD3F + 9BD704817EE7DC8E2B07D32A02790C8B40F8286802FA0C0707B1CD98F19DE9AF""", """984A8ACD327392DF3C16D504AC152BB7484255A9A8E06EE54F4CE61D017B6908 11FA8D1845E72073E775A07FE5C8C028B1FC6E9B8349993D69A058DFA233E9FF 69B2F65D12A80051C823AD81BAA143C958D484E1ED5749F67B21E2D69D7EC6E9 @@ -273,43 +359,7 @@ type RsaPrivateKey = rsa.RsaPrivateKey RsaPublicKey = rsa.RsaPublicKey -suite "RSA 512/1024/2048/4096 test suite": - test "[rsa512] Private key serialize/deserialize test": - for i in 0.. 0 - check: - rkey1.init(skey1).isOk() - rkey2.init(skey2).isOk() - var rkey3 = RsaPrivateKey.init(skey1).expect("key initialization") - var rkey4 = RsaPrivateKey.init(skey2).expect("key initialization") - check: - rkey1 == key - rkey2 == key - rkey3 == key - rkey4 == key - - test "[rsa1024] Private key serialize/deserialize test": - for i in 0.. 0 - check: - rkey1.init(skey1).isOk() - rkey2.init(skey2).isOk() - var rkey3 = RsaPrivateKey.init(skey1).expect("key initialization") - var rkey4 = RsaPrivateKey.init(skey2).expect("key initialization") - check: - rkey1 == key - rkey2 == key - rkey3 == key - rkey4 == key - +suite "RSA 2048/3072/4096 test suite": test "[rsa2048] Private key serialize/deserialize test": var rkey1, rkey2: RsaPrivateKey var skey2 = newSeq[byte](4096) @@ -327,6 +377,23 @@ suite "RSA 512/1024/2048/4096 test suite": rkey3 == key rkey4 == key + test "[rsa3072] Private key serialize/deserialize test": + var rkey1, rkey2: RsaPrivateKey + var skey2 = newSeq[byte](4096) + var key = RsaPrivateKey.random(rng[], 3072).expect("random failed") + var skey1 = key.getBytes().expect("bytes") + check key.toBytes(skey2).expect("bytes") > 0 + check: + rkey1.init(skey1).isOk() + rkey2.init(skey2).isOk() + var rkey3 = RsaPrivateKey.init(skey1).expect("key initialization") + var rkey4 = RsaPrivateKey.init(skey2).expect("key initialization") + check: + rkey1 == key + rkey2 == key + rkey3 == key + rkey4 == key + test "[rsa4096] Private key serialize/deserialize test": # This test is too slow to run in debug mode. when defined(release): @@ -345,42 +412,8 @@ suite "RSA 512/1024/2048/4096 test suite": rkey2 == key rkey3 == key rkey4 == key - - test "[rsa512] Public key serialize/deserialize test": - for i in 0.. 0 - rkey1.init(skey1).isOk() - rkey2.init(skey2).isOk() - var rkey3 = RsaPublicKey.init(skey1).expect("key initialization") - var rkey4 = RsaPublicKey.init(skey2).expect("key initialization") - check: - rkey1 == pair.pubkey - rkey2 == pair.pubkey - rkey3 == pair.pubkey - rkey4 == pair.pubkey - - test "[rsa1024] Public key serialize/deserialize test": - for i in 0.. 0 - rkey1.init(skey1).isOk() - rkey2.init(skey2).isOk() - var rkey3 = RsaPublicKey.init(skey1).expect("key initialization") - var rkey4 = RsaPublicKey.init(skey2).expect("key initialization") - check: - rkey1 == pair.pubkey - rkey2 == pair.pubkey - rkey3 == pair.pubkey - rkey4 == pair.pubkey + else: + skip() test "[rsa2048] Public key serialize/deserialize test": var rkey1, rkey2: RsaPublicKey @@ -399,6 +432,23 @@ suite "RSA 512/1024/2048/4096 test suite": rkey3 == pair.pubkey rkey4 == pair.pubkey + test "[rsa3072] Public key serialize/deserialize test": + var rkey1, rkey2: RsaPublicKey + var skey2 = newSeq[byte](4096) + var pair = RsaKeyPair.random(rng[], 3072).expect("random failed") + var skey1 = pair.pubkey.getBytes().expect("bytes") + check: + pair.pubkey.toBytes(skey2).expect("bytes") > 0 + rkey1.init(skey1).isOk() + rkey2.init(skey2).isOk() + var rkey3 = RsaPublicKey.init(skey1).expect("key initialization") + var rkey4 = RsaPublicKey.init(skey2).expect("key initialization") + check: + rkey1 == pair.pubkey + rkey2 == pair.pubkey + rkey3 == pair.pubkey + rkey4 == pair.pubkey + test "[rsa4096] Public key serialize/deserialize test": when defined(release): var rkey1, rkey2: RsaPublicKey @@ -416,38 +466,8 @@ suite "RSA 512/1024/2048/4096 test suite": rkey2 == pair.pubkey rkey3 == pair.pubkey rkey4 == pair.pubkey - - test "[rsa512] Generate/Sign/Serialize/Deserialize/Verify test": - var message = "message to sign" - for i in 0.. Date: Wed, 15 Jul 2020 11:25:39 +0300 Subject: [PATCH 3/3] Minprotobuf refactoring 2 (#269) * Protobuf refactoring stage II. * Remove NoError. * Change trace level for invalid message. --- libp2p/crypto/crypto.nim | 70 +++-- libp2p/multiaddress.nim | 47 ++-- libp2p/peerid.nim | 21 +- libp2p/protobuf/minprotobuf.nim | 315 ++++++++++++----------- libp2p/protocols/identify.nim | 74 +++--- libp2p/protocols/pubsub/pubsubpeer.nim | 18 +- libp2p/protocols/pubsub/rpc/protobuf.nim | 157 ++++++----- libp2p/protocols/secure/noise.nim | 6 +- tests/testminprotobuf.nim | 158 ++++++++---- 9 files changed, 473 insertions(+), 393 deletions(-) diff --git a/libp2p/crypto/crypto.nim b/libp2p/crypto/crypto.nim index 893a2245f..a6a291e4e 100644 --- a/libp2p/crypto/crypto.nim +++ b/libp2p/crypto/crypto.nim @@ -31,11 +31,6 @@ type ECDSA, NoSupport - CipherScheme* = enum - Aes128 = 0, - Aes256, - Blowfish - DigestSheme* = enum Sha256, Sha512 @@ -283,7 +278,9 @@ proc init*[T: PrivateKey|PublicKey](key: var T, data: openarray[byte]): bool = var buffer: seq[byte] if len(data) > 0: var pb = initProtoBuffer(@data) - if pb.getField(1, id) and pb.getField(2, buffer): + let r1 = pb.getField(1, id) + let r2 = pb.getField(2, buffer) + if r1.isOk() and r1.get() and r2.isOk() and r2.get(): if cast[int8](id) in SupportedSchemesInt and len(buffer) > 0: var scheme = cast[PKScheme](cast[int8](id)) when key is PrivateKey: @@ -743,9 +740,15 @@ proc decodeProposal*(message: seq[byte], nonce, pubkey: var seq[byte], ## ## Procedure returns ``true`` on success and ``false`` on error. var pb = initProtoBuffer(message) - pb.getField(1, nonce) and pb.getField(2, pubkey) and - pb.getField(3, exchanges) and pb.getField(4, ciphers) and - pb.getField(5, hashes) + let r1 = pb.getField(1, nonce) + let r2 = pb.getField(2, pubkey) + let r3 = pb.getField(3, exchanges) + let r4 = pb.getField(4, ciphers) + let r5 = pb.getField(5, hashes) + + r1.isOk() and r1.get() and r2.isOk() and r2.get() and + r3.isOk() and r3.get() and r4.isOk() and r4.get() and + r5.isOk() and r5.get() proc createExchange*(epubkey, signature: openarray[byte]): seq[byte] = ## Create SecIO exchange message using ephemeral public key ``epubkey`` and @@ -763,7 +766,9 @@ proc decodeExchange*(message: seq[byte], ## ## Procedure returns ``true`` on success and ``false`` on error. var pb = initProtoBuffer(message) - pb.getField(1, pubkey) and pb.getField(2, signature) + let r1 = pb.getField(1, pubkey) + let r2 = pb.getField(2, signature) + r1.isOk() and r1.get() and r2.isOk() and r2.get() ## Serialization/Deserialization helpers @@ -825,28 +830,37 @@ proc getValue*(data: var ProtoBuffer, field: int, value: var Signature): int {. value = sig proc getField*[T: PublicKey|PrivateKey](pb: ProtoBuffer, field: int, - value: var T): bool = + value: var T): ProtoResult[bool] = + ## Deserialize public/private key from protobuf's message ``pb`` using field + ## index ``field``. + ## + ## On success deserialized key will be stored in ``value``. var buffer: seq[byte] var key: T - if not(getField(pb, field, buffer)): - return false - if len(buffer) == 0: - return false - if key.init(buffer): - value = key - true + let res = ? pb.getField(field, buffer) + if not(res): + ok(false) else: - false + if key.init(buffer): + value = key + ok(true) + else: + err(ProtoError.IncorrectBlob) -proc getField*(pb: ProtoBuffer, field: int, value: var Signature): bool = +proc getField*(pb: ProtoBuffer, field: int, + value: var Signature): ProtoResult[bool] = + ## Deserialize signature from protobuf's message ``pb`` using field index + ## ``field``. + ## + ## On success deserialized signature will be stored in ``value``. var buffer: seq[byte] var sig: Signature - if not(getField(pb, field, buffer)): - return false - if len(buffer) == 0: - return false - if sig.init(buffer): - value = sig - true + let res = ? pb.getField(field, buffer) + if not(res): + ok(false) else: - false + if sig.init(buffer): + value = sig + ok(true) + else: + err(ProtoError.IncorrectBlob) diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index 3d6d85eb6..f07c2a24d 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -1025,31 +1025,34 @@ proc write*(pb: var ProtoBuffer, field: int, value: MultiAddress) {.inline.} = write(pb, field, value.data.buffer) proc getField*(pb: var ProtoBuffer, field: int, - value: var MultiAddress): bool {.inline.} = + value: var MultiAddress): ProtoResult[bool] {. + inline.} = var buffer: seq[byte] - if not(getField(pb, field, buffer)): - return false - if len(buffer) == 0: - return false - let ma = MultiAddress.init(buffer) - if ma.isOk(): - value = ma.get() - true + let res = ? pb.getField(field, buffer) + if not(res): + ok(false) else: - false + let ma = MultiAddress.init(buffer) + if ma.isOk(): + value = ma.get() + ok(true) + else: + err(ProtoError.IncorrectBlob) proc getRepeatedField*(pb: var ProtoBuffer, field: int, - value: var seq[MultiAddress]): bool {.inline.} = + value: var seq[MultiAddress]): ProtoResult[bool] {. + inline.} = var items: seq[seq[byte]] value.setLen(0) - if not(getRepeatedField(pb, field, items)): - return false - if len(items) == 0: - return true - for item in items: - let ma = MultiAddress.init(item) - if ma.isOk(): - value.add(ma.get()) - else: - value.setLen(0) - return false + let res = ? pb.getRepeatedField(field, items) + if not(res): + ok(false) + else: + for item in items: + let ma = MultiAddress.init(item) + if ma.isOk(): + value.add(ma.get()) + else: + value.setLen(0) + return err(ProtoError.IncorrectBlob) + ok(true) diff --git a/libp2p/peerid.nim b/libp2p/peerid.nim index 5c81c664b..9eeb46791 100644 --- a/libp2p/peerid.nim +++ b/libp2p/peerid.nim @@ -219,16 +219,17 @@ proc write*(pb: var ProtoBuffer, field: int, pid: PeerID) = ## Write PeerID value ``peerid`` to object ``pb`` using ProtoBuf's encoding. write(pb, field, pid.data) -proc getField*(pb: ProtoBuffer, field: int, pid: var PeerID): bool = +proc getField*(pb: ProtoBuffer, field: int, + pid: var PeerID): ProtoResult[bool] {.inline.} = ## Read ``PeerID`` from ProtoBuf's message and validate it var buffer: seq[byte] - var peerId: PeerID - if not(getField(pb, field, buffer)): - return false - if len(buffer) == 0: - return false - if peerId.init(buffer): - pid = peerId - true + let res = ? pb.getField(field, buffer) + if not(res): + ok(false) else: - false + var peerId: PeerID + if peerId.init(buffer): + pid = peerId + ok(true) + else: + err(ProtoError.IncorrectBlob) diff --git a/libp2p/protobuf/minprotobuf.nim b/libp2p/protobuf/minprotobuf.nim index 5a00c4726..4476d0732 100644 --- a/libp2p/protobuf/minprotobuf.nim +++ b/libp2p/protobuf/minprotobuf.nim @@ -11,7 +11,8 @@ {.push raises: [Defect].} -import ../varint, stew/endians2 +import ../varint, stew/[endians2, results] +export results const MaxMessageSize* = 1'u shl 22 @@ -51,12 +52,15 @@ type of StartGroup, EndGroup: discard - ProtoResult {.pure.} = enum - VarintDecodeError, - MessageIncompleteError, - BufferOverflowError, - MessageSizeTooBigError, - NoError + ProtoError* {.pure.} = enum + VarintDecode, + MessageIncomplete, + BufferOverflow, + MessageTooBig, + BadWireType, + IncorrectBlob + + ProtoResult*[T] = Result[T, ProtoError] ProtoScalar* = uint | uint32 | uint64 | zint | zint32 | zint64 | hint | hint32 | hint64 | float32 | float64 @@ -361,7 +365,8 @@ proc finish*(pb: var ProtoBuffer) = else: pb.offset = 0 -proc getHeader(data: var ProtoBuffer, header: var ProtoHeader): bool = +proc getHeader(data: var ProtoBuffer, + header: var ProtoHeader): ProtoResult[void] = var length = 0 var hdr = 0'u64 if PB.getUVarint(data.toOpenArray(), length, hdr).isOk(): @@ -370,34 +375,34 @@ proc getHeader(data: var ProtoBuffer, header: var ProtoHeader): bool = if wire in SupportedWireTypes: data.offset += length header = ProtoHeader(index: index, wire: cast[ProtoFieldKind](wire)) - true + ok() else: - false + err(ProtoError.BadWireType) else: - false + err(ProtoError.VarintDecode) -proc skipValue(data: var ProtoBuffer, header: ProtoHeader): bool = +proc skipValue(data: var ProtoBuffer, header: ProtoHeader): ProtoResult[void] = case header.wire of ProtoFieldKind.Varint: var length = 0 var value = 0'u64 if PB.getUVarint(data.toOpenArray(), length, value).isOk(): data.offset += length - true + ok() else: - false + err(ProtoError.VarintDecode) of ProtoFieldKind.Fixed32: if data.isEnough(sizeof(uint32)): data.offset += sizeof(uint32) - true + ok() else: - false + err(ProtoError.VarintDecode) of ProtoFieldKind.Fixed64: if data.isEnough(sizeof(uint64)): data.offset += sizeof(uint64) - true + ok() else: - false + err(ProtoError.VarintDecode) of ProtoFieldKind.Length: var length = 0 var bsize = 0'u64 @@ -406,19 +411,19 @@ proc skipValue(data: var ProtoBuffer, header: ProtoHeader): bool = if bsize <= uint64(MaxMessageSize): if data.isEnough(int(bsize)): data.offset += int(bsize) - true + ok() else: - false + err(ProtoError.MessageIncomplete) else: - false + err(ProtoError.MessageTooBig) else: - false + err(ProtoError.VarintDecode) of ProtoFieldKind.StartGroup, ProtoFieldKind.EndGroup: - false + err(ProtoError.BadWireType) proc getValue[T: ProtoScalar](data: var ProtoBuffer, header: ProtoHeader, - outval: var T): ProtoResult = + outval: var T): ProtoResult[void] = when (T is uint64) or (T is uint32) or (T is uint): doAssert(header.wire == ProtoFieldKind.Varint) var length = 0 @@ -426,9 +431,9 @@ proc getValue[T: ProtoScalar](data: var ProtoBuffer, if PB.getUVarint(data.toOpenArray(), length, value).isOk(): data.offset += length outval = value - ProtoResult.NoError + ok() else: - ProtoResult.VarintDecodeError + err(ProtoError.VarintDecode) elif (T is zint64) or (T is zint32) or (T is zint) or (T is hint64) or (T is hint32) or (T is hint): doAssert(header.wire == ProtoFieldKind.Varint) @@ -437,29 +442,29 @@ proc getValue[T: ProtoScalar](data: var ProtoBuffer, if getSVarint(data.toOpenArray(), length, value).isOk(): data.offset += length outval = value - ProtoResult.NoError + ok() else: - ProtoResult.VarintDecodeError + err(ProtoError.VarintDecode) elif T is float32: doAssert(header.wire == ProtoFieldKind.Fixed32) if data.isEnough(sizeof(float32)): outval = cast[float32](fromBytesLE(uint32, data.toOpenArray())) data.offset += sizeof(float32) - ProtoResult.NoError + ok() else: - ProtoResult.MessageIncompleteError + err(ProtoError.MessageIncomplete) elif T is float64: doAssert(header.wire == ProtoFieldKind.Fixed64) if data.isEnough(sizeof(float64)): outval = cast[float64](fromBytesLE(uint64, data.toOpenArray())) data.offset += sizeof(float64) - ProtoResult.NoError + ok() else: - ProtoResult.MessageIncompleteError + err(ProtoError.MessageIncomplete) proc getValue[T:byte|char](data: var ProtoBuffer, header: ProtoHeader, outBytes: var openarray[T], - outLength: var int): ProtoResult = + outLength: var int): ProtoResult[void] = doAssert(header.wire == ProtoFieldKind.Length) var length = 0 var bsize = 0'u64 @@ -474,20 +479,20 @@ proc getValue[T:byte|char](data: var ProtoBuffer, header: ProtoHeader, if bsize > 0'u64: copyMem(addr outBytes[0], addr data.buffer[data.offset], int(bsize)) data.offset += int(bsize) - ProtoResult.NoError + ok() else: # Buffer overflow should not be critical failure data.offset += int(bsize) - ProtoResult.BufferOverflowError + err(ProtoError.BufferOverflow) else: - ProtoResult.MessageIncompleteError + err(ProtoError.MessageIncomplete) else: - ProtoResult.MessageSizeTooBigError + err(ProtoError.MessageTooBig) else: - ProtoResult.VarintDecodeError + err(ProtoError.VarintDecode) proc getValue[T:seq[byte]|string](data: var ProtoBuffer, header: ProtoHeader, - outBytes: var T): ProtoResult = + outBytes: var T): ProtoResult[void] = doAssert(header.wire == ProtoFieldKind.Length) var length = 0 var bsize = 0'u64 @@ -501,27 +506,24 @@ proc getValue[T:seq[byte]|string](data: var ProtoBuffer, header: ProtoHeader, if bsize > 0'u64: copyMem(addr outBytes[0], addr data.buffer[data.offset], int(bsize)) data.offset += int(bsize) - ProtoResult.NoError + ok() else: - ProtoResult.MessageIncompleteError + err(ProtoError.MessageIncomplete) else: - ProtoResult.MessageSizeTooBigError + err(ProtoError.MessageTooBig) else: - ProtoResult.VarintDecodeError + err(ProtoError.VarintDecode) proc getField*[T: ProtoScalar](data: ProtoBuffer, field: int, - output: var T): bool = + output: var T): ProtoResult[bool] = checkFieldNumber(field) - var value: T + var current: T var res = false var pb = data - output = T(0) while not(pb.isEmpty()): var header: ProtoHeader - if not(pb.getHeader(header)): - output = T(0) - return false + ? pb.getHeader(header) let wireCheck = when (T is uint64) or (T is uint32) or (T is uint) or (T is zint64) or (T is zint32) or (T is zint) or @@ -533,28 +535,29 @@ proc getField*[T: ProtoScalar](data: ProtoBuffer, field: int, header.wire == ProtoFieldKind.Fixed64 if header.index == uint64(field): if wireCheck: - let r = getValue(pb, header, value) - case r - of ProtoResult.NoError: + var value: T + let vres = pb.getValue(header, value) + if vres.isOk(): res = true - output = value + current = value else: - return false + return err(vres.error) else: # We are ignoring wire types different from what we expect, because it # is how `protoc` is working. - if not(skipValue(pb, header)): - output = T(0) - return false + ? pb.skipValue(header) else: - if not(skipValue(pb, header)): - output = T(0) - return false - res + ? pb.skipValue(header) + + if res: + output = current + ok(true) + else: + ok(false) proc getField*[T: byte|char](data: ProtoBuffer, field: int, output: var openarray[T], - outlen: var int): bool = + outlen: var int): ProtoResult[bool] = checkFieldNumber(field) var pb = data var res = false @@ -563,182 +566,191 @@ proc getField*[T: byte|char](data: ProtoBuffer, field: int, while not(pb.isEmpty()): var header: ProtoHeader - if not(pb.getHeader(header)): + let hres = pb.getHeader(header) + if hres.isErr(): if len(output) > 0: zeroMem(addr output[0], len(output)) outlen = 0 - return false - + return err(hres.error) if header.index == uint64(field): if header.wire == ProtoFieldKind.Length: - let r = getValue(pb, header, output, outlen) - case r - of ProtoResult.NoError: + let vres = pb.getValue(header, output, outlen) + if vres.isOk(): res = true - of ProtoResult.BufferOverflowError: + else: # Buffer overflow error is not critical error, we still can get # field values with proper size. - discard - else: - if len(output) > 0: - zeroMem(addr output[0], len(output)) - return false + if vres.error != ProtoError.BufferOverflow: + if len(output) > 0: + zeroMem(addr output[0], len(output)) + outlen = 0 + return err(vres.error) else: # We are ignoring wire types different from ProtoFieldKind.Length, # because it is how `protoc` is working. - if not(skipValue(pb, header)): + let sres = pb.skipValue(header) + if sres.isErr(): if len(output) > 0: zeroMem(addr output[0], len(output)) outlen = 0 - return false + return err(sres.error) else: - if not(skipValue(pb, header)): + let sres = pb.skipValue(header) + if sres.isErr(): if len(output) > 0: zeroMem(addr output[0], len(output)) outlen = 0 - return false + return err(sres.error) - res + if res: + ok(true) + else: + ok(false) proc getField*[T: seq[byte]|string](data: ProtoBuffer, field: int, - output: var T): bool = + output: var T): ProtoResult[bool] = checkFieldNumber(field) var res = false var pb = data while not(pb.isEmpty()): var header: ProtoHeader - if not(pb.getHeader(header)): + let hres = pb.getHeader(header) + if hres.isErr(): output.setLen(0) - return false - + return err(hres.error) if header.index == uint64(field): if header.wire == ProtoFieldKind.Length: - let r = getValue(pb, header, output) - case r - of ProtoResult.NoError: + let vres = pb.getValue(header, output) + if vres.isOk(): res = true - of ProtoResult.BufferOverflowError: - # Buffer overflow error is not critical error, we still can get - # field values with proper size. - discard else: output.setLen(0) - return false + return err(vres.error) else: # We are ignoring wire types different from ProtoFieldKind.Length, # because it is how `protoc` is working. - if not(skipValue(pb, header)): + let sres = pb.skipValue(header) + if sres.isErr(): output.setLen(0) - return false + return err(sres.error) else: - if not(skipValue(pb, header)): + let sres = pb.skipValue(header) + if sres.isErr(): output.setLen(0) - return false - - res - -proc getField*(pb: ProtoBuffer, field: int, output: var ProtoBuffer): bool {. - inline.} = - var buffer: seq[byte] - if pb.getField(field, buffer): - output = initProtoBuffer(buffer) - true + return err(sres.error) + if res: + ok(true) else: - false + ok(false) + +proc getField*(pb: ProtoBuffer, field: int, + output: var ProtoBuffer): ProtoResult[bool] {.inline.} = + var buffer: seq[byte] + let res = pb.getField(field, buffer) + if res.isOk(): + if res.get(): + output = initProtoBuffer(buffer) + ok(true) + else: + ok(false) + else: + err(res.error) proc getRepeatedField*[T: seq[byte]|string](data: ProtoBuffer, field: int, - output: var seq[T]): bool = + output: var seq[T]): ProtoResult[bool] = checkFieldNumber(field) var pb = data output.setLen(0) while not(pb.isEmpty()): var header: ProtoHeader - if not(pb.getHeader(header)): + let hres = pb.getHeader(header) + if hres.isErr(): output.setLen(0) - return false - + return err(hres.error) if header.index == uint64(field): if header.wire == ProtoFieldKind.Length: var item: T - let r = getValue(pb, header, item) - case r - of ProtoResult.NoError: + let vres = pb.getValue(header, item) + if vres.isOk(): output.add(item) else: output.setLen(0) - return false + return err(vres.error) else: - if not(skipValue(pb, header)): + let sres = pb.skipValue(header) + if sres.isErr(): output.setLen(0) - return false + return err(sres.error) else: - if not(skipValue(pb, header)): + let sres = pb.skipValue(header) + if sres.isErr(): output.setLen(0) - return false + return err(sres.error) if len(output) > 0: - true + ok(true) else: - false + ok(false) -proc getRepeatedField*[T: uint64|float32|float64](data: ProtoBuffer, - field: int, - output: var seq[T]): bool = +proc getRepeatedField*[T: ProtoScalar](data: ProtoBuffer, field: int, + output: var seq[T]): ProtoResult[bool] = checkFieldNumber(field) var pb = data output.setLen(0) while not(pb.isEmpty()): var header: ProtoHeader - if not(pb.getHeader(header)): + let hres = pb.getHeader(header) + if hres.isErr(): output.setLen(0) - return false + return err(hres.error) if header.index == uint64(field): if header.wire in {ProtoFieldKind.Varint, ProtoFieldKind.Fixed32, ProtoFieldKind.Fixed64}: var item: T - let r = getValue(pb, header, item) - case r - of ProtoResult.NoError: + let vres = getValue(pb, header, item) + if vres.isOk(): output.add(item) else: output.setLen(0) - return false + return err(vres.error) else: - if not(skipValue(pb, header)): + let sres = skipValue(pb, header) + if sres.isErr(): output.setLen(0) - return false + return err(sres.error) else: - if not(skipValue(pb, header)): + let sres = skipValue(pb, header) + if sres.isErr(): output.setLen(0) - return false + return err(sres.error) if len(output) > 0: - true + ok(true) else: - false + ok(false) proc getPackedRepeatedField*[T: ProtoScalar](data: ProtoBuffer, field: int, - output: var seq[T]): bool = + output: var seq[T]): ProtoResult[bool] = checkFieldNumber(field) var pb = data output.setLen(0) while not(pb.isEmpty()): var header: ProtoHeader - if not(pb.getHeader(header)): + let hres = pb.getHeader(header) + if hres.isErr(): output.setLen(0) - return false + return err(hres.error) if header.index == uint64(field): if header.wire == ProtoFieldKind.Length: var arritem: seq[byte] - let rarr = getValue(pb, header, arritem) - case rarr - of ProtoResult.NoError: + let ares = getValue(pb, header, arritem) + if ares.isOk(): var pbarr = initProtoBuffer(arritem) let itemHeader = when (T is uint64) or (T is uint32) or (T is uint) or @@ -751,29 +763,30 @@ proc getPackedRepeatedField*[T: ProtoScalar](data: ProtoBuffer, field: int, ProtoHeader(wire: ProtoFieldKind.Fixed64) while not(pbarr.isEmpty()): var item: T - let res = getValue(pbarr, itemHeader, item) - case res - of ProtoResult.NoError: + let vres = getValue(pbarr, itemHeader, item) + if vres.isOk(): output.add(item) else: output.setLen(0) - return false + return err(vres.error) else: output.setLen(0) - return false + return err(ares.error) else: - if not(skipValue(pb, header)): + let sres = skipValue(pb, header) + if sres.isErr(): output.setLen(0) - return false + return err(sres.error) else: - if not(skipValue(pb, header)): + let sres = skipValue(pb, header) + if sres.isErr(): output.setLen(0) - return false + return err(sres.error) if len(output) > 0: - true + ok(true) else: - false + ok(false) proc getVarintValue*(data: var ProtoBuffer, field: int, value: var SomeVarint): int {.deprecated.} = diff --git a/libp2p/protocols/identify.nim b/libp2p/protocols/identify.nim index a998ea983..4de5d4984 100644 --- a/libp2p/protocols/identify.nim +++ b/libp2p/protocols/identify.nim @@ -46,52 +46,56 @@ type proc encodeMsg*(peerInfo: PeerInfo, observedAddr: Multiaddress): ProtoBuffer = result = initProtoBuffer() - result.write(1, peerInfo.publicKey.get().getBytes().tryGet()) - for ma in peerInfo.addrs: result.write(2, ma.data.buffer) - for proto in peerInfo.protocols: result.write(3, proto) - result.write(4, observedAddr.data.buffer) - let protoVersion = ProtoVersion result.write(5, protoVersion) - let agentVersion = AgentVersion result.write(6, agentVersion) result.finish() -proc decodeMsg*(buf: seq[byte]): IdentifyInfo = +proc decodeMsg*(buf: seq[byte]): Option[IdentifyInfo] = + var + iinfo: IdentifyInfo + pubKey: PublicKey + oaddr: MultiAddress + protoVersion: string + agentVersion: string + var pb = initProtoBuffer(buf) - var pubKey: PublicKey - if pb.getField(1, pubKey): - trace "read public key from message", pubKey = ($pubKey).shortLog - result.pubKey = some(pubKey) + let r1 = pb.getField(1, pubKey) + let r2 = pb.getRepeatedField(2, iinfo.addrs) + let r3 = pb.getRepeatedField(3, iinfo.protos) + let r4 = pb.getField(4, oaddr) + let r5 = pb.getField(5, protoVersion) + let r6 = pb.getField(6, agentVersion) - if pb.getRepeatedField(2, result.addrs): - trace "read addresses from message", addresses = result.addrs + let res = r1.isOk() and r2.isOk() and r3.isOk() and + r4.isOk() and r5.isOk() and r6.isOk() - if pb.getRepeatedField(3, result.protos): - trace "read protos from message", protocols = result.protos - - var observableAddr: MultiAddress - if pb.getField(4, observableAddr): - trace "read observableAddr from message", address = observableAddr - result.observedAddr = some(observableAddr) - - var protoVersion = "" - if pb.getField(5, protoVersion): - trace "read protoVersion from message", protoVersion = protoVersion - result.protoVersion = some(protoVersion) - - var agentVersion = "" - if pb.getField(6, agentVersion): - trace "read agentVersion from message", agentVersion = agentVersion - result.agentVersion = some(agentVersion) + if res: + if r1.get(): + iinfo.pubKey = some(pubKey) + if r4.get(): + iinfo.observedAddr = some(oaddr) + if r5.get(): + iinfo.protoVersion = some(protoVersion) + if r6.get(): + iinfo.agentVersion = some(agentVersion) + trace "decodeMsg: decoded message", pubkey = ($pubKey).shortLog, + addresses = $iinfo.addrs, protocols = $iinfo.protos, + observable_address = $iinfo.observedAddr, + proto_version = $iinfo.protoVersion, + agent_version = $iinfo.agentVersion + some(iinfo) + else: + trace "decodeMsg: failed to decode received message" + none[IdentifyInfo]() proc newIdentify*(peerInfo: PeerInfo): Identify = new result @@ -122,11 +126,13 @@ proc identify*(p: Identify, trace "initiating identify", peer = $conn var message = await conn.readLp(64*1024) if len(message) == 0: - trace "identify: Invalid or empty message received!" - raise newException(IdentityInvalidMsgError, - "Invalid or empty message received!") + trace "identify: Empty message received!" + raise newException(IdentityInvalidMsgError, "Empty message received!") - result = decodeMsg(message) + let infoOpt = decodeMsg(message) + if infoOpt.isNone(): + raise newException(IdentityInvalidMsgError, "Incorrect message received!") + result = infoOpt.get() if not isNil(remotePeerInfo) and result.pubKey.isSome: let peer = PeerID.init(result.pubKey.get()) diff --git a/libp2p/protocols/pubsub/pubsubpeer.nim b/libp2p/protocols/pubsub/pubsubpeer.nim index ca72a3224..612a5ebc8 100644 --- a/libp2p/protocols/pubsub/pubsubpeer.nim +++ b/libp2p/protocols/pubsub/pubsubpeer.nim @@ -43,7 +43,7 @@ type RPCHandler* = proc(peer: PubSubPeer, msg: seq[RPCMsg]): Future[void] {.gcsafe.} -func hash*(p: PubSubPeer): Hash = +func hash*(p: PubSubPeer): Hash = # int is either 32/64, so intptr basically, pubsubpeer is a ref cast[pointer](p).hash @@ -114,7 +114,13 @@ proc handle*(p: PubSubPeer, conn: Connection) {.async.} = trace "message already received, skipping", peer = p.id continue - var msg = decodeRpcMsg(data) + var rmsg = decodeRpcMsg(data) + if rmsg.isErr(): + notice "failed to decode msg from peer", peer = p.id + break + + var msg = rmsg.get() + trace "decoded msg from peer", peer = p.id, msg = msg.shortLog # trigger hooks p.recvObservers(msg) @@ -149,11 +155,11 @@ proc send*(p: PubSubPeer, msgs: seq[RPCMsg]) {.async.} = p.sendObservers(mm) let encoded = encodeRpcMsg(mm) - if encoded.buffer.len <= 0: + if encoded.len <= 0: trace "empty message, skipping", peer = p.id return - let digest = $(sha256.digest(encoded.buffer)) + let digest = $(sha256.digest(encoded)) if digest in p.sentRpcCache: trace "message already sent to peer, skipping", peer = p.id libp2p_pubsub_skipped_sent_messages.inc(labelValues = [p.id]) @@ -164,8 +170,8 @@ proc send*(p: PubSubPeer, msgs: seq[RPCMsg]) {.async.} = encoded = digest if p.connected: # this can happen if the remote disconnected trace "sending encoded msgs to peer", peer = p.id, - encoded = encoded.buffer.shortLog - await p.sendConn.writeLp(encoded.buffer) + encoded = encoded.shortLog + await p.sendConn.writeLp(encoded) p.sentRpcCache.put(digest) for m in msgs: diff --git a/libp2p/protocols/pubsub/rpc/protobuf.nim b/libp2p/protocols/pubsub/rpc/protobuf.nim index c5a3eb309..4958320cf 100644 --- a/libp2p/protocols/pubsub/rpc/protobuf.nim +++ b/libp2p/protocols/pubsub/rpc/protobuf.nim @@ -80,163 +80,151 @@ proc encodeMessage*(msg: Message): seq[byte] = proc write*(pb: var ProtoBuffer, field: int, msg: Message) = pb.write(field, encodeMessage(msg)) -proc decodeGraft*(pb: ProtoBuffer): ControlGraft {.inline.} = +proc decodeGraft*(pb: ProtoBuffer): ProtoResult[ControlGraft] {. + inline.} = trace "decodeGraft: decoding message" var control = ControlGraft() - var topicId: string - if pb.getField(1, topicId): - control.topicId = topicId - trace "decodeGraft: read topicId", topic_id = topicId + if ? pb.getField(1, control.topicId): + trace "decodeGraft: read topicId", topic_id = control.topicId else: trace "decodeGraft: topicId is missing" - control + ok(control) -proc decodePrune*(pb: ProtoBuffer): ControlPrune {.inline.} = +proc decodePrune*(pb: ProtoBuffer): ProtoResult[ControlPrune] {. + inline.} = trace "decodePrune: decoding message" var control = ControlPrune() - var topicId: string - if pb.getField(1, topicId): - control.topicId = topicId - trace "decodePrune: read topicId", topic_id = topicId + if ? pb.getField(1, control.topicId): + trace "decodePrune: read topicId", topic_id = control.topicId else: trace "decodePrune: topicId is missing" - control + ok(control) -proc decodeIHave*(pb: ProtoBuffer): ControlIHave {.inline.} = +proc decodeIHave*(pb: ProtoBuffer): ProtoResult[ControlIHave] {. + inline.} = trace "decodeIHave: decoding message" var control = ControlIHave() - var topicId: string - if pb.getField(1, topicId): - control.topicId = topicId - trace "decodeIHave: read topicId", topic_id = topicId + if ? pb.getField(1, control.topicId): + trace "decodeIHave: read topicId", topic_id = control.topicId else: trace "decodeIHave: topicId is missing" - if pb.getRepeatedField(2, control.messageIDs): + if ? pb.getRepeatedField(2, control.messageIDs): trace "decodeIHave: read messageIDs", message_ids = control.messageIDs else: trace "decodeIHave: no messageIDs" - control + ok(control) -proc decodeIWant*(pb: ProtoBuffer): ControlIWant {.inline.} = +proc decodeIWant*(pb: ProtoBuffer): ProtoResult[ControlIWant] {.inline.} = trace "decodeIWant: decoding message" var control = ControlIWant() - if pb.getRepeatedField(1, control.messageIDs): + if ? pb.getRepeatedField(1, control.messageIDs): trace "decodeIWant: read messageIDs", message_ids = control.messageIDs else: trace "decodeIWant: no messageIDs" + ok(control) -proc decodeControl*(pb: ProtoBuffer): Option[ControlMessage] {.inline.} = +proc decodeControl*(pb: ProtoBuffer): ProtoResult[Option[ControlMessage]] {. + inline.} = trace "decodeControl: decoding message" var buffer: seq[byte] - if pb.getField(3, buffer): + if ? pb.getField(3, buffer): var control: ControlMessage var cpb = initProtoBuffer(buffer) var ihavepbs: seq[seq[byte]] var iwantpbs: seq[seq[byte]] var graftpbs: seq[seq[byte]] var prunepbs: seq[seq[byte]] - - discard cpb.getRepeatedField(1, ihavepbs) - discard cpb.getRepeatedField(2, iwantpbs) - discard cpb.getRepeatedField(3, graftpbs) - discard cpb.getRepeatedField(4, prunepbs) - - for item in ihavepbs: - control.ihave.add(decodeIHave(initProtoBuffer(item))) - for item in iwantpbs: - control.iwant.add(decodeIWant(initProtoBuffer(item))) - for item in graftpbs: - control.graft.add(decodeGraft(initProtoBuffer(item))) - for item in prunepbs: - control.prune.add(decodePrune(initProtoBuffer(item))) - - trace "decodeControl: " - some(control) + if ? cpb.getRepeatedField(1, ihavepbs): + for item in ihavepbs: + control.ihave.add(? decodeIHave(initProtoBuffer(item))) + if ? cpb.getRepeatedField(2, iwantpbs): + for item in iwantpbs: + control.iwant.add(? decodeIWant(initProtoBuffer(item))) + if ? cpb.getRepeatedField(3, graftpbs): + for item in graftpbs: + control.graft.add(? decodeGraft(initProtoBuffer(item))) + if ? cpb.getRepeatedField(4, prunepbs): + for item in prunepbs: + control.prune.add(? decodePrune(initProtoBuffer(item))) + trace "decodeControl: message statistics", graft_count = len(control.graft), + prune_count = len(control.prune), + ihave_count = len(control.ihave), + iwant_count = len(control.iwant) + ok(some(control)) else: - none[ControlMessage]() + ok(none[ControlMessage]()) -proc decodeSubscription*(pb: ProtoBuffer): SubOpts {.inline.} = +proc decodeSubscription*(pb: ProtoBuffer): ProtoResult[SubOpts] {.inline.} = trace "decodeSubscription: decoding message" var subflag: uint64 var sub = SubOpts() - if pb.getField(1, subflag): + if ? pb.getField(1, subflag): sub.subscribe = bool(subflag) trace "decodeSubscription: read subscribe", subscribe = subflag else: trace "decodeSubscription: subscribe is missing" - if pb.getField(2, sub.topic): + if ? pb.getField(2, sub.topic): trace "decodeSubscription: read topic", topic = sub.topic else: trace "decodeSubscription: topic is missing" + ok(sub) - sub - -proc decodeSubscriptions*(pb: ProtoBuffer): seq[SubOpts] {.inline.} = +proc decodeSubscriptions*(pb: ProtoBuffer): ProtoResult[seq[SubOpts]] {. + inline.} = trace "decodeSubscriptions: decoding message" var subpbs: seq[seq[byte]] var subs: seq[SubOpts] - if pb.getRepeatedField(1, subpbs): + let res = ? pb.getRepeatedField(1, subpbs) + if res: trace "decodeSubscriptions: read subscriptions", count = len(subpbs) for item in subpbs: - let sub = decodeSubscription(initProtoBuffer(item)) - subs.add(sub) + subs.add(? decodeSubscription(initProtoBuffer(item))) + if len(subs) == 0: + trace "decodeSubscription: no subscriptions found" + ok(subs) - if len(subs) == 0: - trace "decodeSubscription: no subscriptions found" - - subs - -proc decodeMessage*(pb: ProtoBuffer): Message {.inline.} = +proc decodeMessage*(pb: ProtoBuffer): ProtoResult[Message] {.inline.} = trace "decodeMessage: decoding message" var msg: Message - if pb.getField(1, msg.fromPeer): + if ? pb.getField(1, msg.fromPeer): trace "decodeMessage: read fromPeer", fromPeer = msg.fromPeer.pretty() else: trace "decodeMessage: fromPeer is missing" - - if pb.getField(2, msg.data): + if ? pb.getField(2, msg.data): trace "decodeMessage: read data", data = msg.data.shortLog() else: trace "decodeMessage: data is missing" - - if pb.getField(3, msg.seqno): + if ? pb.getField(3, msg.seqno): trace "decodeMessage: read seqno", seqno = msg.data.shortLog() else: trace "decodeMessage: seqno is missing" - - if pb.getRepeatedField(4, msg.topicIDs): + if ? pb.getRepeatedField(4, msg.topicIDs): trace "decodeMessage: read topics", topic_ids = msg.topicIDs else: trace "decodeMessage: topics are missing" - - if pb.getField(5, msg.signature): + if ? pb.getField(5, msg.signature): trace "decodeMessage: read signature", signature = msg.signature.shortLog() else: trace "decodeMessage: signature is missing" - - if pb.getField(6, msg.key): + if ? pb.getField(6, msg.key): trace "decodeMessage: read public key", key = msg.key.shortLog() else: trace "decodeMessage: public key is missing" + ok(msg) - msg - -proc decodeMessages*(pb: ProtoBuffer): seq[Message] {.inline.} = +proc decodeMessages*(pb: ProtoBuffer): ProtoResult[seq[Message]] {.inline.} = trace "decodeMessages: decoding message" var msgpbs: seq[seq[byte]] var msgs: seq[Message] - if pb.getRepeatedField(2, msgpbs): + if ? pb.getRepeatedField(2, msgpbs): trace "decodeMessages: read messages", count = len(msgpbs) for item in msgpbs: - let msg = decodeMessage(initProtoBuffer(item)) - msgs.add(msg) - - if len(msgs) == 0: + msgs.add(? decodeMessage(initProtoBuffer(item))) + else: trace "decodeMessages: no messages found" + ok(msgs) - msgs - -proc encodeRpcMsg*(msg: RPCMsg): ProtoBuffer = +proc encodeRpcMsg*(msg: RPCMsg): seq[byte] = trace "encodeRpcMsg: encoding message", msg = msg.shortLog() var pb = initProtoBuffer() for item in msg.subscriptions: @@ -247,14 +235,13 @@ proc encodeRpcMsg*(msg: RPCMsg): ProtoBuffer = pb.write(3, msg.control.get()) if len(pb.buffer) > 0: pb.finish() - result = pb + pb.buffer -proc decodeRpcMsg*(msg: seq[byte]): RPCMsg = +proc decodeRpcMsg*(msg: seq[byte]): ProtoResult[RPCMsg] {.inline.} = trace "decodeRpcMsg: decoding message", msg = msg.shortLog() var pb = initProtoBuffer(msg) var rpcMsg: RPCMsg - rpcMsg.messages = pb.decodeMessages() - rpcMsg.subscriptions = pb.decodeSubscriptions() - rpcMsg.control = pb.decodeControl() - - rpcMsg + rpcMsg.messages = ? pb.decodeMessages() + rpcMsg.subscriptions = ? pb.decodeSubscriptions() + rpcMsg.control = ? pb.decodeControl() + ok(rpcMsg) diff --git a/libp2p/protocols/secure/noise.nim b/libp2p/protocols/secure/noise.nim index d5398ccbb..c34be2973 100644 --- a/libp2p/protocols/secure/noise.nim +++ b/libp2p/protocols/secure/noise.nim @@ -449,9 +449,11 @@ method handshake*(p: Noise, conn: Connection, initiator: bool): Future[SecureCon remoteSig: Signature remoteSigBytes: seq[byte] - if not(remoteProof.getField(1, remotePubKeyBytes)): + let r1 = remoteProof.getField(1, remotePubKeyBytes) + let r2 = remoteProof.getField(2, remoteSigBytes) + if r1.isErr() or not(r1.get()): raise newException(NoiseHandshakeError, "Failed to deserialize remote public key bytes. (initiator: " & $initiator & ", peer: " & $conn.peerInfo.peerId & ")") - if not(remoteProof.getField(2, remoteSigBytes)): + if r2.isErr() or not(r2.get()): raise newException(NoiseHandshakeError, "Failed to deserialize remote signature bytes. (initiator: " & $initiator & ", peer: " & $conn.peerInfo.peerId & ")") if not remotePubKey.init(remotePubKeyBytes): diff --git a/tests/testminprotobuf.nim b/tests/testminprotobuf.nim index a4fe7fead..d167285c4 100644 --- a/tests/testminprotobuf.nim +++ b/tests/testminprotobuf.nim @@ -88,7 +88,7 @@ suite "MinProtobuf test suite": var value: uint64 var pb = initProtoBuffer(data) let res = pb.getField(1, value) - doAssert(res) + doAssert(res.isOk() == true and res.get() == true) value proc getFixed32EncodedValue(value: float32): seq[byte] = @@ -101,7 +101,7 @@ suite "MinProtobuf test suite": var value: float32 var pb = initProtoBuffer(data) let res = pb.getField(1, value) - doAssert(res) + doAssert(res.isOk() == true and res.get() == true) cast[uint32](value) proc getFixed64EncodedValue(value: float64): seq[byte] = @@ -114,7 +114,7 @@ suite "MinProtobuf test suite": var value: float64 var pb = initProtoBuffer(data) let res = pb.getField(1, value) - doAssert(res) + doAssert(res.isOk() == true and res.get() == true) cast[uint64](value) proc getLengthEncodedValue(value: string): seq[byte] = @@ -134,8 +134,7 @@ suite "MinProtobuf test suite": var valueLen = 0 var pb = initProtoBuffer(data) let res = pb.getField(1, value, valueLen) - - doAssert(res) + doAssert(res.isOk() == true and res.get() == true) value.setLen(valueLen) value @@ -173,17 +172,19 @@ suite "MinProtobuf test suite": # corrupting data.setLen(len(data) - 1) var pb = initProtoBuffer(data) + let res = pb.getField(1, value) check: - pb.getField(1, value) == false + res.isErr() == true test "[varint] non-existent field test": for i in 0 ..< len(VarintValues): var value: uint64 var data = getVarintEncodedValue(VarintValues[i]) var pb = initProtoBuffer(data) + let res = pb.getField(2, value) check: - pb.getField(2, value) == false - value == 0'u64 + res.isOk() == true + res.get() == false test "[varint] corrupted header test": for i in 0 ..< len(VarintValues): @@ -192,15 +193,17 @@ suite "MinProtobuf test suite": var data = getVarintEncodedValue(VarintValues[i]) data.corruptHeader(k) var pb = initProtoBuffer(data) + let res = pb.getField(1, value) check: - pb.getField(1, value) == false + res.isErr() == true test "[varint] empty buffer test": var value: uint64 var pb = initProtoBuffer() + let res = pb.getField(1, value) check: - pb.getField(1, value) == false - value == 0'u64 + res.isOk() == true + res.get() == false test "[varint] Repeated field test": var pb1 = initProtoBuffer() @@ -218,9 +221,12 @@ suite "MinProtobuf test suite": let r2 = pb2.getRepeatedField(2, fieldarr2) let r3 = pb2.getRepeatedField(3, fieldarr3) check: - r1 == true - r2 == true - r3 == false + r1.isOk() == true + r2.isOk() == true + r3.isOk() == true + r1.get() == true + r2.get() == true + r3.get() == false len(fieldarr3) == 0 len(fieldarr2) == 1 len(fieldarr1) == 4 @@ -246,9 +252,12 @@ suite "MinProtobuf test suite": let r2 = pb2.getPackedRepeatedField(2, fieldarr2) let r3 = pb2.getPackedRepeatedField(3, fieldarr3) check: - r1 == true - r2 == true - r3 == false + r1.isOk() == true + r2.isOk() == true + r3.isOk() == true + r1.get() == true + r2.get() == true + r3.get() == false len(fieldarr3) == 0 len(fieldarr2) == 2 len(fieldarr1) == 6 @@ -284,17 +293,19 @@ suite "MinProtobuf test suite": # corrupting data.setLen(len(data) - 1) var pb = initProtoBuffer(data) + let res = pb.getField(1, value) check: - pb.getField(1, value) == false + res.isErr() == true test "[fixed32] non-existent field test": for i in 0 ..< len(Fixed32Values): var value: float32 var data = getFixed32EncodedValue(float32(Fixed32Values[i])) var pb = initProtoBuffer(data) + let res = pb.getField(2, value) check: - pb.getField(2, value) == false - value == float32(0) + res.isOk() == true + res.get() == false test "[fixed32] corrupted header test": for i in 0 ..< len(Fixed32Values): @@ -303,15 +314,17 @@ suite "MinProtobuf test suite": var data = getFixed32EncodedValue(float32(Fixed32Values[i])) data.corruptHeader(k) var pb = initProtoBuffer(data) + let res = pb.getField(1, value) check: - pb.getField(1, value) == false + res.isErr() == true test "[fixed32] empty buffer test": var value: float32 var pb = initProtoBuffer() + let res = pb.getField(1, value) check: - pb.getField(1, value) == false - value == float32(0) + res.isOk() == true + res.get() == false test "[fixed32] Repeated field test": var pb1 = initProtoBuffer() @@ -329,9 +342,12 @@ suite "MinProtobuf test suite": let r2 = pb2.getRepeatedField(2, fieldarr2) let r3 = pb2.getRepeatedField(3, fieldarr3) check: - r1 == true - r2 == true - r3 == false + r1.isOk() == true + r2.isOk() == true + r3.isOk() == true + r1.get() == true + r2.get() == true + r3.get() == false len(fieldarr3) == 0 len(fieldarr2) == 1 len(fieldarr1) == 4 @@ -360,9 +376,12 @@ suite "MinProtobuf test suite": let r2 = pb2.getPackedRepeatedField(2, fieldarr2) let r3 = pb2.getPackedRepeatedField(3, fieldarr3) check: - r1 == true - r2 == true - r3 == false + r1.isOk() == true + r2.isOk() == true + r3.isOk() == true + r1.get() == true + r2.get() == true + r3.get() == false len(fieldarr3) == 0 len(fieldarr2) == 2 len(fieldarr1) == 5 @@ -397,17 +416,19 @@ suite "MinProtobuf test suite": # corrupting data.setLen(len(data) - 1) var pb = initProtoBuffer(data) + let res = pb.getField(1, value) check: - pb.getField(1, value) == false + res.isErr() == true test "[fixed64] non-existent field test": for i in 0 ..< len(Fixed64Values): var value: float64 var data = getFixed64EncodedValue(cast[float64](Fixed64Values[i])) var pb = initProtoBuffer(data) + let res = pb.getField(2, value) check: - pb.getField(2, value) == false - value == float64(0) + res.isOk() == true + res.get() == false test "[fixed64] corrupted header test": for i in 0 ..< len(Fixed64Values): @@ -416,15 +437,17 @@ suite "MinProtobuf test suite": var data = getFixed64EncodedValue(cast[float64](Fixed64Values[i])) data.corruptHeader(k) var pb = initProtoBuffer(data) + let res = pb.getField(1, value) check: - pb.getField(1, value) == false + res.isErr() == true test "[fixed64] empty buffer test": var value: float64 var pb = initProtoBuffer() + let res = pb.getField(1, value) check: - pb.getField(1, value) == false - value == float64(0) + res.isOk() == true + res.get() == false test "[fixed64] Repeated field test": var pb1 = initProtoBuffer() @@ -442,9 +465,12 @@ suite "MinProtobuf test suite": let r2 = pb2.getRepeatedField(2, fieldarr2) let r3 = pb2.getRepeatedField(3, fieldarr3) check: - r1 == true - r2 == true - r3 == false + r1.isOk() == true + r2.isOk() == true + r3.isOk() == true + r1.get() == true + r2.get() == true + r3.get() == false len(fieldarr3) == 0 len(fieldarr2) == 1 len(fieldarr1) == 4 @@ -474,9 +500,12 @@ suite "MinProtobuf test suite": let r2 = pb2.getPackedRepeatedField(2, fieldarr2) let r3 = pb2.getPackedRepeatedField(3, fieldarr3) check: - r1 == true - r2 == true - r3 == false + r1.isOk() == true + r2.isOk() == true + r3.isOk() == true + r1.get() == true + r2.get() == true + r3.get() == false len(fieldarr3) == 0 len(fieldarr2) == 2 len(fieldarr1) == 8 @@ -523,8 +552,9 @@ suite "MinProtobuf test suite": # corrupting data.setLen(len(data) - 1) var pb = initProtoBuffer(data) + let res = pb.getField(1, value, valueLen) check: - pb.getField(1, value, valueLen) == false + res.isErr() == true test "[length] non-existent field test": for i in 0 ..< len(LengthValues): @@ -532,8 +562,10 @@ suite "MinProtobuf test suite": var valueLen = 0 var data = getLengthEncodedValue(LengthValues[i]) var pb = initProtoBuffer(data) + let res = pb.getField(2, value, valueLen) check: - pb.getField(2, value, valueLen) == false + res.isOk() == true + res.get() == false valueLen == 0 test "[length] corrupted header test": @@ -544,15 +576,18 @@ suite "MinProtobuf test suite": var data = getLengthEncodedValue(LengthValues[i]) data.corruptHeader(k) var pb = initProtoBuffer(data) + let res = pb.getField(1, value, valueLen) check: - pb.getField(1, value, valueLen) == false + res.isErr() == true test "[length] empty buffer test": var value = newSeq[byte](len(LengthValues[0])) var valueLen = 0 var pb = initProtoBuffer() + let res = pb.getField(1, value, valueLen) check: - pb.getField(1, value, valueLen) == false + res.isOk() == true + res.get() == false valueLen == 0 test "[length] buffer overflow test": @@ -562,8 +597,10 @@ suite "MinProtobuf test suite": var value = newString(len(LengthValues[i]) - 1) var valueLen = 0 var pb = initProtoBuffer(data) + let res = pb.getField(1, value, valueLen) check: - pb.getField(1, value, valueLen) == false + res.isOk() == true + res.get() == false valueLen == len(LengthValues[i]) isFullZero(value) == true @@ -578,8 +615,10 @@ suite "MinProtobuf test suite": var pb2 = initProtoBuffer(pb1.buffer) var value = newString(4) var valueLen = 0 + let res = pb2.getField(1, value, valueLen) check: - pb2.getField(1, value, valueLen) == true + res.isOk() == true + res.get() == true value == "SOME" test "[length] too big message test": @@ -593,8 +632,9 @@ suite "MinProtobuf test suite": var pb2 = initProtoBuffer(pb1.buffer) var value = newString(MaxMessageSize + 1) var valueLen = 0 + let res = pb2.getField(1, value, valueLen) check: - pb2.getField(1, value, valueLen) == false + res.isErr() == true test "[length] Repeated field test": var pb1 = initProtoBuffer() @@ -612,9 +652,12 @@ suite "MinProtobuf test suite": let r2 = pb2.getRepeatedField(2, fieldarr2) let r3 = pb2.getRepeatedField(3, fieldarr3) check: - r1 == true - r2 == true - r3 == false + r1.isOk() == true + r2.isOk() == true + r3.isOk() == true + r1.get() == true + r2.get() == true + r3.get() == false len(fieldarr3) == 0 len(fieldarr2) == 1 len(fieldarr1) == 4 @@ -662,11 +705,16 @@ suite "MinProtobuf test suite": var lengthValue = newString(10) var lengthSize: int + let r1 = pb.getField(1, varintValue) + let r2 = pb.getField(2, fixed32Value) + let r3 = pb.getField(3, fixed64Value) + let r4 = pb.getField(4, lengthValue, lengthSize) + check: - pb.getField(1, varintValue) == true - pb.getField(2, fixed32Value) == true - pb.getField(3, fixed64Value) == true - pb.getField(4, lengthValue, lengthSize) == true + r1.isOk() == true + r2.isOk() == true + r3.isOk() == true + r4.isOk() == true lengthValue.setLen(lengthSize)