@@ -3460,6 +3460,101 @@ word32 SetBitString(word32 len, byte unusedBits, byte* output)
34603460#endif /* !NO_RSA || HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
34613461
34623462#ifdef ASN_BER_TO_DER
3463+
3464+ #define BER_OCTET_LENGTH 4096
3465+
3466+
3467+ /* Breaks an octet string up into chunks for use with streaming
3468+ * returns 0 on success and updates idx */
3469+ int StreamOctetString(const byte* in, word32 inSz, byte* out, word32* outSz,
3470+ word32* idx)
3471+ {
3472+ word32 i = 0;
3473+ word32 outIdx = *idx;
3474+ byte* tmp = out;
3475+
3476+ if (tmp) tmp += outIdx;
3477+
3478+ while (i < inSz) {
3479+ int ret, sz;
3480+
3481+ sz = BER_OCTET_LENGTH;
3482+
3483+ if ((sz + i) > inSz) {
3484+ sz = inSz - i;
3485+ }
3486+
3487+ ret = SetOctetString(sz, tmp);
3488+ if (ret > 0) {
3489+ outIdx += ret;
3490+ }
3491+
3492+ if (tmp) {
3493+ if (ret + sz + i + outIdx > *outSz) {
3494+ return BUFFER_E;
3495+ }
3496+ XMEMCPY(tmp + ret, in + i, sz);
3497+ tmp += sz + ret;
3498+ }
3499+ outIdx += sz;
3500+ i += sz;
3501+ }
3502+
3503+ if (tmp) {
3504+ *idx = outIdx;
3505+ return 0;
3506+ }
3507+ else {
3508+ *outSz = outIdx;
3509+ return LENGTH_ONLY_E;
3510+ }
3511+ }
3512+
3513+ long SetImplicitBer(byte tag, byte num, const byte* data, word32 dataSz,
3514+ byte* out, word32* outSz)
3515+ {
3516+ word32 sz = 0;
3517+ long outIdx = 0;
3518+ byte berTag = tag;
3519+
3520+ (void)num;
3521+ if (outSz == NULL || data == NULL) {
3522+ return BAD_FUNC_ARG;
3523+ }
3524+
3525+ /* create a list of chuncked up octets */
3526+ if (tag == ASN_OCTET_STRING) {
3527+ berTag = ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC;
3528+ }
3529+
3530+ if (out != NULL) {
3531+ if (*outSz < 2) {
3532+ return BUFFER_E;
3533+ }
3534+ out[outIdx] = berTag;
3535+ out[outIdx + 1] = ASN_INDEF_LENGTH;
3536+ }
3537+ outIdx += 2;
3538+
3539+ sz = *outSz;
3540+ StreamOctetString(data, dataSz, out, &sz, (word32*)&outIdx);
3541+
3542+ if (out) {
3543+ out[outIdx] = 0x00;
3544+ out[outIdx + 1] = 0x00;
3545+ }
3546+ outIdx += 2;
3547+
3548+ if (out) {
3549+ return outIdx;
3550+ }
3551+ else {
3552+ *outSz = outIdx;
3553+ return LENGTH_ONLY_E;
3554+ }
3555+ }
3556+
3557+
34633558/* Convert BER to DER */
34643559
34653560/* Pull informtation from the ASN.1 BER encoded item header */
0 commit comments