From 49cd3f86ca7d93e1d4c001b5f0bf422447139a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Mon, 3 Apr 2006 05:17:30 +0000 Subject: [PATCH] Implement i2d_RSAPublicKey git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@16973 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/des/rsa.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/lib/des/rsa.c b/lib/des/rsa.c index 09eac707e..c54e5b79d 100644 --- a/lib/des/rsa.c +++ b/lib/des/rsa.c @@ -301,6 +301,20 @@ heim_int2BN(const heim_integer *i) return bn; } +static krb5_error_code +bn2heim_int(BIGNUM *bn, heim_integer *integer) +{ + integer->length = BN_num_bytes(bn); + integer->data = malloc(integer->length); + if (integer->data == NULL) { + integer->length = 0; + return ENOMEM; + } + BN_bn2bin(bn, integer->data); + integer->negative = BN_is_negative(bn); + return 0; +} + RSA * d2i_RSAPrivateKey(RSA *rsa, const unsigned char **pp, size_t len) @@ -335,3 +349,34 @@ d2i_RSAPrivateKey(RSA *rsa, const unsigned char **pp, size_t len) free_RSAPrivateKey(&data); return k; } + +int +i2d_RSAPublicKey(RSA *rsa, const unsigned char **pp) +{ + size_t size; + int ret; + + ret = bn2heim_int(rsa->n, &data.modulus); + ret = bn2heim_int(rsa->n, &data.e); + + if (pp == NULL) { + size = length_RSAPublicKey(&data); + } else { + RSAPublicKey data; + void *p; + size_t len; + + ASN1_MALLOC_ENCODE(RSAPublicKey, p, len, &data, &size, ret); + free_RSAPublicKey(&data); + if (ret) + return -1; + if (len != size) + abort(); + + memcpy(*pp, p, size); + + *pp += size; + } + + return size; +}