summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2003-11-02 13:27:43 +0000
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2003-11-02 13:27:43 +0000
commitd45c74b75d8753747672561e760e7832154805c3 (patch)
tree5cda7efb37496c0e5def99958e44d388386293c7
parent43ed1e984fe63abcc5511cf63b3cda3bf52f7504 (diff)
downloadgnutls-d45c74b75d8753747672561e760e7832154805c3.tar.gz
added gnutls_openpgp_key_export() function.
-rw-r--r--includes/gnutls/openpgp.h3
-rw-r--r--libextra/gnutls_openpgp.c1
-rw-r--r--libextra/openpgp/compat.c2
-rw-r--r--libextra/openpgp/openpgp.c85
-rw-r--r--libextra/openpgp/verify.c2
-rw-r--r--src/common.c3
6 files changed, 88 insertions, 8 deletions
diff --git a/includes/gnutls/openpgp.h b/includes/gnutls/openpgp.h
index e209cf67e7..2955631c1c 100644
--- a/includes/gnutls/openpgp.h
+++ b/includes/gnutls/openpgp.h
@@ -43,6 +43,9 @@ void gnutls_openpgp_key_deinit(gnutls_openpgp_key key); /* frees all memory */
int gnutls_openpgp_key_import(gnutls_openpgp_key key,
const gnutls_datum* data, gnutls_openpgp_key_fmt format);
+int gnutls_openpgp_key_export(gnutls_openpgp_key key,
+ gnutls_openpgp_key_fmt format, unsigned char* output_data,
+ size_t* output_data_size);
int gnutls_openpgp_key_get_fingerprint( gnutls_openpgp_key key,
char* result, size_t* result_size);
diff --git a/libextra/gnutls_openpgp.c b/libextra/gnutls_openpgp.c
index 3eda430545..61b47a43ae 100644
--- a/libextra/gnutls_openpgp.c
+++ b/libextra/gnutls_openpgp.c
@@ -66,6 +66,7 @@ _gnutls_map_cdk_rc( int rc )
{
switch( rc ) {
case CDK_Success: return 0;
+ case CDK_Too_Short: return GNUTLS_E_SHORT_MEMORY_BUFFER;
case CDK_General_Error: return GNUTLS_E_INTERNAL_ERROR;
case CDK_File_Error: return GNUTLS_E_FILE_ERROR;
case CDK_MPI_Error: return GNUTLS_E_MPI_SCAN_FAILED;
diff --git a/libextra/openpgp/compat.c b/libextra/openpgp/compat.c
index 9ee39f78ca..4faef21985 100644
--- a/libextra/openpgp/compat.c
+++ b/libextra/openpgp/compat.c
@@ -57,7 +57,7 @@ int gnutls_openpgp_verify_key(const char *trustdb,
gnutls_openpgp_key key = NULL;
gnutls_openpgp_keyring ring = NULL;
gnutls_openpgp_trustdb tdb = NULL;
- unsigned int verify_ring, verify_db, verify_self;
+ unsigned int verify_ring = 0, verify_db = 0, verify_self = 0;
if (!cert_list || cert_list_length != 1) {
gnutls_assert();
diff --git a/libextra/openpgp/openpgp.c b/libextra/openpgp/openpgp.c
index 440c6d4f7f..5072aec411 100644
--- a/libextra/openpgp/openpgp.c
+++ b/libextra/openpgp/openpgp.c
@@ -104,7 +104,12 @@ int rc;
return GNUTLS_E_INTERNAL_ERROR;
}
- cdk_stream_set_armor_flag( key->inp, 0 );
+ rc = cdk_stream_set_armor_flag( key->inp, 0);
+ if (rc) {
+ rc = _gnutls_map_cdk_rc( rc);
+ gnutls_assert();
+ return rc;
+ }
rc = cdk_keydb_get_keyblock( key->inp, &key->knode );
if( rc) {
@@ -117,6 +122,77 @@ int rc;
return 0;
}
+/**
+ * gnutls_openpgp_key_export - This function will export a RAW or BASE64 encoded key
+ * @key: Holds the key.
+ * @format: One of gnutls_openpgp_key_fmt elements.
+ * @output_data: will contain the key base64 encoded or raw
+ * @output_data_size: holds the size of output_data (and will be replaced by the actual size of parameters)
+ *
+ * This function will convert the given key to RAW or Base64 format.
+ * If the buffer provided is not long enough to hold the output, then
+ * GNUTLS_E_SHORT_MEMORY_BUFFER will be returned.
+ *
+ * Returns 0 on success.
+ *
+ **/
+int gnutls_openpgp_key_export(gnutls_openpgp_key key,
+ gnutls_openpgp_key_fmt format, unsigned char* output_data,
+ size_t* output_data_size)
+{
+int rc;
+size_t input_data_size = *output_data_size;
+
+ rc = cdk_kbnode_write_to_mem( key->knode,
+ output_data, output_data_size);
+ if( rc) {
+ rc = _gnutls_map_cdk_rc( rc);
+ gnutls_assert();
+ return rc;
+ }
+
+ if (format == GNUTLS_OPENPGP_FMT_BASE64) {
+ cdk_stream_t s;
+
+ s = cdk_stream_tmp_from_mem( output_data, *output_data_size);
+ if (s == NULL) {
+ gnutls_assert();
+ return GNUTLS_E_MEMORY_ERROR;
+ }
+
+ cdk_stream_tmp_set_mode( s, 1);
+ rc = cdk_stream_set_armor_flag( s, CDK_ARMOR_PUBKEY);
+ if (rc) {
+ rc = _gnutls_map_cdk_rc( rc);
+ gnutls_assert();
+ cdk_stream_close(s);
+ return rc;
+ }
+
+
+ *output_data_size = input_data_size;
+
+ rc = cdk_stream_read( s, output_data, *output_data_size);
+ if (rc==EOF) {
+ gnutls_assert();
+ cdk_stream_close(s);
+ return GNUTLS_E_INTERNAL_ERROR;
+ }
+
+ *output_data_size = rc;
+ if (*output_data_size != cdk_stream_get_length(s)) {
+ *output_data_size = cdk_stream_get_length(s);
+ cdk_stream_close(s);
+ gnutls_assert();
+ return GNUTLS_E_SHORT_MEMORY_BUFFER;
+ }
+
+ cdk_stream_close(s);
+ }
+
+ return 0;
+}
+
/**
* gnutls_openpgp_key_get_fingerprint - Gets the fingerprint
@@ -184,6 +260,10 @@ _gnutls_openpgp_count_key_names( gnutls_openpgp_key key)
* @sizeof_buf: holds the size of 'buf'
*
* Extracts the userID from the parsed OpenPGP key.
+ *
+ * Returns 0 on success, and GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
+ * if the index of the ID does not exist.
+ *
**/
int
gnutls_openpgp_key_get_name( gnutls_openpgp_key key,
@@ -203,8 +283,7 @@ gnutls_openpgp_key_get_name( gnutls_openpgp_key key,
}
if( idx < 0 || idx > _gnutls_openpgp_count_key_names( key) ) {
- gnutls_assert( );
- return GNUTLS_E_INTERNAL_ERROR;
+ return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
}
if( !idx )
diff --git a/libextra/openpgp/verify.c b/libextra/openpgp/verify.c
index 70dfdf8423..a2b3741f24 100644
--- a/libextra/openpgp/verify.c
+++ b/libextra/openpgp/verify.c
@@ -182,7 +182,6 @@ int gnutls_openpgp_key_verify_trustdb( gnutls_openpgp_key key,
gnutls_openpgp_trustdb trustdb,
unsigned int flags, unsigned int *verify)
{
- cdk_keydb_hd_t hd = NULL;
int rc = 0;
if( !key) {
@@ -202,7 +201,6 @@ int gnutls_openpgp_key_verify_trustdb( gnutls_openpgp_key key,
rc = 0;
leave:
- cdk_free( hd );
if( rc ) {
gnutls_assert();
}
diff --git a/src/common.c b/src/common.c
index a443af047c..eb27fd3d3c 100644
--- a/src/common.c
+++ b/src/common.c
@@ -228,7 +228,7 @@ void print_openpgp_info(gnutls_session session, const char* hostname)
fprintf(stderr, "Decoding error: %s\n", str);
return;
}
-#if 0
+
if (print_cert) {
size_t size;
@@ -243,7 +243,6 @@ void print_openpgp_info(gnutls_session session, const char* hostname)
fputs( buffer, stdout);
fputs( "\n", stdout);
}
-#endif
if (hostname != NULL) { /* Check the hostname of the first certificate
* if it matches the name of the host we