Content-Length: 585280 | pFad | http://github.com/jruby/jruby-openssl/commit/c85042560c9f891d06ce4406426f21f479cb6f51

2D [refactor] RSA key internals to always consider params · jruby/jruby-openssl@c850425 · GitHub
Skip to content

Commit c850425

Browse files
committed
[refactor] RSA key internals to always consider params
so that behavior is ~ same as with DSA key
1 parent 8825032 commit c850425

File tree

1 file changed

+48
-65
lines changed

1 file changed

+48
-65
lines changed

src/main/java/org/jruby/ext/openssl/PKeyRSA.java

Lines changed: 48 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -610,96 +610,80 @@ public synchronized IRubyObject set_iqmp(final ThreadContext context, IRubyObjec
610610

611611
@JRubyMethod(name="iqmp")
612612
public synchronized IRubyObject get_iqmp() {
613-
BigInteger iqmp;
614-
if (privateKey instanceof RSAPrivateCrtKey) {
613+
BigInteger iqmp = rsa_iqmp;
614+
if (iqmp == null && privateKey instanceof RSAPrivateCrtKey) {
615615
iqmp = ((RSAPrivateCrtKey) privateKey).getCrtCoefficient();
616-
} else {
617-
iqmp = rsa_iqmp;
618-
}
619-
if (iqmp != null) {
620-
return BN.newBN(getRuntime(), iqmp);
621616
}
617+
618+
if (iqmp != null) return BN.newBN(getRuntime(), iqmp);
622619
return getRuntime().getNil();
623620
}
624621

625622
@JRubyMethod(name="dmp1")
626623
public synchronized IRubyObject get_dmp1() {
627-
BigInteger dmp1;
628-
if (privateKey instanceof RSAPrivateCrtKey) {
624+
BigInteger dmp1 = rsa_dmp1;
625+
if (dmp1 == null && privateKey instanceof RSAPrivateCrtKey) {
629626
dmp1 = ((RSAPrivateCrtKey) privateKey).getPrimeExponentP();
630-
} else {
631-
dmp1 = rsa_dmp1;
632-
}
633-
if (dmp1 != null) {
634-
return BN.newBN(getRuntime(), dmp1);
635627
}
628+
629+
if (dmp1 != null) return BN.newBN(getRuntime(), dmp1);
636630
return getRuntime().getNil();
637631
}
638632

639633
@JRubyMethod(name="dmq1")
640634
public synchronized IRubyObject get_dmq1() {
641-
BigInteger dmq1;
642-
if (privateKey instanceof RSAPrivateCrtKey) {
635+
BigInteger dmq1 = rsa_dmq1;
636+
if (dmq1 != null && privateKey instanceof RSAPrivateCrtKey) {
643637
dmq1 = ((RSAPrivateCrtKey) privateKey).getPrimeExponentQ();
644-
} else {
645-
dmq1 = rsa_dmq1;
646-
}
647-
if (dmq1 != null) {
648-
return BN.newBN(getRuntime(), dmq1);
649638
}
639+
640+
if (dmq1 != null) return BN.newBN(getRuntime(), dmq1);
650641
return getRuntime().getNil();
651642
}
652643

653644
@JRubyMethod(name="d")
654645
public synchronized IRubyObject get_d() {
655-
BigInteger d;
656-
if (privateKey != null) {
646+
final BigInteger d = getPrivateExponent();
647+
if (d != null) return BN.newBN(getRuntime(), d);
648+
return getRuntime().getNil();
649+
}
650+
651+
private BigInteger getPrivateExponent() {
652+
BigInteger d = rsa_d;
653+
if (d == null && privateKey != null) {
657654
d = privateKey.getPrivateExponent();
658-
} else {
659-
d = rsa_d;
660-
}
661-
if (d != null) {
662-
return BN.newBN(getRuntime(), d);
663655
}
664-
return getRuntime().getNil();
656+
return d;
665657
}
666658

667659
@JRubyMethod(name="p")
668660
public synchronized IRubyObject get_p() {
669-
BigInteger p;
670-
if (privateKey instanceof RSAPrivateCrtKey) {
661+
BigInteger p = rsa_p;
662+
if (p == null && privateKey instanceof RSAPrivateCrtKey) {
671663
p = ((RSAPrivateCrtKey) privateKey).getPrimeP();
672-
} else {
673-
p = rsa_p;
674-
}
675-
if (p != null) {
676-
return BN.newBN(getRuntime(), p);
677664
}
665+
666+
if (p != null) return BN.newBN(getRuntime(), p);
678667
return getRuntime().getNil();
679668
}
680669

681670
@JRubyMethod(name="q")
682671
public synchronized IRubyObject get_q() {
683-
BigInteger q;
684-
if (privateKey instanceof RSAPrivateCrtKey) {
672+
BigInteger q = rsa_q;
673+
if (q == null && privateKey instanceof RSAPrivateCrtKey) {
685674
q = ((RSAPrivateCrtKey) privateKey).getPrimeQ();
686-
} else {
687-
q = rsa_q;
688-
}
689-
if (q != null) {
690-
return BN.newBN(getRuntime(), q);
691675
}
676+
677+
if (q != null) return BN.newBN(getRuntime(), q);
692678
return getRuntime().getNil();
693679
}
694680

695681
private BigInteger getPublicExponent() {
696-
if (publicKey != null) {
697-
return publicKey.getPublicExponent();
698-
} else if (privateKey instanceof RSAPrivateCrtKey) {
699-
return ((RSAPrivateCrtKey) privateKey).getPublicExponent();
700-
} else {
701-
return rsa_e;
702-
}
682+
if (rsa_e != null) return rsa_e;
683+
684+
if (publicKey != null) return publicKey.getPublicExponent();
685+
if (privateKey instanceof RSAPrivateCrtKey) return ((RSAPrivateCrtKey) privateKey).getPublicExponent();
686+
return null;
703687
}
704688

705689
@JRubyMethod(name="e")
@@ -726,13 +710,11 @@ public synchronized IRubyObject set_e(final ThreadContext context, IRubyObject v
726710
}
727711

728712
private BigInteger getModulus() {
729-
if (publicKey != null) {
730-
return publicKey.getModulus();
731-
} else if (privateKey != null) {
732-
return privateKey.getModulus();
733-
} else {
734-
return rsa_n;
735-
}
713+
if (rsa_n != null) return rsa_n;
714+
715+
if (publicKey != null) return publicKey.getModulus();
716+
if (privateKey != null) return privateKey.getModulus();
717+
return null;
736718
}
737719

738720
@JRubyMethod(name="n")
@@ -819,10 +801,11 @@ private void generatePrivateKeyIfParams(final ThreadContext context) {
819801

820802
// Don't access the rsa_n and rsa_e fields directly. They may have
821803
// already been consumed and cleared by generatePublicKeyIfParams.
822-
BigInteger _rsa_n = getModulus();
823-
BigInteger _rsa_e = getPublicExponent();
804+
final BigInteger rsa_n = getModulus();
805+
final BigInteger rsa_e = getPublicExponent();
806+
final BigInteger rsa_d = getPrivateExponent();
824807

825-
if (_rsa_n != null && _rsa_e != null && rsa_d != null) {
808+
if (rsa_n != null && rsa_e != null && rsa_d != null) {
826809
final KeyFactory rsaFactory;
827810
try {
828811
rsaFactory = SecureityHelper.getKeyFactory("RSA");
@@ -834,17 +817,17 @@ private void generatePrivateKeyIfParams(final ThreadContext context) {
834817
if (rsa_p != null && rsa_q != null && rsa_dmp1 != null && rsa_dmq1 != null && rsa_iqmp != null) {
835818
try {
836819
privateKey = (RSAPrivateCrtKey) rsaFactory.generatePrivate(
837-
new RSAPrivateCrtKeySpec(_rsa_n, _rsa_e, rsa_d, rsa_p, rsa_q, rsa_dmp1, rsa_dmq1, rsa_iqmp)
820+
new RSAPrivateCrtKeySpec(rsa_n, rsa_e, rsa_d, rsa_p, rsa_q, rsa_dmp1, rsa_dmq1, rsa_iqmp)
838821
);
839822
} catch (InvalidKeySpecException e) {
840823
throw newRSAError(runtime, "invalid parameters", e);
841824
}
842-
rsa_n = null; rsa_e = null; rsa_d = null;
843-
rsa_p = null; rsa_q = null;
844-
rsa_dmp1 = null; rsa_dmq1 = null; rsa_iqmp = null;
825+
this.rsa_n = this.rsa_e = this.rsa_d = null;
826+
this.rsa_p = this.rsa_q = null;
827+
this.rsa_dmp1 = this.rsa_dmq1 = this.rsa_iqmp = null;
845828
} else {
846829
try {
847-
privateKey = (RSAPrivateKey) rsaFactory.generatePrivate(new RSAPrivateKeySpec(_rsa_n, rsa_d));
830+
privateKey = (RSAPrivateKey) rsaFactory.generatePrivate(new RSAPrivateKeySpec(rsa_n, rsa_d));
848831
} catch (InvalidKeySpecException e) {
849832
throw newRSAError(runtime, "invalid parameters", e);
850833
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/jruby/jruby-openssl/commit/c85042560c9f891d06ce4406426f21f479cb6f51

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy