summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Lidén Borell <samuel@slbdata.se>2010-02-05 13:30:31 +0100
committerSamuel Lidén Borell <samuel@slbdata.se>2010-02-05 13:30:31 +0100
commitac9da9a356949ce5e115544c6fe955568eabccaa (patch)
tree55951860e2f61fdecafb3762ac2ae01bc3bae09f
parent35a4a474b31ffaa207bde99b8d33c3bcaa00a17f (diff)
downloadfribid-ac9da9a356949ce5e115544c6fe955568eabccaa.tar.gz
fribid-ac9da9a356949ce5e115544c6fe955568eabccaa.tar.bz2
fribid-ac9da9a356949ce5e115544c6fe955568eabccaa.zip
Replaced malloc+sprintf with a function on it's own
-rw-r--r--client/bankid.c30
-rw-r--r--client/misc.c16
-rw-r--r--client/misc.h2
-rw-r--r--client/xmldsig.c14
4 files changed, 31 insertions, 31 deletions
diff --git a/client/bankid.c b/client/bankid.c
index e26cb17..8cb60b4 100644
--- a/client/bankid.c
+++ b/client/bankid.c
@@ -78,9 +78,7 @@ static char *getVersionString() {
platform_freeConfig(cfg);
- char *result = malloc(strlen(template) -11*4 +
- 10*strlen(versionToEmulate) + 1*21 + 1);
- sprintf(result, template, versionToEmulate, expiry);
+ char *result = rasprintf(template, versionToEmulate, expiry);
if (versionToEmulate != defaultEmulatedVersion) {
free(versionToEmulate);
@@ -111,14 +109,11 @@ static bool checkValidity(bool *valid, char **versionToEmulate) {
*valid = (status == OK);
- *versionToEmulate = malloc(4*3*sizeof(char));
- if (!*versionToEmulate) return false;
-
- sprintf(*versionToEmulate, "%d.%d.%d.%d",
- (response >> 18) & 0xF,
- (response >> 12) & 0x3F,
- (response >> 6) & 0x3F,
- response & 0x3F);
+ *versionToEmulate = rasprintf("%d.%d.%d.%d",
+ (response >> 18) & 0xF,
+ (response >> 12) & 0x3F,
+ (response >> 6) & 0x3F,
+ response & 0x3F);
return true;
}
@@ -269,13 +264,8 @@ static BankIDError sign(const char *p12Data, const int p12Length,
char *version = base64_encode(versionStr, strlen(versionStr));
free(versionStr);
- char *object = malloc(strlen(sign_template) - 6*2 +
- strlen(extra) +
- strlen(challenge) +
- strlen(purpose) +
- strlen(hostname) + strlen(ip) +
- strlen(version) +1);
- sprintf(object, sign_template, extra, challenge, purpose, hostname, ip, version);
+ char *object = rasprintf(sign_template, extra, challenge,
+ purpose, hostname, ip, version);
free(version);
// Sign
@@ -314,9 +304,7 @@ BankIDError bankid_sign(const char *p12Data, const int p12Length,
char **signature) {
BankIDError error;
- char *extra = malloc(strlen(signedText_template) - 1*2 +
- strlen(message) + 1);
- sprintf(extra, signedText_template, message);
+ char *extra = rasprintf(signedText_template, message);
error = sign(p12Data, p12Length, person, password, challenge,
hostname, ip, CERTUSE_SIGNING, "Signing", extra, signature);
diff --git a/client/misc.c b/client/misc.c
index 9c1c8ee..3494f79 100644
--- a/client/misc.c
+++ b/client/misc.c
@@ -25,9 +25,25 @@
#define _BSD_SOURCE 1
#include <string.h>
#include <base64.h>
+#include <glib.h>
+#include <stdarg.h>
#include "misc.h"
+/**
+ * Like sprintf, but allocates and returns a string instead of
+ * using a pre-allocated buffer.
+ */
+char *rasprintf(const char *format, ...) {
+ va_list args;
+ char *str;
+
+ va_start(args, format);
+ str = (char*)g_strdup_vprintf((gchar*)format, args);
+ va_end(args);
+ return str;
+}
+
// Removes newlines from base64 encoded data
static void removeNewlines(char *s) {
const char *readp = s;
diff --git a/client/misc.h b/client/misc.h
index 2350fa2..f391904 100644
--- a/client/misc.h
+++ b/client/misc.h
@@ -27,6 +27,8 @@
#include <stdbool.h>
+char *rasprintf(const char *format, ...);
+
char *base64_encode(const char *data, const int length);
char *base64_decode(const char *encoded);
bool is_canonical_base64(const char *encoded);
diff --git a/client/xmldsig.c b/client/xmldsig.c
index 7bb3cb8..e650d1e 100644
--- a/client/xmldsig.c
+++ b/client/xmldsig.c
@@ -111,17 +111,14 @@ char *xmldsig_sign(const char *p12Data, const int p12Length,
}
free(certs);
- char *keyinfo = malloc(strlen(keyinfo_template) - 2 + certsLength +1);
- sprintf(keyinfo, keyinfo_template, keyinfoInner);
+ char *keyinfo = rasprintf(keyinfo_template, keyinfoInner);
free(keyinfoInner);
// SignedInfo
char *data_sha = sha_base64(data);
char *keyinfo_sha = sha_base64(keyinfo);
- char *signedinfo = malloc(strlen(signedinfo_template) - 2*2 +
- strlen(data_sha) + strlen(keyinfo_sha) +1);
- sprintf(signedinfo, signedinfo_template, data_sha, keyinfo_sha);
+ char *signedinfo = rasprintf(signedinfo_template, data_sha, keyinfo_sha);
free(keyinfo_sha);
free(data_sha);
@@ -141,11 +138,8 @@ char *xmldsig_sign(const char *p12Data, const int p12Length,
free(sigData);
// Glue everything together
- char *complete = malloc(strlen(xmldsig_template) - 4*2 +
- strlen(signedinfo) + strlen(signature) +
- strlen(keyinfo) + strlen(data) +1);
- sprintf(complete, xmldsig_template,
- signedinfo, signature, keyinfo, data);
+ char *complete = rasprintf(xmldsig_template,
+ signedinfo, signature, keyinfo, data);
free(keyinfo);
free(signedinfo);