main
cs 83 lines 2.7 KB
Raw
1 // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3 namespace WixToolset.Core.Native
4 {
5 using System;
6 using System.Collections.Generic;
7 using System.ComponentModel;
8 using System.Globalization;
9 using System.Linq;
10
11 /// <summary>
12 /// Read certificates' public key and thumbprint hashes.
13 /// </summary>
14 public sealed class CertificateHashes
15 {
16 private static readonly char[] TextLineSplitter = new[] { '\t' };
17
18 private CertificateHashes(string path, string publicKey, string thumbprint, Exception exception)
19 {
20 this.Path = path;
21 this.PublicKey = publicKey;
22 this.Thumbprint = thumbprint;
23 this.Exception = exception;
24 }
25
26 /// <summary>
27 /// Path to the file read.
28 /// </summary>
29 public string Path { get; }
30
31 /// <summary>
32 /// Hash of the certificate's public key.
33 /// </summary>
34 public string PublicKey { get; }
35
36 /// <summary>
37 /// Hash of the certificate's thumbprint.
38 /// </summary>
39 public string Thumbprint { get; }
40
41 /// <summary>
42 /// Exception encountered while trying to read certificate's hash.
43 /// </summary>
44 public Exception Exception { get; }
45
46 /// <summary>
47 /// Read the certificate hashes from the provided paths.
48 /// </summary>
49 /// <param name="paths">Paths to read for certificates.</param>
50 /// <returns>Certificate hashes for the provided paths.</returns>
51 public static IReadOnlyList<CertificateHashes> Read(IEnumerable<string> paths)
52 {
53 var result = new List<CertificateHashes>();
54
55 var wixnative = new WixNativeExe("certhashes");
56
57 foreach (var path in paths)
58 {
59 wixnative.AddStdinLine(path);
60 }
61
62 try
63 {
64 var outputLines = wixnative.Run();
65 foreach (var line in outputLines.Where(l => !String.IsNullOrEmpty(l)))
66 {
67 var data = line.Split(TextLineSplitter, StringSplitOptions.None);
68
69 var error = Int32.Parse(data[3].Substring(2), NumberStyles.HexNumber);
70 var exception = error != 0 ? new Win32Exception(error) : null;
71
72 result.Add(new CertificateHashes(data[0], data[1], data[2], exception));
73 }
74 }
75 catch (Exception e)
76 {
77 result.Add(new CertificateHashes(null, null, null, e));
78 }
79
80 return result;
81 }
82 }
83 }