static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub) { unsigned char *p; unsigned int bitlen, magic = 0, keyalg; int outlen, noinc = 0; if (pk->type == EVP_PKEY_DSA) { bitlen = check_bitlen_dsa(pk->pkey.dsa, ispub, &magic); keyalg = MS_KEYALG_DSS_SIGN; } else if (pk->type == EVP_PKEY_RSA) { bitlen = check_bitlen_rsa(pk->pkey.rsa, ispub, &magic); keyalg = MS_KEYALG_RSA_KEYX; } else return -1; if (bitlen == 0) return -1; outlen = 16 + blob_length(bitlen, keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub); if (out == NULL) return outlen; if (*out) p = *out; else { p = (unsigned char*)OPENSSL_malloc(outlen); if (!p) return -1; *out = p; noinc = 1; } if (ispub) *p++ = MS_PUBLICKEYBLOB; else *p++ = MS_PRIVATEKEYBLOB; *p++ = 0x2; *p++ = 0; *p++ = 0; write_ledword(&p, keyalg); write_ledword(&p, magic); write_ledword(&p, bitlen); if (keyalg == MS_KEYALG_DSS_SIGN) write_dsa(&p, pk->pkey.dsa, ispub); else write_rsa(&p, pk->pkey.rsa, ispub); if (!noinc) *out += outlen; return outlen; }
static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub) { unsigned char *p; unsigned int bitlen, magic = 0, keyalg; int outlen, noinc = 0; int pktype = EVP_PKEY_id(pk); if (pktype == EVP_PKEY_DSA) { bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic); keyalg = MS_KEYALG_DSS_SIGN; } else if (pktype == EVP_PKEY_RSA) { bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic); keyalg = MS_KEYALG_RSA_KEYX; } else return -1; if (bitlen == 0) return -1; outlen = 16 + blob_length(bitlen, keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub); if (out == NULL) return outlen; if (*out) p = *out; else { if ((p = OPENSSL_malloc(outlen)) == NULL) { PEMerr(PEM_F_DO_I2B, ERR_R_MALLOC_FAILURE); return -1; } *out = p; noinc = 1; } if (ispub) *p++ = MS_PUBLICKEYBLOB; else *p++ = MS_PRIVATEKEYBLOB; *p++ = 0x2; *p++ = 0; *p++ = 0; write_ledword(&p, keyalg); write_ledword(&p, magic); write_ledword(&p, bitlen); if (keyalg == MS_KEYALG_DSS_SIGN) write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub); else write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub); if (!noinc) *out += outlen; return outlen; }
static EVP_PKEY *do_b2i(const unsigned char **in, unsigned int length, int ispub) { const unsigned char *p = *in; unsigned int bitlen, magic; int isdss; if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) { PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR); return NULL; } length -= 16; if (length < blob_length(bitlen, isdss, ispub)) { PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT); return NULL; } if (isdss) return b2i_dss(&p, bitlen, ispub); else return b2i_rsa(&p, bitlen, ispub); }
static EVP_PKEY *do_b2i_bio(BIO *in, int ispub) { const unsigned char *p; unsigned char hdr_buf[16], *buf = NULL; unsigned int bitlen, magic, length; int isdss; EVP_PKEY *ret = NULL; if (BIO_read(in, hdr_buf, 16) != 16) { PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT); return NULL; } p = hdr_buf; if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0) return NULL; length = blob_length(bitlen, isdss, ispub); buf = (unsigned char*)OPENSSL_malloc(length); if (!buf) { PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE); goto err; } p = buf; if (BIO_read(in, buf, length) != (int)length) { PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT); goto err; } if (isdss) ret = b2i_dss(&p, length, bitlen, ispub); else ret = b2i_rsa(&p, length, bitlen, ispub); err: if (buf) OPENSSL_free(buf); return ret; }