1 | /* asn_public.h
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2017 wolfSSL Inc.
|
---|
4 | *
|
---|
5 | * This file is part of wolfSSL.
|
---|
6 | *
|
---|
7 | * wolfSSL is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * wolfSSL is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 | #ifndef WOLF_CRYPT_ASN_PUBLIC_H
|
---|
25 | #define WOLF_CRYPT_ASN_PUBLIC_H
|
---|
26 |
|
---|
27 | #include <wolfssl/wolfcrypt/types.h>
|
---|
28 |
|
---|
29 | #ifdef __cplusplus
|
---|
30 | extern "C" {
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | /* guard on redeclaration */
|
---|
34 | #ifndef WC_ECCKEY_TYPE_DEFINED
|
---|
35 | typedef struct ecc_key ecc_key;
|
---|
36 | #define WC_ECCKEY_TYPE_DEFINED
|
---|
37 | #endif
|
---|
38 | #ifndef WC_ED25519KEY_TYPE_DEFINED
|
---|
39 | typedef struct ed25519_key ed25519_key;
|
---|
40 | #define WC_ED25519KEY_TYPE_DEFINED
|
---|
41 | #endif
|
---|
42 | #ifndef WC_RSAKEY_TYPE_DEFINED
|
---|
43 | typedef struct RsaKey RsaKey;
|
---|
44 | #define WC_RSAKEY_TYPE_DEFINED
|
---|
45 | #endif
|
---|
46 | #ifndef WC_RNG_TYPE_DEFINED
|
---|
47 | typedef struct WC_RNG WC_RNG;
|
---|
48 | #define WC_RNG_TYPE_DEFINED
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | /* Certificate file Type */
|
---|
52 | enum CertType {
|
---|
53 | CERT_TYPE = 0,
|
---|
54 | PRIVATEKEY_TYPE,
|
---|
55 | DH_PARAM_TYPE,
|
---|
56 | DSA_PARAM_TYPE,
|
---|
57 | CRL_TYPE,
|
---|
58 | CA_TYPE,
|
---|
59 | ECC_PRIVATEKEY_TYPE,
|
---|
60 | DSA_PRIVATEKEY_TYPE,
|
---|
61 | CERTREQ_TYPE,
|
---|
62 | DSA_TYPE,
|
---|
63 | ECC_TYPE,
|
---|
64 | RSA_TYPE,
|
---|
65 | PUBLICKEY_TYPE,
|
---|
66 | RSA_PUBLICKEY_TYPE,
|
---|
67 | ECC_PUBLICKEY_TYPE,
|
---|
68 | TRUSTED_PEER_TYPE,
|
---|
69 | EDDSA_PRIVATEKEY_TYPE,
|
---|
70 | ED25519_TYPE
|
---|
71 | };
|
---|
72 |
|
---|
73 |
|
---|
74 | /* Signature type, by OID sum */
|
---|
75 | enum Ctc_SigType {
|
---|
76 | CTC_SHAwDSA = 517,
|
---|
77 | CTC_MD2wRSA = 646,
|
---|
78 | CTC_MD5wRSA = 648,
|
---|
79 | CTC_SHAwRSA = 649,
|
---|
80 | CTC_SHAwECDSA = 520,
|
---|
81 | CTC_SHA224wRSA = 658,
|
---|
82 | CTC_SHA224wECDSA = 527,
|
---|
83 | CTC_SHA256wRSA = 655,
|
---|
84 | CTC_SHA256wECDSA = 524,
|
---|
85 | CTC_SHA384wRSA = 656,
|
---|
86 | CTC_SHA384wECDSA = 525,
|
---|
87 | CTC_SHA512wRSA = 657,
|
---|
88 | CTC_SHA512wECDSA = 526,
|
---|
89 | CTC_ED25519 = 256
|
---|
90 | };
|
---|
91 |
|
---|
92 | enum Ctc_Encoding {
|
---|
93 | CTC_UTF8 = 0x0c, /* utf8 */
|
---|
94 | CTC_PRINTABLE = 0x13 /* printable */
|
---|
95 | };
|
---|
96 |
|
---|
97 | enum Ctc_Misc {
|
---|
98 | CTC_COUNTRY_SIZE = 2,
|
---|
99 | CTC_NAME_SIZE = 64,
|
---|
100 | CTC_DATE_SIZE = 32,
|
---|
101 | CTC_MAX_ALT_SIZE = 16384, /* may be huge */
|
---|
102 | CTC_SERIAL_SIZE = 16,
|
---|
103 | #ifdef WOLFSSL_CERT_EXT
|
---|
104 | /* AKID could contains: hash + (Option) AuthCertIssuer,AuthCertSerialNum
|
---|
105 | * We support only hash */
|
---|
106 | CTC_MAX_SKID_SIZE = 32, /* SHA256_DIGEST_SIZE */
|
---|
107 | CTC_MAX_AKID_SIZE = 32, /* SHA256_DIGEST_SIZE */
|
---|
108 | CTC_MAX_CERTPOL_SZ = 64,
|
---|
109 | CTC_MAX_CERTPOL_NB = 2 /* Max number of Certificate Policy */
|
---|
110 | #endif /* WOLFSSL_CERT_EXT */
|
---|
111 | };
|
---|
112 |
|
---|
113 |
|
---|
114 | #ifdef WOLFSSL_CERT_GEN
|
---|
115 |
|
---|
116 | typedef struct CertName {
|
---|
117 | char country[CTC_NAME_SIZE];
|
---|
118 | char countryEnc;
|
---|
119 | char state[CTC_NAME_SIZE];
|
---|
120 | char stateEnc;
|
---|
121 | char locality[CTC_NAME_SIZE];
|
---|
122 | char localityEnc;
|
---|
123 | char sur[CTC_NAME_SIZE];
|
---|
124 | char surEnc;
|
---|
125 | char org[CTC_NAME_SIZE];
|
---|
126 | char orgEnc;
|
---|
127 | char unit[CTC_NAME_SIZE];
|
---|
128 | char unitEnc;
|
---|
129 | char commonName[CTC_NAME_SIZE];
|
---|
130 | char commonNameEnc;
|
---|
131 | char email[CTC_NAME_SIZE]; /* !!!! email has to be last !!!! */
|
---|
132 | } CertName;
|
---|
133 |
|
---|
134 |
|
---|
135 | /* for user to fill for certificate generation */
|
---|
136 | typedef struct Cert {
|
---|
137 | int version; /* x509 version */
|
---|
138 | byte serial[CTC_SERIAL_SIZE]; /* serial number */
|
---|
139 | int serialSz; /* serial size */
|
---|
140 | int sigType; /* signature algo type */
|
---|
141 | CertName issuer; /* issuer info */
|
---|
142 | int daysValid; /* validity days */
|
---|
143 | int selfSigned; /* self signed flag */
|
---|
144 | CertName subject; /* subject info */
|
---|
145 | int isCA; /* is this going to be a CA */
|
---|
146 | /* internal use only */
|
---|
147 | int bodySz; /* pre sign total size */
|
---|
148 | int keyType; /* public key type of subject */
|
---|
149 | #ifdef WOLFSSL_ALT_NAMES
|
---|
150 | byte altNames[CTC_MAX_ALT_SIZE]; /* altNames copy */
|
---|
151 | int altNamesSz; /* altNames size in bytes */
|
---|
152 | byte beforeDate[CTC_DATE_SIZE]; /* before date copy */
|
---|
153 | int beforeDateSz; /* size of copy */
|
---|
154 | byte afterDate[CTC_DATE_SIZE]; /* after date copy */
|
---|
155 | int afterDateSz; /* size of copy */
|
---|
156 | #endif
|
---|
157 | #ifdef WOLFSSL_CERT_EXT
|
---|
158 | byte skid[CTC_MAX_SKID_SIZE]; /* Subject Key Identifier */
|
---|
159 | int skidSz; /* SKID size in bytes */
|
---|
160 | byte akid[CTC_MAX_AKID_SIZE]; /* Authority Key Identifier */
|
---|
161 | int akidSz; /* AKID size in bytes */
|
---|
162 | word16 keyUsage; /* Key Usage */
|
---|
163 | byte extKeyUsage; /* Extended Key Usage */
|
---|
164 | char certPolicies[CTC_MAX_CERTPOL_NB][CTC_MAX_CERTPOL_SZ];
|
---|
165 | word16 certPoliciesNb; /* Number of Cert Policy */
|
---|
166 | #endif
|
---|
167 | #ifdef WOLFSSL_CERT_REQ
|
---|
168 | char challengePw[CTC_NAME_SIZE];
|
---|
169 | #endif
|
---|
170 | void* heap; /* heap hint */
|
---|
171 | } Cert;
|
---|
172 |
|
---|
173 |
|
---|
174 | /* Initialize and Set Certificate defaults:
|
---|
175 | version = 3 (0x2)
|
---|
176 | serial = 0 (Will be randomly generated)
|
---|
177 | sigType = SHA_WITH_RSA
|
---|
178 | issuer = blank
|
---|
179 | daysValid = 500
|
---|
180 | selfSigned = 1 (true) use subject as issuer
|
---|
181 | subject = blank
|
---|
182 | isCA = 0 (false)
|
---|
183 | keyType = RSA_KEY (default)
|
---|
184 | */
|
---|
185 | WOLFSSL_API int wc_InitCert(Cert*);
|
---|
186 | WOLFSSL_API int wc_MakeCert_ex(Cert* cert, byte* derBuffer, word32 derSz,
|
---|
187 | int keyType, void* key, WC_RNG* rng);
|
---|
188 | WOLFSSL_API int wc_MakeCert(Cert*, byte* derBuffer, word32 derSz, RsaKey*,
|
---|
189 | ecc_key*, WC_RNG*);
|
---|
190 | #ifdef WOLFSSL_CERT_REQ
|
---|
191 | WOLFSSL_API int wc_MakeCertReq_ex(Cert*, byte* derBuffer, word32 derSz,
|
---|
192 | int, void*);
|
---|
193 | WOLFSSL_API int wc_MakeCertReq(Cert*, byte* derBuffer, word32 derSz,
|
---|
194 | RsaKey*, ecc_key*);
|
---|
195 | #endif
|
---|
196 | WOLFSSL_API int wc_SignCert_ex(int requestSz, int sType, byte* buffer,
|
---|
197 | word32 buffSz, int keyType, void* key,
|
---|
198 | WC_RNG* rng);
|
---|
199 | WOLFSSL_API int wc_SignCert(int requestSz, int sigType, byte* derBuffer,
|
---|
200 | word32 derSz, RsaKey*, ecc_key*, WC_RNG*);
|
---|
201 | WOLFSSL_API int wc_MakeSelfCert(Cert*, byte* derBuffer, word32 derSz, RsaKey*,
|
---|
202 | WC_RNG*);
|
---|
203 | WOLFSSL_API int wc_SetIssuer(Cert*, const char*);
|
---|
204 | WOLFSSL_API int wc_SetSubject(Cert*, const char*);
|
---|
205 | #ifdef WOLFSSL_ALT_NAMES
|
---|
206 | WOLFSSL_API int wc_SetAltNames(Cert*, const char*);
|
---|
207 | #endif
|
---|
208 | WOLFSSL_API int wc_SetIssuerBuffer(Cert*, const byte*, int);
|
---|
209 | WOLFSSL_API int wc_SetSubjectBuffer(Cert*, const byte*, int);
|
---|
210 | WOLFSSL_API int wc_SetAltNamesBuffer(Cert*, const byte*, int);
|
---|
211 | WOLFSSL_API int wc_SetDatesBuffer(Cert*, const byte*, int);
|
---|
212 |
|
---|
213 | #ifdef WOLFSSL_CERT_EXT
|
---|
214 | WOLFSSL_API int wc_SetAuthKeyIdFromPublicKey_ex(Cert *cert, int keyType,
|
---|
215 | void* key);
|
---|
216 | WOLFSSL_API int wc_SetAuthKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey,
|
---|
217 | ecc_key *eckey);
|
---|
218 | WOLFSSL_API int wc_SetAuthKeyIdFromCert(Cert *cert, const byte *der, int derSz);
|
---|
219 | WOLFSSL_API int wc_SetAuthKeyId(Cert *cert, const char* file);
|
---|
220 | WOLFSSL_API int wc_SetSubjectKeyIdFromPublicKey_ex(Cert *cert, int keyType,
|
---|
221 | void* key);
|
---|
222 | WOLFSSL_API int wc_SetSubjectKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey,
|
---|
223 | ecc_key *eckey);
|
---|
224 | WOLFSSL_API int wc_SetSubjectKeyId(Cert *cert, const char* file);
|
---|
225 |
|
---|
226 | #ifdef HAVE_NTRU
|
---|
227 | WOLFSSL_API int wc_SetSubjectKeyIdFromNtruPublicKey(Cert *cert, byte *ntruKey,
|
---|
228 | word16 ntruKeySz);
|
---|
229 | #endif
|
---|
230 |
|
---|
231 | /* Set the KeyUsage.
|
---|
232 | * Value is a string separated tokens with ','. Accepted tokens are :
|
---|
233 | * digitalSignature,nonRepudiation,contentCommitment,keyCertSign,cRLSign,
|
---|
234 | * dataEncipherment,keyAgreement,keyEncipherment,encipherOnly and decipherOnly.
|
---|
235 | *
|
---|
236 | * nonRepudiation and contentCommitment are for the same usage.
|
---|
237 | */
|
---|
238 | WOLFSSL_API int wc_SetKeyUsage(Cert *cert, const char *value);
|
---|
239 |
|
---|
240 | /* Set ExtendedKeyUsage
|
---|
241 | * Value is a string separated tokens with ','. Accepted tokens are :
|
---|
242 | * any,serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,OCSPSigning
|
---|
243 | */
|
---|
244 | WOLFSSL_API int wc_SetExtKeyUsage(Cert *cert, const char *value);
|
---|
245 |
|
---|
246 | #endif /* WOLFSSL_CERT_EXT */
|
---|
247 |
|
---|
248 | #ifdef HAVE_NTRU
|
---|
249 | WOLFSSL_API int wc_MakeNtruCert(Cert*, byte* derBuffer, word32 derSz,
|
---|
250 | const byte* ntruKey, word16 keySz,
|
---|
251 | WC_RNG*);
|
---|
252 | #endif
|
---|
253 |
|
---|
254 | #endif /* WOLFSSL_CERT_GEN */
|
---|
255 |
|
---|
256 | #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
|
---|
257 | #ifndef WOLFSSL_PEMPUBKEY_TODER_DEFINED
|
---|
258 | #ifndef NO_FILESYSTEM
|
---|
259 | /* forward from wolfssl */
|
---|
260 | WOLFSSL_API int wolfSSL_PemPubKeyToDer(const char* fileName,
|
---|
261 | unsigned char* derBuf, int derSz);
|
---|
262 | #endif
|
---|
263 |
|
---|
264 | /* forward from wolfssl */
|
---|
265 | WOLFSSL_API int wolfSSL_PubKeyPemToDer(const unsigned char*, int,
|
---|
266 | unsigned char*, int);
|
---|
267 | #define WOLFSSL_PEMPUBKEY_TODER_DEFINED
|
---|
268 | #endif /* WOLFSSL_PEMPUBKEY_TODER_DEFINED */
|
---|
269 | #endif /* WOLFSSL_CERT_EXT || WOLFSSL_PUB_PEM_TO_DER */
|
---|
270 |
|
---|
271 | #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN) || !defined(NO_DSA) \
|
---|
272 | || defined(OPENSSL_EXTRA)
|
---|
273 | WOLFSSL_API int wc_DerToPem(const byte* der, word32 derSz, byte* output,
|
---|
274 | word32 outputSz, int type);
|
---|
275 | WOLFSSL_API int wc_DerToPemEx(const byte* der, word32 derSz, byte* output,
|
---|
276 | word32 outputSz, byte *cipherIno, int type);
|
---|
277 | #endif
|
---|
278 |
|
---|
279 | #ifdef HAVE_ECC
|
---|
280 | /* private key helpers */
|
---|
281 | WOLFSSL_API int wc_EccPrivateKeyDecode(const byte*, word32*,
|
---|
282 | ecc_key*, word32);
|
---|
283 | WOLFSSL_API int wc_EccKeyToDer(ecc_key*, byte* output, word32 inLen);
|
---|
284 | WOLFSSL_API int wc_EccPrivateKeyToDer(ecc_key* key, byte* output,
|
---|
285 | word32 inLen);
|
---|
286 |
|
---|
287 | /* public key helper */
|
---|
288 | WOLFSSL_API int wc_EccPublicKeyDecode(const byte*, word32*,
|
---|
289 | ecc_key*, word32);
|
---|
290 | #if (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
|
---|
291 | WOLFSSL_API int wc_EccPublicKeyToDer(ecc_key*, byte* output,
|
---|
292 | word32 inLen, int with_AlgCurve);
|
---|
293 | #endif
|
---|
294 | #endif
|
---|
295 |
|
---|
296 | #ifdef HAVE_ED25519
|
---|
297 | /* private key helpers */
|
---|
298 | WOLFSSL_API int wc_Ed25519PrivateKeyDecode(const byte*, word32*,
|
---|
299 | ed25519_key*, word32);
|
---|
300 | WOLFSSL_API int wc_Ed25519KeyToDer(ed25519_key* key, byte* output,
|
---|
301 | word32 inLen);
|
---|
302 | WOLFSSL_API int wc_Ed25519PrivateKeyToDer(ed25519_key* key, byte* output,
|
---|
303 | word32 inLen);
|
---|
304 |
|
---|
305 | /* public key helper */
|
---|
306 | WOLFSSL_API int wc_Ed25519PublicKeyDecode(const byte*, word32*,
|
---|
307 | ed25519_key*, word32);
|
---|
308 | #if (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
|
---|
309 | WOLFSSL_API int wc_Ed25519PublicKeyToDer(ed25519_key*, byte* output,
|
---|
310 | word32 inLen, int with_AlgCurve);
|
---|
311 | #endif
|
---|
312 | #endif
|
---|
313 |
|
---|
314 | /* DER encode signature */
|
---|
315 | WOLFSSL_API word32 wc_EncodeSignature(byte* out, const byte* digest,
|
---|
316 | word32 digSz, int hashOID);
|
---|
317 | WOLFSSL_API int wc_GetCTC_HashOID(int type);
|
---|
318 |
|
---|
319 | WOLFSSL_API int wc_GetPkcs8TraditionalOffset(byte* input,
|
---|
320 | word32* inOutIdx, word32 sz);
|
---|
321 | WOLFSSL_API int wc_CreatePKCS8Key(byte* out, word32* outSz,
|
---|
322 | byte* key, word32 keySz, int algoID, const byte* curveOID, word32 oidSz);
|
---|
323 |
|
---|
324 | /* Time */
|
---|
325 | /* Returns seconds (Epoch/UTC)
|
---|
326 | * timePtr: is "time_t", which is typically "long"
|
---|
327 | * Example:
|
---|
328 | long lTime;
|
---|
329 | rc = wc_GetTime(&lTime, (word32)sizeof(lTime));
|
---|
330 | */
|
---|
331 | WOLFSSL_API int wc_GetTime(void* timePtr, word32 timeSize);
|
---|
332 |
|
---|
333 | #ifdef __cplusplus
|
---|
334 | } /* extern "C" */
|
---|
335 | #endif
|
---|
336 |
|
---|
337 | #endif /* WOLF_CRYPT_ASN_PUBLIC_H */
|
---|
338 |
|
---|