|
45 | 45 | import org.jruby.RubyClass; |
46 | 46 | import org.jruby.RubyModule; |
47 | 47 | import org.jruby.RubyObject; |
| 48 | +import org.jruby.RubyString; |
48 | 49 | import org.jruby.anno.JRubyMethod; |
49 | 50 | import org.jruby.exceptions.RaiseException; |
50 | 51 | import org.jruby.ext.openssl.impl.Base64; |
@@ -163,9 +164,63 @@ private byte[] toDER() throws IOException { |
163 | 164 | } |
164 | 165 |
|
165 | 166 | @JRubyMethod |
166 | | - public IRubyObject to_text() { |
167 | | - warn(getRuntime().getCurrentContext(), "WARNING: unimplemented method called: Netscape::SPKI#to_text"); |
168 | | - return getRuntime().getNil(); |
| 167 | + public IRubyObject to_text(ThreadContext context) { |
| 168 | + final Ruby runtime = context.runtime; |
| 169 | + |
| 170 | + final StringBuilder text = new StringBuilder(256); |
| 171 | + text.append("Netscape SPKI:\n"); |
| 172 | + |
| 173 | + final NetscapeCertRequest cert = (NetscapeCertRequest) this.cert; |
| 174 | + if (cert == null) return StringHelper.newString(runtime, text); |
| 175 | + |
| 176 | + // public key algorithm |
| 177 | + final AlgorithmIdentifier keyAlg = cert.getKeyAlgorithm(); |
| 178 | + final String keyAlgName = resolveAlgorithmName(runtime, keyAlg); |
| 179 | + text.append(" Public Key Algorithm: ").append(keyAlgName).append('\n'); |
| 180 | + |
| 181 | + if (public_key instanceof PKey) { |
| 182 | + try { |
| 183 | + final RubyString keyText = ((PKey) public_key).to_text(); |
| 184 | + for (CharSequence line : StringHelper.split(keyText, '\n')) { |
| 185 | + text.append(" ").append(line).append('\n'); |
| 186 | + } |
| 187 | + } catch (Exception e) { |
| 188 | + text.append(" Unable to load public key\n"); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + final String challenge = cert.getChallenge(); |
| 193 | + if (challenge != null && !challenge.isEmpty()) { |
| 194 | + text.append(" Challenge String: ").append(challenge).append('\n'); |
| 195 | + } |
| 196 | + |
| 197 | + final AlgorithmIdentifier sigAlg = cert.getSigningAlgorithm(); |
| 198 | + final String sigAlgName = resolveAlgorithmName(runtime, sigAlg); |
| 199 | + text.append(" Signature Algorithm: ").append(sigAlgName); |
| 200 | + |
| 201 | + // signature bytes as hex with : separators, 18 bytes per line |
| 202 | + final byte[] sig = cert.getSignatureBits(); |
| 203 | + if (sig != null) { |
| 204 | + for (int i = 0; i < sig.length; i++) { |
| 205 | + if (i % 18 == 0) text.append("\n "); |
| 206 | + text.append(String.format("%02x", sig[i] & 0xFF)); |
| 207 | + if (i + 1 < sig.length) text.append(':'); |
| 208 | + } |
| 209 | + } |
| 210 | + text.append('\n'); |
| 211 | + |
| 212 | + return StringHelper.newString(runtime, text); |
| 213 | + } |
| 214 | + |
| 215 | + private static String resolveAlgorithmName(final Ruby runtime, final AlgorithmIdentifier algId) { |
| 216 | + if (algId == null) return null; |
| 217 | + try { |
| 218 | + final String name = ASN1.oid2name(runtime, algId.getAlgorithm(), true); |
| 219 | + if (name != null) return name; |
| 220 | + } catch (RuntimeException e) { |
| 221 | + debug("Failed to resolve algorithm name: " + algId, e); |
| 222 | + } |
| 223 | + return algId.getAlgorithm().getId(); |
169 | 224 | } |
170 | 225 |
|
171 | 226 | @JRubyMethod |
|
0 commit comments