File: constructors.go

package info (click to toggle)
golang-github-containers-ocicrypt 1.1.9-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 524 kB
  • sloc: sh: 242; makefile: 17
file content (246 lines) | stat: -rw-r--r-- 6,422 bytes parent folder | download | duplicates (3)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
   Copyright The ocicrypt Authors.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package config

import (
	"errors"
	"fmt"
	"strings"

	"github.com/containers/ocicrypt/crypto/pkcs11"
	"gopkg.in/yaml.v3"
)

// EncryptWithJwe returns a CryptoConfig to encrypt with jwe public keys
func EncryptWithJwe(pubKeys [][]byte) (CryptoConfig, error) {
	dc := DecryptConfig{}
	ep := map[string][][]byte{
		"pubkeys": pubKeys,
	}

	return CryptoConfig{
		EncryptConfig: &EncryptConfig{
			Parameters:    ep,
			DecryptConfig: dc,
		},
		DecryptConfig: &dc,
	}, nil
}

// EncryptWithPkcs7 returns a CryptoConfig to encrypt with pkcs7 x509 certs
func EncryptWithPkcs7(x509s [][]byte) (CryptoConfig, error) {
	dc := DecryptConfig{}

	ep := map[string][][]byte{
		"x509s": x509s,
	}

	return CryptoConfig{
		EncryptConfig: &EncryptConfig{
			Parameters:    ep,
			DecryptConfig: dc,
		},
		DecryptConfig: &dc,
	}, nil
}

// EncryptWithGpg returns a CryptoConfig to encrypt with configured gpg parameters
func EncryptWithGpg(gpgRecipients [][]byte, gpgPubRingFile []byte) (CryptoConfig, error) {
	dc := DecryptConfig{}
	ep := map[string][][]byte{
		"gpg-recipients":     gpgRecipients,
		"gpg-pubkeyringfile": {gpgPubRingFile},
	}

	return CryptoConfig{
		EncryptConfig: &EncryptConfig{
			Parameters:    ep,
			DecryptConfig: dc,
		},
		DecryptConfig: &dc,
	}, nil
}

// EncryptWithPkcs11 returns a CryptoConfig to encrypt with configured pkcs11 parameters
func EncryptWithPkcs11(pkcs11Config *pkcs11.Pkcs11Config, pkcs11Pubkeys, pkcs11Yamls [][]byte) (CryptoConfig, error) {
	dc := DecryptConfig{}
	ep := map[string][][]byte{}

	if len(pkcs11Yamls) > 0 {
		if pkcs11Config == nil {
			return CryptoConfig{}, errors.New("pkcs11Config must not be nil")
		}
		p11confYaml, err := yaml.Marshal(pkcs11Config)
		if err != nil {
			return CryptoConfig{}, fmt.Errorf("Could not marshal Pkcs11Config to Yaml: %w", err)
		}

		dc = DecryptConfig{
			Parameters: map[string][][]byte{
				"pkcs11-config": {p11confYaml},
			},
		}
		ep["pkcs11-yamls"] = pkcs11Yamls
	}
	if len(pkcs11Pubkeys) > 0 {
		ep["pkcs11-pubkeys"] = pkcs11Pubkeys
	}

	return CryptoConfig{
		EncryptConfig: &EncryptConfig{
			Parameters:    ep,
			DecryptConfig: dc,
		},
		DecryptConfig: &dc,
	}, nil
}

// EncryptWithKeyProvider returns a CryptoConfig to encrypt with configured keyprovider parameters
func EncryptWithKeyProvider(keyProviders [][]byte) (CryptoConfig, error) {
	dc := DecryptConfig{}
	ep := make(map[string][][]byte)
	for _, keyProvider := range keyProviders {
		keyProvidersStr := string(keyProvider)
		idx := strings.Index(keyProvidersStr, ":")
		if idx > 0 {
			ep[keyProvidersStr[:idx]] = append(ep[keyProvidersStr[:idx]], []byte(keyProvidersStr[idx+1:]))
		} else {
			ep[keyProvidersStr] = append(ep[keyProvidersStr], []byte("Enabled"))
		}
	}

	return CryptoConfig{
		EncryptConfig: &EncryptConfig{
			Parameters:    ep,
			DecryptConfig: dc,
		},
		DecryptConfig: &dc,
	}, nil
}

