|
| 1 | +package security |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/mitchellh/mapstructure" |
| 7 | +) |
| 8 | + |
| 9 | +// Copied from service/kas/access to avoid dep loop. To be removed. |
| 10 | +type CurrentKeyFor struct { |
| 11 | + Algorithm string `mapstructure:"alg"` |
| 12 | + KID string `mapstructure:"kid"` |
| 13 | + Private string `mapstructure:"private"` |
| 14 | + Certificate string `mapstructure:"cert"` |
| 15 | + Active bool `mapstructure:"active"` |
| 16 | + Legacy bool `mapstructure:"legacy"` |
| 17 | +} |
| 18 | + |
| 19 | +// locate finds the index of the key in the Keyring slice. |
| 20 | +func (k *KASConfigDupe) locate(kid string) (int, bool) { |
| 21 | + for i, key := range k.Keyring { |
| 22 | + if key.KID == kid { |
| 23 | + return i, true |
| 24 | + } |
| 25 | + } |
| 26 | + return -1, false |
| 27 | +} |
| 28 | + |
| 29 | +// For entries in keyring that appear with the same value for their KID field, |
| 30 | +// consolidate them into a single entry. If one of the copies has 'Legacy' set, let the consolidated entry have 'Legacy' set. |
| 31 | +// If one of the entries does not have `Legacy` set, set the value of `Active`. |
| 32 | +func (k *KASConfigDupe) consolidate() { |
| 33 | + seen := make(map[string]int) |
| 34 | + for i, key := range k.Keyring { |
| 35 | + if j, ok := seen[key.KID]; ok { |
| 36 | + if key.Legacy { |
| 37 | + k.Keyring[j].Legacy = true |
| 38 | + } else { |
| 39 | + k.Keyring[j].Active = key.Active |
| 40 | + } |
| 41 | + k.Keyring = append(k.Keyring[:i], k.Keyring[i+1:]...) |
| 42 | + i-- |
| 43 | + } else { |
| 44 | + seen[key.KID] = i |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Deprecated |
| 50 | +type KeyPairInfo struct { |
| 51 | + // Valid algorithm. May be able to be derived from Private but it is better to just say it. |
| 52 | + Algorithm string `mapstructure:"alg" json:"alg"` |
| 53 | + // Key identifier. Should be short |
| 54 | + KID string `mapstructure:"kid" json:"kid"` |
| 55 | + // Implementation specific locator for private key; |
| 56 | + // for 'standard' crypto service this is the path to a PEM file |
| 57 | + Private string `mapstructure:"private" json:"private"` |
| 58 | + // Optional locator for the corresponding certificate. |
| 59 | + // If not found, only public key (derivable from Private) is available. |
| 60 | + Certificate string `mapstructure:"cert" json:"cert"` |
| 61 | + // Optional enumeration of intended usages of keypair |
| 62 | + Usage string `mapstructure:"usage" json:"usage"` |
| 63 | + // Optional long form description of key pair including purpose and life cycle information |
| 64 | + Purpose string `mapstructure:"purpose" json:"purpose"` |
| 65 | +} |
| 66 | + |
| 67 | +// Deprecated |
| 68 | +type StandardKeyInfo struct { |
| 69 | + PrivateKeyPath string `mapstructure:"private_key_path" json:"private_key_path"` |
| 70 | + PublicKeyPath string `mapstructure:"public_key_path" json:"public_key_path"` |
| 71 | +} |
| 72 | + |
| 73 | +// Deprecated |
| 74 | +type CryptoConfig2024 struct { |
| 75 | + Keys []KeyPairInfo `mapstructure:"keys" json:"keys"` |
| 76 | + // Deprecated |
| 77 | + RSAKeys map[string]StandardKeyInfo `mapstructure:"rsa,omitempty" json:"rsa,omitempty"` |
| 78 | + // Deprecated |
| 79 | + ECKeys map[string]StandardKeyInfo `mapstructure:"ec,omitempty" json:"ec,omitempty"` |
| 80 | +} |
| 81 | + |
| 82 | +type KASConfigDupe struct { |
| 83 | + // Which keys are currently the default. |
| 84 | + Keyring []CurrentKeyFor `mapstructure:"keyring" json:"keyring"` |
| 85 | + // Deprecated |
| 86 | + ECCertID string `mapstructure:"eccertid" json:"eccertid"` |
| 87 | + // Deprecated |
| 88 | + RSACertID string `mapstructure:"rsacertid" json:"rsacertid"` |
| 89 | +} |
| 90 | + |
| 91 | +func (c CryptoConfig2024) MarshalTo(within map[string]any) error { |
| 92 | + var kasCfg KASConfigDupe |
| 93 | + if err := mapstructure.Decode(within, &kasCfg); err != nil { |
| 94 | + return fmt.Errorf("invalid kas cfg [%v] %w", within, err) |
| 95 | + } |
| 96 | + kasCfg.consolidate() |
| 97 | + for kid, stdKeyInfo := range c.RSAKeys { |
| 98 | + if i, ok := kasCfg.locate(kid); ok { |
| 99 | + kasCfg.Keyring[i].Private = stdKeyInfo.PrivateKeyPath |
| 100 | + kasCfg.Keyring[i].Certificate = stdKeyInfo.PublicKeyPath |
| 101 | + continue |
| 102 | + } |
| 103 | + k := CurrentKeyFor{ |
| 104 | + Algorithm: "rsa:2048", |
| 105 | + KID: kid, |
| 106 | + Private: stdKeyInfo.PrivateKeyPath, |
| 107 | + Certificate: stdKeyInfo.PublicKeyPath, |
| 108 | + Active: true, |
| 109 | + Legacy: true, |
| 110 | + } |
| 111 | + kasCfg.Keyring = append(kasCfg.Keyring, k) |
| 112 | + } |
| 113 | + for kid, stdKeyInfo := range c.ECKeys { |
| 114 | + if i, ok := kasCfg.locate(kid); ok { |
| 115 | + kasCfg.Keyring[i].Private = stdKeyInfo.PrivateKeyPath |
| 116 | + kasCfg.Keyring[i].Certificate = stdKeyInfo.PublicKeyPath |
| 117 | + continue |
| 118 | + } |
| 119 | + k := CurrentKeyFor{ |
| 120 | + Algorithm: "ec:secp256r1", |
| 121 | + KID: kid, |
| 122 | + Private: stdKeyInfo.PrivateKeyPath, |
| 123 | + Certificate: stdKeyInfo.PublicKeyPath, |
| 124 | + Active: true, |
| 125 | + Legacy: true, |
| 126 | + } |
| 127 | + kasCfg.Keyring = append(kasCfg.Keyring, k) |
| 128 | + } |
| 129 | + for _, k := range c.Keys { |
| 130 | + if i, ok := kasCfg.locate(k.KID); ok { |
| 131 | + kasCfg.Keyring[i].Private = k.Private |
| 132 | + kasCfg.Keyring[i].Certificate = k.Certificate |
| 133 | + continue |
| 134 | + } |
| 135 | + kasCfg.Keyring = append(kasCfg.Keyring, CurrentKeyFor{ |
| 136 | + Algorithm: k.Algorithm, |
| 137 | + KID: k.KID, |
| 138 | + Private: k.Private, |
| 139 | + Certificate: k.Certificate, |
| 140 | + }) |
| 141 | + } |
| 142 | + return nil |
| 143 | +} |
0 commit comments