[Tkabber-dev] r2072 - in trunk/tkabber-plugins: . otr otr/tclotr

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Tue Jan 21 12:17:21 MSK 2014


Author: sergei
Date: 2014-01-21 12:17:20 +0400 (Tue, 21 Jan 2014)
New Revision: 2072

Added:
   trunk/tkabber-plugins/otr/tclotr/key.tcl
Modified:
   trunk/tkabber-plugins/ChangeLog
   trunk/tkabber-plugins/otr/README
   trunk/tkabber-plugins/otr/otr.tcl
   trunk/tkabber-plugins/otr/tclotr/otr.tcl
   trunk/tkabber-plugins/otr/tclotr/pkgIndex.tcl
Log:
	* otr/tclotr/key.tcl, otr/tclotr/pkgIndex.tcl: Added a new subpackage
	  which currently encodes and decodes DSA private keys in PEM format.

	* otr/otr.tcl, otr/README: Use ~/.tkabber/otr.private.key.pem file for
	  DSA private key instead of ::OTRPrivateKey variable from the config.


Modified: trunk/tkabber-plugins/ChangeLog
===================================================================
--- trunk/tkabber-plugins/ChangeLog	2014-01-20 18:55:55 UTC (rev 2071)
+++ trunk/tkabber-plugins/ChangeLog	2014-01-21 08:17:20 UTC (rev 2072)
@@ -1,3 +1,11 @@
+2014-01-21  Sergei Golovan <sgolovan at nes.ru>
+
+	* otr/tclotr/key.tcl, otr/tclotr/pkgIndex.tcl: Added a new subpackage
+	  which currently encodes and decodes DSA private keys in PEM format.
+
+	* otr/otr.tcl, otr/README: Use ~/.tkabber/otr.private.key.pem file for
+	  DSA private key instead of ::OTRPrivateKey variable from the config.
+
 2014-01-20  Sergei Golovan <sgolovan at nes.ru>
 
 	* otr/tclotr/otr.tcl: Do not send the OTR query message after an OTR

Modified: trunk/tkabber-plugins/otr/README
===================================================================
--- trunk/tkabber-plugins/otr/README	2014-01-20 18:55:55 UTC (rev 2071)
+++ trunk/tkabber-plugins/otr/README	2014-01-21 08:17:20 UTC (rev 2072)
@@ -14,16 +14,12 @@
 For Windows this plugin also requires the Memchan package. Its [random]
 channel is used as a PRNG.
 
-At the current stage to use this plugin you'll have to specify your
-long term DSA private key in Tkabber's config.tcl file. The key is a
-list of 5 values (p, q, g, y, x). Example:
+At the current stage to use this plugin you'll have to save your
+long term DSA private key in PEM format to the otr.private.key.pem
+file in Tkabber's config directory.
 
-set ::OTRPrivateKey {
-    813749128345192734691234769123846...
-    918764983469182736912691691791698...
-    817356418723187263548172653481726...
-    723645182763458123548126354812653...
-    872136451827364581273548123548235...
-}
+To generate it you can use OpenSSL:
 
-Numbers may be specified in hexadecimal notation as well (0x343dc63a9f7b...).
+openssl dsaparam -out dsaparam.pem 1024
+openssl gendsa -out otr.private.key.pem dsaparam.pem
+

