Skip to content

Commit 2e03aa6

Browse files
updated rsa_encode_text funcs to use BIO_printf
1 parent ceb8ac0 commit 2e03aa6

File tree

1 file changed

+31
-91
lines changed

1 file changed

+31
-91
lines changed

src/wp_rsa_kmgmt.c

Lines changed: 31 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4068,21 +4068,17 @@ static int wp_rsa_text_enc_does_selection(WOLFPROV_CTX* provCtx, int selection)
40684068
/**
40694069
* Encode a portion of the RSA key as hexadecimal.
40704070
*
4071+
* @param [out] out BIO to write data out onto.
40714072
* @param [in] num An mp_int to convert to hexadecimal.
4072-
* @param [out] textData Buffer to store hex in.
4073-
* @param [in] textLen Size of buffer.
4074-
* @param [in, out] pos Current position in buffer.
40754073
* @param [in] label String describing section being encoded.
40764074
* @return 1 on success.
40774075
* @return 0 on failure.
40784076
*/
4079-
static int wp_rsa_encode_text_format_hex(const mp_int* num, char* textData,
4080-
size_t textLen, size_t* pos, const char* label)
4077+
static int wp_rsa_encode_text_format_hex(BIO *out, const mp_int* num,
4078+
const char* label)
40814079
{
40824080
unsigned char* binData = NULL;
40834081
size_t binLen = 0;
4084-
size_t printAmt = 0;
4085-
size_t dPos = *pos;
40864082
int bytes = 0;
40874083
int i;
40884084

@@ -4098,58 +4094,45 @@ static int wp_rsa_encode_text_format_hex(const mp_int* num, char* textData,
40984094
binData = NULL;
40994095
}
41004096
else {
4101-
if ((printAmt = XSNPRINTF(textData + dPos, textLen - dPos,
4102-
"%s:\n ", label)) <= 0) {
4097+
if (BIO_printf(out, "%s:\n ", label) <= 0) {
41034098
ok = 0;
41044099
}
4105-
dPos += printAmt;
41064100

41074101
/* OSSL adds a leading 00 if MSB is set */
41084102
if (ok && *binData > 127) {
4109-
if ((printAmt = XSNPRINTF(textData + dPos, textLen - dPos,
4110-
"00:")) <= 0) {
4103+
if (BIO_printf(out, "00:") <= 0) {
41114104
ok = 0;
41124105
}
4113-
dPos += printAmt;
41144106
bytes++;
41154107
}
41164108

41174109
/* OSSL does a newline + indent every 15 bytes */
41184110
if (ok) {
41194111
for (i = 0; i < (int)binLen - 1; i++) {
41204112
if (bytes >= 14) {
4121-
if ((printAmt = XSNPRINTF(textData + dPos,
4122-
textLen - dPos, "%02x:\n ", binData[i])) <= 0) {
4113+
if (BIO_printf(out, "%02x:\n ", binData[i]) <= 0) {
41234114
ok = 0;
41244115
break;
41254116
}
41264117
bytes = 0;
41274118
}
41284119
else {
4129-
if ((printAmt = XSNPRINTF(textData + dPos,
4130-
textLen - dPos, "%02x:", binData[i])) <= 0) {
4120+
if (BIO_printf(out, "%02x:", binData[i]) <= 0) {
41314121
ok = 0;
41324122
break;
41334123
}
41344124
bytes++;
41354125
}
4136-
dPos += printAmt;
41374126
}
4138-
if (ok && (printAmt = XSNPRINTF(textData + dPos,
4139-
textLen - dPos, "%02x\n", binData[i])) <= 0) {
4127+
if (ok && BIO_printf(out, "%02x\n", binData[i]) <= 0) {
41404128
ok = 0;
41414129
}
4142-
dPos += printAmt;
41434130
}
41444131

41454132
OPENSSL_free(binData);
41464133
binData = NULL;
41474134
}
41484135

4149-
if (ok) {
4150-
*pos = dPos;
4151-
}
4152-
41534136
return ok;
41544137
}
41554138

@@ -4178,10 +4161,6 @@ static int wp_rsa_encode_text(wp_RsaEncDecCtx* ctx, OSSL_CORE_BIO* cBio,
41784161
}
41794162
int hasPriv = (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0;
41804163
int hasPub = (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0;
4181-
char* textData = NULL;
4182-
size_t textLen = 0;
4183-
size_t pos = 0;
4184-
size_t printAmt = 0;
41854164
char* expStr;
41864165
int expLen;
41874166

@@ -4195,52 +4174,27 @@ static int wp_rsa_encode_text(wp_RsaEncDecCtx* ctx, OSSL_CORE_BIO* cBio,
41954174
ok = 0;
41964175
}
41974176

4198-
/* Calculate total size needed for text output */
4199-
if (ok) {
4200-
/* 128 bytes provides space for labels: 'modulus:', 'prime1:', etc. */
4201-
textLen = 128;
4202-
if (hasPriv) {
4203-
/* displaying modulus and private exponent requires roughly 3 bytes
4204-
* per byte in key.
4205-
* prime1, prime2, exponent1, exponent2, and coefficient requires
4206-
* roughly 1.5 bytes per byte inkey.
4207-
* This is then 13.5 bytes per bytes in key + indents, which should
4208-
* be less than bits * 2.*/
4209-
textLen += key->bits << 1;
4210-
}
4211-
else if (hasPub) {
4212-
/* displaying modulus requires roughly 3 bytes per byte in key + a
4213-
* small amount for indents. This should be less than bits / 2 */
4214-
textLen += key->bits >> 1;
4215-
}
4216-
4217-
if ((textData = OPENSSL_malloc(textLen)) == NULL) {
4218-
ok = 0;
4219-
}
4220-
}
4221-
42224177
if (ok) {
42234178
/* OSSL uses nested macros to determine the number of primes, not sure
42244179
* when there wouldn't be two primes */
4225-
if (hasPriv && (printAmt = XSNPRINTF(textData + pos, textLen - pos,
4226-
"Private-Key: (%d bit, 2 primes)\n", key->bits)) <= 0) {
4180+
if (hasPriv && BIO_printf(out, "Private-Key: (%d bit, 2 primes)\n",
4181+
key->bits) <= 0) {
42274182
ok = 0;
42284183
}
4229-
else if (hasPub && (printAmt = XSNPRINTF(textData + pos,
4230-
textLen - pos, "Public-Key: (%d bit)\n", key->bits)) <= 0) {
4184+
else if (hasPub && BIO_printf(out, "Public-Key: (%d bit)\n",
4185+
key->bits) <= 0) {
42314186
ok = 0;
42324187
}
4233-
pos += printAmt;
42344188
}
42354189

42364190
/* OSSL uses 'modulus' and 'Modulus' */
4237-
if (hasPriv) {
4238-
ok = wp_rsa_encode_text_format_hex(&key->key.n, textData, textLen, &pos,
4239-
"modulus");
4240-
}
4241-
else if (hasPub) {
4242-
ok = wp_rsa_encode_text_format_hex(&key->key.n, textData, textLen, &pos,
4243-
"Modulus");
4191+
if (ok) {
4192+
if (hasPriv) {
4193+
ok = wp_rsa_encode_text_format_hex(out, &key->key.n, "modulus");
4194+
}
4195+
else if (hasPub) {
4196+
ok = wp_rsa_encode_text_format_hex(out, &key->key.n, "Modulus");
4197+
}
42444198
}
42454199

42464200
/* OSSL uses 'publicExponent' and 'Exponent' */
@@ -4255,15 +4209,13 @@ static int wp_rsa_encode_text(wp_RsaEncDecCtx* ctx, OSSL_CORE_BIO* cBio,
42554209
expStr = NULL;
42564210
}
42574211
else {
4258-
if (hasPriv && (printAmt = XSNPRINTF(textData + pos,
4259-
textLen - pos, "publicExponent: %s ", expStr)) <= 0) {
4212+
if (hasPriv && BIO_printf(out, "publicExponent: %s ",
4213+
expStr) <= 0) {
42604214
ok = 0;
42614215
}
4262-
else if (hasPub && (printAmt = XSNPRINTF(textData + pos,
4263-
textLen - pos, "Exponent: %s ", expStr)) <= 0) {
4216+
else if (hasPub && BIO_printf(out, "Exponent: %s ", expStr) <= 0) {
42644217
ok = 0;
42654218
}
4266-
pos += printAmt;
42674219

42684220
OPENSSL_free(expStr);
42694221
expStr = NULL;
@@ -4282,12 +4234,10 @@ static int wp_rsa_encode_text(wp_RsaEncDecCtx* ctx, OSSL_CORE_BIO* cBio,
42824234
}
42834235
else {
42844236
/* OSSL does not print a leading zero for the hex part */
4285-
if ((printAmt = XSNPRINTF(textData + pos,
4286-
textLen - pos, "(0x%s)\n",
4287-
(*expStr == '0') ? (expStr + 1) : (expStr))) <= 0) {
4237+
if (BIO_printf(out, "(0x%s)\n",
4238+
(*expStr == '0') ? (expStr + 1) : (expStr)) <= 0) {
42884239
ok = 0;
42894240
}
4290-
pos += printAmt;
42914241

42924242
OPENSSL_free(expStr);
42934243
expStr = NULL;
@@ -4297,43 +4247,33 @@ static int wp_rsa_encode_text(wp_RsaEncDecCtx* ctx, OSSL_CORE_BIO* cBio,
42974247
/* Write private key components */
42984248
if (ok && hasPriv) {
42994249
if (ok) {
4300-
ok = wp_rsa_encode_text_format_hex(&key->key.d, textData, textLen,
4301-
&pos, "privateExponent");
4250+
ok = wp_rsa_encode_text_format_hex(out, &key->key.d,
4251+
"privateExponent");
43024252
}
43034253

43044254
if (ok) {
4305-
ok = wp_rsa_encode_text_format_hex(&key->key.p, textData, textLen,
4306-
&pos, "prime1");
4255+
ok = wp_rsa_encode_text_format_hex(out, &key->key.p, "prime1");
43074256
}
43084257

43094258
if (ok) {
4310-
ok = wp_rsa_encode_text_format_hex(&key->key.q, textData, textLen,
4311-
&pos, "prime2");
4259+
ok = wp_rsa_encode_text_format_hex(out, &key->key.q, "prime2");
43124260
}
43134261

43144262
if (ok) {
4315-
ok = wp_rsa_encode_text_format_hex(&key->key.dP, textData, textLen,
4316-
&pos, "exponent1");
4263+
ok = wp_rsa_encode_text_format_hex(out, &key->key.dP, "exponent1");
43174264
}
43184265

43194266
if (ok) {
4320-
ok = wp_rsa_encode_text_format_hex(&key->key.dQ, textData, textLen,
4321-
&pos, "exponent2");
4267+
ok = wp_rsa_encode_text_format_hex(out, &key->key.dQ, "exponent2");
43224268
}
43234269

43244270
if (ok) {
4325-
ok = wp_rsa_encode_text_format_hex(&key->key.u, textData, textLen,
4326-
&pos, "coefficient");
4271+
ok = wp_rsa_encode_text_format_hex(out, &key->key.u, "coefficient");
43274272
}
43284273
}
43294274

43304275
/* TODO: display RSAPSS info */
43314276

4332-
if (ok && (BIO_write(out, textData, (int)pos) <= 0)) {
4333-
ok = 0;
4334-
}
4335-
4336-
OPENSSL_free(textData);
43374277
BIO_free(out);
43384278

43394279
WOLFPROV_LEAVE(WP_LOG_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);

0 commit comments

Comments
 (0)