Skip to content

Commit

Permalink
tuned TCryptCertPerUsage binary serialization
Browse files Browse the repository at this point in the history
- binary layout is now TBufferWriter.WriteVar() of all DER serialization
  • Loading branch information
Arnaud Bouchez committed Aug 25, 2022
1 parent 662afe6 commit 2e0f595
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
32 changes: 13 additions & 19 deletions src/crypt/mormot.crypt.secure.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,7 @@ TCryptCertPerUsage = record
// - returns the duplicated usages found during adding certificates
function FromPem(algo: TCryptCertAlgo; const pem: RawUtf8): TCryptCertUsages;
/// save all items as a single binary blob of cccCertOnly certificates
// - binary layout is just 32-bit length followed by the DER serialization
// - binary layout is TBufferWriter.WriteVar() of all DER serialization
function AsBinary: RawByteString;
/// clear and load a binary blob of certificates saved by AsBinary
// - returns the duplicated usages found during adding certificates
Expand Down Expand Up @@ -4909,13 +4909,14 @@ function TCryptCertPerUsage.FromPem(
function TCryptCertPerUsage.AsBinary: RawByteString;
var
i: PtrInt;
s: TRawByteStringStream;
tmp: TTextWriterStackBuffer;
s: TBufferWriter;
begin
s := TRawByteStringStream.Create;
s := TBufferWriter.Create(tmp);
try
for i := 0 to length(List) - 1 do
WriteStringToStream(s, List[i].Save(cccCertOnly, '', ccfBinary));
result := s.DataString;
s.Write(List[i].Save(cccCertOnly, '', ccfBinary));
result := s.FlushTo;
finally
s.Free;
end;
Expand All @@ -4924,27 +4925,20 @@ function TCryptCertPerUsage.AsBinary: RawByteString;
function TCryptCertPerUsage.FromBinary(algo: TCryptCertAlgo;
const bin: RawByteString): TCryptCertUsages;
var
s: TRawByteStringStream;
one: RawByteString;
s: TFastReader;
c: ICryptCert;
begin
Clear;
result := [];
if (algo = nil) or
(bin = '') then
exit;
s := TRawByteStringStream.Create(bin);
try
repeat
one := ReadStringFromStream(s, 65536);
if one = '' then
break;
c := algo.Load(one);
if c <> nil then
result := result + Add(c);
until false;
finally
s.Free;
s.Init(bin);
while not s.EOF do
begin
c := algo.Load(s.VarString);
if c <> nil then
result := result + Add(c);
end;
end;

Expand Down
2 changes: 1 addition & 1 deletion src/mormot.commit.inc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
'2.0.3989'
'2.0.3990'
19 changes: 19 additions & 0 deletions test/test.core.crypt.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2607,8 +2607,27 @@ procedure TTestCoreCrypto.Catalog;
check(cpe.GetUsage(cuDataEncipherment, c4));
check(c4 = c3);
end;
s := cpe.AsBinary;
check(s <> '');
cpe.Clear;
check(cpe.Usages = []);
check(cpe.AsBinary = '');
if crt.AlgoName = 'syn-es256-v1' then
begin
check(cpe.FromBinary(crt, s) = CU_ALL);
check(cpe.Usages = CU_ALL);
end
else
begin
check(cpe.FromBinary(crt, s) = [cuDigitalSignature, cuKeyAgreement]);
check(cpe.Usages = [cuCA, cuDigitalSignature, cuKeyCertSign,
cuKeyAgreement, cuDataEncipherment]);
end;
for u := low(u) to high(u) do
begin
check(cpe.GetUsage(u, c4) = (u in cpe.Usages));
check((c4 <> nil) = (u in cpe.Usages));
end;
end;
// validate Store High-Level Algorithms Factory
r := RandomAnsi7(100);
Expand Down

0 comments on commit 2e0f595

Please sign in to comment.