title: How to use openssl to emulate the Java Signature sign method?
body: <p>I'm working with a server that requires that part of the login URL include authentification. The way this scheme works (as recommended in the manuals) is that</p>
  
  <ul>
  <li>a public / private key pair is generated using Java JDK keytool (which creates them in a Java keystore)</li>
  <li>a certificate is generated for the key pair using Java JDK keytool</li>
  <li>the certificate is installed on the server.</li>
  </ul>
  
  <p>When logging on the client (my code)</p>
  
  <ul>
  <li>generates a plaintext token string</li>
  <li>signs the plaintext token string with the private key using SHA1 and DSA</li>
  <li>includes the signed token string in the login URL.</li>
  </ul>
  
  <p>The Java keytool does not provide a mechanism to export the private key from the keystore, but separately I have Java code to extract the private key from the keystore and save it in  a file - based on <a href="https://stackoverflow.com/questions/150167/how-do-i-list-export-private-keys-from-a-keystore">How do I list / export private keys from a keystore?</a>.</p>
  
  <p>This all works when the client uses Java to do the signing, with code like the following.</p>
  
  <pre><code>String plaintext = "This is a sample plaintext token string";
  Signature instance = Signature.getInstance("SHA1withDSA");
  instance.initSign(privateKey);
  instance.update((plaintext).getBytes());
  byte[] signature = instance.sign();
  </code></pre>
  
  <p>It also works when the client uses PHP to do the signing, with code like the following. Here the private key is retrieved from the Java keystore file in PHP.]</p>
  
  <pre><code>$privateKey = openssl_pkey_get_private("file://$keyfile", $keystorePassword);
  openssl_sign($paramsEncoded, $signature, $privateKey, OPENSSL_ALGO_DSS1))
  </code></pre>
  
  <p>However I now have a situation where a customer wants to construct the login URL using a Bash script and openssl - and that does not work. My most current version of that code looks like the following, which does a SHA1 message digest and then a DSA sign. But the server rejects the token.</p>
  
  <pre><code>echo $tokenString | openssl dgst -sha1 &gt; tokendigest
  openssl dgst -dss1 -passin pass:$storePassword -sign $privateKeyFile &gt; tokensigned
  </code></pre>
  
  <p>I came across this post (<a href="https://stackoverflow.com/questions/521101/using-sha1-and-rsa-with-java-security-signature-vs-messagedigest-and-cipher">Using SHA1 and RSA with java.security.Signature vs. MessageDigest and Cipher</a>) which suggests that the Java Signature sign method doesn't sign the digest, but rather signs the concatenation of the digest algorithm ID and the digest. After stepping through the code in the post it appears that (for SHA1) the digest needs to be prefixed with the bytes <code>48 33 48 9 6 5 43 14 3 2 26 5 0 4 20</code>. But even after adding that I still can't get opensll to produce a signed token that is acceptable to the server.</p>
  
  <p>Does anyone know how to use openssl to emulate the Java Signature sign method?</p>