Skip to content

Commit

Permalink
synch with trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Bouchez committed Feb 22, 2017
1 parent dcb6b9c commit 1623068
Show file tree
Hide file tree
Showing 7 changed files with 554 additions and 115 deletions.
513 changes: 424 additions & 89 deletions SynCommons.pas

Large diffs are not rendered by default.

65 changes: 59 additions & 6 deletions SynCrypto.pas
Expand Up @@ -928,6 +928,9 @@ TAESPRNG = class(TAESLocked)
/// returns a binary buffer filled with some pseudorandom data
// - this method is thread-safe
function FillRandomBytes(Len: integer): TBytes;
/// returns an hexa-encoded binary buffer filled with some pseudorandom data
// - this method is thread-safe
function FillRandomHex(Len: integer): RawUTF8;
/// computes a random ASCII password
// - will contain uppercase/lower letters, digits and $.:()?%!-+*/@#
// excluding ;,= to allow direct use in CSV content
Expand Down Expand Up @@ -1386,7 +1389,10 @@ procedure HMAC_CRC256C(const key,msg: RawByteString; out result: THash256); over
procedure Init(key: pointer; keylen: integer);
/// call this method for each continuous message block
// - iterate over all message blocks, then call Done to retrieve the HMAC
procedure Update(msg: pointer; msglen: integer);
procedure Update(msg: pointer; msglen: integer); overload;
/// call this method for each continuous message block
// - iterate over all message blocks, then call Done to retrieve the HMAC
procedure Update(const msg: RawByteString); overload;
/// computes the HMAC of all supplied message according to the key
function Done: cardinal;
/// computes the HMAC of the supplied message according to the key
Expand Down Expand Up @@ -1841,6 +1847,8 @@ TJWTAbstract = class(TSynPersistent)
/// compute a HTTP Authorization header containing a JWT for a given payload
// - just a wrapper around Compute(), returned the HTTP header value:
// $ Authorization: <HttpAuthorizationHeader>
// following the expected pattern:
// $ Authorization: Bearer <Token>
// - this method is thread-safe
function ComputeAuthorizationHeader(const DataNameValue: array of const;
const Issuer: RawUTF8=''; const Subject: RawUTF8=''; const Audience: RawUTF8='';
Expand All @@ -1852,7 +1860,14 @@ TJWTAbstract = class(TSynPersistent)
// - supplied JWT is transmitted e.g. in HTTP header:
// $ Authorization: Bearer <Token>
// - this method is thread-safe
procedure Verify(const Token: RawUTF8; out JWT: TJWTContent);
procedure Verify(const Token: RawUTF8; out JWT: TJWTContent); overload;
/// check a JWT value, and its signature
// - will validate all expected Claims, and the associated signature
// - verification state is returned as function result
// - supplied JWT is transmitted e.g. in HTTP header:
// $ Authorization: Bearer <Token>
// - this method is thread-safe
function Verify(const Token: RawUTF8): TJWTResult; overload;
/// check a HTTP Authorization header value as JWT, and its signature
// - will validate all expected Claims, and the associated signature
// - verification state is returned in JWT.result (jwtValid for a valid JWT),
Expand Down Expand Up @@ -1939,6 +1954,13 @@ TJWTHS256 = class(TJWTAbstract)
aIDIdentifier: TSynUniqueIdentifierProcess=0; aIDObfuscationKey: RawUTF8=''); reintroduce;
/// finalize the instance
destructor Destroy; override;
/// low-level helper to re-compute the internal HMAC shared secret
// - by definition, expects aSecretPBKDF2Rounds>0 (otherwise aSecret is
// expected to be passed directly to the HMAC function)
// - may be used to provide any non Delphi client with the expected secret
// - caller should call FillZero(aHMACSecret) as soon as it consummed it
procedure ComputeHMACSecret(const aSecret: RawUTF8; aSecretPBKDF2Rounds: integer;
out aHMACSecret: THash256);
end;

const
Expand Down Expand Up @@ -2689,6 +2711,11 @@ procedure THMAC_CRC32C.Update(msg: pointer; msglen: integer);
seed := crc32c(seed,msg,msglen);
end;

procedure THMAC_CRC32C.Update(const msg: RawByteString);
begin
seed := crc32c(seed,pointer(msg),length(msg));
end;

