All files / src/crypto/token-signer ecdsa-sig-formatter.ts

92.22% Statements 83/90
82.6% Branches 19/23
100% Functions 4/4
91.95% Lines 80/87

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189    42x   42x 42x 42x 42x 42x 42x 42x     33x     42x 25x 25x       25x   25x   24x 24x 1x     23x 23x       23x 1x                 22x 1x     21x   21x 1x                 20x 1x                 19x 19x   19x 1x     18x   18x 2x         16x 1x                 15x 15x   15x           15x 15x   15x   15x     15x   15x   15x     15x   15x       16x 16x       16x 16x 9x     16x     42x 8x 8x   8x 8x 8x 8x   8x   8x   8x   8x 8x 8x     8x               8x 8x 8x 4x 4x   4x   8x 8x   8x 5x           8x    
// Derived from https://github.com/Brightspace/node-ecdsa-sig-formatter
 
import { base64ToBytes, bytesToBase64Url, copy } from 'micro-stacks/common';
 
const MAX_OCTET = 0x80;
const CLASS_UNIVERSAL = 0;
const PRIMITIVE_BIT = 0x20;
const TAG_SEQ = 0x10;
const TAG_INT = 0x02;
const ENCODED_TAG_SEQ = TAG_SEQ | PRIMITIVE_BIT | (CLASS_UNIVERSAL << 6);
const ENCODED_TAG_INT = TAG_INT | (CLASS_UNIVERSAL << 6);
 
function getSignature(sig: string | Uint8Array) {
  return typeof sig === 'string' ? base64ToBytes(sig) : sig;
}
 
export function derToJoseES256(sig: string | Uint8Array) {
  const signature = getSignature(sig);
  const paramBytes = 32; // (256 / 8)
 
  // the DER encoded param should at most be the param size, plus a padding
  // zero, since due to being a signed integer
  const maxEncodedParamLength = paramBytes + 1;
 
  const inputLength = signature.length;
 
  let offset = 0;
  if (signature[offset++] !== ENCODED_TAG_SEQ) {
    throw new Error('Could not find expected "seq"');
  }
 
  let seqLength = signature[offset++];
  Iif (seqLength === (MAX_OCTET | 1)) {
    seqLength = signature[offset++];
  }
 
  if (inputLength - offset < seqLength) {
    throw new Error(
      '"seq" specified length of "' +
        seqLength +
        '", only "' +
        (inputLength - offset) +
        '" remaining'
    );
  }
 
  if (signature[offset++] !== ENCODED_TAG_INT) {
    throw new Error('Could not find expected "int" for "r"');
  }
 
  const rLength = signature[offset++];
 
  if (inputLength - offset - 2 < rLength) {
    throw new Error(
      '"r" specified length of "' +
        rLength +
        '", only "' +
        (inputLength - offset - 2) +
        '" available'
    );
  }
 
  if (maxEncodedParamLength < rLength) {
    throw new Error(
      '"r" specified length of "' +
        rLength +
        '", max of "' +
        maxEncodedParamLength +
        '" is acceptable'
    );
  }
 
  const rOffset = offset;
  offset += rLength;
 
  if (signature[offset++] !== ENCODED_TAG_INT) {
    throw new Error('Could not find expected "int" for "s"');
  }
 
  const sLength = signature[offset++];
 
  if (inputLength - offset !== sLength) {
    throw new Error(
      '"s" specified length of "' + sLength + '", expected "' + (inputLength - offset) + '"'
    );
  }
 
  if (maxEncodedParamLength < sLength) {
    throw new Error(
      '"s" specified length of "' +
        sLength +
        '", max of "' +
        maxEncodedParamLength +
        '" is acceptable'
    );
  }
 
  const sOffset = offset;
  offset += sLength;
 
  Iif (offset !== inputLength) {
    throw new Error(
      'Expected to consume entire buffer, but "' + (inputLength - offset) + '" bytes remain'
    );
  }
 
  const rPadding = paramBytes - rLength;
  const sPadding = paramBytes - sLength;
 
  const dst = new Uint8Array(rPadding + rLength + sPadding + sLength);
 
  for (offset = 0; offset < rPadding; ++offset) {
    dst[offset] = 0;
  }
  dst.set(signature.slice(rOffset + Math.max(-rPadding, 0), rOffset + rLength), offset);
 
  offset = paramBytes;
 
  for (let o = offset; offset < o + sPadding; ++offset) {
    dst[offset] = 0;
  }
  dst.set(signature.slice(sOffset + Math.max(-sPadding, 0), sOffset + sLength), offset);
 
  return bytesToBase64Url(dst).replace(/=/g, '');
}
 
function countPadding(buf: Uint8Array, start: number, stop: number) {
  let padding = 0;
  while (start + padding < stop && buf[start + padding] === 0) {
    ++padding;
  }
 
  const needsSign = buf[start + padding] >= MAX_OCTET;
  if (needsSign) {
    --padding;
  }
 
  return padding;
}
 
export function joseToDerES256(sig: string | Uint8Array) {
  const signature = getSignature(sig);
  const paramBytes = 32; // (256 / 8)
 
  const rPadding = countPadding(signature, 0, paramBytes);
  const sPadding = countPadding(signature, paramBytes, signature.length);
  const rLength = paramBytes - rPadding;
  const sLength = paramBytes - sPadding;
 
  const rsBytes = 1 + 1 + rLength + 1 + 1 + sLength;
 
  const shortLength = rsBytes < MAX_OCTET;
 
  const dst = new Uint8Array((shortLength ? 2 : 3) + rsBytes);
 
  let offset = 0;
  dst[offset++] = ENCODED_TAG_SEQ;
  if (shortLength) {
    // Bit 8 has value "0"
    // bits 7-1 give the length.
    dst[offset++] = rsBytes;
  } else E{
    // Bit 8 of first octet has value "1"
    // bits 7-1 give the number of additional length octets.
    dst[offset++] = MAX_OCTET | 1;
    // length, base 256
    dst[offset++] = rsBytes & 0xff;
  }
  dst[offset++] = ENCODED_TAG_INT;
  dst[offset++] = rLength;
  if (rPadding < 0) {
    dst[offset++] = 0;
    offset += copy(signature, dst, offset, 0, paramBytes);
  } else {
    offset += copy(signature, dst, offset, rPadding, paramBytes);
  }
  dst[offset++] = ENCODED_TAG_INT;
  dst[offset++] = sLength;
 
  if (sPadding < 0) {
    dst[offset++] = 0;
    copy(signature, dst, offset, paramBytes);
  } else {
    copy(signature, dst, offset, paramBytes + sPadding);
  }
 
  return bytesToBase64Url(dst).replace(/=/g, '');
}