@@ -19,8 +19,6 @@ package keystore
1919
2020import (
2121 "bytes"
22- "crypto/aes"
23- "crypto/cipher"
2422 "crypto/rand"
2523 "encoding/base64"
2624 "encoding/json"
@@ -45,9 +43,6 @@ const (
4543 keyLength = 32
4644)
4745
48- // Version of the keystore format, will be added at the beginning of the file.
49- var version = []byte ("v1" )
50-
5146// Packager defines a keystore that we can read the raw bytes and be packaged in an artifact.
5247type Packager interface {
5348 Package () ([]byte , error )
@@ -322,95 +317,6 @@ func (k *FileKeystore) load() error {
322317 return jsonDecoder .Decode (& k .secrets )
323318}
324319
325- // Encrypt the data payload using a derived keys and the AES-256-GCM algorithm.
326- func (k * FileKeystore ) encrypt (reader io.Reader ) (io.Reader , error ) {
327- // randomly generate the salt and the initialization vector, this information will be saved
328- // on disk in the file as part of the header
329- iv , err := randomBytes (iVLength )
330-
331- if err != nil {
332- return nil , err
333- }
334-
335- salt , err := randomBytes (saltLength )
336- if err != nil {
337- return nil , err
338- }
339-
340- // Stretch the user provided key
341- password , _ := k .password .Get ()
342- passwordBytes , err := k .hashPassword (string (password ), salt )
343- if err != nil {
344- return nil , fmt .Errorf ("could not hash password, error: %w" , err )
345- }
346-
347- // Select AES-256: because len(passwordBytes) == 32 bytes
348- block , err := aes .NewCipher (passwordBytes )
349- if err != nil {
350- return nil , fmt .Errorf ("could not create the keystore cipher to encrypt, error: %w" , err )
351- }
352-
353- aesgcm , err := cipher .NewGCM (block )
354- if err != nil {
355- return nil , fmt .Errorf ("could not create the keystore cipher to encrypt, error: %w" , err )
356- }
357-
358- data , err := io .ReadAll (reader )
359- if err != nil {
360- return nil , fmt .Errorf ("could not read unencrypted data, error: %w" , err )
361- }
362-
363- encodedBytes := aesgcm .Seal (nil , iv , data , nil )
364-
365- // Generate the payload with all the additional information required to decrypt the
366- // output format of the document: VERSION|SALT|IV|PAYLOAD
367- buf := bytes .NewBuffer (salt )
368- buf .Write (iv )
369- buf .Write (encodedBytes )
370-
371- return buf , nil
372- }
373-
374- // should receive an io.reader...
375- func (k * FileKeystore ) decrypt (reader io.Reader ) (io.Reader , error ) {
376- data , err := io .ReadAll (reader )
377- if err != nil {
378- return nil , fmt .Errorf ("could not read all the data from the encrypted file, error: %w" , err )
379- }
380-
381- if len (data ) < saltLength + iVLength + 1 {
382- return nil , fmt .Errorf ("missing information in the file for decrypting the keystore" )
383- }
384-
385- // extract the necessary information to decrypt the data from the data payload
386- salt := data [0 :saltLength ]
387- iv := data [saltLength : saltLength + iVLength ]
388- encodedBytes := data [saltLength + iVLength :]
389-
390- password , _ := k .password .Get ()
391- passwordBytes , err := k .hashPassword (string (password ), salt )
392- if err != nil {
393- return nil , fmt .Errorf ("could not hash password, error: %w" , err )
394- }
395-
396- block , err := aes .NewCipher (passwordBytes )
397- if err != nil {
398- return nil , fmt .Errorf ("could not create the keystore cipher to decrypt the data: %w" , err )
399- }
400-
401- aesgcm , err := cipher .NewGCM (block )
402- if err != nil {
403- return nil , fmt .Errorf ("could not create the keystore cipher to decrypt the data: %w" , err )
404- }
405-
406- decodedBytes , err := aesgcm .Open (nil , iv , encodedBytes , nil )
407- if err != nil {
408- return nil , fmt .Errorf ("could not decrypt keystore data: %w" , err )
409- }
410-
411- return bytes .NewReader (decodedBytes ), nil
412- }
413-
414320// checkPermission enforces permission on the keystore file itself, the file should have strict
415321// permission (0600) and the keystore should refuses to start if its not the case.
416322func (k * FileKeystore ) checkPermissions (f string ) error {
0 commit comments