-
Notifications
You must be signed in to change notification settings - Fork 894
/
Copy pathCertificateX509.cs
55 lines (48 loc) · 1.6 KB
/
CertificateX509.cs
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
using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
/// <summary>
/// Conains a X509 certificate
/// </summary>
public class CertificateX509 : Certificate
{
/// <summary>
/// For mocking purposes
/// </summary>
protected CertificateX509()
{ }
/// <summary>
/// The certificate.
/// </summary>
public virtual X509Certificate Certificate { get; private set; }
internal unsafe CertificateX509(git_certificate_x509* cert)
{
int len = checked((int)cert->len.ToUInt32());
byte[] data = new byte[len];
Marshal.Copy(new IntPtr(cert->data), data, 0, len);
Certificate = new X509Certificate(data);
}
internal CertificateX509(X509Certificate cert)
{
Certificate = cert;
}
internal unsafe IntPtr ToPointers(out IntPtr dataPtr)
{
var certData = Certificate.Export(X509ContentType.Cert);
dataPtr = Marshal.AllocHGlobal(certData.Length);
Marshal.Copy(certData, 0, dataPtr, certData.Length);
var gitCert = new git_certificate_x509()
{
cert_type = GitCertificateType.X509,
data = (byte*)dataPtr.ToPointer(),
len = (UIntPtr)certData.Length,
};
var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(gitCert));
Marshal.StructureToPtr(gitCert, ptr, false);
return ptr;
}
}
}