You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2022/06/16 10:32:41 UTC

[couchdb] branch database_encryption updated (ad9171848 -> 429fd31c2)

This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a change to branch database_encryption
in repository https://gitbox.apache.org/repos/asf/couchdb.git


    from ad9171848 manager assigns key id in new_dek response
     new d25b99018 new_aes_iv function
     new 429fd31c2 better warnings for corrupted encryption headers

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/couch/src/couch_file.erl | 45 +++++++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 17 deletions(-)


[couchdb] 01/02: new_aes_iv function

Posted by rn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch database_encryption
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit d25b990181b0b47e6bd9577a742f47dfc4ec0fa6
Author: Robert Newson <rn...@apache.org>
AuthorDate: Thu Jun 16 11:28:14 2022 +0100

    new_aes_iv function
---
 src/couch/src/couch_file.erl | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/couch/src/couch_file.erl b/src/couch/src/couch_file.erl
index ed2315580..ab707c5cc 100644
--- a/src/couch/src/couch_file.erl
+++ b/src/couch/src/couch_file.erl
@@ -644,7 +644,7 @@ handle_call({truncate, Pos}, _From, #file{fd = Fd, dek = DEK} = File0) when
             case file:truncate(Fd) of
                 ok ->
                     File1 = File0#file{eof = 0},
-                    IV = crypto:strong_rand_bytes(16),
+                    IV = new_aes_iv(),
                     case write_encryption_header(File1, KeyID, WEK, IV) of
                         {ok, File2} ->
                             ok = file:sync(File2#file.fd),
@@ -998,7 +998,7 @@ init_crypto(#file{eof = 0, dek = undefined} = File0, Options) ->
         {db_name, DbName} ->
             case couch_encryption_manager:new_dek(DbName) of
                 {ok, KeyID, DEK, WEK} ->
-                    IV = crypto:strong_rand_bytes(16),
+                    IV = new_aes_iv(),
                     case write_encryption_header(File0, KeyID, WEK, IV) of
                         {ok, File1} ->
                             ok = file:sync(File1#file.fd),
@@ -1140,6 +1140,9 @@ encrypted_pread(#file{} = File, Pos, Len) ->
             Else
     end.
 
+new_aes_iv() ->
+    crypto:strong_rand_bytes(16).
+
 aes_ctr(IV, Pos) ->
     <<(IV + (Pos div 16)):128>>.
 


[couchdb] 02/02: better warnings for corrupted encryption headers

Posted by rn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch database_encryption
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 429fd31c2648b5db0f6930353fe12a9b6cccb5b4
Author: Robert Newson <rn...@apache.org>
AuthorDate: Thu Jun 16 11:32:32 2022 +0100

    better warnings for corrupted encryption headers
---
 src/couch/src/couch_file.erl | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)

diff --git a/src/couch/src/couch_file.erl b/src/couch/src/couch_file.erl
index ab707c5cc..fa511a5e5 100644
--- a/src/couch/src/couch_file.erl
+++ b/src/couch/src/couch_file.erl
@@ -1018,7 +1018,7 @@ init_crypto(#file{eof = 0, dek = undefined} = File0, Options) ->
 init_crypto(#file{eof = Eof, dek = undefined} = File, _Options) when Eof >= ?SIZE_BLOCK ->
     case read_encryption_header(File) of
         {ok, {KeyID, WEK, IV}} ->
-            case couch_encryption_manager:unwrap_dek(KeyID, WEK) of                
+            case couch_encryption_manager:unwrap_dek(KeyID, WEK) of
                 {ok, DEK} ->
                     {ok, init_crypto_file(File, DEK, IV)};
                 {error, Reason} ->
@@ -1061,23 +1061,31 @@ read_encryption_header(#file{} = File) ->
                     {ok, Header};
                 {{ok, Header1}, {ok, Header2}} ->
                     couch_log:warning(
-                        "~s: Header version differences.~nPrimary Header: ~p~nSecondary Header: ~p",
+                        "~s: Encryption header version differences.~nPrimary header: ~p~nSecondary header: ~p. Using primary header",
                         [Filepath, Header1, Header2]
                     ),
                     {ok, Header1};
-                {{ok, Header}, {error, corrupted_header}} ->
-                    couch_log:warning("~s: Secondary header corruption. Using primary header.", [
-                        Filepath
-                    ]),
+                {{ok, Header}, {error, Reason}} ->
+                    couch_log:warning(
+                        "~s: Secondary encryption header corruption (error: ~p). Using primary header.",
+                        [
+                            Filepath, Reason
+                        ]
+                    ),
                     {ok, Header};
-                {{error, corrupted_header}, {ok, Header}} ->
-                    couch_log:warning("~s: Primary header corruption. Using secondary header.", [
-                        Filepath
-                    ]),
+                {{error, Reason}, {ok, Header}} ->
+                    couch_log:warning(
+                        "~s: Primary encryption header corruption (error: ~p). Using secondary header.",
+                        [
+                            Filepath, Reason
+                        ]
+                    ),
                     {ok, Header};
-                {{error, corrupted_header}, {error, corrupted_header}} ->
-                    couch_log:error("~s: Both headers corrupted.", [Filepath]),
-                    {error, corrupted_header}
+                {{error, Reason1}, {error, Reason2}} ->
+                    couch_log:error("~s: Both encryption headers corrupted (errors: ~p, ~p).", [
+                        Filepath, Reason1, Reason2
+                    ]),
+                    {error, corrupted_encryption_header}
             end;
         {ok, _} ->
             not_encrypted;
@@ -1093,10 +1101,10 @@ extract_header(
         true ->
             {ok, {KeyID, WrappedKey, IV}};
         false ->
-            {error, corrupted_header}
+            {error, corrupted_encryption_header}
     end;
 extract_header(_) ->
-    {error, corrupted_header}.
+    {error, corrupted_encryption_header}.
 
 check_header(Bin) ->
     Data = binary:part(Bin, 0, byte_size(Bin) - 32),