/
handler.go
57 lines (48 loc) · 1.49 KB
/
handler.go
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
package api
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"io/ioutil"
"net/http"
"github.com/alibaba/inclavare-containers/shim/runtime/signature/types"
"github.com/golang/glog"
"github.com/gin-gonic/gin"
)
var rng = rand.Reader
func (s *ApiServer) pkcs1Handler(c *gin.Context) {
payload := &types.SignaturePayload{}
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
glog.Errorf("failed to parse request body, err:%v", err.Error())
c.AbortWithStatus(http.StatusBadRequest)
return
}
hashed := sha256.Sum256(body)
signedBytes, err := rsa.SignPKCS1v15(rng, s.privateKey, crypto.SHA256, hashed[:])
if err != nil {
glog.Errorf("failed to sign request, err:%v", err.Error())
c.AbortWithStatus(http.StatusInternalServerError)
return
}
payload.Signature = base64.StdEncoding.EncodeToString(signedBytes)
bytes, err := ioutil.ReadFile(s.publicKeyFilePath)
if err != nil {
glog.Errorf("failed to parse public key, public key path: %s, err:%v", s.publicKeyFilePath, err.Error())
c.AbortWithStatus(http.StatusInternalServerError)
return
}
payload.PublicKey = string(bytes)
c.JSON(http.StatusOK, payload)
}
func (s *ApiServer) publicKeyHandler(c *gin.Context) {
bytes, err := ioutil.ReadFile(s.publicKeyFilePath)
if err != nil {
glog.Errorf("failed to parse public key, public key path: %s, err:%v", s.publicKeyFilePath, err.Error())
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, string(bytes))
}