int checkSizeConstraint(OOCTXT* pctxt, int size) { Asn1SizeCnst* pSize; ASN1UINT upper; ASN1BOOL extbit; int stat; /* If size constraint is present and extendable, decode extension */ /* bit.. */ if (isExtendableSize(pctxt->pSizeConstraint)) { stat = DE_BIT (pctxt, &extbit); if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); } else extbit = 0; /* Now use the value of the extension bit to select the proper */ /* size constraint range specification.. */ pSize = getSizeConstraint (pctxt, extbit); upper = (pSize) ? pSize->upper : ASN1UINT_MAX; if (upper < (ASN1UINT)size) { return LOG_ASN1ERR (pctxt, ASN_E_CONSVIO); } return ASN_OK; }
int encodeBitString (OOCTXT* pctxt, ASN1UINT numbits, const ASN1OCTET* data) { int enclen, octidx = 0, stat; Asn1SizeCnst* pSizeList = pctxt->pSizeConstraint; for (;;) { if ((enclen = encodeLength (pctxt, numbits)) < 0) { return LOG_ASN1ERR (pctxt, enclen); } if (enclen > 0) { ASN1BOOL doAlign; stat = bitAndOctetStringAlignmentTest (pSizeList, numbits, TRUE, &doAlign); if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); if (doAlign) { stat = encodeByteAlign (pctxt); if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); } stat = encodeOctets (pctxt, &data[octidx], enclen); if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); } if (enclen < (int)numbits) { numbits -= enclen; octidx += (enclen/8); } else break; } return ASN_OK; }
int encodeConsWholeNumber (OOCTXT* pctxt, ASN1UINT adjusted_value, ASN1UINT range_value) { ASN1UINT nocts, range_bitcnt = getUIntBitCount (range_value - 1); int stat; if (adjusted_value >= range_value && range_value != ASN1UINT_MAX) { return LOG_ASN1ERR (pctxt, ASN_E_RANGERR); } /* If range is <= 255, bit-field case (10.5.7a) */ if (range_value <= 255) { return encodeBits (pctxt, adjusted_value, range_bitcnt); } /* If range is exactly 256, one-octet case (10.5.7b) */ else if (range_value == 256) { stat = encodeByteAlign (pctxt); if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); return encodeBits (pctxt, adjusted_value, 8); } /* If range > 256 and <= 64k (65536), two-octet case (10.5.7c) */ else if (range_value <= 65536) { stat = encodeByteAlign (pctxt); if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); return encodeBits (pctxt, adjusted_value, 16); } /* If range > 64k, indefinite-length case (10.5.7d) */ else { /* Encode length determinant as a constrained whole number. */ /* Constraint is 1 to max number of bytes needed to hold */ /* the target integer value.. */ if (adjusted_value < 256) nocts = 1; else if (adjusted_value < 65536) nocts = 2; else if (adjusted_value < 0x1000000) nocts = 3; else nocts = 4; stat = encodeBits (pctxt, nocts - 1, 2); if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); stat = encodeByteAlign (pctxt); if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); return encodeNonNegBinInt (pctxt, adjusted_value); } }
int encodeBits (OOCTXT* pctxt, ASN1UINT value, ASN1UINT nbits) { int nbytes = (nbits + 7)/ 8, stat = ASN_OK; if (nbits == 0) return stat; /* If start of new byte, init to zero */ if (pctxt->buffer.bitOffset == 8) { pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; } /* Mask off unused bits from the front of the value */ if (nbits < (sizeof(ASN1UINT) * 8)) value &= ((1 << nbits) - 1); /* If bits will fit in current byte, set them and return */ if (nbits < (unsigned)pctxt->buffer.bitOffset) { pctxt->buffer.bitOffset -= nbits; pctxt->buffer.data[pctxt->buffer.byteIndex] |= ( value << pctxt->buffer.bitOffset ); return stat; } /* Check buffer space and allocate more memory if necessary */ stat = encodeCheckBuffer (pctxt, nbytes); if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); /* Set bits in remainder of the current byte and then loop */ /* to set bits in subsequent bytes.. */ nbits -= pctxt->buffer.bitOffset; pctxt->buffer.data[pctxt->buffer.byteIndex++] |= (ASN1OCTET)( value >> nbits ); pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; while (nbits >= 8) { nbits -= 8; pctxt->buffer.data[pctxt->buffer.byteIndex++] = (ASN1OCTET)( value >> nbits ); pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; } /* copy final partial byte */ pctxt->buffer.bitOffset = 8 - nbits; if (nbits > 0) { pctxt->buffer.data[pctxt->buffer.byteIndex] = (ASN1OCTET)((value & ((1 << nbits)-1)) << pctxt->buffer.bitOffset); } else pctxt->buffer.data[pctxt->buffer.byteIndex] = 0; return stat; }
int encodeCheckBuffer (OOCTXT* pctxt, ASN1UINT nbytes) { int stat = ASN_OK; /* Add one to required bytes because increment logic will always */ /* init the byte at the incremented index to zero.. */ if ( ( pctxt->buffer.byteIndex + nbytes + 1 ) >= pctxt->buffer.size ) { if ((stat = encodeExpandBuffer (pctxt, nbytes+1)) != ASN_OK) { return LOG_ASN1ERR (pctxt, stat); } } return (stat); }
int encodeIdent (OOCTXT* pctxt, ASN1UINT ident) { ASN1UINT mask; int nshifts = 0, stat; if (ident !=0) { ASN1UINT lv; nshifts = getIdentByteCount (ident); while (nshifts > 0) { mask = ((ASN1UINT)0x7f) << (7 * (nshifts - 1)); nshifts--; lv = (ASN1UINT)((ident & mask) >> (nshifts * 7)); if (nshifts != 0) { lv |= 0x80; } if ((stat = encodeBits (pctxt, lv, 8)) != ASN_OK) return LOG_ASN1ERR (pctxt, stat); } }
int encodeConstrainedStringEx (OOCTXT* pctxt, const char* string, const char* charSet, ASN1UINT abits, /* aligned char bits */ ASN1UINT ubits, /* unaligned char bits */ ASN1UINT canSetBits) { ASN1UINT i, len = strlen(string); int stat; /* note: need to save size constraint for use in alignCharStr */ /* because it will be cleared in encodeLength from the context.. */ Asn1SizeCnst* psize = pctxt->pSizeConstraint; /* Encode length */ stat = encodeLength (pctxt, len); if (stat < 0) return LOG_ASN1ERR (pctxt, stat); /* Byte align */ if (alignCharStr (pctxt, len, abits, psize)) { stat = encodeByteAlign (pctxt); if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); } /* Encode data */ if (abits >= canSetBits && canSetBits > 4) { for (i = 0; i < len; i++) { if ((stat = encodeBits (pctxt, string[i], abits)) != ASN_OK) return LOG_ASN1ERR (pctxt, stat); } } else if (0 != charSet) { ASN1UINT nchars = strlen(charSet), pos; const char* ptr; for (i = 0; i < len; i++) { ptr = memchr (charSet, string[i], nchars); if (0 == ptr) return LOG_ASN1ERR (pctxt, ASN_E_CONSVIO); else pos = ptr - charSet; if ((stat = encodeBits (pctxt, pos, abits)) != ASN_OK) return LOG_ASN1ERR (pctxt, stat); } } else return LOG_ASN1ERR (pctxt, ASN_E_INVPARAM); return stat; }
int encodeBMPString (OOCTXT* pctxt, ASN1BMPString value, Asn116BitCharSet* permCharSet) { Asn116BitCharSet charSet; int stat; /* Set character set */ init16BitCharSet (&charSet, BMP_FIRST, BMP_LAST, BMP_ABITS, BMP_UBITS); if (permCharSet) { set16BitCharSet (pctxt, &charSet, permCharSet); } /* Encode constrained string */ stat = encode16BitConstrainedString (pctxt, value, &charSet); if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat); return stat; }