// DecryptWithKeyProvider returns a CryptoConfig to decrypt with configured keyprovider parameters
func DecryptWithKeyProvider(keyProviders [][]byte) (CryptoConfig, error) {
	dp := make(map[string][][]byte)
	ep := map[string][][]byte{}
	for _, keyProvider := range keyProviders {
		keyProvidersStr := string(keyProvider)
		idx := strings.Index(keyProvidersStr, ":")
		if idx > 0 {
			dp[keyProvidersStr[:idx]] = append(dp[keyProvidersStr[:idx]], []byte(keyProvidersStr[idx+1:]))
		} else {
			dp[keyProvidersStr] = append(dp[keyProvidersStr], []byte("Enabled"))
		}
	}
	dc := DecryptConfig{
		Parameters: dp,
	}
	return CryptoConfig{
		EncryptConfig: &EncryptConfig{
			Parameters:    ep,
			DecryptConfig: dc,
		},
		DecryptConfig: &dc,
	}, nil
}

// DecryptWithPrivKeys returns a CryptoConfig to decrypt with configured private keys
func DecryptWithPrivKeys(privKeys [][]byte, privKeysPasswords [][]byte) (CryptoConfig, error) {
	if len(privKeys) != len(privKeysPasswords) {
		return CryptoConfig{}, errors.New("Length of privKeys should match length of privKeysPasswords")
	}

	dc := DecryptConfig{
		Parameters: map[string][][]byte{
			"privkeys":           privKeys,
			"privkeys-passwords": privKeysPasswords,
		},
	}

	ep := map[string][][]byte{}

	return CryptoConfig{
		EncryptConfig: &EncryptConfig{
			Parameters:    ep,
			DecryptConfig: dc,
		},
		DecryptConfig: &dc,
	}, nil
}

// DecryptWithX509s returns a CryptoConfig to decrypt with configured x509 certs
func DecryptWithX509s(x509s [][]byte) (CryptoConfig, error) {
	dc := DecryptConfig{
		Parameters: map[string][][]byte{
			"x509s": x509s,
		},
	}

	ep := map[string][][]byte{}

	return CryptoConfig{
		EncryptConfig: &EncryptConfig{
			Parameters:    ep,
			DecryptConfig: dc,
		},
		DecryptConfig: &dc,
	}, nil
}

// DecryptWithGpgPrivKeys returns a CryptoConfig to decrypt with configured gpg private keys
func DecryptWithGpgPrivKeys(gpgPrivKeys, gpgPrivKeysPwds [][]byte) (CryptoConfig, error) {
	dc := DecryptConfig{
		Parameters: map[string][][]byte{
			"gpg-privatekeys":           gpgPrivKeys,
			"gpg-privatekeys-passwords": gpgPrivKeysPwds,
		},
	}

	ep := map[string][][]byte{}

	return CryptoConfig{
		EncryptConfig: &EncryptConfig{
			Parameters:    ep,
			DecryptConfig: dc,
		},
		DecryptConfig: &dc,
	}, nil
}

// DecryptWithPkcs11Yaml returns a CryptoConfig to decrypt with pkcs11 YAML formatted key files
func DecryptWithPkcs11Yaml(pkcs11Config *pkcs11.Pkcs11Config, pkcs11Yamls [][]byte) (CryptoConfig, error) {
	p11confYaml, err := yaml.Marshal(pkcs11Config)
	if err != nil {
		return CryptoConfig{}, fmt.Errorf("Could not marshal Pkcs11Config to Yaml: %w", err)
	}

	dc := DecryptConfig{
		Parameters: map[string][][]byte{
			"pkcs11-yamls":  pkcs11Yamls,
			"pkcs11-config": {p11confYaml},
		},
	}

	ep := map[string][][]byte{}

	return CryptoConfig{
		EncryptConfig: &EncryptConfig{
			Parameters:    ep,
			DecryptConfig: dc,
		},
		DecryptConfig: &dc,
	}, nil
}