2018-10-31 11:17:51 +01:00
|
|
|
package datastructures
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2021-01-04 20:39:35 +01:00
|
|
|
"fmt"
|
2018-10-31 11:17:51 +01:00
|
|
|
"time"
|
2021-01-04 14:15:12 +01:00
|
|
|
|
|
|
|
"git.cacert.org/cacert-gosigner/shared"
|
2018-10-31 11:17:51 +01:00
|
|
|
)
|
|
|
|
|
2021-01-09 11:24:40 +01:00
|
|
|
const (
|
|
|
|
headerPosSystem = 2
|
|
|
|
headerPosRoot = 3
|
|
|
|
headerPosProfile = 4
|
|
|
|
headerPosSignatureAlgorithm = 5
|
|
|
|
headerPosDay = 6
|
|
|
|
)
|
|
|
|
|
2018-10-31 11:17:51 +01:00
|
|
|
type SignerRequest struct {
|
2021-01-04 20:39:35 +01:00
|
|
|
Version uint8
|
|
|
|
Action shared.Action
|
2021-01-09 11:24:40 +01:00
|
|
|
System shared.CryptoSystemID
|
|
|
|
Root shared.CryptoSystemRootID
|
|
|
|
Profile shared.CertificateProfileID
|
|
|
|
MdAlgorithm shared.SignatureAlgorithmID
|
2021-01-04 20:39:35 +01:00
|
|
|
Days uint16
|
|
|
|
Spkac uint8
|
2021-01-05 19:59:43 +01:00
|
|
|
Content1 []byte
|
|
|
|
Content2 []byte
|
|
|
|
Content3 []byte
|
2018-10-31 11:17:51 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:15:12 +01:00
|
|
|
func SignerRequestFromData(blockData []byte) (*SignerRequest, error) {
|
2021-01-09 11:24:40 +01:00
|
|
|
headerLength := Decode24BitLength(blockData[0:shared.LengthFieldSize])
|
|
|
|
headerBytes := blockData[shared.LengthFieldSize : shared.LengthFieldSize+headerLength]
|
2018-10-31 11:17:51 +01:00
|
|
|
|
2021-01-09 11:24:40 +01:00
|
|
|
contentBytes := blockData[shared.LengthFieldSize+headerLength:]
|
|
|
|
contentLen := Decode24BitLength(contentBytes[0:shared.LengthFieldSize])
|
|
|
|
content := contentBytes[shared.LengthFieldSize : shared.LengthFieldSize+contentLen]
|
2018-10-31 11:17:51 +01:00
|
|
|
|
2021-01-09 11:24:40 +01:00
|
|
|
argument1Offset := shared.LengthFieldSize + contentLen
|
|
|
|
argument1Len := Decode24BitLength(contentBytes[argument1Offset : argument1Offset+shared.LengthFieldSize])
|
|
|
|
argument1 := contentBytes[shared.LengthFieldSize+argument1Offset : shared.LengthFieldSize+argument1Offset+argument1Len]
|
2018-10-31 11:17:51 +01:00
|
|
|
|
2021-01-09 11:24:40 +01:00
|
|
|
argument2Offset := shared.LengthFieldSize + argument1Offset + argument1Len
|
|
|
|
argument2Len := Decode24BitLength(contentBytes[argument2Offset : argument2Offset+shared.LengthFieldSize])
|
|
|
|
argument2 := contentBytes[shared.LengthFieldSize+argument2Offset : shared.LengthFieldSize+argument2Offset+argument2Len]
|
2018-10-31 11:17:51 +01:00
|
|
|
|
|
|
|
return &SignerRequest{
|
2021-01-09 11:24:40 +01:00
|
|
|
Version: headerBytes[headerPosVersion],
|
|
|
|
Action: shared.Action(headerBytes[headerPosAction]),
|
|
|
|
System: shared.CryptoSystemID(headerBytes[headerPosSystem]),
|
|
|
|
Root: shared.CryptoSystemRootID(headerBytes[headerPosRoot]),
|
|
|
|
Profile: shared.CertificateProfileID(headerBytes[headerPosProfile]),
|
|
|
|
MdAlgorithm: shared.SignatureAlgorithmID(headerBytes[headerPosSignatureAlgorithm]),
|
|
|
|
Days: binary.BigEndian.Uint16(headerBytes[headerPosDay : headerPosDay+1]),
|
2021-01-04 20:39:35 +01:00
|
|
|
Spkac: headerBytes[8],
|
2021-01-09 11:24:40 +01:00
|
|
|
Content1: content,
|
|
|
|
Content2: argument1,
|
|
|
|
Content3: argument2,
|
2018-10-31 11:17:51 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 20:39:35 +01:00
|
|
|
func (r *SignerRequest) Serialize() []byte {
|
2018-10-31 11:17:51 +01:00
|
|
|
parameter2Bytes := make([]byte, 2)
|
2021-01-04 20:39:35 +01:00
|
|
|
binary.BigEndian.PutUint16(parameter2Bytes, r.Days)
|
2018-10-31 11:17:51 +01:00
|
|
|
headerBytes := bytes.Join([][]byte{
|
2021-01-04 20:39:35 +01:00
|
|
|
{
|
|
|
|
r.Version,
|
|
|
|
byte(r.Action),
|
|
|
|
byte(r.System),
|
|
|
|
byte(r.Root),
|
|
|
|
byte(r.Profile),
|
|
|
|
byte(r.MdAlgorithm),
|
|
|
|
},
|
|
|
|
parameter2Bytes, {r.Spkac}}, []byte{})
|
2021-01-05 19:59:43 +01:00
|
|
|
content1Bytes := r.Content1
|
|
|
|
content2Bytes := r.Content2
|
|
|
|
content3Bytes := r.Content3
|
2018-10-31 11:17:51 +01:00
|
|
|
blockBytes := bytes.Join([][]byte{
|
2021-01-04 20:39:35 +01:00
|
|
|
Encode24BitLength(headerBytes), headerBytes,
|
|
|
|
Encode24BitLength(content1Bytes), content1Bytes,
|
|
|
|
Encode24BitLength(content2Bytes), content2Bytes,
|
|
|
|
Encode24BitLength(content3Bytes), content3Bytes,
|
2018-10-31 11:17:51 +01:00
|
|
|
}, []byte{})
|
2021-01-09 11:24:40 +01:00
|
|
|
|
2021-01-04 20:39:35 +01:00
|
|
|
return bytes.Join([][]byte{Encode24BitLength(blockBytes), blockBytes}, []byte{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SignerRequest) String() string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"v:%d %s s:%d r:%d p:%d md:%d days:%d spkac:%d '%s' '%s' '%s'",
|
|
|
|
r.Version,
|
|
|
|
r.Action,
|
|
|
|
r.System,
|
|
|
|
r.Root,
|
|
|
|
r.Profile,
|
|
|
|
r.MdAlgorithm,
|
|
|
|
r.Days,
|
|
|
|
r.Spkac,
|
2021-01-08 18:41:29 +01:00
|
|
|
shorten(r.Content1),
|
|
|
|
shorten(r.Content2),
|
|
|
|
shorten(r.Content3),
|
2021-01-04 20:39:35 +01:00
|
|
|
)
|
2018-10-31 11:17:51 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 18:41:29 +01:00
|
|
|
func shorten(original []byte) []byte {
|
2021-01-09 11:24:40 +01:00
|
|
|
const maxLength = 20
|
|
|
|
if len(original) > maxLength {
|
|
|
|
return original[:maxLength]
|
2021-01-08 18:41:29 +01:00
|
|
|
}
|
2021-01-09 11:24:40 +01:00
|
|
|
|
2021-01-08 18:41:29 +01:00
|
|
|
return original
|
|
|
|
}
|
|
|
|
|
2018-10-31 11:17:51 +01:00
|
|
|
func NewNulRequest() *SignerRequest {
|
2020-04-17 19:38:54 +02:00
|
|
|
return &SignerRequest{
|
2021-01-04 14:15:12 +01:00
|
|
|
Version: shared.ProtocolVersion,
|
|
|
|
Action: shared.ActionNul,
|
2021-01-05 19:59:43 +01:00
|
|
|
Content1: []byte(time.Now().UTC().Format(signerTimeFormat)),
|
2020-04-17 19:38:54 +02:00
|
|
|
}
|
2018-10-31 11:17:51 +01:00
|
|
|
}
|