8000 Fix for #19. · openssl-net/openssl-net@b4902d8 · GitHub
[go: up one dir, main page]

Skip to content
< 8000 /div>
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit b4902d8

Browse files
author
Frank Laub
committed
Fix for #19.
On Mono, the runtime attempts to free returned strings by default. Many of the native functions return static strings that must not be freed. In these cases, manually marshal the string without freeing the underlying pointer.
1 parent ee78ac4 commit b4902d8

File tree

11 files changed

+140
-74
lines changed

11 files changed

+140
-74
lines changed

ManagedOpenSsl/Core/Asn1Object.cs

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727

2828
namespace OpenSSL.Core
2929
{
30+
/// <summary>
31+
/// Asn1 object.
32+
/// </summary>
3033
public class Asn1Object
3134
{
3235
[StructLayout(LayoutKind.Sequential)]
@@ -41,45 +44,94 @@ struct asn1_object_st
4144
}
4245

4346
private int nid;
44-
45-
public Asn1Object(int nid) {
47+
48+
/// <summary>
49+
/// Initializes a new instance of the <see cref="OpenSSL.Core.Asn1Object"/> class.
50+
/// </summary>
51+
/// <param name="nid">Nid.</param>
52+
public Asn1Object(int nid)
53+
{
4654
this.nid = nid;
4755
}
48-
49-
public Asn1Object(string sn) {
56+
57+
/// <summary>
58+
/// Initializes a new instance of the <see cref="OpenSSL.Core.Asn1Object"/> class.
59+
/// </summary>
60+
/// <param name="sn">Sn.</param>
61+
public Asn1Object(string sn)
62+
{
5063
nid = Native.OBJ_sn2nid(sn);
5164
}
5265

53-
public int NID {
66+
/// <summary>
67+
/// Gets the NID.
68+
/// </summary>
69+
/// <value>The NID.</value>
70+
public int NID
71+
{
5472
get { return nid; }
5573
}
56-
57-
public string ShortName {
58-
get { return Native.OBJ_nid2sn(nid); }
74+
75+
/// <summary>
76+
/// Gets the short name.
77+
/// </summary>
78+
/// <value>The short name.</value>
79+
public string ShortName
80+
{
81+
get { return Nati F438 ve.StaticString(Native.OBJ_nid2sn(nid)); }
5982
}
60-
61-
public string LongName {
62-
get { return Native.OBJ_nid2ln(nid); }
83+
84+
/// <summary>
85+
/// Gets the long name.
86+
/// </summary>
87+
/// <value>The long name.</value>
88+
public string LongName
89+
{
90+
get { return Native.StaticString(Native.OBJ_nid2ln(nid)); }
6391
}
64-
65-
public static Asn1Object FromShortName(string sn) {
92+
93+
/// <summary>
94+
/// Froms the short name.
95+
/// </summary>
96+
/// <returns>The short name.</returns>
97+
/// <param name="sn">Sn.</param>
98+
public static Asn1Object FromShortName(string sn)
99+
{
66100
return new Asn1Object(sn);
67101
}
68102

69-
public static Asn1Object FromLongName(string sn) {
103+
/// <summary>
104+
/// Froms the long name.
105+
/// </summary>
106+
/// <returns>The long name.</returns>
107+
/// <param name="sn">Sn.</param>
108+
public static Asn1Object FromLongName(string sn)
109+
{
70110
return new Asn1Object(Native.OBJ_ln2nid(sn));
71111
}
72-
73-
public override bool Equals(object obj) {
112+
113+
/// <summary>
114+
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="OpenSSL.Core.Asn1Object"/>.
115+
/// </summary>
116+
/// <param name="obj">The <see cref="System.Object"/> to compare with the current <see cref="OpenSSL.Core.Asn1Object"/>.</param>
117+
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to the current
118+
/// <see cref="OpenSSL.Core.Asn1Object"/>; otherwise, <c>false</c>.</returns>
119+
public override bool Equals(object obj)
120+
{
74121
var rhs = obj as Asn1Object;
75122

76123
if (rhs == null)
77124
return false;
78125

79126
return nid == rhs.nid;
80127
}
81-
82-
public override int GetHashCode() {
128+
129+
/// <summary>
130+
/// Serves as a hash function for a <see cref="OpenSSL.Core.Asn1Object"/> object.
131+
/// </summary>
132+
/// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a hash table.</returns>
133+
public override int GetHashCode()
134+
{
83135
return nid;
84136
}
85137
}

ManagedOpenSsl/Core/BigNumber.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class BigNumber : Base, IComparable<BigNumber>
4444
/// </summary>
4545
public static string Options
4646
{
47-
get { return Native.BN_options(); }
47+
get { return Native.StaticString(Native.BN_options()); }
4848
}
4949
#endregion
5050

ManagedOpenSsl/Core/Crypto.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,39 +90,39 @@ public class CryptoUtil
9090
/// </summary>
9191
public static string MD2_Options
9292
{
93-
get { return Native.MD2_options(); }
93+
get { return Native.StaticString(Native.MD2_options()); }
9494
}
9595

9696
/// <summary>
9797
/// Returns RC4_options()
9898
/// </summary>
9999
public static string RC4_Options
100100
{
101-
get { return Native.RC4_options(); }
101+
get { return Native.StaticString(Native.RC4_options()); }
102102
}
103103

104104
/// <summary>
105105
/// Returns DES_options()
106106
/// </summary>
107107
public static string DES_Options
108108
{
109-
get { return Native.DES_options(); }
109+
get { return Native.StaticString(Native.DES_options()); }
110110
}
111111

112112
/// <summary>
113113
/// Returns idea_options()
114114
/// </summary>
115115
public static string Idea_Options
116116
{
117-
get { return Native.idea_options(); }
117+
get { return Native.StaticString(Native.idea_options()); }
118118
}
119119

120120
/// <summary>
121121
/// Returns BF_options()
122122
/// </summary>
123123
public static string Blowfish_Options
124124
{
125-
get { return Native.BF_options(); }
125+
get { return Native.StaticString(Native.BF_options()); }
126126
}
127127

128128
/// <summary>

ManagedOpenSsl/Core/Native.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,28 +227,28 @@ public static void UninitializeThreads()
227227
public const uint Wrapper = 0x1000201F; //1.0.2a Release
228228

229229
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
230-
public extern static string SSLeay_version(int type);
230+
public extern static IntPtr SSLeay_version(int type);
231231

232232
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
233233
public extern static uint SSLeay();
234234

235235
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
236-
public extern static string BN_options();
236+
public extern static IntPtr BN_options();
237237

238238
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
239-
public extern static string MD2_options();
239+
public extern static IntPtr MD2_options();
240240

241241
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
242-
public extern static string RC4_options();
242+
public extern static IntPtr RC4_options();
243243

244244
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
245-
public extern static string DES_options();
245+
public extern static IntPtr DES_options();
246246

247247
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
248-
public extern static string idea_options();
248+
public extern static IntPtr idea_options();
249249

250250
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
251-
public extern static string BF_options();
251+
public extern static IntPtr BF_options();
252252

253253
#endregion
254254

@@ -428,10 +428,10 @@ public extern static int CRYPTO_set_mem_debug_functions(
428428
public extern static IntPtr OBJ_nid2obj(int n);
429429

430430
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
431-
public extern static string OBJ_nid2ln(int n);
431+
public extern static IntPtr OBJ_nid2ln(int n);
432432

433433
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
434-
public extern static string OBJ_nid2sn(int n);
434+
public extern static IntPtr OBJ_nid2sn(int n);
435435

436436
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
437437
public extern static int OBJ_obj2nid(IntPtr o);
@@ -1660,6 +1660,21 @@ public class bn_gencb_st
16601660
#endregion
16611661

16621662
#region EVP_MD
1663+
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
1664+
public extern static int EVP_MD_type(IntPtr md);
1665+
1666+
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
1667+
public extern static int EVP_MD_pkey_type(IntPtr md);
1668+
1669+
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
1670+
public extern static int EVP_MD_size(IntPtr md);
1671+
1672+
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
1673+
public extern static int EVP_MD_block_size(IntPtr md);
1674+
1675+
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
1676+
public extern static uint EVP_MD_flags(IntPtr md);
1677+
16631678
[DllImport(DLLNAME, CallingConvention=CallingConvention.Cdecl)]
16641679
public extern static IntPtr EVP_get_digestbyname(byte[] name);
16651680

@@ -2601,6 +2616,11 @@ public static int SSL_CTX_get_options(IntPtr ctx)
26012616
#endregion
26022617

26032618
#region Utilities
2619+
public static string StaticString(IntPtr ptr)
2620+
{
2621+
return Marshal.PtrToStringAnsi(ptr);
2622+
}
2623+
26042624
public static string PtrToStringAnsi(IntPtr ptr, bool hasOwnership)
26052625
{
26062626
var len = 0;

ManagedOpenSsl/Core/Version.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public enum Format
205205
/// <param name="format"></param>
206206
public static string GetText(Format format)
207207
{
208-
return Native.SSLeay_version((int)format);
208+
return Native.StaticString(Native.SSLeay_version((int)format));
209209
}
210210
}
211211
}

ManagedOpenSsl/Crypto/Cipher.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,15 +450,15 @@ public uint Flags
450450
/// </summary>
451451
public string LongName
452452
{
453-
get { return Native.OBJ_nid2ln(raw.nid); }
453+
get { return Native.StaticString(Native.OBJ_nid2ln(raw.nid)); }
454454
}
455455

456456
/// <summary>
457457
/// Returns the name for the nid field using OBJ_nid2sn()
458458
/// </summary>
459459
public string Name
460460
{
461-
get { return Native.OBJ_nid2sn(raw.nid); }
461+
get { return Native.StaticString(Native.OBJ_nid2sn(raw.nid)); }
462462
}
463463

464464
/// <summary>
@@ -474,7 +474,7 @@ public int Type
474474
/// </summary>
475475
public string TypeName
476476
{
477-
get { return Native.OBJ_nid2ln(Type); }
477+
get { return Native.StaticString(Native.OBJ_nid2ln(Type)); }
478478
}
479479
#endregion
480480
}

ManagedOpenSsl/Crypto/MessageDigest.cs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ namespace OpenSSL.Crypto
3636
/// </summary>
3737
public class MessageDigest : Base
3838
{
39-
private EVP_MD raw;
4039
/// <summary>
4140
/// Creates a EVP_MD struct
4241
/// </summary>
4342
/// <param name="ptr"></param>
4443
/// <param name="owner"></param>
4544
internal MessageDigest(IntPtr ptr, bool owner) : base(ptr, owner)
4645
{
47-
raw = (EVP_MD)Marshal.PtrToStructure(this.ptr, typeof(EVP_MD));
4846
}
4947

5048
/// <summary>
@@ -94,28 +92,6 @@ public static string[] AllNames
9492
get { return new NameCollector(Native.OBJ_NAME_TYPE_MD_METH, false).Result.ToArray(); }
9593
}
9694

97-
#region EVP_MD
98-
[StructLayout(LayoutKind.Sequential)]
99-
struct EVP_MD
100-
{
101-
public int type;
102-
public int pkey_type;
103-
public int md_size;
104-
public uint flags;
105-
public IntPtr init;
106-
public IntPtr update;
107-
public IntPtr final;
108-
public IntPtr copy;
109-
public IntPtr cleanup;
110-
public IntPtr sign;
111-
public IntPtr verify;
112-
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
113-
public int[] required_pkey_type;
114-
public int block_size;
115-
public int ctx_size;
116-
}
117-
#endregion
118-
11995
#region MessageDigests
12096
/// <summary>
12197
/// EVP_md_null()
@@ -189,31 +165,31 @@ struct EVP_MD
189165
/// </summary>
190166
public int BlockSize
191167
{
192-
get { return raw.block_size; }
168+
get { return Native.EVP_MD_block_size(ptr); }
193169
}
194170

195171
/// <summary>
196172
/// Returns the md_size field
197173
/// </summary>
198174
public int Size
199175
{
200-
get { return this.raw.md_size; }
176+
get { return Native.EVP_MD_size(ptr); }
201177
}
202178

203179
/// <summary>
204180
/// Returns the type field using OBJ_nid2ln()
205181
/// </summary>
206182
public string LongName
207183
{
208-
get { return Native.OBJ_nid2ln(raw.type); }
184+
get { return Native.StaticString(Native.OBJ_nid2ln(Native.EVP_MD_type(ptr))); }
209185
}
210186

211187
/// <summary>
212188
/// Returns the type field using OBJ_nid2sn()
213189
/// </summary>
214190
public string Name
215191
{
216-
get { return Native.OBJ_nid2sn(raw.type); }
192+
get { return Native.StaticString(Native.OBJ_nid2sn(Native.EVP_MD_type(ptr))); }
217193
}
218194

219195
#endregion

ManagedOpenSsl/X509/X509Extension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public X509Extension(X509Certificate issuer, X509Certificate subject, string nam
7373
/// </summary>
7474
public string Name
7575
{
76-
get { return Native.OBJ_nid2ln(NID); }
76+
get { return Native.StaticString(Native.OBJ_nid2ln(NID)); }
7777
}
7878

7979
/// <summary>

0 commit comments

Comments
 (0)
0