asn1util.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. {==============================================================================|
  2. | Project : Ararat Synapse | 002.001.001 |
  3. |==============================================================================|
  4. | Content: support for ASN.1 BER coding and decoding |
  5. |==============================================================================|
  6. | Copyright (c)1999-2021, Lukas Gebauer |
  7. | All rights reserved. |
  8. | |
  9. | Redistribution and use in source and binary forms, with or without |
  10. | modification, are permitted provided that the following conditions are met: |
  11. | |
  12. | Redistributions of source code must retain the above copyright notice, this |
  13. | list of conditions and the following disclaimer. |
  14. | |
  15. | Redistributions in binary form must reproduce the above copyright notice, |
  16. | this list of conditions and the following disclaimer in the documentation |
  17. | and/or other materials provided with the distribution. |
  18. | |
  19. | Neither the name of Lukas Gebauer nor the names of its contributors may |
  20. | be used to endorse or promote products derived from this software without |
  21. | specific prior written permission. |
  22. | |
  23. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
  24. | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
  25. | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
  26. | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
  27. | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
  28. | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
  29. | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
  30. | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  31. | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
  32. | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
  33. | DAMAGE. |
  34. |==============================================================================|
  35. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  36. | Portions created by Lukas Gebauer are Copyright (c) 1999-2021 |
  37. | Portions created by Hernan Sanchez are Copyright (c) 2000. |
  38. | All Rights Reserved. |
  39. |==============================================================================|
  40. | Contributor(s): |
  41. | Hernan Sanchez ([email protected]) |
  42. |==============================================================================|
  43. | History: see HISTORY.HTM from distribution package |
  44. | (Found at URL: http://www.ararat.cz/synapse/) |
  45. |==============================================================================}
  46. {: @abstract(Utilities for handling ASN.1 BER encoding)
  47. By this unit you can parse ASN.1 BER encoded data to elements or build back any
  48. elements to ASN.1 BER encoded buffer. You can dump ASN.1 BER encoded data to
  49. human readable form for easy debugging, too.
  50. Supported element types are: ASN1_BOOL, ASN1_INT, ASN1_OCTSTR, ASN1_NULL,
  51. ASN1_OBJID, ASN1_ENUM, ASN1_SEQ, ASN1_SETOF, ASN1_IPADDR, ASN1_COUNTER,
  52. ASN1_GAUGE, ASN1_TIMETICKS, ASN1_OPAQUE
  53. For sample of using, look to @link(TSnmpSend) or @link(TLdapSend)class.
  54. }
  55. {$Q-}
  56. {$H+}
  57. {$IFDEF FPC}
  58. {$MODE DELPHI}
  59. {$ENDIF}
  60. {$IFDEF UNICODE}
  61. {$WARN IMPLICIT_STRING_CAST OFF}
  62. {$WARN IMPLICIT_STRING_CAST_LOSS OFF}
  63. {$ENDIF}
  64. unit asn1util;
  65. interface
  66. uses
  67. SysUtils, Classes, synautil;
  68. const
  69. ASN1_BOOL = $01;
  70. ASN1_INT = $02;
  71. ASN1_OCTSTR = $04;
  72. ASN1_NULL = $05;
  73. ASN1_OBJID = $06;
  74. ASN1_ENUM = $0a;
  75. ASN1_SEQ = $30;
  76. ASN1_SETOF = $31;
  77. ASN1_IPADDR = $40;
  78. ASN1_COUNTER = $41;
  79. ASN1_GAUGE = $42;
  80. ASN1_TIMETICKS = $43;
  81. ASN1_OPAQUE = $44;
  82. ASN1_COUNTER64 = $46;
  83. {:Encodes OID item to binary form.}
  84. function ASNEncOIDItem(Value: Int64): AnsiString;
  85. {:Decodes an OID item of the next element in the "Buffer" from the "Start"
  86. position.}
  87. function ASNDecOIDItem(var Start: Integer; const Buffer: AnsiString): Int64;
  88. {:Encodes the length of ASN.1 element to binary.}
  89. function ASNEncLen(Len: Integer): AnsiString;
  90. {:Decodes length of next element in "Buffer" from the "Start" position.}
  91. function ASNDecLen(var Start: Integer; const Buffer: AnsiString): Integer;
  92. {:Encodes a signed integer to ASN.1 binary}
  93. function ASNEncInt(Value: Int64): AnsiString;
  94. {:Encodes unsigned integer into ASN.1 binary}
  95. function ASNEncUInt(Value: Integer): AnsiString;
  96. {:Encodes ASN.1 object to binary form.}
  97. function ASNObject(const Data: AnsiString; ASNType: Integer): AnsiString;
  98. {:Beginning with the "Start" position, decode the ASN.1 item of the next element
  99. in "Buffer". Type of item is stored in "ValueType."}
  100. function ASNItem(var Start: Integer; const Buffer: AnsiString;
  101. var ValueType: Integer): AnsiString;
  102. {:Encodes an MIB OID string to binary form.}
  103. function MibToId(Mib: String): AnsiString;
  104. {:Decodes MIB OID from binary form to string form.}
  105. function IdToMib(const Id: AnsiString): String;
  106. {:Encodes an one number from MIB OID to binary form. (used internally from
  107. @link(MibToId))}
  108. function IntMibToStr(const Value: AnsiString): AnsiString;
  109. {:Convert ASN.1 BER encoded buffer to human readable form for debugging.}
  110. function ASNdump(const Value: AnsiString): AnsiString;
  111. implementation
  112. {==============================================================================}
  113. function ASNEncOIDItem(Value: Int64): AnsiString;
  114. var
  115. x: Int64;
  116. xm: Byte;
  117. b: Boolean;
  118. begin
  119. x := Value;
  120. b := False;
  121. Result := '';
  122. repeat
  123. xm := x mod 128;
  124. x := x div 128;
  125. if b then
  126. xm := xm or $80;
  127. if x > 0 then
  128. b := True;
  129. Result := AnsiChar(xm) + Result;
  130. until x = 0;
  131. end;
  132. {==============================================================================}
  133. function ASNDecOIDItem(var Start: Integer; const Buffer: AnsiString): Int64;
  134. var
  135. x: Integer;
  136. b: Boolean;
  137. begin
  138. Result := 0;
  139. repeat
  140. Result := Result * 128;
  141. x := Ord(Buffer[Start]);
  142. Inc(Start);
  143. b := x > $7F;
  144. x := x and $7F;
  145. Result := Result + x;
  146. until not b;
  147. end;
  148. {==============================================================================}
  149. function ASNEncLen(Len: Integer): AnsiString;
  150. var
  151. x, y: Integer;
  152. begin
  153. if Len < $80 then
  154. Result := AnsiChar(Len)
  155. else
  156. begin
  157. x := Len;
  158. Result := '';
  159. repeat
  160. y := x mod 256;
  161. x := x div 256;
  162. Result := AnsiChar(y) + Result;
  163. until x = 0;
  164. y := Length(Result);
  165. y := y or $80;
  166. Result := AnsiChar(y) + Result;
  167. end;
  168. end;
  169. {==============================================================================}
  170. function ASNDecLen(var Start: Integer; const Buffer: AnsiString): Integer;
  171. var
  172. x, n: Integer;
  173. begin
  174. x := Ord(Buffer[Start]);
  175. Inc(Start);
  176. if x < $80 then
  177. Result := x
  178. else
  179. begin
  180. Result := 0;
  181. x := x and $7F;
  182. for n := 1 to x do
  183. begin
  184. Result := Result * 256;
  185. x := Ord(Buffer[Start]);
  186. Inc(Start);
  187. Result := Result + x;
  188. end;
  189. end;
  190. end;
  191. {==============================================================================}
  192. function ASNEncInt(Value: Int64): AnsiString;
  193. var
  194. x: Int64;
  195. y: byte;
  196. neg: Boolean;
  197. begin
  198. neg := Value < 0;
  199. x := Abs(Value);
  200. if neg then
  201. x := x - 1;
  202. Result := '';
  203. repeat
  204. y := x mod 256;
  205. x := x div 256;
  206. if neg then
  207. y := not y;
  208. Result := AnsiChar(y) + Result;
  209. until x = 0;
  210. if (not neg) and (Result[1] > #$7F) then
  211. Result := #0 + Result;
  212. if (neg) and (Result[1] < #$80) then
  213. Result := #$FF + Result;
  214. end;
  215. {==============================================================================}
  216. function ASNEncUInt(Value: Integer): AnsiString;
  217. var
  218. x, y: Integer;
  219. neg: Boolean;
  220. begin
  221. neg := Value < 0;
  222. x := Value;
  223. if neg then
  224. x := x and $7FFFFFFF;
  225. Result := '';
  226. repeat
  227. y := x mod 256;
  228. x := x div 256;
  229. Result := AnsiChar(y) + Result;
  230. until x = 0;
  231. if neg then
  232. Result[1] := AnsiChar(Ord(Result[1]) or $80);
  233. end;
  234. {==============================================================================}
  235. function ASNObject(const Data: AnsiString; ASNType: Integer): AnsiString;
  236. begin
  237. Result := AnsiChar(ASNType) + ASNEncLen(Length(Data)) + Data;
  238. end;
  239. {==============================================================================}
  240. function ASNItem(var Start: Integer; const Buffer: AnsiString;
  241. var ValueType: Integer): AnsiString;
  242. var
  243. ASNType: Integer;
  244. ASNSize: Integer;
  245. y: int64;
  246. n: Integer;
  247. x: byte;
  248. s: AnsiString;
  249. c: AnsiChar;
  250. neg: Boolean;
  251. l: Integer;
  252. begin
  253. Result := '';
  254. ValueType := ASN1_NULL;
  255. l := Length(Buffer);
  256. if l < (Start + 1) then
  257. Exit;
  258. s := '';
  259. ASNType := Ord(Buffer[Start]);
  260. ValueType := ASNType;
  261. Inc(Start);
  262. ASNSize := ASNDecLen(Start, Buffer);
  263. if (Start + ASNSize - 1) > l then
  264. Exit;
  265. if (ASNType and $20) > 0 then
  266. // Result := '$' + IntToHex(ASNType, 2)
  267. Result := Copy(Buffer, Start, ASNSize)
  268. else
  269. case ASNType of
  270. ASN1_INT, ASN1_ENUM, ASN1_BOOL:
  271. begin
  272. y := 0;
  273. neg := False;
  274. for n := 1 to ASNSize do
  275. begin
  276. x := Ord(Buffer[Start]);
  277. if (n = 1) and (x > $7F) then
  278. neg := True;
  279. if neg then
  280. x := not x;
  281. y := y * 256 + x;
  282. Inc(Start);
  283. end;
  284. if neg then
  285. y := -(y + 1);
  286. Result := IntToStr(y);
  287. end;
  288. ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS, ASN1_COUNTER64:
  289. begin
  290. y := 0;
  291. for n := 1 to ASNSize do
  292. begin
  293. y := y * 256 + Ord(Buffer[Start]);
  294. Inc(Start);
  295. end;
  296. Result := IntToStr(y);
  297. end;
  298. ASN1_OCTSTR, ASN1_OPAQUE:
  299. begin
  300. for n := 1 to ASNSize do
  301. begin
  302. c := AnsiChar(Buffer[Start]);
  303. Inc(Start);
  304. s := s + c;
  305. end;
  306. Result := s;
  307. end;
  308. ASN1_OBJID:
  309. begin
  310. for n := 1 to ASNSize do
  311. begin
  312. c := AnsiChar(Buffer[Start]);
  313. Inc(Start);
  314. s := s + c;
  315. end;
  316. Result := IdToMib(s);
  317. end;
  318. ASN1_IPADDR:
  319. begin
  320. s := '';
  321. for n := 1 to ASNSize do
  322. begin
  323. if (n <> 1) then
  324. s := s + '.';
  325. y := Ord(Buffer[Start]);
  326. Inc(Start);
  327. s := s + IntToStr(y);
  328. end;
  329. Result := s;
  330. end;
  331. ASN1_NULL:
  332. begin
  333. Result := '';
  334. Start := Start + ASNSize;
  335. end;
  336. else // unknown
  337. begin
  338. for n := 1 to ASNSize do
  339. begin
  340. c := AnsiChar(Buffer[Start]);
  341. Inc(Start);
  342. s := s + c;
  343. end;
  344. Result := s;
  345. end;
  346. end;
  347. end;
  348. {==============================================================================}
  349. function MibToId(Mib: String): AnsiString;
  350. var
  351. x: int64;
  352. function WalkInt(var s: String): int64;
  353. var
  354. x: Integer;
  355. t: AnsiString;
  356. begin
  357. x := Pos('.', s);
  358. if x < 1 then
  359. begin
  360. t := s;
  361. s := '';
  362. end
  363. else
  364. begin
  365. t := Copy(s, 1, x - 1);
  366. s := Copy(s, x + 1, Length(s) - x);
  367. end;
  368. Result := StrToInt64Def(t, 0);
  369. end;
  370. begin
  371. Result := '';
  372. x := WalkInt(Mib);
  373. x := x * 40 + WalkInt(Mib);
  374. Result := ASNEncOIDItem(x);
  375. while Mib <> '' do
  376. begin
  377. x := WalkInt(Mib);
  378. Result := Result + ASNEncOIDItem(x);
  379. end;
  380. end;
  381. {==============================================================================}
  382. function IdToMib(const Id: AnsiString): String;
  383. var
  384. x, y: int64;
  385. n: Integer;
  386. begin
  387. Result := '';
  388. n := 1;
  389. while Length(Id) + 1 > n do
  390. begin
  391. x := ASNDecOIDItem(n, Id);
  392. if (n - 1) = 1 then
  393. begin
  394. y := x div 40;
  395. x := x mod 40;
  396. Result := IntToStr(y);
  397. end;
  398. Result := Result + '.' + IntToStr(x);
  399. end;
  400. end;
  401. {==============================================================================}
  402. function IntMibToStr(const Value: AnsiString): AnsiString;
  403. var
  404. n, y: Integer;
  405. begin
  406. y := 0;
  407. for n := 1 to Length(Value) - 1 do
  408. y := y * 256 + Ord(Value[n]);
  409. Result := IntToStr(y);
  410. end;
  411. {==============================================================================}
  412. function ASNdump(const Value: AnsiString): AnsiString;
  413. var
  414. i, at, x, n: integer;
  415. s, indent: AnsiString;
  416. il: TStringList;
  417. begin
  418. il := TStringList.Create;
  419. try
  420. Result := '';
  421. i := 1;
  422. indent := '';
  423. while i < Length(Value) do
  424. begin
  425. for n := il.Count - 1 downto 0 do
  426. begin
  427. x := StrToIntDef(il[n], 0);
  428. if x <= i then
  429. begin
  430. il.Delete(n);
  431. Delete(indent, 1, 2);
  432. end;
  433. end;
  434. s := ASNItem(i, Value, at);
  435. Result := Result + indent + '$' + IntToHex(at, 2);
  436. if (at and $20) > 0 then
  437. begin
  438. x := Length(s);
  439. Result := Result + ' constructed: length ' + IntToStr(x);
  440. indent := indent + ' ';
  441. il.Add(IntToStr(x + i - 1));
  442. end
  443. else
  444. begin
  445. case at of
  446. ASN1_BOOL:
  447. Result := Result + ' BOOL: ';
  448. ASN1_INT:
  449. Result := Result + ' INT: ';
  450. ASN1_ENUM:
  451. Result := Result + ' ENUM: ';
  452. ASN1_COUNTER:
  453. Result := Result + ' COUNTER: ';
  454. ASN1_GAUGE:
  455. Result := Result + ' GAUGE: ';
  456. ASN1_TIMETICKS:
  457. Result := Result + ' TIMETICKS: ';
  458. ASN1_OCTSTR:
  459. Result := Result + ' OCTSTR: ';
  460. ASN1_OPAQUE:
  461. Result := Result + ' OPAQUE: ';
  462. ASN1_OBJID:
  463. Result := Result + ' OBJID: ';
  464. ASN1_IPADDR:
  465. Result := Result + ' IPADDR: ';
  466. ASN1_NULL:
  467. Result := Result + ' NULL: ';
  468. ASN1_COUNTER64:
  469. Result := Result + ' COUNTER64: ';
  470. else // other
  471. Result := Result + ' unknown: ';
  472. end;
  473. if IsBinaryString(s) then
  474. s := DumpExStr(s);
  475. Result := Result + s;
  476. end;
  477. Result := Result + #$0d + #$0a;
  478. end;
  479. finally
  480. il.Free;
  481. end;
  482. end;
  483. {==============================================================================}
  484. end.