Просмотр исходного кода

fix: externalRepresentation condition to validate if key is private should be d not prime

I got to this issue recently where the library was giving me a public key DER when the key had a d and no primes,
this is misleading since an RSA key can have d and no primes.
This function should acess if the key is private through the d, if it requires the primes it should return an error.

As part of this PR I also made the primes public, they are already a let, making them public makes the API more
accessible to deal for example if I want to build a JWK with primes included.
Goncalo Frade 1 год назад
Родитель
Сommit
cd7d4abee2
1 измененных файлов с 2 добавлено и 2 удалено
  1. 2 2
      Sources/CryptoSwift/RSA/RSA.swift

+ 2 - 2
Sources/CryptoSwift/RSA/RSA.swift

@@ -61,7 +61,7 @@ public final class RSA: DERCodable {
   public let keySizeBytes: Int
 
   /// The underlying primes used to generate the Private Exponent
-  private let primes: (p: BigUInteger, q: BigUInteger)?
+  public let primes: (p: BigUInteger, q: BigUInteger)?
 
   /// Initialize with RSA parameters
   /// - Parameters:
@@ -388,7 +388,7 @@ extension RSA {
   /// ```
   ///
   public func externalRepresentation() throws -> Data {
-    if self.primes != nil {
+    if self.d != nil {
       return try Data(self.privateKeyDER())
     } else {
       return try Data(self.publicKeyDER())