implemented pkcs1_write() method
authorAndreas Steffen <andreas.steffen@strongswan.org>
Fri, 12 Oct 2007 15:23:29 +0000 (15:23 -0000)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Fri, 12 Oct 2007 15:23:29 +0000 (15:23 -0000)
src/libstrongswan/crypto/rsa/rsa_private_key.c
src/libstrongswan/crypto/rsa/rsa_private_key.h

index b6854aa..fb60388 100644 (file)
@@ -405,19 +405,34 @@ static status_t build_emsa_pkcs1_signature(private_rsa_private_key_t *this,
 }
 
 /**
- * Implementation of rsa_private_key.save_key.
+ * Implementation of rsa_private_key_t.write.
  */
-static status_t save_key(private_rsa_private_key_t *this, char *file)
+static bool pkcs1_write(private_rsa_private_key_t *this, const char *filename, bool force)
 {
-       return NOT_SUPPORTED;
+       bool status;
+
+    chunk_t pkcs1 = asn1_wrap(ASN1_SEQUENCE, "cmmmmmmmm",
+                                               ASN1_INTEGER_0,
+                                               asn1_integer_from_mpz(this->n),
+                                               asn1_integer_from_mpz(this->e),
+                                               asn1_integer_from_mpz(this->d),
+                                               asn1_integer_from_mpz(this->p),
+                                               asn1_integer_from_mpz(this->q),
+                                               asn1_integer_from_mpz(this->exp1),
+                                               asn1_integer_from_mpz(this->exp2),
+                                               asn1_integer_from_mpz(this->coeff));
+
+       status = chunk_write(pkcs1, filename, "pkcs1", 0066, force);
+       chunk_free_randomized(&pkcs1);
+       return status;
 }
 
 /**
- * Implementation of rsa_public_key.get_keysize.
+ * Implementation of rsa_private_key_t.get_public_key.
  */
-static size_t get_keysize(const private_rsa_private_key_t *this)
+rsa_public_key_t *get_public_key(private_rsa_private_key_t *this)
 {
-       return this->k;
+       return NULL;
 }
 
 /**
@@ -549,8 +564,8 @@ static private_rsa_private_key_t *rsa_private_key_create_empty(void)
        /* public functions */
        this->public.pkcs1_decrypt = (status_t (*) (rsa_private_key_t*,chunk_t,chunk_t*))pkcs1_decrypt;
        this->public.build_emsa_pkcs1_signature = (status_t (*) (rsa_private_key_t*,hash_algorithm_t,chunk_t,chunk_t*))build_emsa_pkcs1_signature;
-       this->public.save_key = (status_t (*) (rsa_private_key_t*,char*))save_key;
-       this->public.get_keysize = (size_t (*) (const rsa_private_key_t*))get_keysize;
+       this->public.pkcs1_write = (bool (*) (rsa_private_key_t*,const char*,bool))pkcs1_write;
+       this->public.get_public_key = (rsa_public_key_t* (*) (rsa_private_key_t*))get_public_key;
        this->public.belongs_to = (bool (*) (rsa_private_key_t*,rsa_public_key_t*))belongs_to;
        this->public.destroy = (void (*) (rsa_private_key_t*))destroy;
        
index ae35829..f5ff335 100644 (file)
@@ -44,8 +44,6 @@ typedef struct rsa_private_key_t rsa_private_key_t;
  * 
  * @see rsa_public_key_t
  *
- * @todo Implement get_key(), save_key(), get_public_key()
- *
  * @ingroup rsa
  */
 struct rsa_private_key_t {
@@ -82,24 +80,22 @@ struct rsa_private_key_t {
        status_t (*build_emsa_pkcs1_signature) (rsa_private_key_t *this, hash_algorithm_t hash_algorithm, chunk_t data, chunk_t *signature);
        
        /**
-        * @brief Saves a key to a file.
-        * 
-        * Not implemented!
-        * 
+        * @brief Writes an RSA private key to a file in PKCS#1 format.
+        *
         * @param this                          calling object
-        * @param file                          file to which the key should be written.
-        * @return                                      NOT_SUPPORTED
+        * @param filename                      file to which the key should be written.
+        * @param force                         if TRUE overwrite existing file
+        * @return                                      TRUE if successful - FALSE otherwise
         */
-       status_t (*save_key) (rsa_private_key_t *this, char *file);
+       bool (*pkcs1_write) (rsa_private_key_t *this, const char *filename, bool force);
        
        /**
-        * @brief Get the size of the modulus in bytes.
+        * @brief Create a rsa_public_key_t with the public part of the key.
         * 
         * @param this                          calling object
-        * @return                                      size of the modulus (n) in bytes
+        * @return                                      public_key
         */
-       size_t (*get_keysize) (const rsa_private_key_t *this);
-
+       rsa_public_key_t *(*get_public_key) (rsa_private_key_t *this);
        
        /**
         * @brief Check if a private key belongs to a public key.