2
2
using System . IO ;
3
3
using System . Reflection ;
4
4
using System . Security . Cryptography . X509Certificates ;
5
+ using Titanium . Web . Proxy . Helpers ;
5
6
6
7
namespace Titanium . Web . Proxy . Network
7
8
{
@@ -13,24 +14,24 @@ internal sealed class DefaultCertificateDiskCache : ICertificateCache
13
14
private string rootCertificatePath ;
14
15
private string certificatePath ;
15
16
16
- public X509Certificate2 LoadRootCertificate ( string name , string password , X509KeyStorageFlags storageFlags )
17
+ public X509Certificate2 LoadRootCertificate ( string pathOrName , string password , X509KeyStorageFlags storageFlags )
17
18
{
18
- string filePath = getRootCertificatePath ( name ) ;
19
- return loadCertificate ( filePath , password , storageFlags ) ;
19
+ string path = getRootCertificatePath ( pathOrName ) ;
20
+ return loadCertificate ( path , password , storageFlags ) ;
20
21
}
21
22
22
- public void SaveRootCertificate ( string name , string password , X509Certificate2 certificate )
23
+ public void SaveRootCertificate ( string pathOrName , string password , X509Certificate2 certificate )
23
24
{
24
- string filePath = getRootCertificatePath ( name ) ;
25
+ string path = getRootCertificatePath ( pathOrName ) ;
25
26
byte [ ] exported = certificate . Export ( X509ContentType . Pkcs12 , password ) ;
26
- File . WriteAllBytes ( filePath , exported ) ;
27
+ File . WriteAllBytes ( path , exported ) ;
27
28
}
28
29
29
30
/// <inheritdoc />
30
31
public X509Certificate2 LoadCertificate ( string subjectName , X509KeyStorageFlags storageFlags )
31
32
{
32
- string filePath = Path . Combine ( getCertificatePath ( ) , subjectName + defaultCertificateFileExtension ) ;
33
- return loadCertificate ( filePath , string . Empty , storageFlags ) ;
33
+ string path = Path . Combine ( getCertificatePath ( ) , subjectName + defaultCertificateFileExtension ) ;
34
+ return loadCertificate ( path , string . Empty , storageFlags ) ;
34
35
}
35
36
36
37
/// <inheritdoc />
@@ -55,12 +56,12 @@ public void Clear()
55
56
certificatePath = null ;
56
57
}
57
58
58
- private X509Certificate2 loadCertificate ( string filePath , string password , X509KeyStorageFlags storageFlags )
59
+ private X509Certificate2 loadCertificate ( string path , string password , X509KeyStorageFlags storageFlags )
59
60
{
60
61
byte [ ] exported ;
61
62
try
62
63
{
63
- exported = File . ReadAllBytes ( filePath ) ;
64
+ exported = File . ReadAllBytes ( path ) ;
64
65
}
65
66
catch ( IOException )
66
67
{
@@ -71,15 +72,15 @@ private X509Certificate2 loadCertificate(string filePath, string password, X509K
71
72
return new X509Certificate2 ( exported , password , storageFlags ) ;
72
73
}
73
74
74
- private string getRootCertificatePath ( string filePath )
75
+ private string getRootCertificatePath ( string pathOrName )
75
76
{
76
- if ( Path . IsPathRooted ( filePath ) )
77
+ if ( Path . IsPathRooted ( pathOrName ) )
77
78
{
78
- return filePath ;
79
+ return pathOrName ;
79
80
}
80
81
81
82
return Path . Combine ( getRootCertificateDirectory ( ) ,
82
- string . IsNullOrEmpty ( filePath ) ? defaultRootCertificateFileName : filePath ) ;
83
+ string . IsNullOrEmpty ( pathOrName ) ? defaultRootCertificateFileName : pathOrName ) ;
83
84
}
84
85
85
86
private string getCertificatePath ( )
@@ -104,17 +105,32 @@ private string getRootCertificateDirectory()
104
105
{
105
106
if ( rootCertificatePath == null )
106
107
{
107
- string assemblyLocation = GetType ( ) . Assembly . Location ;
108
-
109
- // dynamically loaded assemblies returns string.Empty location
110
- if ( assemblyLocation == string . Empty )
108
+ if ( RunTime . IsUwpOnWindows )
109
+ {
110
+ rootCertificatePath = Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) ;
111
+ }
112
+ else if ( RunTime . IsLinux )
111
113
{
112
- assemblyLocation = Assembly . GetEntryAssembly ( ) . Location ;
114
+ rootCertificatePath = Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ;
113
115
}
116
+ else if ( RunTime . IsMac )
117
+ {
118
+ rootCertificatePath = Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ;
119
+ }
120
+ else
121
+ {
122
+ string assemblyLocation = GetType ( ) . Assembly . Location ;
114
123
115
- string path = Path . GetDirectoryName ( assemblyLocation ) ;
124
+ // dynamically loaded assemblies returns string.Empty location
125
+ if ( assemblyLocation == string . Empty )
126
+ {
127
+ assemblyLocation = Assembly . GetEntryAssembly ( ) . Location ;
128
+ }
116
129
117
- rootCertificatePath = path ?? throw new NullReferenceException ( ) ;
130
+ string path = Path . GetDirectoryName ( assemblyLocation ) ;
131
+
132
+ rootCertificatePath = path ?? throw new NullReferenceException ( ) ;
133
+ }
118
134
}
119
135
120
136
return rootCertificatePath ;
0 commit comments