Skip to content
Snippets Groups Projects
quipsec.go 1.68 KiB
Newer Older
  • Learn to ignore specific revisions
  • package kms
    
    import (
    	"context"
    	"net"
    	"strconv"
    	"time"
    
    	"code.fbi.h-da.de/danet/quantumlayer"
    	pb "code.fbi.h-da.de/danet/quipsec/gen/go/quipsec"
    	"github.com/sirupsen/logrus"
    	"google.golang.org/grpc/codes"
    	"google.golang.org/grpc/peer"
    	"google.golang.org/grpc/status"
    )
    
    type quipSecServer struct {
    	pb.UnimplementedKmsQkdmCommunicationServiceServer
    	eKMS *EKMS
    }
    
    func (qs *quipSecServer) PushKeys(ctx context.Context, req *pb.PushKeysRequest) (*pb.PushKeysResponse, error) {
    	p, _ := peer.FromContext(ctx)
    	host, _, err := net.SplitHostPort(p.Addr.String())
    	if err != nil {
    	}
    
    
    	// TODO: qm.address is used as key for map. could be
    	// used here to directly access.
    
    	for _, qm := range qs.eKMS.quantumModules {
    		if qm.Address() == host {
    			eqm, ok := qm.(*EmulatedQuantumModule)
    			if !ok {
    				return nil, status.Errorf(codes.Internal, "quantum module is of wrong type")
    			}
    
    			bulkKeyId, err := strconv.ParseInt(req.GetKeyBulk().GetKeyId(), 10, 64)
    			if err != nil {
    				return nil, status.Errorf(codes.Internal, "could not get bulkkeyid")
    			}
    			req.GetKeyBulk().GetKeyId()
    			req.GetKeyBulk().GetKeys()
    			eqm.rawBulkKeysMutex.Lock()
    			eqm.rawBulkKeys[bulkKeyId] = &quantumlayer.QuantumLayerBulkKey{
    				BulkKeyId:     bulkKeyId,
    				BulkKeyLength: int(req.GetKeyBulk().GetKeyLength()),
    				BulkKey:       &req.GetKeyBulk().Keys,
    			}
    			eqm.rawBulkKeysMutex.Unlock()
    			logrus.Infof("%s received a new bulk with id: %s and a length of: %d", qs.eKMS.kmsName, req.GetKeyBulk().GetKeyId(), req.GetKeyBulk().GetKeyLength())
    			return &pb.PushKeysResponse{Timestamp: time.Now().Unix()}, nil
    		}
    	}
    	return nil, status.Errorf(codes.Internal, "could not find a quantum module for host address: %s", host)
    }