Esempio n. 1
0
func (hq *HashQuery) Parse() error {
	// Require HTTP POST
	if hq.Method != "POST" {
		return ErrorInvalidMethod(hq.Method)
	}
	hq.responseChan = make(ResponseChan)
	var body *bytes.Buffer
	{
		defer hq.Body.Close()
		buf, err := ioutil.ReadAll(hq.Body)
		if err != nil {
			return err
		}
		body = bytes.NewBuffer(buf)
	}
	// Parse hashquery POST data
	n, err := recon.ReadInt(body)
	if err != nil {
		return err
	}
	hq.Digests = make([]string, n)
	for i := 0; i < n; i++ {
		hashlen, err := recon.ReadInt(body)
		if err != nil {
			return err
		}
		hash := make([]byte, hashlen)
		_, err = body.Read(hash)
		if err != nil {
			return err
		}
		hq.Digests[i] = hex.EncodeToString(hash)
	}
	return nil
}
Esempio n. 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
}
Esempio n. 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
}
Esempio n. 4
0
func unmarshalNode(buf []byte, bitQuantum int, numSamples int) (node *Node, err error) {
	r := bytes.NewBuffer(buf)
	var keyBits, numElements int
	numElements, err = recon.ReadInt(r)
	if err != nil {
		return
	}
	keyBits, err = recon.ReadInt(r)
	if err != nil {
		return
	}
	keyBytes := keyBits / 8
	if keyBits%8 > 0 {
		keyBytes++
	}
	if keyBytes < 0 {
		err = errors.New(fmt.Sprintf("Invalid bitstring length == %d", keyBytes))
		return
	}
	keyData := make([]byte, keyBytes)
	_, err = r.Read(keyData)
	if err != nil {
		return
	}
	key := NewBitstring(keyBits)
	key.SetBytes(keyData)
	svalues := make([]*Zp, numSamples)
	for i := 0; i < numSamples; i++ {
		svalues[i], err = recon.ReadZp(r)
		if err != nil {
			return
		}
	}
	b := make([]byte, 1)
	_, err = r.Read(b)
	//fmt.Printf("isleaf = %v\n", b)
	if err != nil {
		return
	}
	node = &Node{
		SValues:     svalues,
		NumElements: numElements,
		Key:         key.String(),
		Leaf:        b[0] == 1}
	if node.Leaf {
		var size int
		size, err = recon.ReadInt(r)
		if err != nil {
			return
		}
		node.Fingerprints = make([]string, size)
		for i := 0; i < size; i++ {
			buf := make([]byte, recon.SksZpNbytes)
			_, err = io.ReadFull(r, buf)
			if err != nil {
				return
			}
			node.Fingerprints[i] = fmt.Sprintf("%x", buf)
		}
	} else {
		for i := 0; i < 1<<uint(bitQuantum); i++ {
			child := NewBitstring(key.BitLen() + bitQuantum)
			child.SetBytes(key.Bytes())
			for j := 0; j < bitQuantum; j++ {
				if i&(1<<uint(j)) != 0 {
					child.Set(key.BitLen() + j)
				} else {
					child.Unset(key.BitLen() + j)
				}
			}
			node.Children = append(node.Children, child.String())
		}
	}
	return
}