-
Notifications
You must be signed in to change notification settings - Fork 0
/
cose.go
38 lines (32 loc) · 812 Bytes
/
cose.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
package mdoc
import (
"crypto/x509"
"errors"
"github.com/veraison/go-cose"
)
var (
ErrUnrecognisedHeaderType = errors.New("unrecognised type for header")
)
func x509Chain(from cose.UnprotectedHeader) ([]*x509.Certificate, error) {
x509ChainHeader := from[cose.HeaderLabelX5Chain]
switch x509ChainEncoded := x509ChainHeader.(type) {
case []byte:
cert, err := x509.ParseCertificate(x509ChainEncoded)
if err != nil {
return nil, err
}
return []*x509.Certificate{cert}, nil
case [][]byte:
certs := make([]*x509.Certificate, len(x509ChainEncoded))
for i, certEncoded := range x509ChainEncoded {
cert, err := x509.ParseCertificate(certEncoded)
if err != nil {
return nil, err
}
certs[i] = cert
}
return certs, nil
default:
return nil, ErrUnrecognisedHeaderType
}
}