File: X509.cs

package info (click to toggle)
wolfssl 5.8.4-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 117,604 kB
  • sloc: ansic: 1,584,954; asm: 481,206; sh: 11,586; cs: 6,596; xml: 3,878; perl: 3,291; makefile: 2,058; ada: 1,891; javascript: 748; python: 636; cpp: 131; ruby: 118; objc: 80; tcl: 73
file content (243 lines) | stat: -rw-r--r-- 8,910 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* X509.cs
 *
 * Copyright (C) 2006-2025 wolfSSL Inc.
 *
 * This file is part of wolfSSL.
 *
 * wolfSSL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * wolfSSL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
 */

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace wolfSSL.CSharp
{
    public class X509
    {
        private const string wolfssl_dll = "wolfssl.dll";

#if WindowsCE
        [DllImport(wolfssl_dll)]
        private extern static int wolfSSL_X509_get_pubkey_buffer(IntPtr x509, IntPtr buf, IntPtr bufSz);
        [DllImport(wolfssl_dll)]
        private extern static IntPtr wolfSSL_X509_get_der(IntPtr x509, IntPtr bufSz);
        [DllImport(wolfssl_dll)]
        private extern static void wolfSSL_X509_free(IntPtr x509);
        [DllImport(wolfssl_dll)]
        private extern static int wc_DerToPem(IntPtr der, int derSz, IntPtr pem, int pemSz, int type);

        [DllImport(wolfssl_dll)]
        private extern static IntPtr wolfSSL_X509_get_name_oneline(IntPtr x509Name, IntPtr buf, int bufSz);
        [DllImport(wolfssl_dll)]
        private extern static IntPtr wolfSSL_X509_get_subject_name(IntPtr x509);
        [DllImport(wolfssl_dll)]
        private extern static IntPtr wolfSSL_X509_get_issuer_name(IntPtr x509);
#else
        [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
        private extern static int wolfSSL_X509_get_pubkey_buffer(IntPtr x509, IntPtr buf, IntPtr bufSz);
        [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
        private extern static IntPtr wolfSSL_X509_get_der(IntPtr x509, IntPtr bufSz);
        [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
        private extern static void wolfSSL_X509_free(IntPtr x509);
        [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
        private extern static int wc_DerToPem(IntPtr der, int derSz, IntPtr pem, int pemSz, int type);


        [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
        private extern static IntPtr wolfSSL_X509_get_name_oneline(IntPtr x509Name, IntPtr buf, int bufSz);
        [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
        private extern static IntPtr wolfSSL_X509_get_subject_name(IntPtr x509);
        [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
        private extern static IntPtr wolfSSL_X509_get_issuer_name(IntPtr x509);
#endif

        private IntPtr x509;
        private int    type;
        private bool isDynamic;

        /* public properties */
        public string Issuer;
        public string Subject;


        /* enum from wolfssl */
        private readonly int CERT_TYPE = 0;

        /// <summary>
        /// Creates a new X509 class
        /// </summary>
        /// <param name="x509">Pointer to wolfSSL structure</param>
        /// <param name="isDynamic">Should the lower level x509 be free'd? </param>
        public X509(IntPtr x509, bool isDynamic)
        {
            IntPtr ret;

            this.type = wolfssl.SSL_FILETYPE_PEM;
            this.x509 = x509;
            ret = wolfSSL_X509_get_name_oneline(
                wolfSSL_X509_get_issuer_name(this.x509), IntPtr.Zero, 0);
            this.Issuer = wolfssl.PtrToStringAnsi(ret);

            ret = wolfSSL_X509_get_name_oneline(
                wolfSSL_X509_get_subject_name(this.x509), IntPtr.Zero, 0);
            this.Subject = wolfssl.PtrToStringAnsi(ret);

            this.isDynamic = isDynamic;
        }

        /// <summary>
        /// Free up the C level WOLFSSL_X509 struct if needed
        /// </summary>
        ~X509()
        {
            if (this.isDynamic)
            {
                wolfSSL_X509_free(this.x509);
            }
        }


        /// <summary>
        /// Used for getting the public key buffer
        /// </summary>
        /// <returns>DER public key on success</returns>
        public byte[] GetPublicKey()
        {
            if (this.x509 == IntPtr.Zero)
            {
                return null;
            }

            try
            {
                IntPtr bufSz;
                IntPtr buf;

                int keySz = 0;
                int ret;
                byte[] key = null;

                bufSz = Marshal.AllocHGlobal(4); /* pointer to 4 bytes */
                ret = wolfSSL_X509_get_pubkey_buffer(this.x509, IntPtr.Zero, bufSz);
                if (ret == wolfssl.SUCCESS)
                {
                    keySz = Marshal.ReadInt32(bufSz, 0);
                    buf = Marshal.AllocHGlobal(keySz);
                    ret = wolfSSL_X509_get_pubkey_buffer(this.x509, buf, bufSz);
                    if (ret == wolfssl.SUCCESS)
                    {
                        key = new byte[keySz];
                        Marshal.Copy(buf, key, 0, keySz);
                    }
                    Marshal.FreeHGlobal(buf);
                }
                Marshal.FreeHGlobal(bufSz);
                return key;
            }
            catch (Exception e)
            {
                wolfssl.log(wolfssl.ERROR_LOG, "error getting public key" + e.ToString());
                return null;
            }
        }

        /// <summary>
        /// Gets the X509 buffer
        /// </summary>
        /// <returns>X509 buffer on success</returns>
        public byte[] Export(int type)
        {
            if (this.x509 == IntPtr.Zero)
                return null;
            try
            {
                IntPtr bufSz;
                IntPtr buf;
                byte[] ret = null;

                bufSz = Marshal.AllocHGlobal(4); /* pointer to 4 bytes */
                buf = wolfSSL_X509_get_der(this.x509, bufSz);
                if (buf != IntPtr.Zero)
                {
                    int derSz = Marshal.ReadInt32(bufSz, 0);
                    if (type == wolfssl.SSL_FILETYPE_ASN1)
                    {
                        ret = new byte[derSz];
                        Marshal.Copy(buf, ret, 0, derSz);
                    }
                    else if (type == wolfssl.SSL_FILETYPE_PEM)
                    {
                        int pemSz;

                        pemSz = wc_DerToPem(buf, derSz, IntPtr.Zero, 0, CERT_TYPE);
                        if (pemSz > 0)
                        {
                            IntPtr pem = Marshal.AllocHGlobal(pemSz);
                            pemSz = wc_DerToPem(buf, derSz, pem, pemSz, CERT_TYPE);
                            ret = new byte[pemSz];
                            Marshal.Copy(pem, ret, 0, pemSz);
                            Marshal.FreeHGlobal(pem);
                        }

                    }
                    else
                    {
                        wolfssl.log(wolfssl.ERROR_LOG, "unsupported export type");
                    }
                    Marshal.FreeHGlobal(bufSz);
                    return ret;
                }
                {
                    wolfssl.log(wolfssl.ERROR_LOG, "unable to get buffer");
                }
                Marshal.FreeHGlobal(bufSz);
                return ret;
            }
            catch (Exception e)
            {
                wolfssl.log(wolfssl.ERROR_LOG, "error getting x509 DER" + e.ToString());
                return null;
            }
        }

        /// <summary>
        /// Gets the X509 buffer using this.type set (default PEM)
        /// </summary>
        /// <returns>X509 buffer on success</returns>
        public byte[] Export()
        {
            return Export(this.type);
        }

        /// <summary>
        /// Gets the X509 format
        /// </summary>
        /// <returns>X509 format on success</returns>
        public string GetFormat()
        {
            if (this.type == wolfssl.SSL_FILETYPE_PEM)
            {
                return "PEM";
            }
            if (this.type == wolfssl.SSL_FILETYPE_ASN1)
            {
                return "DER";
            }
            return "Unknown";
        }
    }
}