@@ -3463,6 +3463,18 @@ word32 SetBitString(word32 len, byte unusedBits, byte* output)
34633463
34643464#define BER_OCTET_LENGTH 4096
34653465
3466+ /* sets the terminating 0x00 0x00 at the end of an indefinite length
3467+ * returns the number of bytes written */
3468+ word32 SetIndefEnd(byte* in)
3469+ {
3470+ byte terminate[] = { 0x00, 0x00 };
3471+
3472+ if (in != NULL) {
3473+ XMEMCPY(in, terminate, 2);
3474+ }
3475+ return 2;
3476+ }
3477+
34663478
34673479/* Breaks an octet string up into chunks for use with streaming
34683480 * returns 0 on success and updates idx */
@@ -3510,50 +3522,6 @@ int StreamOctetString(const byte* in, word32 inSz, byte* out, word32* outSz,
35103522 }
35113523}
35123524
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-
35573525
35583526/* Convert BER to DER */
35593527
@@ -15429,21 +15397,32 @@ word32 SetLength(word32 length, byte* output)
1542915397 return i;
1543015398}
1543115399
15400+ word32 SetLengthEx(word32 length, byte* output, byte isIndef)
15401+ {
15402+ if (isIndef) {
15403+ output[0] = ASN_INDEF_LENGTH;
15404+ return 1;
15405+ }
15406+ else {
15407+ return SetLength(length, output);
15408+ }
15409+ }
1543215410/* Encode a DER header - type/tag and length.
1543315411 *
1543415412 * @param [in] tag DER tag of ASN.1 item.
1543515413 * @param [in] len Length of data in ASN.1 item.
1543615414 * @param [out] output Buffer to encode into.
1543715415 * @return Number of bytes encoded.
1543815416 */
15439- static word32 SetHeader(byte tag, word32 len, byte* output)
15417+ static word32 SetHeader(byte tag, word32 len, byte* output, byte isIndef )
1544015418{
1544115419 if (output) {
1544215420 /* Encode tag first. */
1544315421 output[0] = tag;
1544415422 }
1544515423 /* Encode the length. */
15446- return SetLength(len, output ? output + ASN_TAG_SZ : NULL) + ASN_TAG_SZ;
15424+ return SetLengthEx(len, output ? output + ASN_TAG_SZ : NULL, isIndef) +
15425+ ASN_TAG_SZ;
1544715426}
1544815427
1544915428/* Encode a SEQUENCE header in DER.
@@ -15454,7 +15433,12 @@ static word32 SetHeader(byte tag, word32 len, byte* output)
1545415433 */
1545515434word32 SetSequence(word32 len, byte* output)
1545615435{
15457- return SetHeader(ASN_SEQUENCE | ASN_CONSTRUCTED, len, output);
15436+ return SetHeader(ASN_SEQUENCE | ASN_CONSTRUCTED, len, output, 0);
15437+ }
15438+
15439+ word32 SetSequenceEx(word32 len, byte* output, byte isIndef)
15440+ {
15441+ return SetHeader(ASN_SEQUENCE | ASN_CONSTRUCTED, len, output, isIndef);
1545815442}
1545915443
1546015444/* Encode an OCTET STRING header in DER.
@@ -15465,7 +15449,14 @@ word32 SetSequence(word32 len, byte* output)
1546515449 */
1546615450word32 SetOctetString(word32 len, byte* output)
1546715451{
15468- return SetHeader(ASN_OCTET_STRING, len, output);
15452+ return SetHeader(ASN_OCTET_STRING, len, output, 0);
15453+ }
15454+
15455+ word32 SetOctetStringEx(word32 len, byte* output, byte indef)
15456+ {
15457+ if (indef)
15458+ return SetHeader(ASN_OCTET_STRING | ASN_CONSTRUCTED, len, output, indef);
15459+ return SetOctetString(len, output);
1546915460}
1547015461
1547115462/* Encode a SET header in DER.
@@ -15476,7 +15467,7 @@ word32 SetOctetString(word32 len, byte* output)
1547615467 */
1547715468word32 SetSet(word32 len, byte* output)
1547815469{
15479- return SetHeader(ASN_SET | ASN_CONSTRUCTED, len, output);
15470+ return SetHeader(ASN_SET | ASN_CONSTRUCTED, len, output, 0 );
1548015471}
1548115472
1548215473/* Encode an implicit context specific header in DER.
@@ -15489,11 +15480,23 @@ word32 SetSet(word32 len, byte* output)
1548915480 * @param [out] output Buffer to encode into.
1549015481 * @return Number of bytes encoded.
1549115482 */
15492- word32 SetImplicit(byte tag, byte number, word32 len, byte* output)
15483+ word32 SetImplicit(byte tag, byte number, word32 len, byte* output, byte isIndef )
1549315484{
15494- tag = (byte)(((tag == ASN_SEQUENCE || tag == ASN_SET) ? ASN_CONSTRUCTED : 0)
15495- | ASN_CONTEXT_SPECIFIC | number);
15496- return SetHeader(tag, len, output);
15485+ int useIndef = 0;
15486+
15487+ if ((tag == ASN_OCTET_STRING) && isIndef) {
15488+ tag = ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | number;
15489+ }
15490+ else {
15491+ tag = (byte)(((tag == ASN_SEQUENCE || tag == ASN_SET) ?
15492+ ASN_CONSTRUCTED : 0) | ASN_CONTEXT_SPECIFIC | number);
15493+ }
15494+
15495+ if (isIndef && (tag & ASN_CONSTRUCTED)) {
15496+ useIndef = 1;
15497+ }
15498+
15499+ return SetHeader(tag, len, output, useIndef);
1549715500}
1549815501
1549915502/* Encode an explicit context specific header in DER.
@@ -15505,10 +15508,10 @@ word32 SetImplicit(byte tag, byte number, word32 len, byte* output)
1550515508 * @param [out] output Buffer to encode into.
1550615509 * @return Number of bytes encoded.
1550715510 */
15508- word32 SetExplicit(byte number, word32 len, byte* output)
15511+ word32 SetExplicit(byte number, word32 len, byte* output, byte isIndef )
1550915512{
1551015513 return SetHeader((byte)(ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | number),
15511- len, output);
15514+ len, output, isIndef );
1551215515}
1551315516
1551415517#if defined(OPENSSL_EXTRA)
@@ -15534,18 +15537,18 @@ word32 SetOthername(void *name, byte *output)
1553415537 nameSz = (word32)nm->value->value.utf8string->length;
1553515538
1553615539 len = nm->type_id->objSz +
15537- SetHeader(ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC, nameSz + 2, NULL) +
15538- SetHeader(CTC_UTF8, nameSz, NULL) + nameSz;
15540+ SetHeader(ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC, nameSz + 2, NULL, 0 ) +
15541+ SetHeader(CTC_UTF8, nameSz, NULL, 0 ) + nameSz;
1553915542
1554015543 if (output != NULL) {
1554115544 /* otherName OID */
1554215545 XMEMCPY(output, nm->type_id->obj, nm->type_id->objSz);
1554315546 output += nm->type_id->objSz;
1554415547
1554515548 output += SetHeader(ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC, nameSz + 2,
15546- output);
15549+ output, 0 );
1554715550
15548- output += SetHeader(CTC_UTF8, nameSz, output);
15551+ output += SetHeader(CTC_UTF8, nameSz, output, 0 );
1554915552
1555015553 XMEMCPY(output, nameStr, nameSz);
1555115554 }
@@ -34549,7 +34552,7 @@ int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
3454934552 /* pubKey */
3455034553 if (pubKey) {
3455134554 idx += SetHeader(ASN_CONTEXT_SPECIFIC | ASN_ASYMKEY_PUBKEY |
34552- 1, pubKeyLen, output + idx);
34555+ 1, pubKeyLen, output + idx, 0 );
3455334556 XMEMCPY(output + idx, pubKey, pubKeyLen);
3455434557 idx += pubKeyLen;
3455534558 }
0 commit comments