(w http.ResponseWriter, r *http.Request)
| 132 | } |
| 133 | |
| 134 | func (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) |
nothing calls this directly
no test coverage detected