Differences From
Artifact [48369b50e0]:
7 7 rawcode = terra(code: int)
8 8 if code < 0 then code = -code end
9 9 return code and 0xFF80
10 10 end;
11 11 toobig = -lib.pk.MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
12 12 }
13 13 const.maxpemsz = math.floor((const.keybits / 8)*6.4) + 128 -- idk why this formula works but it basically seems to
14 +const.maxdersz = const.maxpemsz -- FIXME this is a safe value but obvs not the correct one
14 15
15 16 local ctx = lib.pk.mbedtls_pk_context
16 17
17 18 local struct hashalg { id: uint8 bytes: intptr }
18 19 local m = {
19 20 pemfile = uint8[const.maxpemsz];
21 + const = const;
20 22 algsz = {
21 23 sha1 = 160/8;
22 24 sha256 = 256/8;
23 25 sha512 = 512/8;
24 26 sha384 = 384/8;
25 27 sha224 = 224/8;
26 28 }
................................................................................
61 63 terra m.pem(pub: bool, key: &ctx, buf: &uint8): bool
62 64 if pub then
63 65 return lib.pk.mbedtls_pk_write_pubkey_pem(key, buf, const.maxpemsz) == 0
64 66 else
65 67 return lib.pk.mbedtls_pk_write_key_pem(key, buf, const.maxpemsz) == 0
66 68 end
67 69 end
70 +
71 +terra m.der(pub: bool, key: &ctx, buf: &uint8): intptr
72 + if pub then
73 + return lib.pk.mbedtls_pk_write_pubkey_der(key, buf, const.maxdersz)
74 + else
75 + return lib.pk.mbedtls_pk_write_key_der(key, buf, const.maxdersz)
76 + end
77 +end
68 78
69 79 m.destroy = lib.dispatch {
70 80 [ctx] = function(v) return `lib.pk.mbedtls_pk_free(&v) end;
71 81
72 82 [false] = function(ptr) return `ptr:free() end;
73 83 }
74 84