MCPcopy Create free account
hub / github.com/evilsocket/pwngrid / SendMessageTo

Method SendMessageTo

api/unit_inbox.go:134–222  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

132}
133
134func (api *API) SendMessageTo(w http.ResponseWriter, r *http.Request) {
135 // authenticate source unit
136 srcUnit := Authenticate(w, r)
137 if srcUnit == nil {
138 return
139 }
140
141 // get dest unit by fingerprint
142 dstUnitFingerprint := chi.URLParam(r, "fingerprint")
143 dstUnit := models.FindUnitByFingerprint(dstUnitFingerprint)
144 if dstUnit == nil {
145 ERROR(w, http.StatusNotFound, ErrRecNotFound)
146 return
147 }
148
149 // read the message and signature from the source unit
150 client := clientIP(r)
151 body, err := ioutil.ReadAll(r.Body)
152 if err != nil {
153 ERROR(w, http.StatusUnprocessableEntity, err)
154 return
155 }
156
157 var message Message
158 if err = json.Unmarshal(body, &message); err != nil {
159 log.Debug("error while decoding message from %s: %v", srcUnit.Identity(), err)
160 log.Debug("%s", body)
161 ERROR(w, http.StatusUnprocessableEntity, err)
162 return
163 }
164
165 if err := models.ValidateMessage(message.Data, message.Signature); err != nil {
166 log.Warning("client %s sent a broken message structure: %v", srcUnit.Identity(), err)
167 ERROR(w, http.StatusUnprocessableEntity, err)
168 return
169 }
170
171 // parse source unit key
172 srcKeys, err := crypto.FromPublicPEM(srcUnit.PublicKey)
173 if err != nil {
174 log.Warning("error decoding key from %s: %v", srcUnit.Identity(), err)
175 log.Debug("%s", srcUnit.PublicKey)
176 ERROR(w, http.StatusUnprocessableEntity, ErrInvalidKey)
177 return
178 }
179
180 // decode data, signature and verify SIGN(SHA256(data))
181 data, err := base64.StdEncoding.DecodeString(message.Data)
182 if err != nil {
183 log.Warning("error decoding message from %s: %v", srcUnit.Identity(), err)
184 log.Debug("%s", message.Data)
185 ERROR(w, http.StatusUnprocessableEntity, ErrDecoding)
186 return
187 }
188
189 signature, err := base64.StdEncoding.DecodeString(message.Signature)
190 if err != nil {
191 log.Warning("error decoding signature from %s: %v", srcUnit.Identity(), err)

Callers

nothing calls this directly

Calls 10

FindUnitByFingerprintFunction · 0.92
ValidateMessageFunction · 0.92
FromPublicPEMFunction · 0.92
CreateFunction · 0.92
AuthenticateFunction · 0.85
ERRORFunction · 0.85
clientIPFunction · 0.85
JSONFunction · 0.85
IdentityMethod · 0.80
VerifyMessageMethod · 0.80

Tested by

no test coverage detected