Skip to content

Commit 25d1184

Browse files
authored
feat: add aki, ski and keyUsage to certificate output (#17)
Add Authority Key Identifier, Subject Key Identifier and Key Usages to the certificate output
1 parent 67ac46c commit 25d1184

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

certify.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func CertInfo(cert *x509.Certificate) string {
163163

164164
if cert.KeyUsage != 0 {
165165
buf.WriteString(fmt.Sprintf("%12sX509v3 Key Usage:\n", ""))
166-
buf.WriteString(fmt.Sprintf("%16s%v\n", "", parseKeyUsage(cert.KeyUsage)))
166+
buf.WriteString(fmt.Sprintf("%16s%v\n", "", strings.Join(parseKeyUsage(cert.KeyUsage), ", ")))
167167
}
168168

169169
buf.WriteString(fmt.Sprintf("%12sX509v3 Basic Constraints:\n", ""))
@@ -174,6 +174,11 @@ func CertInfo(cert *x509.Certificate) string {
174174
buf.WriteString(fmt.Sprintf("%16s%v\n", "", formatKeyIDWithColon(cert.SubjectKeyId)))
175175
}
176176

177+
if cert.AuthorityKeyId != nil {
178+
buf.WriteString(fmt.Sprintf("%12sX509v3 Authority Key Identifier:\n", ""))
179+
buf.WriteString(fmt.Sprintf("%16s%v\n", "", formatKeyIDWithColon(cert.AuthorityKeyId)))
180+
}
181+
177182
if len(cert.IPAddresses) != 0 || len(cert.DNSNames) != 0 {
178183
buf.WriteString(fmt.Sprintf("%12sX509v3 Subject Alternative Name:\n", ""))
179184
if len(cert.IPAddresses) != 0 {

helper.go

+31-10
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,38 @@ func GetPublicKey(pub interface{}) (string, error) {
2626
return w.String(), err
2727
}
2828

29-
func parseKeyUsage(ku x509.KeyUsage) string {
30-
switch ku {
31-
case x509.KeyUsageCRLSign:
32-
return "CRL Sign"
33-
case x509.KeyUsageCertSign:
34-
return "Cert Sign"
35-
case x509.KeyUsageDigitalSignature:
36-
return "Digital Signature"
37-
default:
38-
return ""
29+
func parseKeyUsage(ku x509.KeyUsage) []string {
30+
usages := []string{}
31+
32+
if ku&x509.KeyUsageDigitalSignature > 0 {
33+
usages = append(usages, "Digital Signature")
34+
}
35+
if ku&x509.KeyUsageContentCommitment > 0 {
36+
usages = append(usages, "Content Commitment")
37+
}
38+
if ku&x509.KeyUsageDataEncipherment > 0 {
39+
usages = append(usages, "Key Encipherment")
40+
}
41+
if ku&x509.KeyUsageDataEncipherment > 0 {
42+
usages = append(usages, "Data Encipherment")
43+
}
44+
if ku&x509.KeyUsageKeyAgreement > 0 {
45+
usages = append(usages, "Key Agreement")
3946
}
47+
if ku&x509.KeyUsageCertSign > 0 {
48+
usages = append(usages, "Cert Sign")
49+
}
50+
if ku&x509.KeyUsageCRLSign > 0 {
51+
usages = append(usages, "CRL Sign")
52+
}
53+
if ku&x509.KeyUsageEncipherOnly > 0 {
54+
usages = append(usages, "Enchiper Only")
55+
}
56+
if ku&x509.KeyUsageDecipherOnly > 0 {
57+
usages = append(usages, "Dechiper Only")
58+
}
59+
60+
return usages
4061
}
4162

4263
func parseExtKeyUsage(ekus []x509.ExtKeyUsage) string {

helper_test.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package certify
33
import (
44
"crypto/x509"
55
"os"
6+
"reflect"
67
"testing"
78
)
89

@@ -32,34 +33,34 @@ func TestParseKeyUsage(t *testing.T) {
3233
tests := []struct {
3334
Name string
3435
KeyUsage x509.KeyUsage
35-
Expected string
36+
Expected []string
3637
}{
3738
{
38-
Name: "Test Cert Sign Key Usage",
39-
KeyUsage: x509.KeyUsageCertSign,
40-
Expected: "Cert Sign",
39+
Name: "Test Cert Sign and CRL Sign Key Usage",
40+
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
41+
Expected: []string{"Cert Sign", "CRL Sign"},
4142
},
4243
{
4344
Name: "Test CRL Sign Key Usage",
4445
KeyUsage: x509.KeyUsageCRLSign,
45-
Expected: "CRL Sign",
46+
Expected: []string{"CRL Sign"},
4647
},
4748
{
4849
Name: "Test Digital Signature Key Usage",
4950
KeyUsage: x509.KeyUsageDigitalSignature,
50-
Expected: "Digital Signature",
51+
Expected: []string{"Digital Signature"},
5152
},
5253
{
5354
Name: "Test other Key Usage",
54-
KeyUsage: x509.KeyUsageEncipherOnly,
55-
Expected: "",
55+
KeyUsage: x509.KeyUsage(0),
56+
Expected: []string{},
5657
},
5758
}
5859

5960
for _, tt := range tests {
6061
t.Run(tt.Name, func(t *testing.T) {
6162
got := parseKeyUsage(tt.KeyUsage)
62-
if got != tt.Expected {
63+
if !reflect.DeepEqual(got, tt.Expected) {
6364
t.Fatalf("got %v, want %v", got, tt.Expected)
6465
}
6566
})

0 commit comments

Comments
 (0)