X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Ftrust%2Fcertificate.py;h=f7dec97cab8910a74231cb2241fa2e1e8542af2b;hb=40971b94986fbc73719d7be19b0bb81fe673a170;hp=c49c50ccf11f6d9a71314c35c520e5eda0ea3ebb;hpb=e9162f83292361d81fcc9b241191a0b84baac87c;p=sfa.git diff --git a/sfa/trust/certificate.py b/sfa/trust/certificate.py index c49c50cc..f7dec97c 100644 --- a/sfa/trust/certificate.py +++ b/sfa/trust/certificate.py @@ -22,8 +22,7 @@ import traceback from OpenSSL import crypto import M2Crypto from M2Crypto import X509 -from M2Crypto import EVP -from random import randint +from tempfile import mkstemp from sfa.util.faults import * @@ -315,19 +314,24 @@ class Certificate: # Save the certificate to a file. # @param save_parents If save_parents==True, then also save the parent certificates. - def save_to_file(self, filename, save_parents=True): + def save_to_file(self, filename, save_parents=True, filep=None): string = self.save_to_string(save_parents=save_parents) - open(filename, 'w').write(string) + if filep: + f = filep + else: + f = open(filename, 'w') + f.write(string) + f.close() + ## # Save the certificate to a random file in /tmp/ - # @param save_parents If save_parents==True, then also save the parent certificates. - def save_to_random_tmp_file(self, save_parents=True): - while True: - filename = "/tmp/cred_%d" % randint(0,999999999) - if not os.path.isfile(filename): - break - self.save_to_file(filename, save_parents) - return filename + # @param save_parents If save_parents==True, then also save the parent certificates. + def save_to_random_tmp_file(self, save_parents=True): + fp, filename = mkstemp(suffix='cert', text=True) + fp = os.fdopen(fp, "w") + self.save_to_file(filename, save_parents=True, filep=fp) + return filename + ## # Sets the issuer private key and name # @param key Keypair object containing the private key of the issuer