/
mappubkey.go
99 lines (84 loc) · 2.71 KB
/
mappubkey.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package oid
import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"github.com/pkg/errors"
)
// PublicKeyAlgorithmInfo provides OID info for Public Key algorithms
type PublicKeyAlgorithmInfo struct {
name string
oid asn1.ObjectIdentifier
oidstr string
registration string
publey x509.PublicKeyAlgorithm
}
// Algorithm returns x509.PublicKeyAlgorithm
func (h PublicKeyAlgorithmInfo) Algorithm() x509.PublicKeyAlgorithm {
return h.publey
}
// Name is friendly name of the OID: SHA1, etc
func (h PublicKeyAlgorithmInfo) Name() string {
return h.name
}
// OID is ASN1 ObjectIdentifier
func (h PublicKeyAlgorithmInfo) OID() asn1.ObjectIdentifier {
return h.oid
}
// Registration returns official registration info in
// "{iso(1) identified-organization(3) oiw(14) secsig(3) algorithm(2) 26}" format
func (h PublicKeyAlgorithmInfo) Registration() string {
return h.registration
}
// String returns string representation of OID: "1.2.840.113549.1"
func (h PublicKeyAlgorithmInfo) String() string {
if h.oidstr == "" {
h.oidstr = h.oid.String()
}
return h.oidstr
}
// Type specifies OID algorithm type for PubKey
func (h PublicKeyAlgorithmInfo) Type() AlgType {
return AlgPubKey
}
//
// Public Key Algorithms
//
// RSA specifies RFC 3279, 2.3 Public Key Algorithm info
var RSA = PublicKeyAlgorithmInfo{
name: x509.RSA.String(),
publey: x509.RSA,
oid: SignatureAlgorithmRSA,
registration: "{iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) 1}",
}
// ECDSA specifies RFC 3279, Algorithms and Identifiers for the Internet X.509 Public Key Infrastructure
var ECDSA = PublicKeyAlgorithmInfo{
name: x509.ECDSA.String(),
publey: x509.ECDSA,
oid: SignatureAlgorithmECDSA,
registration: "{iso(1) member-body(2) us(840) ansi-X9-62(10045) keyType(2) 1}",
}
// PublicKeyAlgorithmToSignatureAlgorithm maps certificate public key
// algorithms to CMS signature algorithms.
var PublicKeyAlgorithmToSignatureAlgorithm = map[x509.PublicKeyAlgorithm]pkix.AlgorithmIdentifier{
x509.RSA: {Algorithm: SignatureAlgorithmRSA},
x509.ECDSA: {Algorithm: SignatureAlgorithmECDSA},
}
// PublicKeyAlgorithmByOID returns an algorithm by OID
func PublicKeyAlgorithmByOID(oid string) (*PublicKeyAlgorithmInfo, error) {
item := LookupByOID(oid)
algo, ok := item.(PublicKeyAlgorithmInfo)
if !ok {
return nil, errors.Errorf("algorithm not found: %s", oid)
}
return &algo, nil
}
// PublicKeyAlgorithmByName returns an algorithm by name
func PublicKeyAlgorithmByName(name string) (*PublicKeyAlgorithmInfo, error) {
item := LookupByName(name)
algo, ok := item.(*PublicKeyAlgorithmInfo)
if !ok {
return nil, errors.Errorf("algorithm not found: %s", name)
}
return algo, nil
}