1 | /* asn_public.h
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2015 wolfSSL Inc.
|
---|
4 | *
|
---|
5 | * This file is part of wolfSSL. (formerly known as CyaSSL)
|
---|
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-1301, USA
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | #ifndef WOLF_CRYPT_ASN_PUBLIC_H
|
---|
24 | #define WOLF_CRYPT_ASN_PUBLIC_H
|
---|
25 |
|
---|
26 | #include <wolfssl/wolfcrypt/types.h>
|
---|
27 | #ifdef HAVE_ECC
|
---|
28 | #include <wolfssl/wolfcrypt/ecc.h>
|
---|
29 | #endif
|
---|
30 | #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
|
---|
31 | #include <wolfssl/wolfcrypt/rsa.h>
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #ifdef __cplusplus
|
---|
35 | extern "C" {
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | /* Certificate file Type */
|
---|
39 | enum CertType {
|
---|
40 | CERT_TYPE = 0,
|
---|
41 | PRIVATEKEY_TYPE,
|
---|
42 | DH_PARAM_TYPE,
|
---|
43 | CRL_TYPE,
|
---|
44 | CA_TYPE,
|
---|
45 | ECC_PRIVATEKEY_TYPE,
|
---|
46 | DSA_PRIVATEKEY_TYPE,
|
---|
47 | CERTREQ_TYPE,
|
---|
48 | DSA_TYPE,
|
---|
49 | ECC_TYPE,
|
---|
50 | RSA_TYPE,
|
---|
51 | PUBLICKEY_TYPE,
|
---|
52 | RSA_PUBLICKEY_TYPE,
|
---|
53 | ECC_PUBLICKEY_TYPE
|
---|
54 | };
|
---|
55 |
|
---|
56 |
|
---|
57 | /* Signature type, by OID sum */
|
---|
58 | enum Ctc_SigType {
|
---|
59 | CTC_SHAwDSA = 517,
|
---|
60 | CTC_MD2wRSA = 646,
|
---|
61 | CTC_MD5wRSA = 648,
|
---|
62 | CTC_SHAwRSA = 649,
|
---|
63 | CTC_SHAwECDSA = 520,
|
---|
64 | CTC_SHA256wRSA = 655,
|
---|
65 | CTC_SHA256wECDSA = 524,
|
---|
66 | CTC_SHA384wRSA = 656,
|
---|
67 | CTC_SHA384wECDSA = 525,
|
---|
68 | CTC_SHA512wRSA = 657,
|
---|
69 | CTC_SHA512wECDSA = 526
|
---|
70 | };
|
---|
71 |
|
---|
72 | enum Ctc_Encoding {
|
---|
73 | CTC_UTF8 = 0x0c, /* utf8 */
|
---|
74 | CTC_PRINTABLE = 0x13 /* printable */
|
---|
75 | };
|
---|
76 |
|
---|
77 |
|
---|
78 | #ifdef WOLFSSL_CERT_GEN
|
---|
79 |
|
---|
80 | #ifndef HAVE_ECC
|
---|
81 | typedef struct ecc_key ecc_key;
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | enum Ctc_Misc {
|
---|
85 | CTC_NAME_SIZE = 64,
|
---|
86 | CTC_DATE_SIZE = 32,
|
---|
87 | CTC_MAX_ALT_SIZE = 16384, /* may be huge */
|
---|
88 | CTC_SERIAL_SIZE = 8,
|
---|
89 | #ifdef WOLFSSL_CERT_EXT
|
---|
90 | /* AKID could contains: hash + (Option) AuthCertIssuer,AuthCertSerialNum
|
---|
91 | * We support only hash */
|
---|
92 | CTC_MAX_SKID_SIZE = SHA256_DIGEST_SIZE,
|
---|
93 | CTC_MAX_AKID_SIZE = SHA256_DIGEST_SIZE,
|
---|
94 | CTC_MAX_CERTPOL_SZ = 64,
|
---|
95 | CTC_MAX_CERTPOL_NB = 2 /* Max number of Certificate Policy */
|
---|
96 | #endif /* WOLFSSL_CERT_EXT */
|
---|
97 | };
|
---|
98 |
|
---|
99 | typedef struct CertName {
|
---|
100 | char country[CTC_NAME_SIZE];
|
---|
101 | char countryEnc;
|
---|
102 | char state[CTC_NAME_SIZE];
|
---|
103 | char stateEnc;
|
---|
104 | char locality[CTC_NAME_SIZE];
|
---|
105 | char localityEnc;
|
---|
106 | char sur[CTC_NAME_SIZE];
|
---|
107 | char surEnc;
|
---|
108 | char org[CTC_NAME_SIZE];
|
---|
109 | char orgEnc;
|
---|
110 | char unit[CTC_NAME_SIZE];
|
---|
111 | char unitEnc;
|
---|
112 | char commonName[CTC_NAME_SIZE];
|
---|
113 | char commonNameEnc;
|
---|
114 | char email[CTC_NAME_SIZE]; /* !!!! email has to be last !!!! */
|
---|
115 | } CertName;
|
---|
116 |
|
---|
117 |
|
---|
118 | /* for user to fill for certificate generation */
|
---|
119 | typedef struct Cert {
|
---|
120 | int version; /* x509 version */
|
---|
121 | byte serial[CTC_SERIAL_SIZE]; /* serial number */
|
---|
122 | int sigType; /* signature algo type */
|
---|
123 | CertName issuer; /* issuer info */
|
---|
124 | int daysValid; /* validity days */
|
---|
125 | int selfSigned; /* self signed flag */
|
---|
126 | CertName subject; /* subject info */
|
---|
127 | int isCA; /* is this going to be a CA */
|
---|
128 | /* internal use only */
|
---|
129 | int bodySz; /* pre sign total size */
|
---|
130 | int keyType; /* public key type of subject */
|
---|
131 | #ifdef WOLFSSL_ALT_NAMES
|
---|
132 | byte altNames[CTC_MAX_ALT_SIZE]; /* altNames copy */
|
---|
133 | int altNamesSz; /* altNames size in bytes */
|
---|
134 | byte beforeDate[CTC_DATE_SIZE]; /* before date copy */
|
---|
135 | int beforeDateSz; /* size of copy */
|
---|
136 | byte afterDate[CTC_DATE_SIZE]; /* after date copy */
|
---|
137 | int afterDateSz; /* size of copy */
|
---|
138 | #endif
|
---|
139 | #ifdef WOLFSSL_CERT_EXT
|
---|
140 | byte skid[CTC_MAX_SKID_SIZE]; /* Subject Key Identifier */
|
---|
141 | int skidSz; /* SKID size in bytes */
|
---|
142 | byte akid[CTC_MAX_AKID_SIZE]; /* Authority Key Identifier */
|
---|
143 | int akidSz; /* AKID size in bytes */
|
---|
144 | word16 keyUsage; /* Key Usage */
|
---|
145 | char certPolicies[CTC_MAX_CERTPOL_NB][CTC_MAX_CERTPOL_SZ];
|
---|
146 | word16 certPoliciesNb; /* Number of Cert Policy */
|
---|
147 | #endif
|
---|
148 | #ifdef WOLFSSL_CERT_REQ
|
---|
149 | char challengePw[CTC_NAME_SIZE];
|
---|
150 | #endif
|
---|
151 | } Cert;
|
---|
152 | #endif /* WOLFSSL_CERT_GEN */
|
---|
153 |
|
---|
154 |
|
---|
155 | #ifdef WOLFSSL_CERT_GEN
|
---|
156 |
|
---|
157 |
|
---|
158 |
|
---|
159 | /* Initialize and Set Certficate defaults:
|
---|
160 | version = 3 (0x2)
|
---|
161 | serial = 0 (Will be randomly generated)
|
---|
162 | sigType = SHA_WITH_RSA
|
---|
163 | issuer = blank
|
---|
164 | daysValid = 500
|
---|
165 | selfSigned = 1 (true) use subject as issuer
|
---|
166 | subject = blank
|
---|
167 | isCA = 0 (false)
|
---|
168 | keyType = RSA_KEY (default)
|
---|
169 | */
|
---|
170 | WOLFSSL_API void wc_InitCert(Cert*);
|
---|
171 | WOLFSSL_API int wc_MakeCert(Cert*, byte* derBuffer, word32 derSz, RsaKey*,
|
---|
172 | ecc_key*, WC_RNG*);
|
---|
173 | #ifdef WOLFSSL_CERT_REQ
|
---|
174 | WOLFSSL_API int wc_MakeCertReq(Cert*, byte* derBuffer, word32 derSz,
|
---|
175 | RsaKey*, ecc_key*);
|
---|
176 | #endif
|
---|
177 | WOLFSSL_API int wc_SignCert(int requestSz, int sigType, byte* derBuffer,
|
---|
178 | word32 derSz, RsaKey*, ecc_key*, WC_RNG*);
|
---|
179 | WOLFSSL_API int wc_MakeSelfCert(Cert*, byte* derBuffer, word32 derSz, RsaKey*,
|
---|
180 | WC_RNG*);
|
---|
181 | WOLFSSL_API int wc_SetIssuer(Cert*, const char*);
|
---|
182 | WOLFSSL_API int wc_SetSubject(Cert*, const char*);
|
---|
183 | #ifdef WOLFSSL_ALT_NAMES
|
---|
184 | WOLFSSL_API int wc_SetAltNames(Cert*, const char*);
|
---|
185 | #endif
|
---|
186 | WOLFSSL_API int wc_SetIssuerBuffer(Cert*, const byte*, int);
|
---|
187 | WOLFSSL_API int wc_SetSubjectBuffer(Cert*, const byte*, int);
|
---|
188 | WOLFSSL_API int wc_SetAltNamesBuffer(Cert*, const byte*, int);
|
---|
189 | WOLFSSL_API int wc_SetDatesBuffer(Cert*, const byte*, int);
|
---|
190 |
|
---|
191 | #ifdef WOLFSSL_CERT_EXT
|
---|
192 | WOLFSSL_API int wc_SetAuthKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey,
|
---|
193 | ecc_key *eckey);
|
---|
194 | WOLFSSL_API int wc_SetAuthKeyIdFromCert(Cert *cert, const byte *der, int derSz);
|
---|
195 | WOLFSSL_API int wc_SetAuthKeyId(Cert *cert, const char* file);
|
---|
196 | WOLFSSL_API int wc_SetSubjectKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey,
|
---|
197 | ecc_key *eckey);
|
---|
198 | WOLFSSL_API int wc_SetSubjectKeyId(Cert *cert, const char* file);
|
---|
199 |
|
---|
200 | #ifdef HAVE_NTRU
|
---|
201 | WOLFSSL_API int wc_SetSubjectKeyIdFromNtruPublicKey(Cert *cert, byte *ntruKey,
|
---|
202 | word16 ntruKeySz);
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | /* Set the KeyUsage.
|
---|
206 | * Value is a string separated tokens with ','. Accepted tokens are :
|
---|
207 | * digitalSignature,nonRepudiation,contentCommitment,keyCertSign,cRLSign,
|
---|
208 | * dataEncipherment,keyAgreement,keyEncipherment,encipherOnly and decipherOnly.
|
---|
209 | *
|
---|
210 | * nonRepudiation and contentCommitment are for the same usage.
|
---|
211 | */
|
---|
212 | WOLFSSL_API int wc_SetKeyUsage(Cert *cert, const char *value);
|
---|
213 |
|
---|
214 | /* encode Certificate Policies, return total bytes written
|
---|
215 | * each input value must be ITU-T X.690 formatted : a.b.c...
|
---|
216 | * input must be an array of values with a NULL terminated for the latest
|
---|
217 | * RFC5280 : non-critical */
|
---|
218 | WOLFSSL_API int wc_SetCertificatePolicies(Cert *cert, const char **input);
|
---|
219 |
|
---|
220 | #ifndef WOLFSSL_PEMPUBKEY_TODER_DEFINED
|
---|
221 | #ifndef NO_FILESYSTEM
|
---|
222 | /* forward from wolfssl */
|
---|
223 | WOLFSSL_API int wolfSSL_PemPubKeyToDer(const char* fileName,
|
---|
224 | unsigned char* derBuf, int derSz);
|
---|
225 | #endif
|
---|
226 |
|
---|
227 | /* forward from wolfssl */
|
---|
228 | WOLFSSL_API int wolfSSL_PubKeyPemToDer(const unsigned char*, int,
|
---|
229 | unsigned char*, int);
|
---|
230 | #define WOLFSSL_PEMPUBKEY_TODER_DEFINED
|
---|
231 | #endif /* WOLFSSL_PEMPUBKEY_TODER_DEFINED */
|
---|
232 | #endif /* WOLFSSL_CERT_EXT */
|
---|
233 |
|
---|
234 | #ifdef HAVE_NTRU
|
---|
235 | WOLFSSL_API int wc_MakeNtruCert(Cert*, byte* derBuffer, word32 derSz,
|
---|
236 | const byte* ntruKey, word16 keySz,
|
---|
237 | WC_RNG*);
|
---|
238 | #endif
|
---|
239 |
|
---|
240 | #endif /* WOLFSSL_CERT_GEN */
|
---|
241 |
|
---|
242 |
|
---|
243 | #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN) || !defined(NO_DSA)
|
---|
244 | WOLFSSL_API int wc_DerToPem(const byte* der, word32 derSz, byte* output,
|
---|
245 | word32 outputSz, int type);
|
---|
246 | WOLFSSL_API int wc_DerToPemEx(const byte* der, word32 derSz, byte* output,
|
---|
247 | word32 outputSz, byte *cipherIno, int type);
|
---|
248 | #endif
|
---|
249 |
|
---|
250 | #ifdef HAVE_ECC
|
---|
251 | /* private key helpers */
|
---|
252 | WOLFSSL_API int wc_EccPrivateKeyDecode(const byte*, word32*,
|
---|
253 | ecc_key*, word32);
|
---|
254 | WOLFSSL_API int wc_EccKeyToDer(ecc_key*, byte* output, word32 inLen);
|
---|
255 |
|
---|
256 | /* public key helper */
|
---|
257 | WOLFSSL_API int wc_EccPublicKeyDecode(const byte*, word32*,
|
---|
258 | ecc_key*, word32);
|
---|
259 | #endif
|
---|
260 |
|
---|
261 | /* DER encode signature */
|
---|
262 | WOLFSSL_API word32 wc_EncodeSignature(byte* out, const byte* digest,
|
---|
263 | word32 digSz, int hashOID);
|
---|
264 | WOLFSSL_API int wc_GetCTC_HashOID(int type);
|
---|
265 |
|
---|
266 | #ifdef __cplusplus
|
---|
267 | } /* extern "C" */
|
---|
268 | #endif
|
---|
269 |
|
---|
270 | #endif /* WOLF_CRYPT_ASN_PUBLIC_H */
|
---|
271 |
|
---|