4242using Org . BouncyCastle . Asn1 . X509 ;
4343using Org . BouncyCastle . Asn1 . X9 ;
4444using Org . BouncyCastle . Math . EC ;
45+ using Opc . Ua . Security . Certificates . BouncyCastle ;
4546#endif
4647
4748namespace Opc . Ua . Security . Certificates
@@ -297,35 +298,36 @@ public override ICertificateBuilderCreateForECDsaAny SetECDsaPublicKey(byte[] pu
297298 var publicKeyInfo = SubjectPublicKeyInfo . GetInstance ( asn1Obj ) ;
298299 var algParams = publicKeyInfo . Algorithm . Parameters ;
299300 var x962Params = X962Parameters . GetInstance ( algParams ) ;
300- var ecPoint = publicKeyInfo . PublicKey . GetBytes ( ) ;
301301
302302 ECParameters ecParameters = new ECParameters ( ) ;
303- X9ECParameters ecParametersAsn1 = null ;
303+
304+ var domainParameters = asymmetricPubKeyParameters . Parameters ;
305+ var q = asymmetricPubKeyParameters . Q ;
306+ // calculate keySize round up (bitLength + 7) / 8
307+ int keySizeBytes = ( domainParameters . N . BitLength + 7 ) / 8 ;
304308
305309 if ( x962Params . IsNamedCurve )
306310 {
307311 // Named
308312 var namedCurveOid = ( DerObjectIdentifier ) x962Params . Parameters ;
309313 string curveName = namedCurveOid . Id ;
310-
311314 var ecCurve = System . Security . Cryptography . ECCurve . CreateFromOid ( new Oid ( curveName ) ) ;
312315 ecParameters . Curve = ecCurve ;
313316 }
314317 else
315318 {
316- // Explicit but still need to create the curve as named since the platform does not support it's creation
317- // otherwise
318- ecParametersAsn1 = X9ECParameters . GetInstance ( x962Params . Parameters ) ;
319- ecParameters . Curve = BouncyCastle . X509Utils . IdentifyEccCurveByCoefficients ( ecParametersAsn1 . Curve . A . GetEncoded ( ) ,
320- ecParametersAsn1 . Curve . B . GetEncoded ( ) ) ; // instead of ecParametersAsn1.Curve;
321-
319+ // Explicit parameters
320+ var a = X509Utils . PadWithLeadingZeros ( domainParameters . Curve . A . ToBigInteger ( ) . ToByteArrayUnsigned ( ) , keySizeBytes ) ;
321+ var b = X509Utils . PadWithLeadingZeros ( domainParameters . Curve . B . ToBigInteger ( ) . ToByteArrayUnsigned ( ) , keySizeBytes ) ;
322+ ecParameters . Curve = BouncyCastle . X509Utils . IdentifyEccCurveByCoefficients ( a , b ) ;
322323 }
323324
324- //Extract the public key coordinates
325- var publicKeyPoint = new X9ECPoint ( ecParametersAsn1 . Curve , ecPoint ) . Point ;
325+ var x = X509Utils . PadWithLeadingZeros ( q . AffineXCoord . ToBigInteger ( ) . ToByteArrayUnsigned ( ) , keySizeBytes ) ;
326+ var y = X509Utils . PadWithLeadingZeros ( q . AffineYCoord . ToBigInteger ( ) . ToByteArrayUnsigned ( ) , keySizeBytes ) ;
327+ // Use the Q point
326328 ecParameters . Q = new System . Security . Cryptography . ECPoint {
327- X = publicKeyPoint . AffineXCoord . ToBigInteger ( ) . ToByteArrayUnsigned ( ) ,
328- Y = publicKeyPoint . AffineYCoord ? . ToBigInteger ( ) . ToByteArrayUnsigned ( )
329+ X = x ,
330+ Y = y
329331 } ;
330332
331333 m_ecdsaPublicKey . ImportParameters ( ecParameters ) ;
@@ -334,6 +336,7 @@ public override ICertificateBuilderCreateForECDsaAny SetECDsaPublicKey(byte[] pu
334336#else
335337 m_ecdsaPublicKey . ImportSubjectPublicKeyInfo ( publicKey , out bytes ) ;
336338#endif
339+ SetECCurve ( m_ecdsaPublicKey . ExportParameters ( false ) . Curve ) ;
337340 }
338341 catch ( Exception e )
339342 {
0 commit comments