-
Notifications
You must be signed in to change notification settings - Fork 4
/
formats.go
74 lines (68 loc) · 1.47 KB
/
formats.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
package schema
import (
"crypto/x509"
"encoding/pem"
"github.com/kong/koko/internal/crypto"
"github.com/santhosh-tekuri/jsonschema/v5"
)
func isPEMEncodedCertificate(v interface{}) bool {
switch v := v.(type) {
case []byte:
_, err := crypto.ParsePEMCert(v)
return err == nil
case string:
_, err := crypto.ParsePEMCert([]byte(v))
return err == nil
default:
return false
}
}
func isPEMEncodedPrivateKey(v interface{}) bool {
var block *pem.Block
switch v := v.(type) {
case []byte:
block, _ = pem.Decode(v)
case string:
block, _ = pem.Decode([]byte(v))
default:
return false
}
if block == nil {
return false
}
_, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err == nil {
return true
}
_, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err == nil {
return true
}
_, err = x509.ParseECPrivateKey(block.Bytes)
return err == nil
}
func isPEMEncodedPublicKey(v interface{}) bool {
var block *pem.Block
switch v := v.(type) {
case []byte:
block, _ = pem.Decode(v)
case string:
block, _ = pem.Decode([]byte(v))
default:
return false
}
if block == nil {
return false
}
_, err := x509.ParsePKIXPublicKey(block.Bytes)
if err == nil {
return true
}
_, err = x509.ParsePKCS1PublicKey(block.Bytes)
return err == nil
}
func init() {
jsonschema.Formats["pem-encoded-cert"] = isPEMEncodedCertificate
jsonschema.Formats["pem-encoded-private-key"] = isPEMEncodedPrivateKey
jsonschema.Formats["pem-encoded-public-key"] = isPEMEncodedPublicKey
}