1
- namespace k8s
2
- {
3
- using System ;
4
- using System . Diagnostics ;
5
- using System . Globalization ;
6
- using System . IO ;
7
- using System . Runtime . InteropServices ;
8
- using System . Security . Cryptography ;
9
- using System . Security . Cryptography . X509Certificates ;
10
- using System . Text ;
11
- using System . Threading . Tasks ;
12
-
13
- using Org . BouncyCastle . Crypto ;
14
- using Org . BouncyCastle . Crypto . Parameters ;
15
- using Org . BouncyCastle . Security ;
16
- using Org . BouncyCastle . OpenSsl ;
1
+ using System ;
2
+ using System . IO ;
3
+ using System . Security . Cryptography . X509Certificates ;
4
+ using System . Text ;
5
+ using k8s . Exceptions ;
6
+ using Org . BouncyCastle . Crypto ;
7
+ using Org . BouncyCastle . Crypto . Parameters ;
8
+ using Org . BouncyCastle . OpenSsl ;
9
+ using Org . BouncyCastle . Pkcs ;
10
+ using Org . BouncyCastle . Security ;
11
+ using Org . BouncyCastle . X509 ;
17
12
13
+ namespace k8s
14
+ {
18
15
public static class Utils
19
16
{
20
17
/// <summary>
21
- /// Encode string in base64 format.
18
+ /// Encode string in base64 format.
22
19
/// </summary>
23
20
/// <param name="text">string to be encoded.</param>
24
21
/// <returns>Encoded string.</returns>
@@ -28,7 +25,7 @@ public static string Base64Encode(string text)
28
25
}
29
26
30
27
/// <summary>
31
- /// Encode string in base64 format.
28
+ /// Encode string in base64 format.
32
29
/// </summary>
33
30
/// <param name="text">string to be encoded.</param>
34
31
/// <returns>Encoded string.</returns>
@@ -38,16 +35,15 @@ public static string Base64Decode(string text)
38
35
}
39
36
40
37
/// <summary>
41
- /// Generates pfx from client configuration
38
+ /// Generates pfx from client configuration
42
39
/// </summary>
43
40
/// <param name="config">Kuberentes Client Configuration</param>
44
41
/// <returns>Generated Pfx Path</returns>
45
42
public static X509Certificate2 GeneratePfx ( KubernetesClientConfiguration config )
46
43
{
47
- var keyData = new byte [ ] { } ;
48
- var certData = new byte [ ] { } ;
44
+ byte [ ] keyData = null ;
45
+ byte [ ] certData = null ;
49
46
50
- var filePrefix = config . CurrentContext ;
51
47
if ( ! string . IsNullOrWhiteSpace ( config . ClientCertificateKey ) )
52
48
{
53
49
keyData = Convert . FromBase64String ( config . ClientCertificateKey ) ;
@@ -57,6 +53,11 @@ public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
57
53
keyData = File . ReadAllBytes ( config . ClientKey ) ;
58
54
}
59
55
56
+ if ( keyData == null )
57
+ {
58
+ throw new KubeConfigException ( "certData is empty" ) ;
59
+ }
60
+
60
61
if ( ! string . IsNullOrWhiteSpace ( config . ClientCertificateData ) )
61
62
{
62
63
certData = Convert . FromBase64String ( config . ClientCertificateData ) ;
@@ -66,23 +67,35 @@ public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
66
67
certData = File . ReadAllBytes ( config . ClientCertificate ) ;
67
68
}
68
69
69
- var cert = new X509Certificate2 ( certData ) ;
70
- return addPrivateKey ( cert , keyData ) ;
71
- }
70
+ if ( certData == null )
71
+ {
72
+ throw new KubeConfigException ( "certData is empty" ) ;
73
+ }
72
74
73
- public static X509Certificate2 addPrivateKey ( X509Certificate2 cert , byte [ ] keyData )
74
- {
75
+ var cert = new X509CertificateParser ( ) . ReadCertificate ( new MemoryStream ( certData ) ) ;
76
+
77
+ object obj ;
75
78
using ( var reader = new StreamReader ( new MemoryStream ( keyData ) ) )
76
79
{
77
- var obj = new PemReader ( reader ) . ReadObject ( ) ;
78
- if ( obj is AsymmetricCipherKeyPair ) {
79
- var cipherKey = ( AsymmetricCipherKeyPair ) obj ;
80
+ obj = new PemReader ( reader ) . ReadObject ( ) ;
81
+ var key = obj as AsymmetricCipherKeyPair ;
82
+ if ( key != null )
83
+ {
84
+ var cipherKey = key ;
80
85
obj = cipherKey . Private ;
81
86
}
82
- var rsaKeyParams = ( RsaPrivateCrtKeyParameters ) obj ;
83
- var rsaKey = RSA . Create ( DotNetUtilities . ToRSAParameters ( rsaKeyParams ) ) ;
84
- return cert . CopyWithPrivateKey ( rsaKey ) ;
87
+ }
88
+
89
+ var rsaKeyParams = ( RsaPrivateCrtKeyParameters ) obj ;
90
+
91
+ var store = new Pkcs12StoreBuilder ( ) . Build ( ) ;
92
+ store . SetKeyEntry ( "K8SKEY" , new AsymmetricKeyEntry ( rsaKeyParams ) , new [ ] { new X509CertificateEntry ( cert ) } ) ;
93
+
94
+ using ( var pkcs = new MemoryStream ( ) )
95
+ {
96
+ store . Save ( pkcs , new char [ 0 ] , new SecureRandom ( ) ) ;
97
+ return new X509Certificate2 ( pkcs . ToArray ( ) ) ;
85
98
}
86
99
}
87
100
}
88
- }
101
+ }
0 commit comments