mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-10-14 08:46:08 +02:00
* Add X509 functions and example from Silvio Clecio to generate private key
git-svn-id: trunk@40800 -
This commit is contained in:
parent
d9ccc42cc4
commit
2ceb5c9683
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -3833,6 +3833,7 @@ packages/gnome1/src/zvt/lists.inc svneol=native#text/plain
|
||||
packages/gnome1/src/zvt/vt.inc svneol=native#text/plain
|
||||
packages/gnome1/src/zvt/vtx.inc svneol=native#text/plain
|
||||
packages/gnutls/examples/httpget.pp svneol=native#text/plain
|
||||
packages/gnutls/examples/privkey.pp svneol=native#text/plain
|
||||
packages/gnutls/examples/testgnutls.pp svneol=native#text/plain
|
||||
packages/gnutls/fpmake.pp svneol=native#text/plain
|
||||
packages/gnutls/src/gnutls.pp svneol=native#text/plain
|
||||
|
67
packages/gnutls/examples/privkey.pp
Normal file
67
packages/gnutls/examples/privkey.pp
Normal file
@ -0,0 +1,67 @@
|
||||
{
|
||||
Simple Low-level example showing how to generate a
|
||||
RSA private key (4096 bytes) using the GnuTLS binding.
|
||||
|
||||
Author: Silvio Clecio (silvioprog)
|
||||
Date: Mon Jan 7 01:36:18 -03 2019
|
||||
GnuTLS version: 3.4+
|
||||
}
|
||||
|
||||
program privkey;
|
||||
|
||||
{$MODE OBJFPC}{$H+}
|
||||
{$ASSERTIONS ON}
|
||||
|
||||
uses
|
||||
sysutils,
|
||||
ctypes,
|
||||
gnutls;
|
||||
|
||||
type
|
||||
{ cchar = Byte;
|
||||
Pcchar = PAnsiChar;
|
||||
Pcsize_t = PNativeUInt;
|
||||
Pcvoid = Pointer;}
|
||||
|
||||
EGnuTLS = Exception;
|
||||
|
||||
|
||||
procedure CheckRet(ret: cint);
|
||||
var
|
||||
P: Pchar;
|
||||
S: string;
|
||||
begin
|
||||
if ret = GNUTLS_E_SUCCESS then
|
||||
Exit;
|
||||
P := gnutls_strerror(ret);
|
||||
S:=StrPas(P);// SetString(S, @P[0], Length(Pcchar(@P[0])));
|
||||
SetCodePage(RawByteString(S), CP_UTF8, False);
|
||||
raise EGnuTLS.Create(S);
|
||||
end;
|
||||
|
||||
var
|
||||
priv_key: String;
|
||||
priv_key_size: cuint = SizeOf(priv_key);
|
||||
key: Tgnutls_x509_privkey_t;
|
||||
|
||||
begin
|
||||
LoadGnuTLS;
|
||||
try
|
||||
Assert(GnuTLSLoaded);
|
||||
try
|
||||
CheckRet(gnutls_x509_privkey_init(@key));
|
||||
priv_key_size := gnutls_sec_param_to_pk_bits(GNUTLS_PK_RSA, GNUTLS_SEC_PARAM_HIGH);
|
||||
setLength(Priv_key,priv_key_size*2);
|
||||
CheckRet(gnutls_x509_privkey_generate(key, GNUTLS_PK_RSA, priv_key_size, 0));
|
||||
CheckRet(gnutls_x509_privkey_export(key, GNUTLS_X509_FMT_PEM, @priv_key[1], @priv_key_size));
|
||||
setLength(Priv_key,priv_key_size);
|
||||
|
||||
WriteLn(priv_key);
|
||||
except
|
||||
gnutls_x509_privkey_deinit(key);
|
||||
raise;
|
||||
end;
|
||||
finally
|
||||
FreeGnuTLS;
|
||||
end;
|
||||
end.
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user