@@ -108,10 +108,6 @@ func NewCommInstanceWithServer(port int, idMapper identity.Mapper, peerIdentity
108
108
proto .RegisterGossipServer (s , commInst )
109
109
}
110
110
111
- if viper .GetBool ("peer.gossip.skipHandshake" ) {
112
- commInst .skipHandshake = true
113
- }
114
-
115
111
return commInst , nil
116
112
}
117
113
@@ -416,10 +412,11 @@ func (c *commImpl) authenticateRemotePeer(stream stream) (*proto.ConnectionInfo,
416
412
var err error
417
413
var cMsg * proto.SignedGossipMessage
418
414
var signer proto.Signer
415
+ useTLS := c .selfCertHash != nil
419
416
420
- // If TLS is detected , sign the hash of our cert to bind our TLS cert
421
- // to the gRPC session
422
- if remoteCertHash != nil && c . selfCertHash != nil && ! c . skipHandshake {
417
+ // If TLS is enabled , sign the connection message in order to bind
418
+ // the TLS session to the peer's identity
419
+ if useTLS {
423
420
signer = func (msg []byte ) ([]byte , error ) {
424
421
return c .idMapper .Sign (msg )
425
422
}
@@ -430,50 +427,57 @@ func (c *commImpl) authenticateRemotePeer(stream stream) (*proto.ConnectionInfo,
430
427
}
431
428
}
432
429
430
+ // TLS enabled but not detected on other side
431
+ if useTLS && len (remoteCertHash ) == 0 {
432
+ c .logger .Warningf ("%s didn't send TLS certificate" , remoteAddress )
433
+ return nil , errors .New ("No TLS certificate" )
434
+ }
435
+
433
436
cMsg = c .createConnectionMsg (c .PKIID , c .selfCertHash , c .peerIdentity , signer )
434
437
435
438
c .logger .Debug ("Sending" , cMsg , "to" , remoteAddress )
436
439
stream .Send (cMsg .Envelope )
437
440
m , err := readWithTimeout (stream , util .GetDurationOrDefault ("peer.gossip.connTimeout" , defConnTimeout ), remoteAddress )
438
441
if err != nil {
439
- err := fmt .Errorf ("Failed reading messge from %s, reason: %v" , remoteAddress , err )
440
- c .logger .Warning (err )
442
+ c .logger .Warningf ("Failed reading messge from %s, reason: %v" , remoteAddress , err )
441
443
return nil , err
442
444
}
443
445
receivedMsg := m .GetConn ()
444
446
if receivedMsg == nil {
445
- c .logger .Warning ("Expected connection message but got" , receivedMsg )
447
+ c .logger .Warning ("Expected connection message from" , remoteAddress , " but got" , receivedMsg )
446
448
return nil , errors .New ("Wrong type" )
447
449
}
448
450
449
451
if receivedMsg .PkiId == nil {
450
- c .logger .Warning ("%s didn't send a pkiID" )
451
- return nil , fmt . Errorf ( "%s didn't send a pkiID" , remoteAddress )
452
+ c .logger .Warning ("%s didn't send a pkiID" , remoteAddress )
453
+ return nil , errors . New ( "No PKI-ID" )
452
454
}
453
455
454
456
c .logger .Debug ("Received" , receivedMsg , "from" , remoteAddress )
455
- err = c .idMapper .Put (receivedMsg .PkiId , receivedMsg .Cert )
457
+ err = c .idMapper .Put (receivedMsg .PkiId , receivedMsg .Identity )
456
458
if err != nil {
457
459
c .logger .Warning ("Identity store rejected" , remoteAddress , ":" , err )
458
460
return nil , err
459
461
}
460
462
461
463
connInfo := & proto.ConnectionInfo {
462
464
ID : receivedMsg .PkiId ,
463
- Identity : receivedMsg .Cert ,
465
+ Identity : receivedMsg .Identity ,
464
466
Endpoint : remoteAddress ,
465
467
}
466
468
467
469
// if TLS is enabled and detected, verify remote peer
468
- if remoteCertHash != nil && c .selfCertHash != nil && ! c .skipHandshake {
469
- if ! bytes .Equal (remoteCertHash , receivedMsg .Hash ) {
470
- return nil , fmt .Errorf ("Expected %v in remote hash, but got %v" , remoteCertHash , receivedMsg .Hash )
470
+ if useTLS {
471
+ // If the remote peer sent its TLS certificate, make sure it actually matches the TLS cert
472
+ // that the peer used.
473
+ if ! bytes .Equal (remoteCertHash , receivedMsg .TlsCertHash ) {
474
+ return nil , fmt .Errorf ("Expected %v in remote hash of TLS cert, but got %v" , remoteCertHash , receivedMsg .TlsCertHash )
471
475
}
472
476
verifier := func (peerIdentity []byte , signature , message []byte ) error {
473
477
pkiID := c .idMapper .GetPKIidOfCert (api .PeerIdentityType (peerIdentity ))
474
478
return c .idMapper .Verify (pkiID , signature , message )
475
479
}
476
- err = m .Verify (receivedMsg .Cert , verifier )
480
+ err = m .Verify (receivedMsg .Identity , verifier )
477
481
if err != nil {
478
482
c .logger .Error ("Failed verifying signature from" , remoteAddress , ":" , err )
479
483
return nil , err
@@ -484,13 +488,6 @@ func (c *commImpl) authenticateRemotePeer(stream stream) (*proto.ConnectionInfo,
484
488
}
485
489
}
486
490
487
- // TLS enabled but not detected on other side, and we're not configured to skip handshake verification
488
- if remoteCertHash == nil && c .selfCertHash != nil && ! c .skipHandshake {
489
- err = fmt .Errorf ("Remote peer %s didn't send TLS certificate" , remoteAddress )
490
- c .logger .Warning (err )
491
- return nil , err
492
- }
493
-
494
491
c .logger .Debug ("Authenticated" , remoteAddress )
495
492
496
493
return connInfo , nil
@@ -583,15 +580,15 @@ func readWithTimeout(stream interface{}, timeout time.Duration, address string)
583
580
}
584
581
}
585
582
586
- func (c * commImpl ) createConnectionMsg (pkiID common.PKIidType , hash []byte , cert api.PeerIdentityType , signer proto.Signer ) * proto.SignedGossipMessage {
583
+ func (c * commImpl ) createConnectionMsg (pkiID common.PKIidType , certHash []byte , cert api.PeerIdentityType , signer proto.Signer ) * proto.SignedGossipMessage {
587
584
m := & proto.GossipMessage {
588
585
Tag : proto .GossipMessage_EMPTY ,
589
586
Nonce : 0 ,
590
587
Content : & proto.GossipMessage_Conn {
591
588
Conn : & proto.ConnEstablish {
592
- Hash : hash ,
593
- Cert : cert ,
594
- PkiId : pkiID ,
589
+ TlsCertHash : certHash ,
590
+ Identity : cert ,
591
+ PkiId : pkiID ,
595
592
},
596
593
},
597
594
}
0 commit comments