title: How can I use RFC3161 (trusted) timestamps to prove the age of commits in my Git repository?
body: <h2>Updated</h2>
  
  <p>I have <a href="https://codereview.stackexchange.com/questions/15380/am-i-making-any-beginner-errors-in-the-bash-script-for-adding-trusted-timestamps">posted a script</a> I'm using for this to the StackExchange Code Review site.</p>
  
  <p>My original question for this was <strong>Is there a way I can sign a Git commit with an X.509 certificate and timestamp?</strong>.  For a while I thought I could only get things I've signed with my X.509 certificate timestamped by a trusted third party.  This is not the case.  Digital signing with an X.509 certificate and trusted time stamping are mutually exclusive.  I have updated my question to reflect this.</p>
  
  <p>As pointed out by VonC, signing Git commits with an X.509 certificate doesn't add any value.  Using a GPG key is a much better option because of Git's built in support.</p>
  
  <p>I have accepted Greg's answer because it's the closest to what I was asking for, even though my original question was a bit ambiguous.  As Greg points out, if you can prove you knew a commit hash at a certain point in time, that guarantees you knew the repository content the hash is for at that time and there's no need to store any extra data in the repository.  The timestamp data can be stored anywhere.</p>
  
  <p>It's possible to use <code>openssl</code> (v1.0.0+) and <code>curl</code> to request RFC3161 timestamps for commit hashes.</p>
  
  <h3>Request A Timestamp</h3>
  
  <p>You'll need to have a bit of info for this:</p>
  
  <ul>
  <li>URL - An RFC3161 time-stamping service</li>
  <li>REV - The revision (hash) you want a timstamp for.  Must be a full hash.</li>
  </ul>
  
  <pre class="lang-bash prettyprint-override"><code>CONTENT_TYPE="Content-Type: application/timestamp-query"
  ACCEPT_TYPE="Accept: application/timestamp-reply"
  
  openssl ts -query -cert -digest "$REV" -sha1 \
      | curl -s -H "$CONTENT_TYPE" -H "$ACCEPT_TYPE" --data-binary @- $URL
  </code></pre>
  
  <p>The above will output the signed timestamp to <code>stdout</code>.  It may also output an error if the timestamp service refuses the request.</p>
  
  <h3>Verify A Timestamp</h3>
  
  <p>This is very similar to requesting a timestamp, but you also need:</p>
  
  <ul>
  <li>CAFILE - A certificate chain from the timestamp service back to a root CA</li>
  </ul>
  
  <p>The time-stamping service should be signing timestamps with a certificate that was issued by a trusted authority.  If not, your timestamps don't have much credibility.  If you can't find or create a proper certificate chain, try using the <code>cacert.pem</code> published by <code>curl</code>.  It's <a href="http://curl.haxx.se/docs/caextract.html" rel="nofollow noreferrer">here</a>.</p>
  
  <p>The below snippet assumes an existing, signed timestamp reply is being passed to <code>stdin</code>.  It should be possible to pipe the above request directly into the below verify command.  If you store the response from the request in a variable it may be necessary to base64 encode / decode it (<code>man base64</code>).</p>
  
  <pre class="lang-bash prettyprint-override"><code>openssl ts -verify -digest "$REV" -in /dev/stdin -CAfile "$CAFILE"
  </code></pre>
  
  <p>If you examine a reply, you'll notice the request digest matches the Git revision that was used.  You can examine a plain text version of a reply with this command.</p>
  
  <pre class="lang-bash prettyprint-override"><code>openssl ts -reply -in /dev/stdin -text
  </code></pre>
  
  <p>Here is an example of a reply where I've added the Git revision at the top.</p>
  
  <pre><code>--------------------------------------------------------------------------------
  Revision: 871d715e5c072b1fbfacecc986f678214fa0b585
  --------------------------------------------------------------------------------
  Status info:
  Status: Granted.
  Status description: unspecified
  Failure info: unspecified
  
  TST info:
  Version: 1
  Policy OID: 1.3.6.1.4.1.6449.2.1.1
  Hash Algorithm: sha1
  Message data:
      0000 - 87 1d 71 5e 5c 07 2b 1f-bf ac ec c9 86 f6 78 21   ..q^\.+.......x!
      0010 - 4f a0 b5 85                                       O...
  Serial number: 0xB2EA9485C1AFF55C6FFEDC0491F257C8393DB5DC
  Time stamp: Aug 15 08:41:48 2012 GMT
  Accuracy: unspecified
  Ordering: no
  Nonce: 0x615F0BF6FCBBFE23
  TSA: DirName:/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO Time Stamping Signer
  Extensions:
  </code></pre>
  
  <h3>Other Notes</h3>
  
  <p>A lot of time-stamping services ask users to add a delay to scripted signing requests.  Make sure you find out if the service you plan to use requires it.  At the time of writing the one I'm using, Comodo, asks for a 15 second delay between scripted requests.  The only reason I chose to use Comodo is because that's who I bought my code signing certificate from.</p>
  
  <p>Git notes seems like the obvious choice for storing signed timestamp replies, but I don't quite have a complete solution to post.  I'm stuck on <a href="https://stackoverflow.com/questions/11967405/git-how-do-i-push-objects-created-with-git-hash-object-back-to-origin">this</a> at the moment.</p>
  
  <p>My original question and updates are below.</p>
  
  <hr>
  
  <p>I would like to be able to prove <strong>when</strong> my Git commits are happening and that the history of my repository hasn't been re-written.  It doesn't have to be every commit.  Once a day or once a week would be sufficient.  Is there a recommended way to do so?</p>
  
  <p>I know I can sign Git commits with a GPG key, but I'm wondering if there's a way I can sign my commits with an X.509 certificate and the use of an online time-stamping service like <a href="http://timestamp.comodoca.com/rfc3161" rel="nofollow noreferrer">http://timestamp.comodoca.com/rfc3161</a>.</p>
  
  <p>If not, would dumping the current revision using <code>git rev-parse --verify HEAD</code> into a text file once a day, signing that file, and committing be sufficient to prove (roughly) when my code was written?</p>
  
  <p><strong>Added Info For Clarity</strong></p>
  
  <p>I know that Git guarantees the integrity of a repository, but, as far as I understand, if I control the repository a third party would have to trust that I haven't re-written the history of the repository or rolled my clock back and created a completely fake repository just to 'prove' my code is older than it actually is?  I also don't want to publish my repository publicly.</p>
  
  <p>Here's a fictional use cases that should give a better idea of what I want to do.</p>
  
  <p>I publish some code online.  A year later someone copies and publishes the same code in a book or article and claims I was the one that copied them.  At that point, I would like to be able to take my repository and prove that I committed that code a year ago, before they re-published it.</p>
  
  <p>By using an X.509 certificate with a time-stamping service I can prove <strong>when</strong> the signing occurred.  As long as I can prove I knew the hash for the year old commit, Git guarantees the integrity of the archive.</p>
  
  <p>Alternatively, is there a way to sign a commit using a GPG key, but with a verified time-stamp?  Is there a trusted third party that provides a time-stamping service similar to the ones available for X.509 certificates, but for GPG?</p>
  
  <p>Maybe I could use a combination of a GPG key and an X.509 certificate.  Assuming I keep a copy of my (public) GPG key in the repository, would the following work if I did it at the end of each day?</p>
  
  <ol>
  <li>Sign my (public) GPG key using my X.509 certificate and an online time-stamping service.</li>
  <li>Commit the change to the repository with a signature from my (private) GPG key.</li>
  </ol>