Exemplo n.º 1
0
func removeKey(ks *store.KeyStore, cfg *config) error {
	cfg.Label = cfg.Args[0]

	if cfg.Label == "self" {
		return errors.New("cannot remove own key")
	} else if !ks.Has(cfg.Label) {
		return fmt.Errorf("no key was found under label %s", cfg.Label)
	}

	fmt.Println("Removing key ", cfg.Label)
	delete(ks.Keys, cfg.Label)
	return nil
}
Exemplo n.º 2
0
func process(ks *store.KeyStore, cmd command) *response {
	var resp response

	switch cmd.op {
	case "public":
		log.Printf("public key lookup")
		if cmd.data["label"] == "" {
			log.Printf("public key request with no label")
			resp.err = errors.New("missing label")
		}
		ok := ks.Has(cmd.data["label"])
		if !ok {
			log.Printf("keystore doesn't have key with label %s", cmd.data["label"])
			resp.err = errors.New("export failed")
		}

		if cmd.data["label"] == "self" {
			resp.out = ks.ExportKey[:]
		} else {
			rec := ks.Keys[cmd.data["label"]]
			if rec == nil {
				log.Printf("keystore lookup failed")
				resp.err = errors.New("export failed")
				break
			}
			vkey := &store.VerifiedKey{
				Public:    rec.Keys,
				Signer:    rec.KeySigner,
				Timestamp: rec.SignatureTime,
				Signature: rec.KeySignature,
			}
			resp.out, resp.err = vkey.Serialise()
		}
	case "fingerprint":
		log.Printf("fingerprint lookup")
		if cmd.data["label"] == "" {
			log.Printf("fingerprint request with no label")
			resp.err = errors.New("missing label")
		}
		ok := ks.Has(cmd.data["label"])
		if !ok {
			log.Printf("keystore doesn't have key with label %s",
				cmd.data["label"])
			resp.err = errors.New("export failed")
		}

		var pub []byte

		if cmd.data["label"] == "self" {
			pub = ks.PublicKey[:]
		} else {
			rec := ks.Keys[cmd.data["label"]]
			if rec == nil {
				log.Printf("keystore lookup failed")
				resp.err = errors.New("fingerprint failed")
				break
			}
			pub = rec.Keys[:]
		}
		h := sha256.New()
		h.Write(pub)
		resp.out = []byte(fmt.Sprintf("%x", h.Sum(nil)))
	case "upload":
		log.Printf("upload request")
		resp = checkUpload(ks, cmd)
	case "audit":
		log.Printf("audit request")
		ok := ks.KeyAudit()
		if !ok {
			resp.err = errors.New("audit failure")
		}
	default:
		resp.err = errors.New("invalid command")
	}
	return &resp
}