forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCertificateCommands.cs
More file actions
239 lines (209 loc) · 8.33 KB
/
Copy pathCertificateCommands.cs
File metadata and controls
239 lines (209 loc) · 8.33 KB
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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using Dbg = System.Management.Automation.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Management.Automation;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Defines the implementation of the get-pfxcertificate cmdlet
/// </summary>
[Cmdlet(VerbsCommon.Get, "PfxCertificate", DefaultParameterSetName = "ByPath", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113323")]
[OutputType(typeof(X509Certificate2))]
public sealed class GetPfxCertificateCommand : PSCmdlet
{
/// <summary>
/// Gets or sets the path of the item for which to obtain the
/// certificate.
/// </summary>
[Parameter(Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Mandatory = true, ParameterSetName = "ByPath")]
public string[] FilePath
{
get
{
return _path;
}
set
{
_path = value;
}
}
private string[] _path;
/// <summary>
/// Gets or sets the literal path of the item for which to obtain the
/// certificate.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true, ParameterSetName = "ByLiteralPath")]
[Alias("PSPath")]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public string[] LiteralPath
{
get
{
return _path;
}
set
{
_path = value;
_isLiteralPath = true;
}
}
private bool _isLiteralPath = false;
//
// list of files that were not found
//
private ArrayList _filesNotFound = new ArrayList();
/// <summary>
/// Initializes a new instance of the GetPfxCertificateCommand
/// class
/// </summary>
public GetPfxCertificateCommand() : base()
{
}
/// <summary>
/// Processes records from the input pipeline.
/// For each input file, the command retrieves its
/// corresponding certificate.
/// </summary>
protected override void ProcessRecord()
{
//
// this cannot happen as we have specified the Path
// property to be a mandatory parameter
//
Dbg.Assert((FilePath != null) && (FilePath.Length > 0),
"GetCertificateCommand: Param binder did not bind path");
X509Certificate2 cert = null;
foreach (string p in FilePath)
{
List<string> paths = new List<string>();
// Expand wildcard characters
if (_isLiteralPath)
{
paths.Add(SessionState.Path.GetUnresolvedProviderPathFromPSPath(p));
}
else
{
try
{
foreach (PathInfo tempPath in SessionState.Path.GetResolvedPSPathFromPSPath(p))
{
paths.Add(tempPath.ProviderPath);
}
}
catch (ItemNotFoundException)
{
_filesNotFound.Add(p);
}
}
foreach (string resolvedPath in paths)
{
string resolvedProviderPath =
SecurityUtils.GetFilePathOfExistingFile(this, resolvedPath);
if (resolvedProviderPath == null)
{
_filesNotFound.Add(p);
}
else
{
try
{
cert = GetCertFromPfxFile(resolvedProviderPath);
}
catch (CryptographicException)
{
//
// CryptographicException is thrown when any error
// occurs inside the crypto class library. It has a
// protected member HResult that indicates the exact
// error but it is not available outside the class.
// Thus we have to assume that the above exception
// was thrown because the pfx file is password
// protected.
//
SecureString password = null;
string prompt = null;
prompt = CertificateCommands.GetPfxCertPasswordPrompt;
password = SecurityUtils.PromptForSecureString(Host.UI, prompt);
try
{
cert = GetCertFromPfxFile(resolvedProviderPath,
password);
}
catch (CryptographicException e)
{
//
// since we cannot really figure out the
// meaning of a given CryptographicException
// we have to use NotSpecified category here
//
ErrorRecord er =
new ErrorRecord(e,
"GetPfxCertificateUnknownCryptoError",
ErrorCategory.NotSpecified,
null);
WriteError(er);
continue;
}
}
WriteObject(cert);
}
}
}
if (_filesNotFound.Count > 0)
{
if (_filesNotFound.Count == FilePath.Length)
{
ErrorRecord er =
SecurityUtils.CreateFileNotFoundErrorRecord(
CertificateCommands.NoneOfTheFilesFound,
"GetPfxCertCommandNoneOfTheFilesFound");
ThrowTerminatingError(er);
}
else
{
//
// we found some files but not others.
// Write error for each missing file
//
foreach (string f in _filesNotFound)
{
ErrorRecord er =
SecurityUtils.CreateFileNotFoundErrorRecord(
CertificateCommands.FileNotFound,
"GetPfxCertCommandFileNotFound",
f
);
WriteError(er);
}
}
}
}
private static X509Certificate2 GetCertFromPfxFile(string path)
{
X509Certificate2 cert = new X509Certificate2();
cert.Import(path);
return cert;
}
private static X509Certificate2 GetCertFromPfxFile(string path, SecureString password)
{
X509Certificate2 cert = new X509Certificate2();
//
// NTRAID#DevDiv Bugs-33007-2004/7/08-kumarp
// the following will not be required once X509Certificate2.Import()
// accepts a SecureString
//
string clearTextPassword = SecurityUtils.GetStringFromSecureString(password);
cert.Import(path, clearTextPassword, X509KeyStorageFlags.DefaultKeySet);
return cert;
}
}
}