1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
// Copyright 2013-2015 Apcera Inc. All rights reserved.
package test
import (
"encoding/base64"
"io/ioutil"
"net/http"
"github.com/apcera/gssapi"
)
// This test handler accepts the context, unwraps, and then re-wraps the request body
func HandleUnwrap(c *Context, w http.ResponseWriter, r *http.Request) (code int, message string) {
ctx, code, message := allowed(c, w, r)
if ctx == nil {
return code, message
}
// Unwrap the request
wrappedbytes, err := ioutil.ReadAll(
base64.NewDecoder(base64.StdEncoding, r.Body))
if err != nil {
return http.StatusInternalServerError, err.Error()
}
wrapped, err := c.MakeBufferBytes(wrappedbytes)
if err != nil {
return http.StatusInternalServerError, err.Error()
}
defer wrapped.Release()
unwrapped, _, _, err := ctx.Unwrap(wrapped)
if err != nil {
return http.StatusInternalServerError, err.Error()
}
defer unwrapped.Release()
// Re-wrap the for the response
_, wrapped, err = ctx.Wrap(true, gssapi.GSS_C_QOP_DEFAULT, unwrapped)
if err != nil {
return http.StatusInternalServerError, err.Error()
}
defer wrapped.Release()
wrapped64 := base64.StdEncoding.EncodeToString(wrapped.Bytes())
w.Write([]byte(wrapped64))
return http.StatusOK, "OK"
}
func HandleVerifyMIC(c *Context, w http.ResponseWriter, r *http.Request) (code int, message string) {
ctx, code, message := allowed(c, w, r)
if ctx == nil {
return code, message
}
mic64 := r.Header.Get(micHeader)
if mic64 == "" {
return http.StatusInternalServerError, "No " + micHeader + " header"
}
micbytes, err := base64.StdEncoding.DecodeString(mic64)
if err != nil {
return http.StatusInternalServerError, err.Error()
}
mic, err := c.MakeBufferBytes(micbytes)
if err != nil {
return http.StatusInternalServerError, err.Error()
}
bodybytes, err := ioutil.ReadAll(r.Body)
if err != nil {
return http.StatusInternalServerError, err.Error()
}
body, err := c.MakeBufferBytes(bodybytes)
if err != nil {
return http.StatusInternalServerError, err.Error()
}
_, err = ctx.VerifyMIC(body, mic)
if err != nil {
return http.StatusInternalServerError, err.Error()
}
w.Write([]byte("OK"))
return http.StatusOK, "OK"
}
|