// Get Encrypted Public Key from database. func GetPubkey(log chan string, addrHash objects.Hash) *objects.EncryptedPubkey { mutex.Lock() defer mutex.Unlock() hash := addrHash.GetBytes() if hashList == nil || dbConn == nil { return nil } if hashList[string(hash)] != PUBKEY { return nil } for s, err := dbConn.Query("SELECT payload FROM pubkey WHERE hash=?", hash); err == nil; err = s.Next() { var payload []byte s.Scan(&payload) // Assigns 1st column to rowid, the rest to row pub := new(objects.EncryptedPubkey) pub.AddrHash = addrHash copy(pub.IV[:], payload[:16]) pub.Payload = payload[16:] return pub } // Not Found return nil }
func checkPubkey(config *api.ApiConfig, addrHash objects.Hash) []byte { // First check local DB detail, err := localdb.GetAddressDetail(addrHash) if err != nil { // If not in database, won't be able to decrypt anyway! return nil } if len(detail.Pubkey) > 0 { if db.Contains(addrHash) != db.PUBKEY { enc := new(objects.EncryptedPubkey) enc.IV, enc.Payload, _ = encryption.SymmetricEncrypt(detail.Address, string(detail.Pubkey)) enc.AddrHash = objects.MakeHash(detail.Address) config.RecvQueue <- *objects.MakeFrame(objects.PUBKEY, objects.BROADCAST, enc) } return detail.Pubkey } // If not there, check local database if db.Contains(addrHash) == db.PUBKEY { enc := db.GetPubkey(config.Log, addrHash) pubkey := encryption.SymmetricDecrypt(enc.IV, detail.Address, enc.Payload) pubkey = pubkey[:65] // Check public Key x, y := encryption.UnmarshalPubkey(pubkey) if x == nil { config.Log <- "Decrypted Public Key Invalid" return nil } address2 := encryption.GetAddress(config.Log, x, y) if string(detail.Address) != string(address2) { config.Log <- "Decrypted Public Key doesn't match provided address!" return nil } detail.Pubkey = pubkey err := localdb.AddUpdateAddress(detail) if err != nil { config.Log <- "Error adding pubkey to local database!" return nil } return pubkey } // If not there, send a pubkey request config.RecvQueue <- *objects.MakeFrame(objects.PUBKEY_REQUEST, objects.BROADCAST, &addrHash) return nil }
func (service *EMPService) CreateAddress(r *http.Request, args *NilParam, reply *objects.AddressDetail) error { if !basicAuth(service.Config, r) { service.Config.Log <- fmt.Sprintf("Unauthorized RPC Request from: %s", r.RemoteAddr) return errors.New("Unauthorized") } // Create Address priv, x, y := encryption.CreateKey(service.Config.Log) reply.Privkey = priv if x == nil { return errors.New("Key Pair Generation Error") } reply.Pubkey = encryption.MarshalPubkey(x, y) reply.IsRegistered = true reply.Address = encryption.GetAddress(service.Config.Log, x, y) if reply.Address == nil { return errors.New("Could not create address, function returned nil.") } reply.String = encryption.AddressToString(reply.Address) // Add Address to Database err := localdb.AddUpdateAddress(reply) if err != nil { service.Config.Log <- fmt.Sprintf("Error Adding Address: ", err) return err } // Send Pubkey to Network encPub := new(objects.EncryptedPubkey) encPub.AddrHash = objects.MakeHash(reply.Address) encPub.IV, encPub.Payload, err = encryption.SymmetricEncrypt(reply.Address, string(reply.Pubkey)) if err != nil { service.Config.Log <- fmt.Sprintf("Error Encrypting Pubkey: ", err) return nil } // Record Pubkey for Network service.Config.RecvQueue <- *objects.MakeFrame(objects.PUBKEY, objects.BROADCAST, encPub) return nil }