handler.go 1.5 KB
Newer Older
S
stormgbs 已提交
1 2 3 4 5 6 7
package api

import (
	"crypto"
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha256"
8
	"encoding/base64"
S
stormgbs 已提交
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
	"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
	}

38
	payload.Signature = base64.StdEncoding.EncodeToString(signedBytes)
J
jiazhiguang 已提交
39 40 41 42 43 44 45
	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)
S
stormgbs 已提交
46 47
	c.JSON(http.StatusOK, payload)
}
J
jiazhiguang 已提交
48 49 50 51 52 53 54 55 56 57

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))
}