Kaynağa Gözat

* Example how to create public/private RSA key

git-svn-id: trunk@25709 -
michael 11 yıl önce
ebeveyn
işleme
4bdf29795c

+ 2 - 0
.gitattributes

@@ -5790,6 +5790,8 @@ packages/openssl/Makefile svneol=native#text/plain
 packages/openssl/Makefile.fpc svneol=native#text/plain
 packages/openssl/Makefile.fpc.fpcmake svneol=native#text/plain
 packages/openssl/examples/Makefile svneol=native#text/plain
+packages/openssl/examples/genkeypair.lpi svneol=native#text/plain
+packages/openssl/examples/genkeypair.lpr svneol=native#text/plain
 packages/openssl/examples/test1.pas svneol=native#text/plain
 packages/openssl/fpmake.pp svneol=native#text/plain
 packages/openssl/src/openssl.pas svneol=native#text/plain

+ 2 - 1
packages/openssl/examples/Makefile

@@ -3,7 +3,8 @@ ARGS=-O2 -XX -Xs
 DELP=delp
 
 all:
-	$(PP) $(ARGS) test1.pas
+	$(PP) $(ARGS) -Fu../src test1.pas
+	$(PP) $(ARGS) -Fu../src genkeypair.lpr
 
 clean:
 	$(DELP) .

+ 69 - 0
packages/openssl/examples/genkeypair.lpi

@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<CONFIG>
+  <ProjectOptions>
+    <Version Value="9"/>
+    <General>
+      <Flags>
+        <MainUnitHasCreateFormStatements Value="False"/>
+        <MainUnitHasTitleStatement Value="False"/>
+      </Flags>
+      <SessionStorage Value="InProjectDir"/>
+      <MainUnit Value="0"/>
+      <Title Value="genkeypair"/>
+      <UseAppBundle Value="False"/>
+      <ResourceType Value="res"/>
+    </General>
+    <i18n>
+      <EnableI18N LFM="False"/>
+    </i18n>
+    <VersionInfo>
+      <StringTable ProductVersion=""/>
+    </VersionInfo>
+    <BuildModes Count="1">
+      <Item1 Name="Default" Default="True"/>
+    </BuildModes>
+    <PublishOptions>
+      <Version Value="2"/>
+    </PublishOptions>
+    <RunParams>
+      <local>
+        <FormatVersion Value="1"/>
+      </local>
+    </RunParams>
+    <Units Count="1">
+      <Unit0>
+        <Filename Value="genkeypair.lpr"/>
+        <IsPartOfProject Value="True"/>
+        <UnitName Value="genkeypair"/>
+      </Unit0>
+    </Units>
+  </ProjectOptions>
+  <CompilerOptions>
+    <Version Value="11"/>
+    <SearchPaths>
+      <IncludeFiles Value="$(ProjOutDir)"/>
+      <OtherUnitFiles Value="../src"/>
+    </SearchPaths>
+    <Parsing>
+      <SyntaxOptions>
+        <UseAnsiStrings Value="False"/>
+      </SyntaxOptions>
+    </Parsing>
+    <Other>
+      <CompilerPath Value="$(CompPath)"/>
+    </Other>
+  </CompilerOptions>
+  <Debugging>
+    <Exceptions Count="3">
+      <Item1>
+        <Name Value="EAbort"/>
+      </Item1>
+      <Item2>
+        <Name Value="ECodetoolError"/>
+      </Item2>
+      <Item3>
+        <Name Value="EFOpenError"/>
+      </Item3>
+    </Exceptions>
+  </Debugging>
+</CONFIG>

+ 107 - 0
packages/openssl/examples/genkeypair.lpr

@@ -0,0 +1,107 @@
+{$mode objfpc}
+{$h+}
+program genkeypair;
+
+uses sysutils, openssl;
+
+// This is normally only used when you specify a cipher for encoding the private key.
+
+function PasswordCallback(buf:PAnsiChar; size:Integer; rwflag:Integer; userdata: Pointer):Integer; cdecl;
+
+begin
+  Result:=0;
+  Buf^:=#0;
+end;
+
+procedure DoKey(Const FNPrivate, FNPublic : String; AKeySize : Integer = 1024);
+
+  Procedure RaiseErr(Const Msg : String);
+
+  Var
+    Err : String;
+
+  begin
+    SetLength(Err,1024);
+    ErrErrorString(ErrGetError,Err,1024);
+    Raise Exception.Create(Msg+' : '+Err);
+  end;
+
+  Function GetKey(K : pBIO) : String;
+
+  Var
+    L : Integer;
+    p : pchar;
+
+  begin
+    l:=BIO_ctrl(K,BIO_CTRL_INFO,0,PChar(@P));
+    setlength(Result,l);
+    move(P^,Result[1],l);
+  end;
+
+  Procedure WriteKey(Const FN,Key : String);
+
+  Var
+    F : Text;
+
+  begin
+    Assign(F,FN);
+    Rewrite(F);
+    try
+      Write(F,Key);
+    finally
+      Close(F);
+    end;
+  end;
+
+
+Var
+  rsa: PRSA;
+  PK :PEVP_PKEY;
+  PrivKey, PubKey: pBIO;
+  Key : string;
+
+begin
+  InitLibeaInterface(true);
+  InitSSLEAInterface(true);
+  InitSSLInterface(true);
+  ERR_load_crypto_strings;
+  OpenSSL_add_all_ciphers;
+  pk := EvpPkeynew;
+  if (pk=Nil) then
+    Raise exception.Create('Could not create key structure.');
+  rsa:=RsaGenerateKey(AKeySize,$10001,Nil,Nil);
+  if rsa=nil then
+    Raise exception.Create('Could not create RSA key.');
+  if EvpPkeyAssign(pk, EVP_PKEY_RSA, rsa)=0 then
+    Raise exception.Create('Could not assign created RSA key to key structure.');
+  // Generate private key
+  PrivKey:=BIOnew(BIOsmem);
+  if PrivKey=Nil then
+    Raise exception.Create('Could not allocate BIO structure for private key.');
+  try
+    if PEM_write_bio_PrivateKey(PrivKey, PK, nil, nil, 0, @PasswordCallBack, Nil)=0 then
+      RaiseErr('Could not write private key');
+    Key:=GetKey(PrivKey);
+    WriteKey(FNPrivate,Key);
+  finally
+    BioFreeAll(PrivKey);
+  end;
+  // Get public key
+  PubKey:= BIOnew(BIOsmem);
+  if PubKey=Nil then
+    Raise exception.Create('Could not allocate BIO structure for public key.');
+  try
+    if PEM_write_bio_PubKey(PubKey,PK)=0 then
+      RaiseErr('Could not write public key');
+    Key:=GetKey(PubKey);
+    WriteKey(FNPublic,Key);
+  finally
+    BioFreeAll(PubKey);
+  end;
+end;
+
+begin
+  writeln('Writing private/public key of length 1024 to id_rsa/id_rsa.pub');
+  DoKey('id_rsa','id_rsa.pub',1024);
+end.
+