-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compat] implement PKey::RSA public_to_der and public_to_pem
- Loading branch information
Showing
3 changed files
with
136 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,7 @@ | |
import static org.jruby.ext.openssl.impl.PKey.readRSAPublicKey; | ||
import static org.jruby.ext.openssl.impl.PKey.toASN1Primitive; | ||
import static org.jruby.ext.openssl.impl.PKey.toDerRSAKey; | ||
import static org.jruby.ext.openssl.impl.PKey.toDerRSAPublicKey; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Ola Bini</a> | ||
|
@@ -356,6 +357,21 @@ public RubyBoolean private_p() { | |
return getRuntime().newBoolean(isPrivateKey()); | ||
} | ||
|
||
@JRubyMethod(name = "public_to_der") | ||
public RubyString public_to_der(ThreadContext context) { | ||
final byte[] bytes; | ||
try { | ||
bytes = toDerRSAPublicKey(publicKey); | ||
} | ||
catch (NoClassDefFoundError e) { | ||
throw newRSAError(context.runtime, bcExceptionMessage(e)); | ||
} | ||
catch (Exception e) { | ||
throw newRSAError(getRuntime(), e.getMessage(), e); | ||
} | ||
return StringHelper.newString(context.runtime, bytes); | ||
} | ||
|
||
@Override | ||
@JRubyMethod(name = "to_der") | ||
public RubyString to_der() { | ||
|
@@ -366,8 +382,8 @@ public RubyString to_der() { | |
catch (NoClassDefFoundError e) { | ||
throw newRSAError(getRuntime(), bcExceptionMessage(e)); | ||
} | ||
catch (IOException e) { | ||
throw newRSAError(getRuntime(), e.getMessage()); | ||
catch (Exception e) { | ||
throw newRSAError(getRuntime(), e.getMessage(), e); | ||
} | ||
return StringHelper.newString(getRuntime(), bytes); | ||
} | ||
|
@@ -470,6 +486,21 @@ public RubyString to_pem(ThreadContext context, final IRubyObject[] args) { | |
} | ||
} | ||
|
||
@JRubyMethod | ||
public RubyString public_to_pem(ThreadContext context) { | ||
try { | ||
final StringWriter writer = new StringWriter(); | ||
PEMInputOutput.writeRSAPublicKey(writer, publicKey); | ||
return RubyString.newString(context.runtime, writer.getBuffer()); | ||
} | ||
catch (NoClassDefFoundError ncdfe) { | ||
throw newRSAError(context.runtime, bcExceptionMessage(ncdfe)); | ||
} | ||
catch (IOException ioe) { | ||
throw newRSAError(context.runtime, ioe.getMessage()); | ||
} | ||
} | ||
|
||
private String getPadding(final int padding) { | ||
if ( padding < 1 || padding > 4 ) { | ||
throw newRSAError(getRuntime(), ""); | ||
|
@@ -745,6 +776,7 @@ public IRubyObject set_key(final ThreadContext context, IRubyObject n, IRubyObje | |
this.rsa_n = BN.getBigInteger(n); | ||
this.rsa_e = BN.getBigInteger(e); | ||
this.rsa_d = BN.getBigInteger(d); | ||
generatePublicKeyIfParams(context); | ||
generatePrivateKeyIfParams(context); | ||
return this; | ||
} | ||
|
@@ -769,8 +801,6 @@ public IRubyObject set_crt_params(final ThreadContext context, IRubyObject dmp1, | |
private void generatePublicKeyIfParams(final ThreadContext context) { | ||
final Ruby runtime = context.runtime; | ||
|
||
if ( publicKey != null ) throw newRSAError(runtime, "illegal modification"); | ||
|
||
// Don't access the rsa_n and rsa_e fields directly. They may have | ||
// already been consumed and cleared by generatePrivateKeyIfParams. | ||
BigInteger _rsa_n = getModulus(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters