-
Notifications
You must be signed in to change notification settings - Fork 18
/
pem.go
40 lines (35 loc) · 922 Bytes
/
pem.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
package tsa
import (
"encoding/pem"
"github.com/InfiniteLoopSpace/go_S-MIME/asn1"
cms "github.com/InfiniteLoopSpace/go_S-MIME/cms/protocol"
"github.com/mandelsoft/goutils/errors"
)
const PRM_BLOCK_TYPE = "TIMESTAMP INFO"
func ToPem(sd *TimeStamp) ([]byte, error) {
data, err := asn1.Marshal(*sd)
if err != nil {
return nil, errors.Wrapf(err, "cannot marshal timestamp data")
}
pemBytes := pem.EncodeToMemory(&pem.Block{
Type: PRM_BLOCK_TYPE,
Headers: nil,
Bytes: data,
})
return pemBytes, nil
}
func FromPem(data []byte) (*TimeStamp, error) {
block, rest := pem.Decode(data)
if block == nil || len(rest) > 0 {
return nil, errors.ErrInvalid("timestamp")
}
if block.Type != PRM_BLOCK_TYPE {
return nil, errors.ErrInvalid("PEM block type", block.Type, "timestamp")
}
var n cms.SignedData
_, err := asn1.Unmarshal(block.Bytes, &n)
if err != nil {
return nil, err
}
return &n, nil
}