From e37e5e287539bbb2e9a40f1097a0eb1ffbd78e12 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Mon, 5 Aug 2024 22:24:56 -0400 Subject: [PATCH] Search subkeys Search subkeys when looking for KeyIds. Do this for both public and private keys. The KeyIds are indexd in a map when keys are loaded to make finding them easier. Subkeys are not stored directly in the map; the whole parent entity is stored instead which includes the subkey. --- server.go | 113 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 97 insertions(+), 16 deletions(-) diff --git a/server.go b/server.go index 7b0ca03..b9dcee0 100644 --- a/server.go +++ b/server.go @@ -19,24 +19,82 @@ type Server struct { PublicKeys openpgp.EntityList PrivateKeys openpgp.EntityList + KeyMap map[string]*openpgp.Entity + //PubkeyGroups []*openpgp.Entity } func NewServer(publicKeys, privateKeys []string) (*Server, error) { - pub, err := loadKeysFromFiles(publicKeys) + s := &Server{KeyMap: make(map[string]*openpgp.Entity)} + + pub, err := s.loadKeysFromFiles(publicKeys) if err != nil { return nil, err } - priv, err := loadKeysFromFiles(privateKeys) + s.PublicKeys = pub + + fmt.Println("Public keys:") + for _, k := range pub { + for name, _ := range k.Identities { + fmt.Println("\t"+name) + } + + fmt.Printf("\t%X\n", k.PrimaryKey.KeyId) + if len(k.Subkeys) == 0 { + continue + } + + fmt.Println("\tSubkeys:") + for _, sub := range k.Subkeys { + str := []string{} + if sub.PublicKey != nil { + str = append(str, fmt.Sprintf("public: %X", sub.PublicKey.KeyId)) + } + if sub.PrivateKey != nil { + str = append(str, fmt.Sprintf("private: %X", sub.PrivateKey.KeyId)) + } + + fmt.Println("\t\t"+strings.Join(str, " / ")) + } + } + + priv, err := s.loadKeysFromFiles(privateKeys) if err != nil { return nil, err } - return &Server{PublicKeys: pub, PrivateKeys: priv}, nil + s.PrivateKeys = priv + + fmt.Println("Private keys:") + for _, k := range priv { + for name, _ := range k.Identities { + fmt.Println("\t"+name) + } + + fmt.Printf("\t%X\n", k.PrimaryKey.KeyId) + if len(k.Subkeys) == 0 { + continue + } + + fmt.Println("\tSubkeys:") + for _, sub := range k.Subkeys { + str := []string{} + if sub.PublicKey != nil { + str = append(str, fmt.Sprintf("public: %X", sub.PublicKey.KeyId)) + } + if sub.PrivateKey != nil { + str = append(str, fmt.Sprintf("private: %X", sub.PrivateKey.KeyId)) + } + + fmt.Println("\t\t"+strings.Join(str, " / ")) + } + } + + return s, nil } -func loadKeysFromFiles(filenames []string) (openpgp.EntityList, error) { +func (s *Server) loadKeysFromFiles(filenames []string) (openpgp.EntityList, error) { var keyring openpgp.EntityList for _, f := range filenames { @@ -52,7 +110,20 @@ func loadKeysFromFiles(filenames []string) (openpgp.EntityList, error) { } file.Close() - keyring = append(keyring, keys...) + for _, key := range keys { + keyring = append(keyring, key) + s.KeyMap[fmt.Sprintf("%x", key.PrimaryKey.KeyId)] = key + + for _, sub := range key.Subkeys { + if sub.PublicKey != nil { + s.KeyMap[fmt.Sprintf("%x", sub.PublicKey.KeyId)] = key + } + + if sub.PrivateKey != nil { + s.KeyMap[fmt.Sprintf("%x", sub.PrivateKey.KeyId)] = key + } + } + } } return keyring, nil @@ -71,7 +142,7 @@ func (s *Server) handler_publickey(w http.ResponseWriter, r *http.Request) { key := s.findPubKey(search) if key == nil { - http.Error(w, "Key not found", 500) + http.Error(w, fmt.Sprintf("Key not found; searched for %q", search), 500) return } @@ -82,6 +153,14 @@ func (s *Server) handler_publickey(w http.ResponseWriter, r *http.Request) { } func (s *Server) findPubKey(search string) *openpgp.Entity { + if k, found := s.KeyMap[search]; found { + for _, sub := range k.Subkeys { + if sub.PublicKey != nil && fmt.Sprintf("%x", sub.PublicKey.KeyId) == search { + return k + } + } + } + for _, k := range s.PublicKeys { for _, id := range k.Identities { if strings.ToLower(id.UserId.Name) == search { @@ -121,6 +200,18 @@ func (s *Server) handler_privatekey(w http.ResponseWriter, r *http.Request) { } func (s *Server) findPrivKey(search string) *openpgp.Entity { + if k, found := s.KeyMap[search]; found { + if k.PrivateKey != nil && fmt.Sprintf("%x", k.PrivateKey.KeyId) == search { + return k + } + + for _, sub := range k.Subkeys { + if sub.PrivateKey != nil && fmt.Sprintf("%x", sub.PrivateKey.KeyId) == search { + return k + } + } + } + for _, k := range s.PrivateKeys { fmt.Printf("%X\n", k.PrimaryKey.KeyId) for _, id := range k.Identities { @@ -132,16 +223,6 @@ func (s *Server) findPrivKey(search string) *openpgp.Entity { return k } } - - for _, sub := range k.Subkeys { - fmt.Printf("%#v\n", sub) - if sub.PrivateKey == nil { - continue - } - if fmt.Sprintf("%x", sub.PrivateKey.KeyId) == search { - return k - } - } } return nil