Пример #1
0
func readHashes(db *openpgp.DB, hashes chan *symflux.Zp) {
	rows, err := db.DB.Query("SELECT md5 FROM openpgp_pubkey")
	if err != nil {
		die(err)
	}
	for rows.Next() {
		var md5str string
		if err = rows.Scan(&md5str); err != nil {
			die(err)
		}
		digest, err := hex.DecodeString(md5str)
		if err != nil {
			log.Println("Bad md5:", md5str)
			continue
		}
		digest = recon.PadSksElement(digest)
		digestZp := symflux.Zb(symflux.P_SKS, digest)
		hashes <- digestZp
	}
	if err = rows.Err(); err != nil {
		log.Println("Error during hash query:", err)
	}
}
Пример #2
0
func (r *SksPeer) deleteLocalChunk(rcvr *recon.Recover, chunk []*Zp) (err error) {
	//Get MappedDomains for authenticatioon of add
	verifiedDomains, errM := RecoveryAuthentication(rcvr.RemoteAllStatesJSON)
	if errM != nil {
		err = errM
		return
	}

	fmt.Println("deleteLocal", verifiedDomains)

	// Search keys from Local DB
	hqBuf := bytes.NewBuffer(nil)
	err = recon.WriteInt(hqBuf, len(chunk))
	if err != nil {
		return err
	}
	for _, z := range chunk {
		zb := z.Bytes()
		zb = recon.PadSksElement(zb)
		// Hashquery elements are 16 bytes (length_of(P_SKS)-1)
		zb = zb[:len(zb)-1]
		err = recon.WriteInt(hqBuf, len(zb))
		if err != nil {
			return err
		}
		_, err = hqBuf.Write(zb)
		if err != nil {
			return err
		}
	}
	ownAuth, err1 := GetOwnAuthority()
	if err1 != nil {
		err = err1
		return
	}
	//fmt.Println("Making HashQuerry to self for = ", string(hqBuf.Bytes()))
	resp, err := http.Post(fmt.Sprintf("http://%s/pks/hashquery", ownAuth.HkpAddr),
		"sks/hashquery", bytes.NewReader(hqBuf.Bytes()))
	if err != nil {
		return err
	}

	// Store response in memory. Connection may timeout if we
	// read directly from it while loading.
	var body *bytes.Buffer
	{
		defer resp.Body.Close()
		bodyBuf, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return err
		}
		body = bytes.NewBuffer(bodyBuf)
	}
	var nkeys, keyLen int
	nkeys, err = recon.ReadInt(body)
	if err != nil {
		return err
	}
	log.Println("Response from Self:", nkeys, " keys found")
	fmt.Println("Response from Self:", nkeys, " keys found")

	for i := 0; i < nkeys; i++ {
		keyLen, err = recon.ReadInt(body)
		if err != nil {
			return err
		}
		keyBuf := bytes.NewBuffer(nil)
		_, err = io.CopyN(keyBuf, body, int64(keyLen))
		if err != nil {
			return err
		}
		log.Println("Key#", i+1, ":", keyLen, "bytes")
		// Merge locally

		localDeleteKey := &LocalDeleteKey{
			Keytext:         keyBuf.Bytes(),
			response:        make(chan hkp.Response),
			verifiedDomains: verifiedDomains}

		//PRC End
		go func() {
			r.LocalDeleteKey <- localDeleteKey

		}()

		resp := <-localDeleteKey.response
		if resp != nil && resp.Error() != nil {
			log.Println("Error Deleting key:", resp.Error())
		}

	}

	// Read last two bytes (CRLF, why?), or SKS will complain.
	body.Read(make([]byte, 2))
	return
}
Пример #3
0
func (r *SksPeer) requestChunk(rcvr *recon.Recover, chunk []*Zp) (err error) {
	//Get MappedDomains for authenticatioon of add
	//defer fmt.Println("Error While Returning from requestRecoverd", err)
	verifiedDomains, errM := RecoveryAuthentication(rcvr.RemoteAllStatesJSON)
	if errM != nil {
		err = errM
		return
	}
	fmt.Println("requestRecovered", verifiedDomains)

	var remoteAddr string
	remoteAddr, err = rcvr.HkpAddr()
	if err != nil {
		return err
	}
	// Make an sks hashquery request
	hqBuf := bytes.NewBuffer(nil)
	err = recon.WriteInt(hqBuf, len(chunk))
	if err != nil {
		return err
	}
	for _, z := range chunk {
		zb := z.Bytes()
		zb = recon.PadSksElement(zb)
		// Hashquery elements are 16 bytes (length_of(P_SKS)-1)
		zb = zb[:len(zb)-1]
		err = recon.WriteInt(hqBuf, len(zb))
		if err != nil {
			return err
		}
		_, err = hqBuf.Write(zb)
		if err != nil {
			return err
		}
	}

	//if len(hqBuf.Bytes()) == 0 {
	//	return
	//}
	//fmt.Println("Sending Hashquerry to ", remoteAddr, " for md5uuid= ", string(hqBuf.Bytes()))
	resp, err1 := http.Post(fmt.Sprintf("http://%s/pks/hashquery", remoteAddr),
		"sks/hashquery", bytes.NewReader(hqBuf.Bytes()))
	if err1 != nil {
		err = err1
		return
	}

	// Store response in memory. Connection may timeout if we
	// read directly from it while loading.
	var body *bytes.Buffer
	{
		defer resp.Body.Close()
		bodyBuf, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return err
		}
		body = bytes.NewBuffer(bodyBuf)
	}
	var nkeys, keyLen int
	nkeys, err = recon.ReadInt(body)
	if err != nil {
		return err
	}
	log.Println("Response from server:", nkeys, " keys found")
	fmt.Println("Response from server:", nkeys, " keys found")

	for i := 0; i < nkeys; i++ {
		keyLen, err = recon.ReadInt(body)
		if err != nil {
			return err
		}
		keyBuf := bytes.NewBuffer(nil)
		_, err = io.CopyN(keyBuf, body, int64(keyLen))
		if err != nil {
			return err
		}
		log.Println("Key#", i+1, ":", keyLen, "bytes")
		// Merge locally

		recoverKey := &RecoverKey{
			Keytext: keyBuf.Bytes(),
			//RecoverSet:      elements,
			Source:          rcvr.RemoteAddr.String(),
			response:        make(chan hkp.Response),
			verifiedDomains: verifiedDomains}

		//PRC Start

		go func() {
			r.RecoverKey <- recoverKey
		}()
		resp := <-recoverKey.response
		if resp != nil && resp.Error() != nil {
			log.Println("Error Adding key :", resp.Error())
		}
		//PRC End
	}
	// Read last two bytes (CRLF, why?), or SKS will complain.
	body.Read(make([]byte, 2))
	return
}
Пример #4
0
func (r *SksPeer) HandleKeyUpdates() {
	for {

		select {
		case keyChange, ok := <-r.KeyChanges:
			if !ok {
				return
			}

			digest, err := hex.DecodeString(keyChange.CurrentMd5)
			if err != nil {
				log.Println("bad digest:", keyChange.CurrentMd5)
				continue
			}
			digest = recon.PadSksElement(digest)
			digestZp := Zb(P_SKS, digest)
			//PRC START
			//Handle Key Delete
			//remove node from ptree
			if keyChange.Type == KeyDeleted {
				log.Println("MD5:", keyChange.CurrentMd5)
				log.Println("Prefix Tree: Remove:", digestZp)
				err = r.Peer.Remove(digestZp)
				if err != nil {
					log.Println(err)
					continue
				}
				fmt.Print("-")

				//PRC END
			} else {
				//Before else if keyChange.PreviousMd5 != keyChange.CurrentMd5
				//Changed according to latest hockeypuck,issue insert without checking for change,
				//The ptree will itself not add duplicate elements.
				log.Println("Prefix tree: Insert:", hex.EncodeToString(digestZp.Bytes()), keyChange, keyChange.CurrentMd5)
				err := r.Peer.Insert(digestZp)
				if err != nil {
					log.Println(err)
					continue
				}
				fmt.Print("+")

				if keyChange.PreviousMd5 != "" && keyChange.PreviousMd5 != keyChange.CurrentMd5 {
					prevDigest, err := hex.DecodeString(keyChange.PreviousMd5)
					if err != nil {
						log.Println("bad digest:", keyChange.PreviousMd5)
						continue
					}
					prevDigest = recon.PadSksElement(prevDigest)
					prevDigestZp := Zb(P_SKS, prevDigest)
					log.Println("Prefix Tree: Remove:", prevDigestZp)
					err = r.Peer.Remove(prevDigestZp)
					if err != nil {
						log.Println(err)
						continue
					}
					fmt.Print("-")

				}
			}
			//PRC Start
			//errOwn := UpdateOwnLocalState()
			//if errOwn != nil {
			//	fmt.Println("Error updating Own Local State")
			//	log.Println("Error updating Own Local State")
			//}
			//PRC End

		}

	}
}
Пример #5
0
func (ec *loadCmd) loadAllKeys(path string) {
	keyfiles, err := filepath.Glob(path)
	if err != nil {
		die(err)
	}

	limit := ec.totalKeyLimit
	for _, keyfile := range keyfiles {
		var f *os.File
		if f, err = os.Open(keyfile); err != nil {
			log.Println("Failed to open", keyfile, ":", err)
			continue
		}
		defer f.Close()
		log.Println("Loading keys from", keyfile)
		defer ec.flushDb()
		for keyRead := range openpgp.ReadKeys(f) {
			if keyRead.Error != nil {
				log.Println("Error reading key:", keyRead.Error)
				continue
			}

			//PRC Start
			//Only load Keys which are in our own authority
			if keyRead.Pubkey == nil {
				fmt.Println("no Pub Key found")
				continue

			}
			underAuth := openpgp.IsUnderAuth(*keyRead.Pubkey)
			if underAuth != nil {
				//fmt.Println("Load : " + underAuth.Error())
				continue
			}
			//PRC End

			digest, err := hex.DecodeString(keyRead.Pubkey.Md5)
			if err != nil {
				log.Println("bad digest:", keyRead.Pubkey.Md5)
				continue
			}
			digest = recon.PadSksElement(digest)
			digestZp := symflux.Zb(symflux.P_SKS, digest)
			err = ec.ptree.Insert(digestZp)
			if err != nil {
				log.Println("Error inserting digest ", keyRead.Pubkey.Md5, ":", err)
				continue
			}
			err = ec.insertKey(keyRead)
			if err != nil {
				log.Println("Error inserting key", keyRead.Pubkey.Md5, "into database:", err)
				// Attempt to remove digest from ptree, since it was not successfully loaded
				ec.ptree.Remove(digestZp)
				continue
			}
			if ec.totalKeyLimit != 0 {
				limit--
				if limit == 0 {
					fmt.Println("Loaded ", ec.totalKeyLimit, " keys")
					return
				}
			}
		}
	}

}