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

@ -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{},
}
}