This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathget_certs.csx
70 lines (58 loc) · 2.17 KB
/
get_certs.csx
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
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
public static class MyExtensions
{
public static string SplitLines(this string str)
{
var chunkSize = 64;
var chunksCount = (int)Math.Ceiling((decimal)str.Length / chunkSize);
var chunks = Enumerable.Range(0, chunksCount)
.Select(i =>
{
return i == chunksCount - 1 ? str.Substring(i * chunkSize) :
str.Substring(i * chunkSize, chunkSize);
})
.ToList();
return String.Join("\n", chunks);
}
public static string ExportPEMEncoded(this X509Certificate2 cert)
{
return "-----BEGIN CERTIFICATE-----\n" +
Convert.ToBase64String(cert.Export(X509ContentType.Cert)).SplitLines() +
"\n-----END CERTIFICATE-----";
}
}
public class Startup
{
public async Task<object> Invoke(object input)
{
dynamic options = (dynamic)input;
X509Store store;
if (options.hasStoreName && options.hasStoreLocation) {
var storeName = Enum.Parse(typeof(StoreName), options.storeName);
var storeLocation = Enum.Parse(typeof(StoreLocation), options.storeLocation);
store = new X509Store(storeName, storeLocation);
} else if (options.hasStoreName) {
var storeName = Enum.Parse(typeof(StoreName), options.storeName);
store = new X509Store(storeName);
} else if (options.hasStoreLocation) {
var storeLocation = Enum.Parse(typeof(StoreLocation), options.storeLocation);
store = new X509Store(storeLocation);
} else {
store = new X509Store();
}
store.Open(OpenFlags.ReadOnly);
var result = store.Certificates.Cast<X509Certificate2>().Select(cert => new {
pem = cert.ExportPEMEncoded(),
subject = cert.SubjectName.Name,
thumbprint = cert.Thumbprint,
issuer = cert.IssuerName.Name
});
return result;
}
}