55// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
66// and limitations under the License.
77
8- using System ;
9-
108using Keyfactor . Logging ;
11- using Keyfactor . Orchestrators . Extensions ;
129using Keyfactor . Orchestrators . Common . Enums ;
10+ using Keyfactor . Orchestrators . Extensions ;
1311using Keyfactor . PKI . Extensions ;
14-
15- using Org . BouncyCastle . X509 ;
16-
1712using Microsoft . Extensions . Logging ;
18-
1913using Newtonsoft . Json ;
2014using Org . BouncyCastle . Pkcs ;
15+ using Org . BouncyCastle . X509 ;
16+ using System ;
2117using System . IO ;
18+ using static Org . BouncyCastle . Math . EC . ECCurve ;
2219
2320namespace Keyfactor . Extensions . Orchestrator . RemoteFile
2421{
@@ -50,21 +47,11 @@ public JobResult ProcessJob(ManagementJobConfiguration config)
5047 if ( ! certificateStore . DoesStoreExist ( ) )
5148 {
5249 if ( ApplicationSettings . CreateStoreIfMissing )
53- CreateStore ( certificateStoreSerializer , config ) ;
50+ CreateStore ( certificateStoreSerializer , config , logger ) ;
5451 else
5552 throw new RemoteFileException ( $ "Certificate store { config . CertificateStoreDetails . StorePath } does not exist on server { config . CertificateStoreDetails . ClientMachine } .") ;
5653 }
5754 certificateStore . LoadCertificateStore ( certificateStoreSerializer , false ) ;
58-
59- using ( MemoryStream ms = new MemoryStream ( Convert . FromBase64String ( config . JobCertificate . Contents ) ) )
60- {
61- Pkcs12StoreBuilder storeBuilder = new Pkcs12StoreBuilder ( ) ;
62- Pkcs12Store store = storeBuilder . Build ( ) ;
63-
64- store . Load ( ms , config . JobCertificate . PrivateKeyPassword . ToCharArray ( ) ) ;
65-
66- store . Aliases [ 0 ]
67- }
6855 certificateStore . AddCertificate ( ( config . JobCertificate . Alias ?? new X509Certificate ( ) , config . JobCertificate . Contents , config . Overwrite , config . JobCertificate . PrivateKeyPassword , RemoveRootCertificate ) ;
6956 certificateStore . SaveCertificateStore ( certificateStoreSerializer . SerializeRemoteCertificateStore ( certificateStore . GetCertificateStore ( ) , storePathFile . Path , storePathFile . File , StorePassword , certificateStore . RemoteHandler ) ) ;
7057
@@ -119,8 +106,10 @@ public JobResult ProcessJob(ManagementJobConfiguration config)
119106 return new JobResult ( ) { Result = OrchestratorJobStatusJobResult . Success , JobHistoryId = config . JobHistoryId } ;
120107 }
121108
122- private void CreateStore ( ICertificateStoreSerializer certificateStoreSerializer , ManagementJobConfiguration config )
109+ private void CreateStore ( ICertificateStoreSerializer certificateStoreSerializer , ManagementJobConfiguration config , ILogger logger )
123110 {
111+ logger . MethodEntry ( LogLevel . Debug ) ;
112+
124113 dynamic properties = JsonConvert . DeserializeObject ( config . CertificateStoreDetails . Properties . ToString ( ) ) ;
125114 string linuxFilePermissions = properties . LinuxFilePermissionsOnStoreCreation == null || string . IsNullOrEmpty ( properties . LinuxFilePermissionsOnStoreCreation . Value ) ?
126115 ApplicationSettings . DefaultLinuxPermissionsOnStoreCreation :
@@ -131,6 +120,32 @@ private void CreateStore(ICertificateStoreSerializer certificateStoreSerializer,
131120 properties . LinuxFileOwnerOnStoreCreation . Value ;
132121
133122 certificateStore . CreateCertificateStore ( certificateStoreSerializer , config . CertificateStoreDetails . StorePath , linuxFilePermissions , linuxFileOwner ) ;
123+
124+ logger . MethodExit ( LogLevel . Debug ) ;
125+ }
126+
127+ private string GetThumbprint ( ManagementJobCertificate jobCertificate , ILogger logger )
128+ {
129+ logger . MethodEntry ( LogLevel . Debug ) ;
130+
131+ string thumbprint = string . Empty ;
132+
133+ using ( MemoryStream ms = new MemoryStream ( Convert . FromBase64String ( jobCertificate . Contents ) ) )
134+ {
135+ Pkcs12StoreBuilder storeBuilder = new Pkcs12StoreBuilder ( ) ;
136+ Pkcs12Store store = storeBuilder . Build ( ) ;
137+
138+ store . Load ( ms , jobCertificate . PrivateKeyPassword . ToCharArray ( ) ) ;
139+
140+ foreach ( string alias in store . Aliases )
141+ {
142+ thumbprint = store . GetCertificate ( alias ) . Certificate . Thumbprint ( ) ;
143+ break ;
144+ }
145+ }
146+
147+ logger . MethodExit ( LogLevel . Debug ) ;
148+ return thumbprint ;
134149 }
135150 }
136151}
0 commit comments