From 5baa9413177c624be8398f6a23614e2ce0bdbba3 Mon Sep 17 00:00:00 2001 From: Nathaniel McCallum Date: Thu, 8 May 2014 11:06:16 -0400 Subject: Implement OTP token importing This patch adds support for importing tokens using RFC 6030 key container files. This includes decryption support. For sysadmin sanity, any tokens which fail to add will be written to the output file for examination. The main use case here is where a small subset of a large set of tokens fails to validate or add. Using the output file, the sysadmin can attempt to recover these specific tokens. This code is implemented as a server-side script. However, it doesn't actually need to run on the server. This was done because importing is an odd fit for the IPA command framework: 1. We need to write an output file. 2. The operation may be long-running (thousands of tokens). 3. Only admins need to perform this task and it only happens infrequently. https://fedorahosted.org/freeipa/ticket/4261 Reviewed-By: Alexander Bokovoy --- ipatests/test_ipaserver/data/full.xml | 48 ++++++++ ipatests/test_ipaserver/data/pskc-figure3.xml | 32 +++++ ipatests/test_ipaserver/data/pskc-figure4.xml | 31 +++++ ipatests/test_ipaserver/data/pskc-figure5.xml | 57 +++++++++ ipatests/test_ipaserver/data/pskc-figure6.xml | 47 ++++++++ ipatests/test_ipaserver/data/pskc-figure7.xml | 68 +++++++++++ ipatests/test_ipaserver/data/pskc-figure8.xml | 53 +++++++++ ipatests/test_ipaserver/data/pskc-invalid.xml | 3 + ipatests/test_ipaserver/data/pskc-mini.xml | 4 + ipatests/test_ipaserver/test_otptoken_import.py | 151 ++++++++++++++++++++++++ 10 files changed, 494 insertions(+) create mode 100644 ipatests/test_ipaserver/data/full.xml create mode 100644 ipatests/test_ipaserver/data/pskc-figure3.xml create mode 100644 ipatests/test_ipaserver/data/pskc-figure4.xml create mode 100644 ipatests/test_ipaserver/data/pskc-figure5.xml create mode 100644 ipatests/test_ipaserver/data/pskc-figure6.xml create mode 100644 ipatests/test_ipaserver/data/pskc-figure7.xml create mode 100644 ipatests/test_ipaserver/data/pskc-figure8.xml create mode 100644 ipatests/test_ipaserver/data/pskc-invalid.xml create mode 100644 ipatests/test_ipaserver/data/pskc-mini.xml create mode 100644 ipatests/test_ipaserver/test_otptoken_import.py (limited to 'ipatests') diff --git a/ipatests/test_ipaserver/data/full.xml b/ipatests/test_ipaserver/data/full.xml new file mode 100644 index 000000000..0281b2881 --- /dev/null +++ b/ipatests/test_ipaserver/data/full.xml @@ -0,0 +1,48 @@ + + + + + iana.dummy + SerialNo + Model + IssueNo + DeviceBinding + 2006-05-01T00:00:00Z + 2012-05-01T00:00:00Z + DeviceUserId + + + CMID + + + Issuer + + Suite + + + + KeyProfileId + KeyReference + FriendlyName + + + MTIzNDU2Nzg5MDEyMzQ1Njc4OTA= + + + 0 + + + 200 + + + 300 + + + KeyUserId + + 2006-05-01T00:00:00Z + 2006-05-31T00:00:00Z + + + + diff --git a/ipatests/test_ipaserver/data/pskc-figure3.xml b/ipatests/test_ipaserver/data/pskc-figure3.xml new file mode 100644 index 000000000..b02ac7945 --- /dev/null +++ b/ipatests/test_ipaserver/data/pskc-figure3.xml @@ -0,0 +1,32 @@ + + + + + Manufacturer + 987654321 + DC=example-bank,DC=net + + + CM_ID_001 + + + Issuer + + + + + + MTIzNDU2Nzg5MDEyMzQ1Njc4OTA= + + + + 0 + + + UID=jsmith,DC=example-bank,DC=net + + + diff --git a/ipatests/test_ipaserver/data/pskc-figure4.xml b/ipatests/test_ipaserver/data/pskc-figure4.xml new file mode 100644 index 000000000..186e02901 --- /dev/null +++ b/ipatests/test_ipaserver/data/pskc-figure4.xml @@ -0,0 +1,31 @@ + + + + + Manufacturer + 987654321 + + + CM_ID_001 + + + Issuer + + + + keyProfile1 + MasterKeyLabel + + + + 0 + + + + OTP + + + + diff --git a/ipatests/test_ipaserver/data/pskc-figure5.xml b/ipatests/test_ipaserver/data/pskc-figure5.xml new file mode 100644 index 000000000..16ab9bb3c --- /dev/null +++ b/ipatests/test_ipaserver/data/pskc-figure5.xml @@ -0,0 +1,57 @@ + + + + + Manufacturer + 987654321 + + + CM_ID_001 + + + Issuer + + + + + + MTIzNDU2Nzg5MDEyMzQ1Njc4OTA= + + + + 0 + + + + + OTP + + + + + + Manufacturer + 987654321 + + + CM_ID_001 + + + Issuer + + + + + + MTIzNA== + + + + + diff --git a/ipatests/test_ipaserver/data/pskc-figure6.xml b/ipatests/test_ipaserver/data/pskc-figure6.xml new file mode 100644 index 000000000..0f4cd334f --- /dev/null +++ b/ipatests/test_ipaserver/data/pskc-figure6.xml @@ -0,0 +1,47 @@ + + + + Pre-shared-key + + + + + + ESIzRFVmd4iZABEiM0RVZgKn6WjLaTC1sbeBMSvIhRejN9vJa2BOlSaMrR7I5wSX + + + + + + Manufacturer + 987654321 + + + CM_ID_001 + + + Issuer + + + + + + + + + AAECAwQFBgcICQoLDA0OD+cIHItlB3Wra1DUpxVvOx2lef1VmNPCMl8jwZqIUqGv + + + Su+NvtQfmvfJzF6bmQiJqoLRExc= + + + 0 + + + + + diff --git a/ipatests/test_ipaserver/data/pskc-figure7.xml b/ipatests/test_ipaserver/data/pskc-figure7.xml new file mode 100644 index 000000000..1fb04fc31 --- /dev/null +++ b/ipatests/test_ipaserver/data/pskc-figure7.xml @@ -0,0 +1,68 @@ + + + + + + + + Ej7/PEpyEpw= + + 1000 + 16 + + + + + + + My Password 1 + + + + + + + + 2GTTnLwM3I4e5IO5FkufoOEiOhNj91fhKRQBtBJYluUDsPOLTfUvoU2dStyOwYZx + + + + + + + TokenVendorAcme + 987654321 + + + CM_ID_001 + + + Example-Issuer + + + + + + + + + + oTvo+S22nsmS2Z/RtcoF8Hfh+jzMe0RkiafpoDpnoZTjPYZu6V+A4aEn032yCr4f + + + + LP6xMvjtypbfT9PdkJhBZ+D6O4w= + + + + + + diff --git a/ipatests/test_ipaserver/data/pskc-figure8.xml b/ipatests/test_ipaserver/data/pskc-figure8.xml new file mode 100644 index 000000000..c9f63cf02 --- /dev/null +++ b/ipatests/test_ipaserver/data/pskc-figure8.xml @@ -0,0 +1,53 @@ + + + + + MIIB5zCCAVCgAwIBAgIESZp/vDANBgkqhkiG9w0BAQUFADA4M + Q0wCwYDVQQKEwRJRVRGMRMwEQYDVQQLEwpLZXlQcm92IFdHMRIwEAYDVQQDEwlQU0tDIF + Rlc3QwHhcNMDkwMjE3MDkxMzMyWhcNMTEwMjE3MDkxMzMyWjA4MQ0wCwYDVQQKEwRJRVR + GMRMwEQYDVQQLEwpLZXlQcm92IFdHMRIwEAYDVQQDEwlQU0tDIFRlc3QwgZ8wDQYJKoZI + hvcNAQEBBQADgY0AMIGJAoGBALCWLDa2ItYJ6su80hd1gL4cggQYdyyKK17btt/aS6Q/e + DsKjsPyFIODsxeKVV/uA3wLT4jQJM5euKJXkDajzGGOy92+ypfzTX4zDJMkh61SZwlHNJ + xBKilAM5aW7C+BQ0RvCxvdYtzx2LTdB+X/KMEBA7uIYxLfXH2Mnub3WIh1AgMBAAEwDQY + JKoZIhvcNAQEFBQADgYEAe875m84sYUJ8qPeZ+NG7REgTvlHTmoCdoByU0LBBLotUKuqf + rnRuXJRMeZXaaEGmzY1kLonVjQGzjAkU4dJ+RPmiDlYuHLZS41Pg6VMwY+03lhk6I5A/w + 4rnqdkmwZX/NgXg06alnc2pBsXWhL4O7nk0S2ZrLMsQZ6HcsXgdmHo= + + + + + + TokenVendorAcme + 987654321 + + + Example-Issuer + + + + + + + + + hJ+fvpoMPMO9BYpK2rdyQYGIxiATYHTHC7e/sPLKYo5/r1v+4 + xTYG3gJolCWuVMydJ7Ta0GaiBPHcWa8ctCVYmHKfSz5fdeV5nqbZApe6dofTqhRwZK6 + Yx4ufevi91cjN2vBpSxYafvN3c3+xIgk0EnTV4iVPRCR0rBwyfFrPc4= + + + + + + 0 + + + + + diff --git a/ipatests/test_ipaserver/data/pskc-invalid.xml b/ipatests/test_ipaserver/data/pskc-invalid.xml new file mode 100644 index 000000000..688e3479d --- /dev/null +++ b/ipatests/test_ipaserver/data/pskc-invalid.xml @@ -0,0 +1,3 @@ + + + diff --git a/ipatests/test_ipaserver/data/pskc-mini.xml b/ipatests/test_ipaserver/data/pskc-mini.xml new file mode 100644 index 000000000..e6ee7b55c --- /dev/null +++ b/ipatests/test_ipaserver/data/pskc-mini.xml @@ -0,0 +1,4 @@ + + + + diff --git a/ipatests/test_ipaserver/test_otptoken_import.py b/ipatests/test_ipaserver/test_otptoken_import.py new file mode 100644 index 000000000..7ee0754da --- /dev/null +++ b/ipatests/test_ipaserver/test_otptoken_import.py @@ -0,0 +1,151 @@ +# Authors: +# Nathaniel McCallum +# +# Copyright (C) 2014 Red Hat +# see file 'COPYING' for use and warranty information +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import os +import sys +import nose +from nss import nss + +from ipaserver.install.ipa_otptoken_import import PSKCDocument, ValidationError + +basename = os.path.join(os.path.dirname(__file__), "data") + +class test_otptoken_import(object): + def test_figure3(self): + doc = PSKCDocument(os.path.join(basename, "pskc-figure3.xml")) + assert doc.keyname is None + assert [(t.id, t.options) for t in doc.getKeyPackages()] == \ + [(u'12345678', { + 'ipatokenotpkey': u'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ', + 'ipatokenvendor': u'Manufacturer', + 'ipatokenserial': u'987654321', + 'ipatokenhotpcounter': 0L, + 'ipatokenotpdigits': 8, + 'type': u'hotp', + })] + + def test_figure4(self): + doc = PSKCDocument(os.path.join(basename, "pskc-figure4.xml")) + assert doc.keyname is None + try: + [(t.id, t.options) for t in doc.getKeyPackages()] + except ValidationError: # Referenced keys are not supported. + pass + else: + assert False + + def test_figure5(self): + doc = PSKCDocument(os.path.join(basename, "pskc-figure5.xml")) + assert doc.keyname is None + try: + [(t.id, t.options) for t in doc.getKeyPackages()] + except ValidationError: # PIN Policy is not supported. + pass + else: + assert False + + def test_figure6(self): + nss.nss_init_nodb() + try: + doc = PSKCDocument(os.path.join(basename, "pskc-figure6.xml")) + assert doc.keyname == 'Pre-shared-key' + doc.setKey('12345678901234567890123456789012'.decode('hex')) + assert [(t.id, t.options) for t in doc.getKeyPackages()] == \ + [(u'12345678', { + 'ipatokenotpkey': u'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ', + 'ipatokenvendor': u'Manufacturer', + 'ipatokenserial': u'987654321', + 'ipatokenhotpcounter': 0L, + 'ipatokenotpdigits': 8, + 'type': u'hotp'})] + finally: + nss.nss_shutdown() + + def test_figure7(self): + nss.nss_init_nodb() + try: + doc = PSKCDocument(os.path.join(basename, "pskc-figure7.xml")) + assert doc.keyname == 'My Password 1' + doc.setKey('qwerty') + assert [(t.id, t.options) for t in doc.getKeyPackages()] == \ + [(u'123456', { + 'ipatokenotpkey': u'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ', + 'ipatokenvendor': u'TokenVendorAcme', + 'ipatokenserial': u'987654321', + 'ipatokenotpdigits': 8, + 'type': u'hotp'})] + finally: + nss.nss_shutdown() + + def test_figure8(self): + nss.nss_init_nodb() + try: + doc = PSKCDocument(os.path.join(basename, "pskc-figure8.xml")) + except NotImplementedError: # X.509 is not supported. + pass + else: + assert False + finally: + nss.nss_shutdown() + + def test_invalid(self): + nss.nss_init_nodb() + try: + doc = PSKCDocument(os.path.join(basename, "pskc-invalid.xml")) + except ValueError: # File is invalid. + pass + else: + assert False + finally: + nss.nss_shutdown() + + def test_mini(self): + nss.nss_init_nodb() + try: + doc = PSKCDocument(os.path.join(basename, "pskc-mini.xml")) + [(t.id, t.options) for t in doc.getKeyPackages()] + except ValidationError: # Unsupported token type. + pass + else: + assert False + finally: + nss.nss_shutdown() + + def test_full(self): + nss.nss_init_nodb() + try: + doc = PSKCDocument(os.path.join(basename, "full.xml")) + assert [(t.id, t.options) for t in doc.getKeyPackages()] == \ + [(u'KID1', { + 'ipatokenotpkey': u'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ', + 'ipatokennotafter': u'20060531000000Z', + 'ipatokennotbefore': u'20060501000000Z', + 'ipatokenserial': u'SerialNo-IssueNo', + 'ipatokentotpclockoffset': 60000, + 'ipatokenotpalgorithm': u'sha1', + 'ipatokenvendor': u'iana.dummy', + 'description': u'FriendlyName', + 'ipatokentotptimestep': 200, + 'ipatokenhotpcounter': 0L, + 'ipatokenmodel': u'Model', + 'ipatokenotpdigits': 8, + 'type': u'hotp', + })] + finally: + nss.nss_shutdown() -- cgit