From: Victor Wagner Date: Sun, 21 Dec 2014 17:57:35 +0000 (+0300) Subject: Covered StackOfX509 by tests, fixed some typos in pkey docstrings X-Git-Url: http://wagner.pp.ru/gitweb/?a=commitdiff_plain;h=5eb6b4548beeacbd191b9c49a9b6cb7acf339837;p=oss%2Fctypescrypto.git Covered StackOfX509 by tests, fixed some typos in pkey docstrings --- diff --git a/ctypescrypto/pkey.py b/ctypescrypto/pkey.py index fdf384d..9257dfb 100644 --- a/ctypescrypto/pkey.py +++ b/ctypescrypto/pkey.py @@ -36,10 +36,10 @@ class PKey(object): self.key=ptr self.cansign=cansign if not privkey is None or not pubkey is None: - raise TypeError("Just one of pubkey or privkey can be specified") + raise TypeError("Just one of ptr, pubkey or privkey can be specified") elif not privkey is None: if not pubkey is None: - raise TypeError("Just one of pubkey or privkey can be specified") + raise TypeError("Just one of ptr, pubkey or privkey can be specified") b=Membio(privkey) self.cansign=True if format == "PEM": @@ -152,7 +152,7 @@ class PKey(object): rsa_keygen_bits=number - size of key to be generated rsa_keygen_pubexp - RSA public expontent(default 65537) - Algorithn specific parameters for DSA,DH and EC + Algorithm specific parameters for DSA,DH and EC paramsfrom=PKey object diff --git a/ctypescrypto/x509.py b/ctypescrypto/x509.py index 4f08632..dd4cba2 100644 --- a/ctypescrypto/x509.py +++ b/ctypescrypto/x509.py @@ -154,7 +154,7 @@ class X509Name(object): b=Membio() libcrypto.ASN1_STRING_print_ex(b.bio,s,self.PRINT_FLAG) return unicode(b) - elif isinstance(key,int): + elif isinstance(key,(int,long)): # Return OID, string tuple entry=libcrypto.X509_NAME_get_entry(self.ptr,key) if entry is None: @@ -164,12 +164,19 @@ class X509Name(object): b=Membio() libcrypto.ASN1_STRING_print_ex(b.bio,s,self.PRINT_FLAG) return (oid,unicode(b)) + else: + raise TypeError("X509 NAME can be indexed by Oids or integers only") def __setitem__(self,key,val): if not self.writable: raise ValueError("Attempt to modify constant X509 object") else: raise NotImplementedError + def __delitem__(self,key): + if not self.writable: + raise ValueError("Attempt to modify constant X509 object") + else: + raise NotImplementedError class _x509_ext(Structure): """ Represens C structure X509_EXTENSION """ @@ -487,11 +494,11 @@ class StackOfX509(object): """ if ptr is None: self.need_free = True - self.ptr=libcrypt.sk_new_null() + self.ptr=libcrypto.sk_new_null() if certs is not None: for crt in certs: self.append(crt) - elif not certs is None: + elif certs is not None: raise ValueError("cannot handle certs an ptr simultaneously") else: self.need_free = disposable @@ -503,12 +510,15 @@ class StackOfX509(object): raise IndexError p=libcrypto.sk_value(self.ptr,index) return X509(ptr=libcrypto.X509_dup(p)) - def __putitem__(self,index,value): + def __setitem__(self,index,value): if not self.need_free: raise ValueError("Stack is read-only") if index <0 or index>=len(self): raise IndexError - p=libcrypto.sk_set(self.ptr,index,libcrypto.X509_dup(value.cert)) + if not isinstance(value,X509): + raise TypeError('StackOfX508 can contain only X509 objects') + p=libcrypto.sk_value(self.ptr,index) + libcrypto.sk_set(self.ptr,index,libcrypto.X509_dup(value.cert)) libcrypto.X509_free(p) def __delitem__(self,index): if not self.need_free: @@ -523,6 +533,8 @@ class StackOfX509(object): def append(self,value): if not self.need_free: raise ValueError("Stack is read-only") + if not isinstance(value,X509): + raise TypeError('StackOfX508 can contain only X509 objects') libcrypto.sk_push(self.ptr,libcrypto.X509_dup(value.cert)) libcrypto.i2a_ASN1_INTEGER.argtypes=(c_void_p,c_void_p) libcrypto.ASN1_STRING_print_ex.argtypes=(c_void_p,c_void_p,c_long) @@ -549,3 +561,10 @@ libcrypto.X509V3_EXT_print.argtypes=(c_void_p,POINTER(_x509_ext),c_long,c_int) libcrypto.X509_get_ext.restype=c_void_p libcrypto.X509_get_ext.argtypes=(c_void_p,c_int) libcrypto.X509V3_EXT_print.argtypes=(c_void_p,POINTER(_x509_ext),c_long,c_int) +libcrypto.sk_set.argtypes=(c_void_p,c_int,c_void_p) +libcrypto.sk_set.restype=c_void_p +libcrypto.sk_value.argtypes=(c_void_p,c_int) +libcrypto.sk_value.restype=c_void_p +libcrypto.X509_dup.restype=c_void_p +libcrypto.sk_new_null.restype=c_void_p +libcrypto.X509_dup.argtypes=(c_void_p,) diff --git a/tests/testx509.py b/tests/testx509.py index 82b6d55..060cc09 100644 --- a/tests/testx509.py +++ b/tests/testx509.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -from ctypescrypto.x509 import X509,X509Store,utc +from ctypescrypto.x509 import X509,X509Store,utc,StackOfX509 from ctypescrypto.oid import Oid from tempfile import NamedTemporaryFile import datetime @@ -130,7 +130,15 @@ zVMSW4SOwg/H7ZMZ2cn6j1g0djIvruFQFGHUqFijyDATI+/GJYw2jxyA def test_subjectfields(self): c=X509(self.cert1) self.assertEqual(c.subject[Oid("C")],"RU") + with self.assertRaises(TypeError): + x=c.subject["CN"] self.assertEqual(c.subject[Oid("L")],u'\u041c\u043e\u0441\u043a\u0432\u0430') + def test_subjectmodify(self): + c=X509(self.cert1) + with self.assertRaises(ValueError): + c.subject[Oid("CN")]=u'Foo' + with self.assertRaises(ValueError): + del c.subject[Oid('CN')] def test_subjectbadsubfield(self): c=X509(self.cert1) with self.assertRaises(KeyError): @@ -184,7 +192,7 @@ zVMSW4SOwg/H7ZMZ2cn6j1g0djIvruFQFGHUqFijyDATI+/GJYw2jxyA ext_id=ext.oid self.assertTrue(isinstance(ext_id,Oid)) self.assertEqual(ext_id,Oid('basicConstraints')) - def text_extension_text(self): + def test_extension_text(self): cert=X509(self.cert1) ext=cert.extensions[0] self.assertEqual(str(ext),'CA:FALSE') @@ -244,5 +252,53 @@ zVMSW4SOwg/H7ZMZ2cn6j1g0djIvruFQFGHUqFijyDATI+/GJYw2jxyA trusted.close() def test_verify_by_dirstore(self): pass + def test_certstack1(self): + l=[] + l.append(X509(self.cert1)) + self.assertEqual(unicode(l[0].subject[Oid('CN')]),u'Виктор Вагнер') + l.append(X509(self.ca_cert)) + l.append(X509(self.digicert_cert)) + stack=StackOfX509(certs=l) + self.assertEqual(len(stack),3) + self.assertTrue(isinstance(stack[1],X509)) + self.assertEqual(unicode(stack[0].subject[Oid('CN')]),u'Виктор Вагнер') + with self.assertRaises(IndexError): + c=stack[-1] + with self.assertRaises(IndexError): + c=stack[3] + del stack[1] + self.assertEqual(len(stack),2) + self.assertEqual(unicode(stack[0].subject[Oid('CN')]),u'Виктор Вагнер') + self.assertEqual(unicode(stack[1].subject[Oid('CN')]),u'DigiCert High Assurance EV CA-1') + def test_certstack2(self): + stack=StackOfX509() + stack.append(X509(self.cert1)) + stack.append(X509(self.ca_cert)) + c=stack[1] + stack[1]=X509(self.digicert_cert) + self.assertEqual(len(stack),2) + self.assertEqual(unicode(stack[1].subject[Oid('CN')]),u'DigiCert High Assurance EV CA-1') + with self.assertRaises(IndexError): + stack[-1]=c + with self.assertRaises(IndexError): + stack[3]=c + with self.assertRaises(TypeError): + stack[0]=self.cert1 + with self.assertRaises(TypeError): + stack.append(self.cert1) + def test_certstack3(self): + l=[] + l.append(X509(self.cert1)) + self.assertEqual(unicode(l[0].subject[Oid('CN')]),u'Виктор Вагнер') + l.append(X509(self.ca_cert)) + l.append(X509(self.digicert_cert)) + stack=StackOfX509(certs=l) + stack2=StackOfX509(ptr=stack.ptr,disposable=False) + with self.assertRaises(ValueError): + stack3=StackOfX509(ptr=stack.ptr,certs=l) + with self.assertRaises(ValueError): + stack2[1]=l[0] + with self.assertRaises(ValueError): + stack2.append(l[0]) if __name__ == '__main__': unittest.main()