Differences From
Artifact [a12c25b6dd]:
14 14 const.maxdersz = const.maxpemsz -- FIXME this is a safe value but obvs not the correct one
15 15
16 16 local ctx = lib.pk.mbedtls_pk_context
17 17 terra ctx:free() lib.pk.mbedtls_pk_free(self) end
18 18
19 19 local struct hashalg { id: uint8 bytes: intptr }
20 20 local m = {
21 - pemfile = uint8[const.maxpemsz];
21 + pemfile = int8[const.maxpemsz];
22 + derfile = uint8[const.maxdersz];
22 23 const = const;
23 24 algsz = {
24 25 sha1 = 160/8;
25 26 sha256 = 256/8;
26 27 sha512 = 512/8;
27 28 sha384 = 384/8;
28 29 sha224 = 224/8;
................................................................................
71 72 v = v % (to - from) + from -- only works with unsigned!!
72 73 in v end
73 74 end)
74 75
75 76 terra callbacks.randomize(ctx: &opaque, dest: &uint8, sz: intptr)
76 77 return m.spray(dest,sz) end
77 78
78 -terra m.pem(pub: bool, key: &ctx, buf: &uint8): bool
79 +terra m.pem(pub: bool, key: &ctx, buf: &int8): bool
79 80 if pub then
80 - return lib.pk.mbedtls_pk_write_pubkey_pem(key, buf, const.maxpemsz) == 0
81 + return lib.pk.mbedtls_pk_write_pubkey_pem(key, [&uint8](buf), const.maxpemsz) == 0
81 82 else
82 - return lib.pk.mbedtls_pk_write_key_pem(key, buf, const.maxpemsz) == 0
83 + return lib.pk.mbedtls_pk_write_key_pem(key, [&uint8](buf), const.maxpemsz) == 0
83 84 end
84 85 end
85 86
86 87 local binblob = lib.mem.ptr(uint8)
87 88 terra m.der(pub: bool, key: &ctx, buf: &uint8): binblob
88 89 var ofs: ptrdiff
89 90 if pub then
................................................................................
112 113 lib.pk.mbedtls_pk_setup(&pk, lib.pk.mbedtls_pk_info_from_type(lib.pk.MBEDTLS_PK_RSA))
113 114 var rsa = [&lib.rsa.mbedtls_rsa_context](pk.pk_ctx)
114 115 lib.rsa.mbedtls_rsa_gen_key(rsa, callbacks.randomize, nil, const.keybits, 65537)
115 116
116 117 return pk
117 118 end
118 119
119 -terra m.loadpriv(buf: &uint8, len: intptr): lib.stat(ctx)
120 +local binblob = lib.mem.ptr(uint8)
121 +terra m.loadpriv(buf: binblob): lib.stat(ctx)
120 122 lib.dbg('parsing saved private key')
121 123
122 124 var pk: ctx
123 125 lib.pk.mbedtls_pk_init(&pk)
124 - var rt = lib.pk.mbedtls_pk_parse_key(&pk, buf, len + 1, nil, 0)
126 + var rt = lib.pk.mbedtls_pk_parse_key(&pk, buf.ptr, buf.ct, nil, 0)
125 127 if rt == 0 then
126 128 return [lib.stat(ctx)] { ok = true, val = pk }
127 129 else
128 130 lib.pk.mbedtls_pk_free(&pk)
129 - return [lib.stat(ctx)] { ok = false }
131 + return [lib.stat(ctx)] { ok = false, error = rt }
130 132 end
131 133 end
132 134
133 -terra m.loadpub(buf: &uint8, len: intptr): lib.stat(ctx)
135 +terra m.loadpub(buf: binblob): lib.stat(ctx)
134 136 lib.dbg('parsing saved key')
135 137
136 138 var pk: ctx
137 139 lib.pk.mbedtls_pk_init(&pk)
138 - var rt = lib.pk.mbedtls_pk_parse_public_key(&pk, buf, len)
140 + var rt = lib.pk.mbedtls_pk_parse_public_key(&pk, buf.ptr, buf.ct)
139 141 if rt == 0 then
140 142 return [lib.stat(ctx)] { ok = true, val = pk }
141 143 else
142 144 lib.pk.mbedtls_pk_free(&pk)
143 145 return [lib.stat(ctx)] { ok = false, error = rt }
144 146 end
145 147 end