@@ -5,7 +5,8 @@ use std::ffi::c_void;
5
5
use crate :: {
6
6
CertificateDer , CertificateHash , CertificateReference , CertificateReferenceIdOrHash ,
7
7
DecryptMessageError , DecryptedMessage , EncryptMessageError , EncryptedMessage ,
8
- GetDerEncodedCertificateError , SignMessageError , SignedMessage ,
8
+ EncryptionAlgorithm , GetDerEncodedCertificateError , SignMessageError , SignatureAlgorithm ,
9
+ SignedMessage ,
9
10
} ;
10
11
use bytes:: Bytes ;
11
12
use schannel:: {
@@ -221,18 +222,19 @@ pub fn sign_message(
221
222
let reference = get_id_and_hash_from_cert_context ( & cert_context)
222
223
. map_err ( SignMessageError :: CannotGetCertificateInfo ) ?;
223
224
let keypair = get_keypair ( & cert_context) ?;
224
- let signed_message = match keypair {
225
+ let ( algo , signed_message) = match keypair {
225
226
// We do not support a CryptoAPI provider as its API is marked for depreciation by windows.
226
227
PrivateKey :: CryptProv ( ..) => {
227
228
todo ! ( "Use CryptGetUserKey to get the keypair" )
228
229
}
229
230
// Handle to a CryptoGraphy Next Generation (CNG) API
230
- PrivateKey :: NcryptKey ( handle) => ncrypt_sign_message ( message, & handle) . map ( Into :: into ) ,
231
+ PrivateKey :: NcryptKey ( handle) => ncrypt_sign_message_with_rsa ( message, & handle) ,
231
232
}
232
233
. map_err ( SignMessageError :: CannotSign ) ?;
233
234
234
235
Ok ( SignedMessage {
235
- signed_message,
236
+ algo,
237
+ signed_message : signed_message. into ( ) ,
236
238
cert_ref : reference,
237
239
} )
238
240
}
@@ -246,7 +248,11 @@ fn get_keypair(context: &CertContext) -> Result<PrivateKey, crate::errors::BaseK
246
248
. map_err ( crate :: errors:: BaseKeyPairError :: CannotAcquireKeypair )
247
249
}
248
250
249
- fn ncrypt_sign_message ( message : & [ u8 ] , handle : & NcryptKey ) -> std:: io:: Result < Vec < u8 > > {
251
+ fn ncrypt_sign_message_with_rsa (
252
+ message : & [ u8 ] ,
253
+ handle : & NcryptKey ,
254
+ ) -> std:: io:: Result < ( SignatureAlgorithm , Vec < u8 > ) > {
255
+ const ALGO : SignatureAlgorithm = SignatureAlgorithm :: RsassaPssSha256 ;
250
256
let hash = sha2:: Sha256 :: digest ( message) ;
251
257
// SAFETY: NcryptKey is obtain from an NCRYPT_KEY_HANDLE, here we retrieve the underlying
252
258
// handle.
@@ -305,7 +311,7 @@ fn ncrypt_sign_message(message: &[u8], handle: &NcryptKey) -> std::io::Result<Ve
305
311
if res != 0 {
306
312
return Err ( std:: io:: Error :: last_os_error ( ) ) ;
307
313
}
308
- Ok ( buff)
314
+ Ok ( ( ALGO , buff) )
309
315
}
310
316
}
311
317
@@ -319,23 +325,28 @@ pub fn encrypt_message(
319
325
let reference = get_id_and_hash_from_cert_context ( & cert_context)
320
326
. map_err ( EncryptMessageError :: CannotGetCertificateInfo ) ?;
321
327
let keypair = get_keypair ( & cert_context) ?;
322
- let ciphered = match keypair {
328
+ let ( algo , ciphered) = match keypair {
323
329
// We do not support a CryptoAPI provider as its API is marked for depreciation by windows.
324
330
PrivateKey :: CryptProv ( ..) => {
325
331
todo ! ( "Use CryptGetUserKey to get the keypair" )
326
332
}
327
333
// Handle to a CryptoGraphy Next Generation (CNG) API
328
- PrivateKey :: NcryptKey ( handle) => ncrypt_encrypt_message ( message, & handle) . map ( Into :: into ) ,
334
+ PrivateKey :: NcryptKey ( handle) => ncrypt_encrypt_message_with_rsa ( message, & handle) ,
329
335
}
330
336
. map_err ( EncryptMessageError :: CannotEncrypt ) ?;
331
337
332
338
Ok ( EncryptedMessage {
333
- ciphered,
339
+ algo,
340
+ ciphered : ciphered. into ( ) ,
334
341
cert_ref : reference,
335
342
} )
336
343
}
337
344
338
- fn ncrypt_encrypt_message ( message : & [ u8 ] , handle : & NcryptKey ) -> std:: io:: Result < Vec < u8 > > {
345
+ fn ncrypt_encrypt_message_with_rsa (
346
+ message : & [ u8 ] ,
347
+ handle : & NcryptKey ,
348
+ ) -> std:: io:: Result < ( EncryptionAlgorithm , Vec < u8 > ) > {
349
+ const ALGO : EncryptionAlgorithm = EncryptionAlgorithm :: RsaesOaepSha256 ;
339
350
// SAFETY: NcryptKey is obtain from an NCRYPT_KEY_HANDLE, here we retrieve the underlying
340
351
// handle.
341
352
let raw_handle = unsafe { RawPointer :: as_ptr ( handle) } as NCRYPT_KEY_HANDLE ;
@@ -390,11 +401,12 @@ fn ncrypt_encrypt_message(message: &[u8], handle: &NcryptKey) -> std::io::Result
390
401
if res != 0 {
391
402
return Err ( std:: io:: Error :: last_os_error ( ) ) ;
392
403
}
393
- Ok ( buff)
404
+ Ok ( ( ALGO , buff) )
394
405
}
395
406
}
396
407
397
408
pub fn decrypt_message (
409
+ algo : EncryptionAlgorithm ,
398
410
encrypted_message : & [ u8 ] ,
399
411
certificate_ref : & CertificateReference ,
400
412
) -> Result < DecryptedMessage , DecryptMessageError > {
@@ -411,7 +423,10 @@ pub fn decrypt_message(
411
423
}
412
424
// Handle to a CryptoGraphy Next Generation (CNG) API
413
425
PrivateKey :: NcryptKey ( handle) => {
414
- ncrypt_decrypt_message ( encrypted_message, & handle) . map ( Into :: into)
426
+ if algo != EncryptionAlgorithm :: RsaesOaepSha256 {
427
+ todo ! ( "Unsupported encryption algo '{algo}'" ) ;
428
+ }
429
+ ncrypt_decrypt_message_with_rsa ( encrypted_message, & handle) . map ( Into :: into)
415
430
}
416
431
}
417
432
. map_err ( DecryptMessageError :: CannotDecrypt ) ?;
@@ -422,7 +437,7 @@ pub fn decrypt_message(
422
437
} )
423
438
}
424
439
425
- fn ncrypt_decrypt_message (
440
+ fn ncrypt_decrypt_message_with_rsa (
426
441
encrypted_message : & [ u8 ] ,
427
442
handle : & NcryptKey ,
428
443
) -> std:: io:: Result < Vec < u8 > > {
0 commit comments