function THMAC_CRC32C.Done: cardinal;
begin
result := crc32c(seed,@step7data,sizeof(step7data));
Expand Down Expand Up @@ -9093,6 +9120,17 @@ function TAESPRNG.FillRandomBytes(Len: integer): TBytes;
FillRandom(pointer(result),Len);
end;

function TAESPRNG.FillRandomHex(Len: integer): RawUTF8;
var bin: pointer;
begin
SetString(result,nil,Len*2);
if Len=0 then
exit;
bin := @PByteArray(result)[Len]; // temporary store random bytes at the end
FillRandom(bin,Len);
SynCommons.BinToHex(bin,pointer(result),Len);
end;

function TAESPRNG.RandomPassword(Len: integer): RawUTF8;
const CHARS: array[0..137] of AnsiChar =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'+
Expand Down Expand Up @@ -9648,6 +9686,13 @@ procedure TJWTAbstract.Verify(const Token: RawUTF8; out JWT: TJWTContent);
fCache.Add(Token,JWT);
end;

function TJWTAbstract.Verify(const Token: RawUTF8): TJWTResult;
var jwt: TJWTContent;
begin
Verify(Token,jwt);
result := jwt.result;
end;

function TJWTAbstract.CheckAgainstActualTimestamp(var JWT: TJWTContent): boolean;
var nowunix, unix: cardinal;
begin
Expand Down Expand Up @@ -9851,13 +9896,21 @@ constructor TJWTHS256.Create(const aSecret: RawUTF8; aSecretPBKDF2Rounds: intege
var secret: THash256;
begin
inherited Create('HS256',aClaims,aAudience,aExpirationMinutes,aIDIdentifier,aIDObfuscationKey);
if aSecretPBKDF2Rounds>0 then begin
PBKDF2_HMAC_SHA256(aSecret,fHeaderB64,aSecretPBKDF2Rounds,secret);
if (aSecret<>'') and (aSecretPBKDF2Rounds>0) then begin
ComputeHMACSecret(aSecret,aSecretPBKDF2Rounds,secret);
fHmacPrepared.Init(@secret,sizeof(secret));
FillZero(secret);
end else
fHmacPrepared.Init(pointer(aSecret),length(aSecret));
fHmacPrepared.Update(pointer(fHeaderB64),length(fHeaderB64));
fHmacPrepared.Update(pointer(fHeaderB64),length(fHeaderB64));
end;

procedure TJWTHS256.ComputeHMACSecret(const aSecret: RawUTF8; aSecretPBKDF2Rounds: integer;
out aHMACSecret: THash256);
begin
if (self<>nil) and (aSecret<>'') and (aSecretPBKDF2Rounds>0) then
PBKDF2_HMAC_SHA256(aSecret,fHeaderB64,aSecretPBKDF2Rounds,aHMACSecret) else
FillZero(aHMACSecret);
end;

function TJWTHS256.ComputeSignature(const payload64: RawUTF8): RawUTF8;
Expand All @@ -9881,7 +9934,7 @@ procedure TJWTHS256.CheckSignature(var JWT: TJWTContent; const payload64: RawUTF
hmac := fHmacPrepared; // thread-safe re-use of prepared HMAC(header+'.')
hmac.Update(pointer(payload64),length(payload64));
hmac.Done(res);
if CompareMem(@res,pointer(signature),sizeof(res)) then
if IsEqual(res,PSHA256Digest(signature)^) then
JWT.result := jwtValid;
end;

Expand Down
47 changes: 39 additions & 8 deletions SynFPCTypInfo.pas
Expand Up @@ -52,12 +52,7 @@

interface

{$MODE objfpc}
{$MODESWITCH AdvancedRecords}
{$inline on}
{$h+}
{$r-}
{$q-}
{$I Synopse.inc} // define HASINLINE USETYPEINFO CPU32 CPU64 OWNNORMTOUPPER

uses
SysUtils,
Expand All @@ -79,8 +74,12 @@ function GetFPCAlignPtr(P: pointer): pointer; inline;

function GetFPCEnumName(TypeInfo: PTypeInfo; Value: Integer): PShortString; inline;
function GetFPCEnumValue(TypeInfo: PTypeInfo; const Name: string): Integer; inline;
Function AlignTypeData(p : Pointer) : Pointer;
function GetFPCTypeData(TypeInfo: PTypeInfo): PTypeData; inline;
function GetFPCPropInfo(AClass: TClass; const PropName: string): PPropInfo; inline;
{$ifdef FPC_NEWRTTI}
function GetFPCRecInitData(TypeData: Pointer): Pointer; inline;
{$endif}


implementation
Expand Down Expand Up @@ -144,16 +143,38 @@ function GetFPCEnumName(TypeInfo: PTypeInfo; Value: Integer): PShortString;
end;
end;

Function AlignTypeData(p : Pointer) : Pointer;
{$push}
{$packrecords c}
type
TAlignCheck = record
b : byte;
q : qword;
end;
{$pop}
begin
{$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
{$ifdef VER3_0}
Result:=Pointer(align(p,SizeOf(Pointer)));
{$else VER3_0}
Result:=Pointer(align(p,PtrInt(@TAlignCheck(nil^).q)))
{$endif VER3_0}
{$else FPC_REQUIRES_PROPER_ALIGNMENT}
Result:=p;
{$endif FPC_REQUIRES_PROPER_ALIGNMENT}
end;

function GetFPCTypeData(TypeInfo: PTypeInfo): PTypeData;
begin
result := PTypeData(AlignToPtr(PTypeData(pointer(TypeInfo)+2+PByte(pointer(TypeInfo)+1)^)));
result := PTypeData(AlignTypeData(PTypeData(pointer(TypeInfo)+2+PByte(pointer(TypeInfo)+1)^)));
end;

{$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}

function GetFPCAlignPtr(P: pointer): pointer;
begin
result := AlignToPtr(P-SizeOf(Pointer)+2+PByte(P)[1]);
result := AlignTypeData(P+2+Length(PTypeInfo(P)^.Name));
Dec(PtrUInt(result),SizeOf(pointer));
end;

{$endif}
Expand Down Expand Up @@ -196,4 +217,14 @@ function GetFPCPropInfo(AClass: TClass; const PropName: string): PPropInfo;
result := typinfo.GetPropInfo(AClass,PropName);
end;

{$ifdef FPC_NEWRTTI}
function GetFPCRecInitData(TypeData: Pointer): Pointer;
begin
if PTypeData(TypeData)^.RecInitInfo = nil then
result := TypeData
else
result := AlignTypeData(pointer(PTypeData(TypeData)^.RecInitData));
end;
{$endif}

end.
7 changes: 7 additions & 0 deletions SynGdiPlus.pas
Expand Up @@ -414,6 +414,7 @@ TSynPicture = class(TGraphic)
CompressionQuality: integer=80; IfBitmapSetResolution: single=0): TGdipStatus;
public
constructor CreateFromFile(const FileName: string);
constructor CreateFromBuffer(Buffer: pointer; Len: integer);
destructor Destroy; override;
{$ifdef FPC}
procedure Clear; override;
Expand Down Expand Up @@ -1226,6 +1227,12 @@ constructor TSynPicture.CreateFromFile(const FileName: string);
LoadFromFile(FileName);
end;

constructor TSynPicture.CreateFromBuffer(Buffer: pointer; Len: integer);
begin
inherited Create;
LoadFromBuffer(Buffer,Len);
end;

destructor TSynPicture.Destroy;
begin
Clear;
Expand Down
6 changes: 3 additions & 3 deletions SynPdf.pas
Expand Up @@ -9222,10 +9222,10 @@ TPdfEnum = class
STOCKPENCOLOR: array[WHITE_PEN..BLACK_PEN] of cardinal = (
clWhite, clBlack);

function CenterPoint(const Rect: TRect): TPoint;
function CenterPoint(const Rect: TRect): TPoint; {$ifdef HASINLINE}inline;{$endif}
begin
result.X := (Rect.Right-Rect.Left) div 2+Rect.Left;
result.Y := (Rect.Bottom-Rect.Top) div 2+Rect.Top;
result.X := (Rect.Right+Rect.Left) div 2;
result.Y := (Rect.Bottom+Rect.Top) div 2;
end;

/// EMF enumeration callback function, called from GDI
Expand Down
29 changes: 21 additions & 8 deletions Synopse.inc
Expand Up @@ -157,6 +157,7 @@
{$define UNDIRECTDYNARRAY}
{$endif}


{$define INCLUDE_FTS3}
// define this if you want to include the FTS3/FTS4 feature into the library
// - FTS3 is an SQLite module implementing full-text search
Expand Down Expand Up @@ -212,6 +213,15 @@
{$define HASINTERFACEASTOBJECT}
{$define FPC_OR_UNICODE}
{$define FPC_ENUMHASINNER}
{$ifdef VER3_1_1} // if FPC_FULLVERSION>30100 ... ifend is not Delphi 5 compatible :(
{$define FPC_NEWRTTI} // RTTI has been enhanced in latest trunk
{$endif}
{$ifdef FPC_NEWRTTI}
{$define ISDELPHI2010_OR_FPC_NEWRTTI}
{$else}
{$define DELPHI_OR_FPC_OLDRTTI}
{$define FPC_OLDRTTI}
{$endif}

{.$define FPCSQLITE3STATIC}
// allow static linking of the SQlite3 engine (including crypto) to the project
Expand All @@ -234,7 +244,10 @@
{$ifdef CPUX64}
{$define FPCSQLITE3STATIC} // we supply Linux 64-bit x86_64 .o
{$endif}
{$endif}
{$ifdef CPUARM}
{$define FPCSQLITE3STATIC} // we supply Linux 32-bit ARM .o
{$endif}
{$endif}

{$ifdef ANDROID}
{$define LINUX}
Expand All @@ -244,7 +257,9 @@
// this includes Darwin and BSD family like FreeBSD
{$define LINUX} // not true, but a POSIX/BSD system
{$define PUREPASCAL} // e.g. low-level stack layout differs
{$ifndef DARWIN}
{$ifdef DARWIN}
{$define FPCSQLITE3STATIC} // we supply Darwin static libs
{$else}
{$define BSDNOTDARWIN}
{$endif}
{$endif}
Expand All @@ -267,7 +282,7 @@

// FPC has its own RTTI layout only since late 3.x
// when http://bugs.freepascal.org/view.php?id=26774 has been fixed
{$ifdef FPC_HAS_EXTENDEDINTERFACERTTI} // use dedicated branch conditional
{$ifdef FPC_NEWRTTI} // use dedicated branch conditional
{$ifdef CPUINTEL}
{$define HASINTERFACERTTI}
{$endif}
Expand All @@ -277,7 +292,7 @@
{$ifdef CPUAARCH64}
{$define HASINTERFACERTTI}
{$endif}
{$endif FPC_HAS_EXTENDEDINTERFACERTTI}
{$endif FPC_NEWRTTI}

{$define FPC_OR_PUREPASCAL}
{$define FPC_OR_KYLIX}
Expand Down Expand Up @@ -323,6 +338,7 @@

{$else FPC}

{$define DELPHI_OR_FPC_OLDRTTI}
{$ifndef PUREPASCAL}
{$define CPUINTEL} // Delphi only for Intel by now
{$endif}
Expand Down Expand Up @@ -395,6 +411,7 @@
{$if CompilerVersion >= 21.0}
// Delphi 2010/XE: Reduce EXE size by disabling much RTTI
{$define ISDELPHI2010}
{$define ISDELPHI2010_OR_FPC_NEWRTTI}
{$define FPC_OR_UNICODE}
{$define HASTTHREADSTART}
{$define HASINTERFACEASTOBJECT}
Expand Down Expand Up @@ -527,15 +544,11 @@
{$define NOSQLITE3STATIC}
{$endif}
{$endif}
{$ifndef CPUINTEL}
{$define NOSQLITE3STATIC}
{$endif}
{$ifdef ANDROID}
{$define NOSQLITE3STATIC}
{$endif}
{$ifdef BSD}
{$ifdef Darwin}
{$define NOSQLITE3STATIC}
{$else}
// not yet sure if needed, but it works !
{$define NOSQLITE3STATIC}
Expand Down
2 changes: 1 addition & 1 deletion SynopseCommit.inc
@@ -1 +1 @@
'1.18.3369'
'1.18.3422'

0 comments on commit 1623068

Please sign in to comment.