...
1
2
3 package jwa
4
5 import (
6 "fmt"
7 "sort"
8 "sync"
9
10 "github.com/pkg/errors"
11 )
12
13
14 type KeyEncryptionAlgorithm string
15
16
17 const (
18 A128GCMKW KeyEncryptionAlgorithm = "A128GCMKW"
19 A128KW KeyEncryptionAlgorithm = "A128KW"
20 A192GCMKW KeyEncryptionAlgorithm = "A192GCMKW"
21 A192KW KeyEncryptionAlgorithm = "A192KW"
22 A256GCMKW KeyEncryptionAlgorithm = "A256GCMKW"
23 A256KW KeyEncryptionAlgorithm = "A256KW"
24 DIRECT KeyEncryptionAlgorithm = "dir"
25 ECDH_ES KeyEncryptionAlgorithm = "ECDH-ES"
26 ECDH_ES_A128KW KeyEncryptionAlgorithm = "ECDH-ES+A128KW"
27 ECDH_ES_A192KW KeyEncryptionAlgorithm = "ECDH-ES+A192KW"
28 ECDH_ES_A256KW KeyEncryptionAlgorithm = "ECDH-ES+A256KW"
29 PBES2_HS256_A128KW KeyEncryptionAlgorithm = "PBES2-HS256+A128KW"
30 PBES2_HS384_A192KW KeyEncryptionAlgorithm = "PBES2-HS384+A192KW"
31 PBES2_HS512_A256KW KeyEncryptionAlgorithm = "PBES2-HS512+A256KW"
32 RSA1_5 KeyEncryptionAlgorithm = "RSA1_5"
33 RSA_OAEP KeyEncryptionAlgorithm = "RSA-OAEP"
34 RSA_OAEP_256 KeyEncryptionAlgorithm = "RSA-OAEP-256"
35 )
36
37 var allKeyEncryptionAlgorithms = map[KeyEncryptionAlgorithm]struct{}{
38 A128GCMKW: {},
39 A128KW: {},
40 A192GCMKW: {},
41 A192KW: {},
42 A256GCMKW: {},
43 A256KW: {},
44 DIRECT: {},
45 ECDH_ES: {},
46 ECDH_ES_A128KW: {},
47 ECDH_ES_A192KW: {},
48 ECDH_ES_A256KW: {},
49 PBES2_HS256_A128KW: {},
50 PBES2_HS384_A192KW: {},
51 PBES2_HS512_A256KW: {},
52 RSA1_5: {},
53 RSA_OAEP: {},
54 RSA_OAEP_256: {},
55 }
56
57 var listKeyEncryptionAlgorithmOnce sync.Once
58 var listKeyEncryptionAlgorithm []KeyEncryptionAlgorithm
59
60
61 func KeyEncryptionAlgorithms() []KeyEncryptionAlgorithm {
62 listKeyEncryptionAlgorithmOnce.Do(func() {
63 listKeyEncryptionAlgorithm = make([]KeyEncryptionAlgorithm, 0, len(allKeyEncryptionAlgorithms))
64 for v := range allKeyEncryptionAlgorithms {
65 listKeyEncryptionAlgorithm = append(listKeyEncryptionAlgorithm, v)
66 }
67 sort.Slice(listKeyEncryptionAlgorithm, func(i, j int) bool {
68 return string(listKeyEncryptionAlgorithm[i]) < string(listKeyEncryptionAlgorithm[j])
69 })
70 })
71 return listKeyEncryptionAlgorithm
72 }
73
74
75
76 func (v *KeyEncryptionAlgorithm) Accept(value interface{}) error {
77 var tmp KeyEncryptionAlgorithm
78 if x, ok := value.(KeyEncryptionAlgorithm); ok {
79 tmp = x
80 } else {
81 var s string
82 switch x := value.(type) {
83 case fmt.Stringer:
84 s = x.String()
85 case string:
86 s = x
87 default:
88 return errors.Errorf(`invalid type for jwa.KeyEncryptionAlgorithm: %T`, value)
89 }
90 tmp = KeyEncryptionAlgorithm(s)
91 }
92 if _, ok := allKeyEncryptionAlgorithms[tmp]; !ok {
93 return errors.Errorf(`invalid jwa.KeyEncryptionAlgorithm value`)
94 }
95
96 *v = tmp
97 return nil
98 }
99
100
101 func (v KeyEncryptionAlgorithm) String() string {
102 return string(v)
103 }
104
105
106 func (v KeyEncryptionAlgorithm) IsSymmetric() bool {
107 switch v {
108 case A128GCMKW, A128KW, A192GCMKW, A192KW, A256GCMKW, A256KW, DIRECT, PBES2_HS256_A128KW, PBES2_HS384_A192KW, PBES2_HS512_A256KW:
109 return true
110 }
111 return false
112 }
113
View as plain text