fix disconnect bug in libvncserver
[presencevnc] / libvnc / classes / ssl / onetimekey
1 #!/bin/sh
2 #
3 # usage: onetimekey path/to/mycert.pem
4 #
5 # Takes an openssl cert+key pem file and turns into a long string
6 # for the x11vnc SSL VNC Java Viewer.
7 #
8 # The Java applet URL parameter can be  oneTimeKey=<str> where str is
9 # the output of this program, or can be oneTimeKey=PROMPT in which
10 # case the applet will ask you to paste in the string.
11 #
12 # The problem trying to be solved here is it is difficult to get
13 # the Java applet to have or use a keystore with the key saved
14 # in it.  Also, as the name implies, an HTTPS server can create
15 # a one time key to send to the applet (the user has already
16 # logged in via password to the HTTPS server).
17
18 in=$1
19 der=/tmp/1time$$.der
20 touch $der
21 chmod 600 $der
22
23 openssl pkcs8 -topk8 -nocrypt -in "$in" -out "$der" -outform der
24
25 pbinhex=/tmp/pbinhex.$$
26 cat > $pbinhex <<END
27 #!/usr/bin/perl
28
29 \$str = '';
30 while (1) {
31         \$c = getc(STDIN);
32         last if \$c eq '';
33         \$str .= sprintf("%02x", unpack("C", \$c));
34 }
35
36 print "\$str\n";
37 END
38
39 chmod 700 $pbinhex 
40
41 str1=`$pbinhex < "$der"`
42 rm -f "$der"
43
44 n=`grep -n 'BEGIN CERTIFICATE' $in | awk -F: '{print $1}' | head -1`
45 str2=`tail +$n $in | $pbinhex`
46 echo "$str1,$str2"
47 rm -f $pbinhex