Repository navigation
Expand file tree
/
Copy pathbearssl_pkey_decoder.nim
More file actions
65 lines (52 loc) · 1.76 KB
/
Copy pathbearssl_pkey_decoder.nim
File metadata and controls
65 lines (52 loc) · 1.76 KB
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
import bearssl, strutils
from os import DirSep
const bearPath =
currentSourcePath.rsplit(DirSep, 1)[0] & DirSep & "bearssl_pkey_decoder" & DirSep &
"csources"
{.compile(bearPath & DirSep & "bearssl_pkey_decoder.c", "-I" & bearPath).}
# This pragma should be the same as in nim-bearssl/decls.nim
{.pragma: bearSslFunc, cdecl, gcsafe, noSideEffect, raises: [].}
type
INNER_C_UNION_KEY* {.union.} = object
rsa*: RsaPublicKey
ec*: EcPublicKey
INNER_C_STRUCT_CPU* = object
dp*: ptr uint32
rp*: ptr uint32
ip*: ptr cuchar
PkeyDecoderContext* = object
key*: INNER_C_UNION_KEY
cpu*: INNER_C_STRUCT_CPU
dpStack*: array[32, uint32]
rpStack*: array[32, uint32]
err*: cint
hbuf*: pointer
hlen*: uint
pad*: array[256, byte]
key_type*: uint8
key_data*: array[3 * X509_BUFSIZE_SIG, byte]
proc pkeyDecoderInit*(
ctx: ptr PkeyDecoderContext
) {.bearSslFunc, importc: "br_pkey_decoder_init".}
proc pkeyDecoderPush*(
ctx: ptr PkeyDecoderContext, data: pointer, len: csize_t
) {.bearSslFunc, importc: "br_pkey_decoder_push".}
proc skeyDecoderPushShim*(
ctx: pointer, data: pointer, len: csize_t
) {.cdecl, noSideEffect, gcsafe, importc.}
proc pkeyDecoderLastError*(ctx: ptr PkeyDecoderContext): cint =
if ctx.err != 0:
return ctx.err
if ctx.key_type == 0:
return ERR_X509_TRUNCATED
proc pkeyDecoderKeyType*(ctx: ptr PkeyDecoderContext): cint =
if ctx.err == 0:
return cast[cint](ctx.key_type)
else:
return 0
proc pkeyDecoderGetRsa*(ctx: ptr PkeyDecoderContext): ptr RsaPublicKey =
if ctx.err == 0 and ctx.key_type == KEYTYPE_RSA:
return addr ctx.key.rsa
proc pkeyDecoderGetEc*(ctx: ptr PkeyDecoderContext): ptr EcPublicKey =
if ctx.err == 0 and ctx.key_type == KEYTYPE_EC:
return addr ctx.key.ec