Modified: trunk/tkabber-plugins/otr/otr.tcl
===================================================================
--- trunk/tkabber-plugins/otr/otr.tcl	2014-01-20 18:55:55 UTC (rev 2071)
+++ trunk/tkabber-plugins/otr/otr.tcl	2014-01-21 08:17:20 UTC (rev 2072)
@@ -1,11 +1,6 @@
 # $Id$
 
 namespace eval otr {
-    # TODO: DSA private key management
-    if {![info exists ::OTRPrivateKey]} {
-	return
-    }
-
     # Prepare to load the local TclOTR library
 
     set scriptdir [file dirname [info script]]
@@ -22,7 +17,7 @@
     ::msgcat::mcload [file join $scriptdir msgs]
     ::trans::load [file join $scriptdir trans]
 
-    # Attempt to load KHIM. Warn and quit loading plugin if we can't:
+    # Attempt to load TclOTR. Warn and quit loading plugin if we can't:
 
     if {[catch { package require otr } err]} {
 	puts stderr $err
@@ -34,6 +29,16 @@
 	return
     }
 
+    # TODO: DSA private key management
+    set keyfile [file join $::configdir otr.private.key.pem]
+    if {![file readable $keyfile]} {
+	puts stderr "To use the OTR plugin, generate your DSA private key\
+		     and save it into $keyfile file."
+	return
+    }
+
+    set ::OTRPrivateKey [::otr::key::readPEM $keyfile]
+
     # Auxiliary namespace. Rewrite_message_hook will use it to add some
     # supplemetary info.
     set ns tkabber:otr

Added: trunk/tkabber-plugins/otr/tclotr/key.tcl
===================================================================
--- trunk/tkabber-plugins/otr/tclotr/key.tcl	                        (rev 0)
+++ trunk/tkabber-plugins/otr/tclotr/key.tcl	2014-01-21 08:17:20 UTC (rev 2072)
@@ -0,0 +1,69 @@
+# key.tcl --
+#
+#       This file is a part of the Off-the-Record messaging protocol
+#       implementation. It contains the private DSA key encoding/decoding
+#       procedures.
+#
+# Copyright (c) 2014 Sergei Golovan <sgolovan at nes.ru>
+#
+# See the file "license.terms" for information on usage and redistribution
+# of this file, and for a DISCLAMER OF ALL WARRANTIES.
+#
+# $Id$
+
+package require base64
+package require asn
+package require math::bignum
+
+package provide otr::key 0.1
+
+##############################################################################
+
+namespace eval ::otr::key {}
+
+proc ::otr::key::encodePEM {key} {
+    lassign $key p q g y x
+    set ev [::asn::asnInteger 0]
+    set ep [::asn::asnBigInteger [::math::bignum::fromstr $p]]
+    set eq [::asn::asnBigInteger [::math::bignum::fromstr $q]]
+    set eg [::asn::asnBigInteger [::math::bignum::fromstr $g]]
+    set ey [::asn::asnBigInteger [::math::bignum::fromstr $y]]
+    set ex [::asn::asnBigInteger [::math::bignum::fromstr $x]]
+    set seq [::asn::asnSequence $ev $ep $eq $eg $ey $ex]
+    format \
+        "-----BEGIN DSA PRIVATE KEY-----\n%s\n-----END DSA PRIVATE KEY-----" \
+        [::base64::encode -maxlen 64 $seq]
+}
+
+proc ::otr::key::decodePEM {data} {
+    if {![regexp -- {-----BEGIN DSA PRIVATE KEY-----\n(.*)\n-----END DSA PRIVATE KEY-----} \
+                 $data -> base64]} {
+        return -code error "Incorrect DSA private key PEM data"
+    }
+    set binary [::base64::decode $base64]
+    ::asn::asnGetSequence binary sequence
+    ::asn::asnGetInteger sequence version
+    if {$version != 0} {
+        return -code error "Unsupported DSA private key PEM version"
+    }
+    ::asn::asnGetBigInteger sequence bp
+    set p [::math::bignum::tostr $bp]
+    ::asn::asnGetBigInteger sequence bq
+    set q [::math::bignum::tostr $bq]
+    ::asn::asnGetBigInteger sequence bg
+    set g [::math::bignum::tostr $bg]
+    ::asn::asnGetBigInteger sequence by
+    set y [::math::bignum::tostr $by]
+    ::asn::asnGetBigInteger sequence bx
+    set x [::math::bignum::tostr $bx]
+    list $p $q $g $y $x
+}
+
+proc ::otr::key::readPEM {file} {
+    set fd [open $file]
+    set data [read $fd]
+    close $fd
+    decodePEM $data
+}
+
+# vim:ts=8:sw=4:sts=4:et

Modified: trunk/tkabber-plugins/otr/tclotr/otr.tcl
===================================================================
--- trunk/tkabber-plugins/otr/tclotr/otr.tcl	2014-01-20 18:55:55 UTC (rev 2071)
+++ trunk/tkabber-plugins/otr/tclotr/otr.tcl	2014-01-21 08:17:20 UTC (rev 2072)
@@ -13,6 +13,7 @@
 # $Id$
 
 package require otr::data
+package require otr::key
 package require otr::crypto
 package require otr::auth
 package require otr::smp

Modified: trunk/tkabber-plugins/otr/tclotr/pkgIndex.tcl
===================================================================
--- trunk/tkabber-plugins/otr/tclotr/pkgIndex.tcl	2014-01-20 18:55:55 UTC (rev 2071)
+++ trunk/tkabber-plugins/otr/tclotr/pkgIndex.tcl	2014-01-21 08:17:20 UTC (rev 2072)
@@ -11,6 +11,7 @@
 # $Id$
 
 package ifneeded otr 0.1 [list source [file join $dir otr.tcl]]
+package ifneeded otr::key 0.1 [list source [file join $dir key.tcl]]
 package ifneeded otr::data 0.1 [list source [file join $dir data.tcl]]
 package ifneeded otr::crypto 0.1 [list source [file join $dir crypto.tcl]]
 package ifneeded otr::auth 0.1 [list source [file join $dir auth.tcl]]



More information about the Tkabber-dev mailing list