|
@@ -35,7 +35,7 @@ const
|
|
// ASN_organizationName = '2.5.4.10';
|
|
// ASN_organizationName = '2.5.4.10';
|
|
// ASN_organizationalUnitName = '2.5.4.11';
|
|
// ASN_organizationalUnitName = '2.5.4.11';
|
|
// ASN_countryName = '2.5.4.6';
|
|
// ASN_countryName = '2.5.4.6';
|
|
- // ASN_stateOrProvinceName = '2.5.4.8';
|
|
|
|
|
|
+ // ASN_stateOrProvince Name = '2.5.4.8';
|
|
// ASN_localityName = '2.5.4.7';
|
|
// ASN_localityName = '2.5.4.7';
|
|
ASN_ecPublicKey = '1.2.840.10045.2.1';
|
|
ASN_ecPublicKey = '1.2.840.10045.2.1';
|
|
// ASN_prime256v1 = '1.2.840.10045.3.1.7';
|
|
// ASN_prime256v1 = '1.2.840.10045.3.1.7';
|
|
@@ -50,8 +50,8 @@ const
|
|
//------------------------------------------------------------------------------
|
|
//------------------------------------------------------------------------------
|
|
// ASN
|
|
// ASN
|
|
//------------------------------------------------------------------------------
|
|
//------------------------------------------------------------------------------
|
|
-procedure ASNEncodeOID(const Value: Int64; var Result: String);
|
|
|
|
-function ASNDecodeOID(var Start: Integer; const S: String): Int64; overload;
|
|
|
|
|
|
+procedure ASNEncodeOID(const Value: Int64; var Result: AnsiString);
|
|
|
|
+function ASNDecodeOID(var Start: Integer; const S: AnsiString): Int64; overload;
|
|
function ASNDecodeOID(var Buffer: PByte; BufferEnd: PByte): Int64; overload;
|
|
function ASNDecodeOID(var Buffer: PByte; BufferEnd: PByte): Int64; overload;
|
|
function ASNGetEncodedLen(const Len: Integer): Integer;
|
|
function ASNGetEncodedLen(const Len: Integer): Integer;
|
|
procedure ASNEncodeLen(const Len: Integer; var Buffer: TBytes);
|
|
procedure ASNEncodeLen(const Len: Integer; var Buffer: TBytes);
|
|
@@ -69,11 +69,11 @@ function ASNWriteBitStrBegin(s: TMemoryStream): int64;
|
|
procedure ASNWriteBitStrEnd(BitStrBegin: int64; s: TMemoryStream);
|
|
procedure ASNWriteBitStrEnd(BitStrBegin: int64; s: TMemoryStream);
|
|
|
|
|
|
// Encodes ASN.1 object to binary form
|
|
// Encodes ASN.1 object to binary form
|
|
-procedure ASNObject(const Data: String; const ASNType: Integer; var Buffer: TBytes);
|
|
|
|
|
|
+procedure ASNObject(const Data: AnsiString; const ASNType: Integer; var Buffer: TBytes);
|
|
// Encodes an MIB OID String to binary form
|
|
// Encodes an MIB OID String to binary form
|
|
-procedure MibToId(Mib: String; var Result: String);
|
|
|
|
|
|
+procedure MibToId(Mib: AnsiString; var Result: AnsiString);
|
|
// Decodes MIB OID from binary form to String form.
|
|
// Decodes MIB OID from binary form to String form.
|
|
-procedure IdToMib(const Id: String; var Result: String); overload;
|
|
|
|
|
|
+procedure IdToMib(const Id: AnsiString; var Result: AnsiString); overload;
|
|
function IdToMib(Buffer, BufferEnd: PByte): string; overload;
|
|
function IdToMib(Buffer, BufferEnd: PByte): string; overload;
|
|
|
|
|
|
procedure ASNDebug(const Buffer: TBytes; var Output: TBytes);
|
|
procedure ASNDebug(const Buffer: TBytes; var Output: TBytes);
|
|
@@ -83,6 +83,7 @@ procedure ASNParse_GetItem(List: TStrings; Index: integer; out ASNType, ASNSize:
|
|
function ASNParse_GetIntBytes(List: TStrings; ListIndex: integer; ID: int64): TBytes;
|
|
function ASNParse_GetIntBytes(List: TStrings; ListIndex: integer; ID: int64): TBytes;
|
|
function ASNFetch(var Buffer: PByte; BufferEnd: PByte; Out ASNType, ASNSize: Int32): Boolean; overload;
|
|
function ASNFetch(var Buffer: PByte; BufferEnd: PByte; Out ASNType, ASNSize: Int32): Boolean; overload;
|
|
function ASNFetchOID(var Buffer: PByte; BufferEnd: PByte; out OID: String): Boolean; overload;
|
|
function ASNFetchOID(var Buffer: PByte; BufferEnd: PByte; out OID: String): Boolean; overload;
|
|
|
|
+function ASNFetchOID(var Buffer: PByte; BufferEnd: PByte; out OID: AnsiString): Boolean; overload;
|
|
|
|
|
|
implementation
|
|
implementation
|
|
|
|
|
|
@@ -90,13 +91,13 @@ implementation
|
|
// ASN
|
|
// ASN
|
|
//------------------------------------------------------------------------------
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
-procedure ASNEncodeOID(const Value: Int64; var Result: String);
|
|
|
|
|
|
+procedure ASNEncodeOID(const Value: Int64; var Result: AnsiString);
|
|
var
|
|
var
|
|
B: Boolean;
|
|
B: Boolean;
|
|
I: Integer;
|
|
I: Integer;
|
|
x: Int64;
|
|
x: Int64;
|
|
Modulo: Byte;
|
|
Modulo: Byte;
|
|
- S: String;
|
|
|
|
|
|
+ S: AnsiString;
|
|
|
|
|
|
begin
|
|
begin
|
|
S:='';
|
|
S:='';
|
|
@@ -109,14 +110,14 @@ begin
|
|
Modulo := Modulo or $80;
|
|
Modulo := Modulo or $80;
|
|
if x > 0 then
|
|
if x > 0 then
|
|
B := True;
|
|
B := True;
|
|
- S:=S+Char(Modulo);
|
|
|
|
|
|
+ S:=S+AnsiChar(Modulo);
|
|
until x = 0;
|
|
until x = 0;
|
|
for I:=Length(S) downto 1 do
|
|
for I:=Length(S) downto 1 do
|
|
Result:=Result+S[I];
|
|
Result:=Result+S[I];
|
|
end;
|
|
end;
|
|
|
|
|
|
// @Start=0
|
|
// @Start=0
|
|
-function ASNDecodeOID(var Start: Integer; const S: String): Int64;
|
|
|
|
|
|
+function ASNDecodeOID(var Start: Integer; const S: AnsiString): Int64;
|
|
var
|
|
var
|
|
x: Integer;
|
|
x: Integer;
|
|
begin
|
|
begin
|
|
@@ -160,11 +161,11 @@ begin
|
|
repeat
|
|
repeat
|
|
y := x mod 256;
|
|
y := x mod 256;
|
|
x := x div 256;
|
|
x := x div 256;
|
|
- S:=S+Char(y);
|
|
|
|
|
|
+ S:=S+AnsiChar(y);
|
|
until x = 0;
|
|
until x = 0;
|
|
y := Length(S);
|
|
y := Length(S);
|
|
y := y or $80;
|
|
y := y or $80;
|
|
- S:=S+Char(y);
|
|
|
|
|
|
+ S:=S+AnsiChar(y);
|
|
for x := Length(S) downto 1 do
|
|
for x := Length(S) downto 1 do
|
|
Buffer:=Concat(Buffer,[Ord(S[x])]);
|
|
Buffer:=Concat(Buffer,[Ord(S[x])]);
|
|
end;
|
|
end;
|
|
@@ -212,7 +213,7 @@ var
|
|
x: Int64;
|
|
x: Int64;
|
|
y: byte;
|
|
y: byte;
|
|
neg: Boolean;
|
|
neg: Boolean;
|
|
- S : String;
|
|
|
|
|
|
+ S : AnsiString;
|
|
begin
|
|
begin
|
|
S:='';
|
|
S:='';
|
|
neg := Value < 0;
|
|
neg := Value < 0;
|
|
@@ -309,7 +310,7 @@ end;
|
|
|
|
|
|
procedure ASNWriteObjID(const ObjID: string; s: TStream);
|
|
procedure ASNWriteObjID(const ObjID: string; s: TStream);
|
|
var
|
|
var
|
|
- Mib: string;
|
|
|
|
|
|
+ Mib: Ansistring;
|
|
aLen: TBytes;
|
|
aLen: TBytes;
|
|
begin
|
|
begin
|
|
Mib:='';
|
|
Mib:='';
|
|
@@ -363,7 +364,7 @@ begin
|
|
ASNWriteSequenceEnd(BitStrBegin,s);
|
|
ASNWriteSequenceEnd(BitStrBegin,s);
|
|
end;
|
|
end;
|
|
|
|
|
|
-Procedure AppendStringToBuffer(var Buffer: TBytes; const aString : String);
|
|
|
|
|
|
+Procedure AppendStringToBuffer(var Buffer: TBytes; const aString : AnsiString);
|
|
Var
|
|
Var
|
|
Buflen,sLen : integer;
|
|
Buflen,sLen : integer;
|
|
begin
|
|
begin
|
|
@@ -374,7 +375,7 @@ begin
|
|
Move(aString[1],Buffer[Buflen],sLen);
|
|
Move(aString[1],Buffer[Buflen],sLen);
|
|
end;
|
|
end;
|
|
|
|
|
|
-procedure ASNObject(const Data: String; const ASNType: Integer; var Buffer: TBytes);
|
|
|
|
|
|
+procedure ASNObject(const Data: AnsiString; const ASNType: Integer; var Buffer: TBytes);
|
|
begin
|
|
begin
|
|
Buffer:=Concat(Buffer,[ASNType]);
|
|
Buffer:=Concat(Buffer,[ASNType]);
|
|
ASNEncodeLen(Length(Data), Buffer);
|
|
ASNEncodeLen(Length(Data), Buffer);
|
|
@@ -401,14 +402,14 @@ begin
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
-procedure OutputHexa(var Output: TBytes; const S: String);
|
|
|
|
|
|
+procedure OutputHexa(var Output: TBytes; const S: AnsiString);
|
|
|
|
|
|
var
|
|
var
|
|
I: Integer;
|
|
I: Integer;
|
|
P: PByte;
|
|
P: PByte;
|
|
|
|
|
|
begin
|
|
begin
|
|
- P := PByte(PChar(S));
|
|
|
|
|
|
+ P := PByte(PAnsiChar(S));
|
|
for I := 1 to Length(S) do
|
|
for I := 1 to Length(S) do
|
|
begin
|
|
begin
|
|
AppendStringToBuffer(Output, HexStr(P^,2));
|
|
AppendStringToBuffer(Output, HexStr(P^,2));
|
|
@@ -416,9 +417,9 @@ begin
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
-procedure MibToId(Mib: String; var Result: String);
|
|
|
|
|
|
+procedure MibToId(Mib: AnsiString; var Result: AnsiString);
|
|
|
|
|
|
- function WalkInt(var S: String): Integer;
|
|
|
|
|
|
+ function WalkInt(var S: AnsiString): Integer;
|
|
var
|
|
var
|
|
P : Integer;
|
|
P : Integer;
|
|
begin
|
|
begin
|
|
@@ -442,7 +443,7 @@ begin
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
-procedure IdToMib(const Id: String; var Result: String);
|
|
|
|
|
|
+procedure IdToMib(const Id: AnsiString; var Result: AnsiString);
|
|
var
|
|
var
|
|
x, y, Index: Integer;
|
|
x, y, Index: Integer;
|
|
begin
|
|
begin
|
|
@@ -506,21 +507,24 @@ end;
|
|
// Decode the ASN.1 item of the next element in @Buffer. Type of item is stored in @ASNType
|
|
// Decode the ASN.1 item of the next element in @Buffer. Type of item is stored in @ASNType
|
|
procedure ASNDebugItem(var Buffer: PByte; BufferEnd: PByte; Out ASNType, ASNSize: Integer; var Output: TBytes);
|
|
procedure ASNDebugItem(var Buffer: PByte; BufferEnd: PByte; Out ASNType, ASNSize: Integer; var Output: TBytes);
|
|
|
|
|
|
- procedure BufToString(out S : String);
|
|
|
|
|
|
+ procedure BufToString(out S : AnsiString);
|
|
|
|
+
|
|
|
|
+ var
|
|
|
|
+ SA : AnsiString;
|
|
|
|
|
|
begin
|
|
begin
|
|
- S:='';
|
|
|
|
- SetLength(S,ASNSize);
|
|
|
|
|
|
+ SetLength(SA,ASNSize);
|
|
if ASNSize>0 then
|
|
if ASNSize>0 then
|
|
begin
|
|
begin
|
|
- Move(Buffer^,S[1],ASNSize);
|
|
|
|
|
|
+ Move(Buffer^,SA[1],ASNSize);
|
|
inc(Buffer,ASNSize);
|
|
inc(Buffer,ASNSize);
|
|
end;
|
|
end;
|
|
|
|
+ S:=SA;
|
|
end;
|
|
end;
|
|
|
|
|
|
var
|
|
var
|
|
n: Integer;
|
|
n: Integer;
|
|
- S, S2: String;
|
|
|
|
|
|
+ S, S2: AnsiString;
|
|
y: Int64;
|
|
y: Int64;
|
|
OldBuffer: PByte;
|
|
OldBuffer: PByte;
|
|
begin
|
|
begin
|
|
@@ -689,7 +693,7 @@ end;
|
|
procedure ASNDebug(const Buffer: TBytes; var Output: TBytes);
|
|
procedure ASNDebug(const Buffer: TBytes; var Output: TBytes);
|
|
|
|
|
|
const
|
|
const
|
|
- SSpaces: String = ' ';
|
|
|
|
|
|
+ SSpaces: AnsiString = ' ';
|
|
|
|
|
|
var
|
|
var
|
|
ASNSize, ASNType, n: Integer;
|
|
ASNSize, ASNType, n: Integer;
|
|
@@ -706,7 +710,7 @@ begin
|
|
EndP:=StartP+length(Buffer);
|
|
EndP:=StartP+length(Buffer);
|
|
while p<EndP do
|
|
while p<EndP do
|
|
begin
|
|
begin
|
|
- //writeln('ASNDebug p=',p-StartP,' Type=',hexstr(p^,2),' Indent=',length(IndentList));
|
|
|
|
|
|
+ writeln('ASNDebug p=',p-StartP,' Type=',hexstr(p^,2),' Indent=',length(IndentList));
|
|
// check if any sequence/set has ended and unindent
|
|
// check if any sequence/set has ended and unindent
|
|
for n := Length(IndentList)-1 downto 0 do
|
|
for n := Length(IndentList)-1 downto 0 do
|
|
begin
|
|
begin
|
|
@@ -740,18 +744,19 @@ end;
|
|
|
|
|
|
procedure ASNParseAddInt(var Buffer: PByte; BufferEnd: PByte; List: TStrings; const ASNType, ASNSize: Integer; Signed: boolean);
|
|
procedure ASNParseAddInt(var Buffer: PByte; BufferEnd: PByte; List: TStrings; const ASNType, ASNSize: Integer; Signed: boolean);
|
|
|
|
|
|
- procedure BufToString(var S : String);
|
|
|
|
|
|
+ procedure BufToString(var S : AnsiString);
|
|
|
|
|
|
begin
|
|
begin
|
|
SetLength(S,ASNSize);
|
|
SetLength(S,ASNSize);
|
|
- if ASNSize=0 then exit;
|
|
|
|
|
|
+ if ASNSize=0 then
|
|
|
|
+ exit;
|
|
Move(Buffer^,S[1],ASNSize);
|
|
Move(Buffer^,S[1],ASNSize);
|
|
inc(Buffer, ASNSize);
|
|
inc(Buffer, ASNSize);
|
|
end;
|
|
end;
|
|
|
|
|
|
var
|
|
var
|
|
- S, S2: String;
|
|
|
|
- y: Int64;
|
|
|
|
|
|
+ S, S2: AnsiString;
|
|
|
|
+ y: Int64;
|
|
begin
|
|
begin
|
|
S:='';
|
|
S:='';
|
|
S2:='';
|
|
S2:='';
|
|
@@ -765,7 +770,7 @@ begin
|
|
end else
|
|
end else
|
|
begin
|
|
begin
|
|
BufToString(S2);
|
|
BufToString(S2);
|
|
- if S2[1] = Char(#00) then
|
|
|
|
|
|
+ if S2[1] = AnsiChar(#00) then
|
|
Delete(S2,1,1);
|
|
Delete(S2,1,1);
|
|
BytesToHexStr(S,GetRawStringBytes(S2));
|
|
BytesToHexStr(S,GetRawStringBytes(S2));
|
|
end;
|
|
end;
|
|
@@ -799,6 +804,16 @@ begin
|
|
Result:=true;
|
|
Result:=true;
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+function ASNFetchOID(var Buffer: PByte; BufferEnd: PByte; out OID: AnsiString): Boolean; overload;
|
|
|
|
+
|
|
|
|
+Var
|
|
|
|
+ OIDS : String;
|
|
|
|
+
|
|
|
|
+begin
|
|
|
|
+ Result:=ASNFetchOID(Buffer,BufferEnd,OIDS);
|
|
|
|
+ OID:=OIDS;
|
|
|
|
+end;
|
|
|
|
+
|
|
function ASNFetchOID(var Buffer: PByte; BufferEnd: PByte; out OID: String): Boolean;
|
|
function ASNFetchOID(var Buffer: PByte; BufferEnd: PByte; out OID: String): Boolean;
|
|
var
|
|
var
|
|
ASNType, ASNSize: Int32;
|
|
ASNType, ASNSize: Int32;
|
|
@@ -828,18 +843,18 @@ end;
|
|
|
|
|
|
function ASNParseItem(var Buffer: PByte; BufferEnd: PByte; List: TStrings): boolean;
|
|
function ASNParseItem(var Buffer: PByte; BufferEnd: PByte; List: TStrings): boolean;
|
|
|
|
|
|
- function BufToString(Len : Integer): String;
|
|
|
|
|
|
+ function BufToString(Len : Integer): AnsiString;
|
|
begin
|
|
begin
|
|
SetLength(Result{%H-},Len);
|
|
SetLength(Result{%H-},Len);
|
|
if Len=0 then exit;
|
|
if Len=0 then exit;
|
|
- Move(Buffer^,Result[1],Len);
|
|
|
|
|
|
+ Move(Buffer^,Result[1],Len);
|
|
inc(Buffer, Len);
|
|
inc(Buffer, Len);
|
|
end;
|
|
end;
|
|
|
|
|
|
var
|
|
var
|
|
ASNType, ASNSize: Integer;
|
|
ASNType, ASNSize: Integer;
|
|
n: Integer;
|
|
n: Integer;
|
|
- S, S2: String;
|
|
|
|
|
|
+ S,S2: AnsiString;
|
|
y: Int64;
|
|
y: Int64;
|
|
OldBuffer: PByte;
|
|
OldBuffer: PByte;
|
|
|
|
|
|
@@ -937,7 +952,10 @@ end;
|
|
procedure ASNParse(const Buffer: TBytes; List: TStrings);
|
|
procedure ASNParse(const Buffer: TBytes; List: TStrings);
|
|
var
|
|
var
|
|
P, EndP: PByte;
|
|
P, EndP: PByte;
|
|
|
|
+ O : Tbytes;
|
|
begin
|
|
begin
|
|
|
|
+ ASNDebug(Buffer,O);
|
|
|
|
+ Writeln(TEncoding.UTF8.GetAnsiString(O));
|
|
if length(Buffer)=0 then exit;
|
|
if length(Buffer)=0 then exit;
|
|
P:=@Buffer[0];
|
|
P:=@Buffer[0];
|
|
EndP:=P+length(Buffer);
|
|
EndP:=P+length(Buffer);
|