Refactor code structure

Move X.509 and Openpgp operations into custom packages. Implement more
robust input reading. Do not convert []byte to string unnecessarily.

Finish implementation of X.509 CRL creation.
This commit is contained in:
Jan Dittberner 2021-01-05 19:59:43 +01:00
parent 2de9771472
commit 9f0916b14a
9 changed files with 715 additions and 493 deletions

View file

@ -18,9 +18,9 @@ type SignerRequest struct {
MdAlgorithm shared.MessageDigestAlgorithmId
Days uint16
Spkac uint8
Content1 string
Content2 string
Content3 string
Content1 []byte
Content2 []byte
Content3 []byte
}
func SignerRequestFromData(blockData []byte) (*SignerRequest, error) {
@ -29,15 +29,15 @@ func SignerRequestFromData(blockData []byte) (*SignerRequest, error) {
contentBytes := blockData[3+headerLength:]
content1Length := Decode24BitLength(contentBytes[0:3])
content1 := string(contentBytes[3 : 3+content1Length])
content1 := contentBytes[3 : 3+content1Length]
content2Offset := 3 + content1Length
content2Length := Decode24BitLength(contentBytes[content2Offset : content2Offset+3])
content2 := string(contentBytes[3+content2Offset : 3+content2Offset+content2Length])
content2 := contentBytes[3+content2Offset : 3+content2Offset+content2Length]
content3Offset := 3 + content2Offset + content2Length
content3Length := Decode24BitLength(contentBytes[content3Offset : content3Offset+3])
content3 := string(contentBytes[3+content3Offset : 3+content3Offset+content3Length])
content3 := contentBytes[3+content3Offset : 3+content3Offset+content3Length]
return &SignerRequest{
Version: headerBytes[0],
@ -67,9 +67,9 @@ func (r *SignerRequest) Serialize() []byte {
byte(r.MdAlgorithm),
},
parameter2Bytes, {r.Spkac}}, []byte{})
content1Bytes := []byte(r.Content1)
content2Bytes := []byte(r.Content2)
content3Bytes := []byte(r.Content3)
content1Bytes := r.Content1
content2Bytes := r.Content2
content3Bytes := r.Content3
blockBytes := bytes.Join([][]byte{
Encode24BitLength(headerBytes), headerBytes,
Encode24BitLength(content1Bytes), content1Bytes,
@ -100,6 +100,6 @@ func NewNulRequest() *SignerRequest {
return &SignerRequest{
Version: shared.ProtocolVersion,
Action: shared.ActionNul,
Content1: time.Now().UTC().Format(signerTimeFormat),
Content1: []byte(time.Now().UTC().Format(signerTimeFormat)),
}
}

View file

@ -13,9 +13,9 @@ type SignerResponse struct {
Action shared.Action
Reserved1 uint8
Reserved2 uint8
Content1 string
Content2 string
Content3 string
Content []byte
Argument1 []byte
Argument2 []byte
}
func SignerResponseFromData(lengthBytes []byte, blockData []byte, checkSum byte) (*SignerResponse, error) {
@ -29,14 +29,14 @@ func SignerResponseFromData(lengthBytes []byte, blockData []byte, checkSum byte)
headerBytes := blockData[offset : offset+headerLength]
offset += headerLength
content := make([]string, 3)
content := make([][]byte, 3)
for offset < len(blockData) {
dataLength := Decode24BitLength(blockData[offset : offset+3])
if len(blockData)-3 < dataLength {
return nil, errors.New("structure cut off")
}
offset += 3
content = append(content, string(blockData[offset:offset+dataLength]))
content = append(content, blockData[offset:offset+dataLength])
offset += dataLength
}
@ -50,32 +50,49 @@ func SignerResponseFromData(lengthBytes []byte, blockData []byte, checkSum byte)
Action: shared.Action(headerBytes[1]),
Reserved1: headerBytes[2],
Reserved2: headerBytes[3],
Content1: content[0],
Content2: content[1],
Content3: content[2],
Content: content[0],
Argument1: content[1],
Argument2: content[2],
}, nil
}
func (r SignerResponse) Serialize() []byte {
headerBytes := []byte{r.Version, byte(r.Action), r.Reserved1, r.Reserved2}
content1Bytes := []byte(r.Content1)
content2Bytes := []byte(r.Content2)
content3Bytes := []byte(r.Content3)
blockBytes := bytes.Join([][]byte{
Encode24BitLength(headerBytes), headerBytes,
Encode24BitLength(content1Bytes), content1Bytes,
Encode24BitLength(content2Bytes), content2Bytes,
Encode24BitLength(content3Bytes), content3Bytes,
Encode24BitLength(r.Content), r.Content,
Encode24BitLength(r.Argument1), r.Argument1,
Encode24BitLength(r.Argument2), r.Argument2,
}, []byte{})
return bytes.Join([][]byte{Encode24BitLength(blockBytes), blockBytes}, []byte{})
}
func NewNulResponse(version byte) *SignerResponse {
return &SignerResponse{
Version: version,
Action: shared.ActionNul,
Content1: "",
Content2: "",
Content3: "",
Version: version,
Action: shared.ActionNul,
Content: []byte{},
Argument1: []byte{},
Argument2: []byte{},
}
}
func NewRevokeResponse(version byte, content []byte) *SignerResponse {
return &SignerResponse{
Version: version,
Action: shared.ActionRevoke,
Content: content,
Argument1: []byte{},
Argument2: []byte{},
}
}
func NewSignResponse(version byte, content []byte) *SignerResponse {
return &SignerResponse{
Version: version,
Action: shared.ActionSign,
Content: content,
Argument1: []byte{},
Argument2: []byte{},
}
}