extractrsa.lpr 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. {
  2. This file is part of the Free Component Library.
  3. Copyright (c) 2023 by the Free Pascal team.
  4. Demo to dump the RSA private key in a PEM file
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. }
  11. program extractrsa;
  12. uses sysutils, types, custapp, classes, fpasn, basenenc, fprsa;
  13. Type
  14. { TExtractRSAApplication }
  15. TDumpApplication = class(TCustomApplication)
  16. private
  17. FRaw : Boolean;
  18. procedure DumpX509RSA(Key: TX509RSAPrivateKey);
  19. function GetBytes(FN: String) : TBytes;
  20. procedure ShowASN(FN: String);
  21. procedure Usage(aError : String);
  22. Protected
  23. procedure DoRun; override;
  24. end;
  25. Procedure TDumpApplication.DumpX509RSA(Key : TX509RSAPrivateKey);
  26. function DumpBytes(B : TBytes) : string;
  27. begin
  28. Result:=Base16.Encode(B,True);
  29. end;
  30. begin
  31. Writeln('X509 RSA Private Key:');
  32. With key do
  33. begin
  34. Writeln('Version: ',Version);
  35. Writeln('Modulus (m/n): ',DumpBytes(Modulus));
  36. Writeln('PublicExponent (e) : ',DumpBytes(PublicExponent));
  37. Writeln('PrivateExponent (d) : ',DumpBytes(PrivateExponent));
  38. Writeln('Prime1 (p) : ',DumpBytes(Prime1));
  39. Writeln('Prime2 (q) : ',DumpBytes(Prime2));
  40. Writeln('Exponent1 (dp) : ',DumpBytes(Exponent1));
  41. Writeln('Exponent2 (dq) : ',DumpBytes(Exponent2));
  42. Writeln('Coefficient (qi) : ',DumpBytes(Coefficient));
  43. end;
  44. end;
  45. function TDumpApplication.GetBytes(FN : String) : TBytes;
  46. Var
  47. L : TStrings;
  48. S : String;
  49. I : Integer;
  50. begin
  51. if FRaw then
  52. Result:=GetFileContents(FN)
  53. else
  54. begin
  55. L:=TStringList.Create;
  56. try
  57. L.LoadFromFile(FN);
  58. S:='';
  59. For I:=1 to L.Count-2 do
  60. S:=S+Trim(L[i]);
  61. Result:=BaseNenc.Base64.Decode(S);
  62. finally
  63. L.Free;
  64. end;
  65. end;
  66. end;
  67. Procedure TDumpApplication.ShowASN(FN : String);
  68. var
  69. Bytes : TBytes;
  70. RSA : TX509RSAPrivateKey;
  71. begin
  72. Writeln('ASN.1 Contents of '+FN);
  73. Bytes:=GetBytes(FN);
  74. X509RsaPrivateKeyInitFromDER(Rsa,Bytes);
  75. DumpX509RSA(RSA);
  76. end;
  77. procedure TDumpApplication.Usage(aError: String);
  78. begin
  79. if (aError<>'') then
  80. Writeln(aError);
  81. Writeln('Usage : ',ExtractFileName(ParamStr(0)),' [options] FileName1 [FileName2..FileNameN]');
  82. Writeln('Where options is one of:');
  83. Writeln('-h --help This help');
  84. Writeln('-r --raw Treat filenames as raw byte dumps (default is to assume .PEM format)');
  85. ExitCode:=Ord(aError<>'');
  86. end;
  87. procedure TDumpApplication.DoRun ;
  88. const
  89. Short = 'hr';
  90. Long : Array of string = ('help','raw');
  91. var
  92. S,FN : String;
  93. NonOpt : TStringDynArray;
  94. begin
  95. Terminate;
  96. S:=CheckOptions(Short,Long);
  97. if S='' then
  98. begin
  99. NonOpt:=GetNonOptions(Short,Long);
  100. if 0=Length(NonOpt) then
  101. S:='One or more filenames must be specified';
  102. end;
  103. if (S<>'') or HasOption('h','help') then
  104. Usage(S)
  105. else
  106. begin
  107. for FN in nonOpt do
  108. ShowAsn(FN);
  109. end;
  110. end;
  111. begin
  112. With TDumpApplication.Create(Nil) do
  113. try
  114. Initialize;
  115. Run;
  116. finally
  117. Free;
  118. end;
  119. end.