github.com/primecitizens/pcz/std@v0.2.1/plat/js/webext/platformkeys/bindings/ffi_bindings.ts (about)

     1  import { importModule, Application, heap, Pointer } from "@ffi";
     2  
     3  importModule("plat/js/webext/platformkeys", (A: Application) => {
     4    const WEBEXT = typeof globalThis.browser === "undefined" ? globalThis.chrome : globalThis.browser;
     5  
     6    return {
     7      "constof_ClientCertificateType": (ref: heap.Ref<string>): number => {
     8        const idx = ["rsaSign", "ecdsaSign"].indexOf(A.H.get(ref));
     9        return idx < 0 ? 0 : idx + 1;
    10      },
    11  
    12      "store_ClientCertificateRequest": (ptr: Pointer, ref: heap.Ref<any>) => {
    13        const x = A.H.get<any>(ref);
    14  
    15        if (typeof x === "undefined") {
    16          A.store.Bool(ptr + 8, false);
    17          A.store.Ref(ptr + 0, undefined);
    18          A.store.Ref(ptr + 4, undefined);
    19        } else {
    20          A.store.Bool(ptr + 8, true);
    21          A.store.Ref(ptr + 0, x["certificateTypes"]);
    22          A.store.Ref(ptr + 4, x["certificateAuthorities"]);
    23        }
    24      },
    25      "load_ClientCertificateRequest": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
    26        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
    27  
    28        x["certificateTypes"] = A.load.Ref(ptr + 0, undefined);
    29        x["certificateAuthorities"] = A.load.Ref(ptr + 4, undefined);
    30        return create === A.H.TRUE ? A.H.push(x) : ref;
    31      },
    32  
    33      "store_Match": (ptr: Pointer, ref: heap.Ref<any>) => {
    34        const x = A.H.get<any>(ref);
    35  
    36        if (typeof x === "undefined") {
    37          A.store.Bool(ptr + 8, false);
    38          A.store.Ref(ptr + 0, undefined);
    39          A.store.Ref(ptr + 4, undefined);
    40        } else {
    41          A.store.Bool(ptr + 8, true);
    42          A.store.Ref(ptr + 0, x["certificate"]);
    43          A.store.Ref(ptr + 4, x["keyAlgorithm"]);
    44        }
    45      },
    46      "load_Match": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
    47        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
    48  
    49        x["certificate"] = A.load.Ref(ptr + 0, undefined);
    50        x["keyAlgorithm"] = A.load.Ref(ptr + 4, undefined);
    51        return create === A.H.TRUE ? A.H.push(x) : ref;
    52      },
    53  
    54      "store_SelectDetails": (ptr: Pointer, ref: heap.Ref<any>) => {
    55        const x = A.H.get<any>(ref);
    56  
    57        if (typeof x === "undefined") {
    58          A.store.Bool(ptr + 18, false);
    59  
    60          A.store.Bool(ptr + 0 + 8, false);
    61          A.store.Ref(ptr + 0 + 0, undefined);
    62          A.store.Ref(ptr + 0 + 4, undefined);
    63          A.store.Ref(ptr + 12, undefined);
    64          A.store.Bool(ptr + 17, false);
    65          A.store.Bool(ptr + 16, false);
    66        } else {
    67          A.store.Bool(ptr + 18, true);
    68  
    69          if (typeof x["request"] === "undefined") {
    70            A.store.Bool(ptr + 0 + 8, false);
    71            A.store.Ref(ptr + 0 + 0, undefined);
    72            A.store.Ref(ptr + 0 + 4, undefined);
    73          } else {
    74            A.store.Bool(ptr + 0 + 8, true);
    75            A.store.Ref(ptr + 0 + 0, x["request"]["certificateTypes"]);
    76            A.store.Ref(ptr + 0 + 4, x["request"]["certificateAuthorities"]);
    77          }
    78          A.store.Ref(ptr + 12, x["clientCerts"]);
    79          A.store.Bool(ptr + 17, "interactive" in x ? true : false);
    80          A.store.Bool(ptr + 16, x["interactive"] ? true : false);
    81        }
    82      },
    83      "load_SelectDetails": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
    84        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
    85  
    86        if (A.load.Bool(ptr + 0 + 8)) {
    87          x["request"] = {};
    88          x["request"]["certificateTypes"] = A.load.Ref(ptr + 0 + 0, undefined);
    89          x["request"]["certificateAuthorities"] = A.load.Ref(ptr + 0 + 4, undefined);
    90        } else {
    91          delete x["request"];
    92        }
    93        x["clientCerts"] = A.load.Ref(ptr + 12, undefined);
    94        if (A.load.Bool(ptr + 17)) {
    95          x["interactive"] = A.load.Bool(ptr + 16);
    96        } else {
    97          delete x["interactive"];
    98        }
    99        return create === A.H.TRUE ? A.H.push(x) : ref;
   100      },
   101  
   102      "store_VerificationResult": (ptr: Pointer, ref: heap.Ref<any>) => {
   103        const x = A.H.get<any>(ref);
   104  
   105        if (typeof x === "undefined") {
   106          A.store.Bool(ptr + 9, false);
   107          A.store.Bool(ptr + 8, false);
   108          A.store.Bool(ptr + 0, false);
   109          A.store.Ref(ptr + 4, undefined);
   110        } else {
   111          A.store.Bool(ptr + 9, true);
   112          A.store.Bool(ptr + 8, "trusted" in x ? true : false);
   113          A.store.Bool(ptr + 0, x["trusted"] ? true : false);
   114          A.store.Ref(ptr + 4, x["debug_errors"]);
   115        }
   116      },
   117      "load_VerificationResult": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
   118        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
   119  
   120        if (A.load.Bool(ptr + 8)) {
   121          x["trusted"] = A.load.Bool(ptr + 0);
   122        } else {
   123          delete x["trusted"];
   124        }
   125        x["debug_errors"] = A.load.Ref(ptr + 4, undefined);
   126        return create === A.H.TRUE ? A.H.push(x) : ref;
   127      },
   128  
   129      "store_VerificationDetails": (ptr: Pointer, ref: heap.Ref<any>) => {
   130        const x = A.H.get<any>(ref);
   131  
   132        if (typeof x === "undefined") {
   133          A.store.Bool(ptr + 8, false);
   134          A.store.Ref(ptr + 0, undefined);
   135          A.store.Ref(ptr + 4, undefined);
   136        } else {
   137          A.store.Bool(ptr + 8, true);
   138          A.store.Ref(ptr + 0, x["serverCertificateChain"]);
   139          A.store.Ref(ptr + 4, x["hostname"]);
   140        }
   141      },
   142      "load_VerificationDetails": (ptr: Pointer, create: heap.Ref<boolean>, ref: heap.Ref<any>): heap.Ref<any> => {
   143        const x = create === A.H.TRUE ? {} : A.H.get<any>(ref);
   144  
   145        x["serverCertificateChain"] = A.load.Ref(ptr + 0, undefined);
   146        x["hostname"] = A.load.Ref(ptr + 4, undefined);
   147        return create === A.H.TRUE ? A.H.push(x) : ref;
   148      },
   149      "has_GetKeyPair": (): heap.Ref<boolean> => {
   150        if (WEBEXT?.platformKeys && "getKeyPair" in WEBEXT?.platformKeys) {
   151          return A.H.TRUE;
   152        }
   153        return A.H.FALSE;
   154      },
   155      "func_GetKeyPair": (fn: Pointer): void => {
   156        A.store.Ref(fn, WEBEXT.platformKeys.getKeyPair);
   157      },
   158      "call_GetKeyPair": (
   159        retPtr: Pointer,
   160        certificate: heap.Ref<object>,
   161        parameters: heap.Ref<object>,
   162        callback: heap.Ref<object>
   163      ): void => {
   164        const _ret = WEBEXT.platformKeys.getKeyPair(
   165          A.H.get<object>(certificate),
   166          A.H.get<object>(parameters),
   167          A.H.get<object>(callback)
   168        );
   169      },
   170      "try_GetKeyPair": (
   171        retPtr: Pointer,
   172        errPtr: Pointer,
   173        certificate: heap.Ref<object>,
   174        parameters: heap.Ref<object>,
   175        callback: heap.Ref<object>
   176      ): heap.Ref<boolean> => {
   177        try {
   178          const _ret = WEBEXT.platformKeys.getKeyPair(
   179            A.H.get<object>(certificate),
   180            A.H.get<object>(parameters),
   181            A.H.get<object>(callback)
   182          );
   183          return A.H.TRUE;
   184        } catch (err: any) {
   185          A.store.Ref(errPtr, err);
   186          return A.H.FALSE;
   187        }
   188      },
   189      "has_GetKeyPairBySpki": (): heap.Ref<boolean> => {
   190        if (WEBEXT?.platformKeys && "getKeyPairBySpki" in WEBEXT?.platformKeys) {
   191          return A.H.TRUE;
   192        }
   193        return A.H.FALSE;
   194      },
   195      "func_GetKeyPairBySpki": (fn: Pointer): void => {
   196        A.store.Ref(fn, WEBEXT.platformKeys.getKeyPairBySpki);
   197      },
   198      "call_GetKeyPairBySpki": (
   199        retPtr: Pointer,
   200        publicKeySpkiDer: heap.Ref<object>,
   201        parameters: heap.Ref<object>,
   202        callback: heap.Ref<object>
   203      ): void => {
   204        const _ret = WEBEXT.platformKeys.getKeyPairBySpki(
   205          A.H.get<object>(publicKeySpkiDer),
   206          A.H.get<object>(parameters),
   207          A.H.get<object>(callback)
   208        );
   209      },
   210      "try_GetKeyPairBySpki": (
   211        retPtr: Pointer,
   212        errPtr: Pointer,
   213        publicKeySpkiDer: heap.Ref<object>,
   214        parameters: heap.Ref<object>,
   215        callback: heap.Ref<object>
   216      ): heap.Ref<boolean> => {
   217        try {
   218          const _ret = WEBEXT.platformKeys.getKeyPairBySpki(
   219            A.H.get<object>(publicKeySpkiDer),
   220            A.H.get<object>(parameters),
   221            A.H.get<object>(callback)
   222          );
   223          return A.H.TRUE;
   224        } catch (err: any) {
   225          A.store.Ref(errPtr, err);
   226          return A.H.FALSE;
   227        }
   228      },
   229      "has_SelectClientCertificates": (): heap.Ref<boolean> => {
   230        if (WEBEXT?.platformKeys && "selectClientCertificates" in WEBEXT?.platformKeys) {
   231          return A.H.TRUE;
   232        }
   233        return A.H.FALSE;
   234      },
   235      "func_SelectClientCertificates": (fn: Pointer): void => {
   236        A.store.Ref(fn, WEBEXT.platformKeys.selectClientCertificates);
   237      },
   238      "call_SelectClientCertificates": (retPtr: Pointer, details: Pointer, callback: heap.Ref<object>): void => {
   239        const details_ffi = {};
   240  
   241        if (A.load.Bool(details + 0 + 8)) {
   242          details_ffi["request"] = {};
   243          details_ffi["request"]["certificateTypes"] = A.load.Ref(details + 0 + 0, undefined);
   244          details_ffi["request"]["certificateAuthorities"] = A.load.Ref(details + 0 + 4, undefined);
   245        }
   246        details_ffi["clientCerts"] = A.load.Ref(details + 12, undefined);
   247        if (A.load.Bool(details + 17)) {
   248          details_ffi["interactive"] = A.load.Bool(details + 16);
   249        }
   250  
   251        const _ret = WEBEXT.platformKeys.selectClientCertificates(details_ffi, A.H.get<object>(callback));
   252      },
   253      "try_SelectClientCertificates": (
   254        retPtr: Pointer,
   255        errPtr: Pointer,
   256        details: Pointer,
   257        callback: heap.Ref<object>
   258      ): heap.Ref<boolean> => {
   259        try {
   260          const details_ffi = {};
   261  
   262          if (A.load.Bool(details + 0 + 8)) {
   263            details_ffi["request"] = {};
   264            details_ffi["request"]["certificateTypes"] = A.load.Ref(details + 0 + 0, undefined);
   265            details_ffi["request"]["certificateAuthorities"] = A.load.Ref(details + 0 + 4, undefined);
   266          }
   267          details_ffi["clientCerts"] = A.load.Ref(details + 12, undefined);
   268          if (A.load.Bool(details + 17)) {
   269            details_ffi["interactive"] = A.load.Bool(details + 16);
   270          }
   271  
   272          const _ret = WEBEXT.platformKeys.selectClientCertificates(details_ffi, A.H.get<object>(callback));
   273          return A.H.TRUE;
   274        } catch (err: any) {
   275          A.store.Ref(errPtr, err);
   276          return A.H.FALSE;
   277        }
   278      },
   279      "has_SubtleCrypto": (): heap.Ref<boolean> => {
   280        if (WEBEXT?.platformKeys && "subtleCrypto" in WEBEXT?.platformKeys) {
   281          return A.H.TRUE;
   282        }
   283        return A.H.FALSE;
   284      },
   285      "func_SubtleCrypto": (fn: Pointer): void => {
   286        A.store.Ref(fn, WEBEXT.platformKeys.subtleCrypto);
   287      },
   288      "call_SubtleCrypto": (retPtr: Pointer): void => {
   289        const _ret = WEBEXT.platformKeys.subtleCrypto();
   290        A.store.Ref(retPtr, _ret);
   291      },
   292      "try_SubtleCrypto": (retPtr: Pointer, errPtr: Pointer): heap.Ref<boolean> => {
   293        try {
   294          const _ret = WEBEXT.platformKeys.subtleCrypto();
   295          A.store.Ref(retPtr, _ret);
   296          return A.H.TRUE;
   297        } catch (err: any) {
   298          A.store.Ref(errPtr, err);
   299          return A.H.FALSE;
   300        }
   301      },
   302      "has_VerifyTLSServerCertificate": (): heap.Ref<boolean> => {
   303        if (WEBEXT?.platformKeys && "verifyTLSServerCertificate" in WEBEXT?.platformKeys) {
   304          return A.H.TRUE;
   305        }
   306        return A.H.FALSE;
   307      },
   308      "func_VerifyTLSServerCertificate": (fn: Pointer): void => {
   309        A.store.Ref(fn, WEBEXT.platformKeys.verifyTLSServerCertificate);
   310      },
   311      "call_VerifyTLSServerCertificate": (retPtr: Pointer, details: Pointer, callback: heap.Ref<object>): void => {
   312        const details_ffi = {};
   313  
   314        details_ffi["serverCertificateChain"] = A.load.Ref(details + 0, undefined);
   315        details_ffi["hostname"] = A.load.Ref(details + 4, undefined);
   316  
   317        const _ret = WEBEXT.platformKeys.verifyTLSServerCertificate(details_ffi, A.H.get<object>(callback));
   318      },
   319      "try_VerifyTLSServerCertificate": (
   320        retPtr: Pointer,
   321        errPtr: Pointer,
   322        details: Pointer,
   323        callback: heap.Ref<object>
   324      ): heap.Ref<boolean> => {
   325        try {
   326          const details_ffi = {};
   327  
   328          details_ffi["serverCertificateChain"] = A.load.Ref(details + 0, undefined);
   329          details_ffi["hostname"] = A.load.Ref(details + 4, undefined);
   330  
   331          const _ret = WEBEXT.platformKeys.verifyTLSServerCertificate(details_ffi, A.H.get<object>(callback));
   332          return A.H.TRUE;
   333        } catch (err: any) {
   334          A.store.Ref(errPtr, err);
   335          return A.H.FALSE;
   336        }
   337      },
   338    };
   339  });