1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2008-2020. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20
21%%
22-module(public_key_SUITE).
23
24-include_lib("common_test/include/ct.hrl").
25-include_lib("public_key/include/public_key.hrl").
26
27-export([
28         suite/0,
29         all/0,
30         groups/0,
31         init_per_suite/1,
32         end_per_suite/1,
33         init_per_group/2,
34         end_per_group/2,
35         init_per_testcase/2,
36         init_common_per_testcase/1,
37         end_per_testcase/2,
38         app/0,
39         app/1,
40         appup/0,
41         appup/1,
42         dsa_pem/0,
43         dsa_pem/1,
44         dsa_priv_pkcs8/0,
45         dsa_priv_pkcs8/1,
46         rsa_pem/0,
47         rsa_pem/1,
48         rsa_pss_pss_pem/0,
49         rsa_pss_pss_pem/1,
50         rsa_priv_pkcs8/0,
51         rsa_priv_pkcs8/1,
52         ec_pem/0,
53         ec_pem/1,
54         ec_pem2/0,
55         ec_pem2/1,
56         ec_priv_pkcs8/0,
57         ec_priv_pkcs8/1,
58         init_ec_pem_encode_generated/1,
59         ec_pem_encode_generated/0,
60         ec_pem_encode_generated/1,
61         encrypted_pem/0,
62         encrypted_pem/1,
63         dh_pem/0,
64         dh_pem/1,
65         pkcs10_pem/0,
66         pkcs10_pem/1,
67         pkcs7_pem/0,
68         pkcs7_pem/1,
69         cert_pem/0,
70         cert_pem/1,
71         encrypt_decrypt/0,
72         encrypt_decrypt/1,
73         rsa_sign_verify/0,
74         rsa_sign_verify/1,
75         rsa_pss_sign_verify/0,
76         rsa_pss_sign_verify/1,
77         dsa_sign_verify/0,
78         dsa_sign_verify/1,
79         pkix/0,
80         pkix/1,
81         pkix_countryname/0,
82         pkix_countryname/1,
83         pkix_emailaddress/0,
84         pkix_emailaddress/1,
85         pkix_path_validation/0,
86         pkix_path_validation/1,
87         pkix_path_validation_root_expired/0,
88         pkix_path_validation_root_expired/1,
89         pkix_verify_hostname_cn/1,
90         pkix_verify_hostname_subjAltName/1,
91         pkix_verify_hostname_options/1,
92         pkix_verify_hostname_subjAltName_IP/1,
93         pkix_iso_rsa_oid/0,
94         pkix_iso_rsa_oid/1,
95         pkix_iso_dsa_oid/0,
96         pkix_iso_dsa_oid/1,
97         pkix_dsa_sha2_oid/0,
98         pkix_dsa_sha2_oid/1,
99         pkix_crl/0,
100         pkix_crl/1,
101         general_name/0,
102         general_name/1,
103         pkix_hash_type/0,
104         pkix_hash_type/1,
105         pkix_test_data_all_default/0,
106         pkix_test_data_all_default/1,
107         pkix_test_data/0,
108         pkix_test_data/1,
109         short_cert_issuer_hash/0,
110         short_cert_issuer_hash/1,
111         short_crl_issuer_hash/0,
112         short_crl_issuer_hash/1,
113         gen_ec_param_prime_field/0,
114         gen_ec_param_prime_field/1,
115         gen_ec_param_char_2_field/0,
116         gen_ec_param_char_2_field/1
117        ]).
118
119-define(TIMEOUT, 120000). % 2 min
120
121
122%%--------------------------------------------------------------------
123%% Common Test interface functions -----------------------------------
124%%--------------------------------------------------------------------
125
126suite() ->
127    [].
128
129all() ->
130    [app,
131     appup,
132     {group, pem_decode_encode},
133     encrypt_decrypt,
134     {group, sign_verify},
135     pkix,
136     pkix_countryname,
137     pkix_emailaddress,
138     pkix_path_validation,
139     pkix_path_validation_root_expired,
140     pkix_iso_rsa_oid,
141     pkix_iso_dsa_oid,
142     pkix_dsa_sha2_oid,
143     pkix_crl,
144     pkix_hash_type,
145     general_name,
146     pkix_verify_hostname_cn,
147     pkix_verify_hostname_subjAltName,
148     pkix_verify_hostname_subjAltName_IP,
149     pkix_verify_hostname_options,
150     pkix_test_data_all_default,
151     pkix_test_data,
152     short_cert_issuer_hash,
153     short_crl_issuer_hash
154    ].
155
156groups() ->
157    [{pem_decode_encode, [], [dsa_pem, rsa_pem, rsa_pss_pss_pem, ec_pem, encrypted_pem,
158			      dh_pem, cert_pem, pkcs7_pem, pkcs10_pem, ec_pem2,
159			      rsa_priv_pkcs8, dsa_priv_pkcs8, ec_priv_pkcs8,
160                              ec_pem_encode_generated,
161                              gen_ec_param_prime_field, gen_ec_param_char_2_field
162                             ]},
163     {sign_verify, [], [rsa_sign_verify, rsa_pss_sign_verify, dsa_sign_verify]}
164    ].
165%%-------------------------------------------------------------------
166init_per_suite(Config) ->
167    application:stop(crypto),
168    try crypto:start() of
169	ok ->
170	    application:start(asn1),
171	    Config
172    catch _:_ ->
173	    {skip, "Crypto did not start"}
174    end.
175
176end_per_suite(_Config) ->
177    application:stop(asn1),
178    application:stop(crypto).
179
180%%-------------------------------------------------------------------
181init_per_group(_GroupName, Config) ->
182    Config.
183
184end_per_group(_GroupName, Config) ->
185    Config.
186%%-------------------------------------------------------------------
187
188init_per_testcase(pkix_test_data_all_default, Config) ->
189     case crypto:ec_curves() of
190         [] ->
191             {skip, missing_ecc_support};
192         _ ->
193             init_common_per_testcase(Config)
194     end;
195
196init_per_testcase(gen_ec_param_prime_field=TC, Config) ->
197    init_per_testcase_gen_ec_param(TC, secp521r1, Config);
198
199init_per_testcase(gen_ec_param_char_2_field=TC, Config) ->
200    init_per_testcase_gen_ec_param(TC, sect571r1, Config);
201
202init_per_testcase(rsa_pss_sign_verify, Config) ->
203    Supports = crypto:supports(),
204    RSAOpts = proplists:get_value(rsa_opts, Supports),
205
206    case lists:member(rsa_pkcs1_pss_padding, RSAOpts)
207        andalso lists:member(rsa_pss_saltlen, RSAOpts)
208        andalso lists:member(rsa_mgf1_md, RSAOpts) of
209        true ->
210            Config;
211        false ->
212            {skip, not_supported_by_crypto}
213    end;
214init_per_testcase(TestCase, Config) ->
215    case TestCase of
216        ec_pem_encode_generated ->
217            init_ec_pem_encode_generated(Config);
218	_ -> init_common_per_testcase(Config)
219    end.
220
221init_common_per_testcase(Config0) ->
222    Config = lists:keydelete(watchdog, 1, Config0),
223    Dog = ct:timetrap(?TIMEOUT),
224    [{watchdog, Dog} | Config].
225
226
227end_per_testcase(_TestCase, _Config) ->
228    ok.
229
230%%--------------------------------------------------------------------
231%% Test Cases --------------------------------------------------------
232%%--------------------------------------------------------------------
233
234app() ->
235    [{doc, "Test that the public_key app file is ok"}].
236app(Config) when is_list(Config) ->
237    ok = ?t:app_test(public_key).
238
239%%--------------------------------------------------------------------
240
241appup() ->
242    [{doc, "Test that the public_key appup file is ok"}].
243appup(Config) when is_list(Config) ->
244    ok = ?t:appup_test(public_key).
245
246%%--------------------------------------------------------------------
247
248dsa_pem() ->
249    [{doc, "DSA PEM-file decode/encode"}].
250dsa_pem(Config) when is_list(Config) ->
251    Datadir = proplists:get_value(data_dir, Config),
252
253     [{'DSAPrivateKey', DerDSAKey, not_encrypted} = Entry0 ] =
254	erl_make_certs:pem_to_der(filename:join(Datadir, "dsa.pem")),
255
256    DSAKey = public_key:der_decode('DSAPrivateKey', DerDSAKey),
257
258    DSAKey = public_key:pem_entry_decode(Entry0),
259
260    {ok, DSAPubPem} = file:read_file(filename:join(Datadir, "dsa_pub.pem")),
261    [{'SubjectPublicKeyInfo', _, _} = PubEntry0] =
262        public_key:pem_decode(DSAPubPem),
263    DSAPubKey = public_key:pem_entry_decode(PubEntry0),
264    true = check_entry_type(DSAPubKey, 'DSAPublicKey'),
265    PubEntry0 = public_key:pem_entry_encode('SubjectPublicKeyInfo', DSAPubKey),
266    DSAPubPemNoEndNewLines = strip_superfluous_newlines(DSAPubPem),
267    DSAPubPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([PubEntry0])).
268
269dsa_priv_pkcs8() ->
270    [{doc, "DSA PKCS8 private key decode/encode"}].
271dsa_priv_pkcs8(Config) when is_list(Config) ->
272    Datadir = proplists:get_value(data_dir, Config),
273    {ok, DsaPem} = file:read_file(filename:join(Datadir, "dsa_key_pkcs8.pem")),
274    [{'PrivateKeyInfo', DerDSAKey, not_encrypted} = Entry0 ] = public_key:pem_decode(DsaPem),
275    DSAKey = public_key:der_decode('PrivateKeyInfo', DerDSAKey),
276    DSAKey = public_key:pem_entry_decode(Entry0),
277    true = check_entry_type(DSAKey, 'DSAPrivateKey'),
278    PrivEntry0 = public_key:pem_entry_encode('PrivateKeyInfo', DSAKey),
279    DSAPemNoEndNewLines = strip_superfluous_newlines(DsaPem),
280    DSAPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([PrivEntry0])).
281
282%%--------------------------------------------------------------------
283
284rsa_pem() ->
285    [{doc, "RSA PEM-file decode/encode"}].
286rsa_pem(Config) when is_list(Config) ->
287    Datadir = proplists:get_value(data_dir, Config),
288    [{'RSAPrivateKey', DerRSAKey, not_encrypted} =  Entry0 ] =
289	erl_make_certs:pem_to_der(filename:join(Datadir, "client_key.pem")),
290
291    RSAKey0 = public_key:der_decode('RSAPrivateKey', DerRSAKey),
292
293    RSAKey0 = public_key:pem_entry_decode(Entry0),
294
295    [{'RSAPrivateKey', _, {_,_}} = Entry1] =
296	erl_make_certs:pem_to_der(filename:join(Datadir, "rsa.pem")),
297
298    true = check_entry_type(public_key:pem_entry_decode(Entry1, "abcd1234"),
299			    'RSAPrivateKey'),
300
301    {ok, RSAPubPem} = file:read_file(filename:join(Datadir, "rsa_pub.pem")),
302    [{'SubjectPublicKeyInfo', _, _} = PubEntry0] =
303        public_key:pem_decode(RSAPubPem),
304    RSAPubKey = public_key:pem_entry_decode(PubEntry0),
305    true = check_entry_type(RSAPubKey, 'RSAPublicKey'),
306    PubEntry0 = public_key:pem_entry_encode('SubjectPublicKeyInfo', RSAPubKey),
307    RSAPubPemNoEndNewLines = strip_superfluous_newlines(RSAPubPem),
308    RSAPubPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([PubEntry0])),
309
310    {ok, RSARawPem} = file:read_file(filename:join(Datadir, "rsa_pub_key.pem")),
311    [{'RSAPublicKey', _, _} = PubEntry1] =
312        public_key:pem_decode(RSARawPem),
313    RSAPubKey = public_key:pem_entry_decode(PubEntry1),
314    RSARawPemNoEndNewLines = strip_superfluous_newlines(RSARawPem),
315    RSARawPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([PubEntry1])).
316
317rsa_pss_pss_pem() ->
318    [{doc, "RSA PKCS8 RSASSA-PSS private key decode/encode"}].
319rsa_pss_pss_pem(Config) when is_list(Config) ->
320    Datadir = proplists:get_value(data_dir, Config),
321    {ok, RsaPem} = file:read_file(filename:join(Datadir, "rsa_pss_pss_key.pem")),
322    [{'PrivateKeyInfo', DerRSAKey, not_encrypted} = Entry0 ] = public_key:pem_decode(RsaPem),
323    {RSAKey, Parms} = public_key:der_decode('PrivateKeyInfo', DerRSAKey),
324    {RSAKey, Parms} = public_key:pem_entry_decode(Entry0),
325    true = check_entry_type(RSAKey, 'RSAPrivateKey'),
326    PrivEntry0 = public_key:pem_entry_encode('PrivateKeyInfo', {RSAKey, Parms}),
327    RSAPemNoEndNewLines = strip_superfluous_newlines(RsaPem),
328    RSAPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([PrivEntry0])).
329
330rsa_priv_pkcs8() ->
331    [{doc, "RSA PKCS8 private key decode/encode"}].
332rsa_priv_pkcs8(Config) when is_list(Config) ->
333    Datadir = proplists:get_value(data_dir, Config),
334    {ok, RsaPem} = file:read_file(filename:join(Datadir, "rsa_key_pkcs8.pem")),
335    [{'PrivateKeyInfo', DerRSAKey, not_encrypted} = Entry0 ] = public_key:pem_decode(RsaPem),
336    RSAKey = public_key:der_decode('PrivateKeyInfo', DerRSAKey),
337    RSAKey = public_key:pem_entry_decode(Entry0),
338    true = check_entry_type(RSAKey, 'RSAPrivateKey'),
339    PrivEntry0 = public_key:pem_entry_encode('PrivateKeyInfo', RSAKey),
340    RSAPemNoEndNewLines = strip_superfluous_newlines(RsaPem),
341    RSAPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([PrivEntry0])).
342
343%%--------------------------------------------------------------------
344
345ec_pem() ->
346    [{doc, "EC key PEM-file decode/encode"}].
347ec_pem(Config) when is_list(Config) ->
348    Datadir = proplists:get_value(data_dir, Config),
349    {ok, ECPubPem} = file:read_file(filename:join(Datadir, "ec_pubkey.pem")),
350    [{'SubjectPublicKeyInfo', _, _} = PubEntry0] =
351        public_key:pem_decode(ECPubPem),
352    ECPubKey = public_key:pem_entry_decode(PubEntry0),
353    true = check_entry_type(ECPubKey, 'ECPoint'),
354    PubEntry0 = public_key:pem_entry_encode('SubjectPublicKeyInfo', ECPubKey),
355    ECPubPemNoEndNewLines = strip_superfluous_newlines(ECPubPem),
356    ECPubPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([PubEntry0])),
357
358    {ok, ECPrivPem} = file:read_file(filename:join(Datadir, "ec_key.pem")),
359    [{'EcpkParameters', _, not_encrypted} = Entry1,
360     {'ECPrivateKey', _, not_encrypted} = Entry2] = public_key:pem_decode(ECPrivPem),
361
362    ECParams = public_key:pem_entry_decode(Entry1),
363    true = check_entry_type(ECParams, 'EcpkParameters'),
364    ECPrivKey = public_key:pem_entry_decode(Entry2),
365    true = check_entry_type(ECPrivKey, 'ECPrivateKey'),
366    true = check_entry_type(ECPrivKey#'ECPrivateKey'.parameters, 'EcpkParameters'),
367    ECPemNoEndNewLines = strip_superfluous_newlines(ECPrivPem),
368    ECPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([Entry1, Entry2])).
369
370ec_pem2() ->
371    [{doc, "EC key w/explicit params PEM-file decode/encode"}].
372ec_pem2(Config) when is_list(Config) ->
373    Datadir = proplists:get_value(data_dir, Config),
374
375    %% Load key with explicit curve parameters.  Generated with...
376    %% openssl ecparam -name secp521r1 -genkey -param_enc explicit -out ec_key2.pem
377    {ok, ECPrivPem} = file:read_file(filename:join(Datadir, "ec_key2.pem")),
378    [{'EcpkParameters', _, not_encrypted} = Entry1,
379     {'ECPrivateKey', _, not_encrypted} = Entry2] = public_key:pem_decode(ECPrivPem),
380
381    ECParams = public_key:pem_entry_decode(Entry1),
382    true = check_entry_type(ECParams, 'EcpkParameters'),
383    ECPrivKey = public_key:pem_entry_decode(Entry2),
384    true = check_entry_type(ECPrivKey, 'ECPrivateKey'),
385    true = check_entry_type(ECPrivKey#'ECPrivateKey'.parameters, 'EcpkParameters'),
386    ECPemNoEndNewLines = strip_superfluous_newlines(ECPrivPem),
387    ECPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([Entry1, Entry2])).
388
389ec_priv_pkcs8() ->
390    [{doc, "EC PKCS8 private key decode/encode"}].
391ec_priv_pkcs8(Config) when is_list(Config) ->
392    Datadir = proplists:get_value(data_dir, Config),
393    {ok, ECPrivPem} = file:read_file(filename:join(Datadir, "ec_key_pkcs8.pem")),
394    [{'PrivateKeyInfo', _, not_encrypted} = PKCS8Key] = public_key:pem_decode(ECPrivPem),
395    ECPrivKey = public_key:pem_entry_decode(PKCS8Key),
396    true = check_entry_type(ECPrivKey, 'ECPrivateKey'),
397    true = check_entry_type(ECPrivKey#'ECPrivateKey'.parameters, 'EcpkParameters'),
398    PrivEntry0 = public_key:pem_entry_encode('PrivateKeyInfo', ECPrivKey),
399    ECPemNoEndNewLines = strip_superfluous_newlines(ECPrivPem),
400    ECPemNoEndNewLines = strip_superfluous_newlines(public_key:pem_encode([PrivEntry0])).
401
402init_ec_pem_encode_generated(Config) ->
403    case catch true = lists:member('secp384r1', crypto:ec_curves()) of
404        {'EXIT', _} -> {skip, {'secp384r1', not_supported}};
405        _           -> init_common_per_testcase(Config)
406    end.
407
408ec_pem_encode_generated() ->
409    [{doc, "PEM-encode generated EC key"}].
410ec_pem_encode_generated(_Config) ->
411
412    Key1 = public_key:generate_key({namedCurve, 'secp384r1'}),
413    public_key:pem_entry_encode('ECPrivateKey', Key1),
414
415    Key2 = public_key:generate_key({namedCurve, ?'secp384r1'}),
416    public_key:pem_entry_encode('ECPrivateKey', Key2).
417
418
419%%--------------------------------------------------------------------
420
421encrypted_pem() ->
422    [{doc, "Encrypted PEM-file decode/encode"}].
423encrypted_pem(Config) when is_list(Config) ->
424    Datadir = proplists:get_value(data_dir, Config),
425
426    [{'RSAPrivateKey', DerRSAKey, not_encrypted}] =
427	erl_make_certs:pem_to_der(filename:join(Datadir, "client_key.pem")),
428
429    RSAKey = public_key:der_decode('RSAPrivateKey', DerRSAKey),
430
431    Salt0 = crypto:strong_rand_bytes(8),
432    Entry0 = public_key:pem_entry_encode('RSAPrivateKey', RSAKey,
433					 {{"DES-EDE3-CBC", Salt0}, "1234abcd"}),
434    RSAKey = public_key:pem_entry_decode(Entry0,"1234abcd"),
435    Des3KeyFile = filename:join(Datadir, "des3_client_key.pem"),
436    erl_make_certs:der_to_pem(Des3KeyFile, [Entry0]),
437    [{'RSAPrivateKey', _, {"DES-EDE3-CBC", Salt0}}] =
438	erl_make_certs:pem_to_der(Des3KeyFile),
439
440    Salt1 = crypto:strong_rand_bytes(8),
441    Entry1 = public_key:pem_entry_encode('RSAPrivateKey', RSAKey,
442					   {{"DES-CBC", Salt1}, "4567efgh"}),
443    DesKeyFile = filename:join(Datadir, "des_client_key.pem"),
444    erl_make_certs:der_to_pem(DesKeyFile, [Entry1]),
445    [{'RSAPrivateKey', _, {"DES-CBC", Salt1}} =Entry2] =
446	erl_make_certs:pem_to_der(DesKeyFile),
447    {ok, Pem} = file:read_file(DesKeyFile),
448    check_encapsulated_header(Pem),
449    true = check_entry_type(public_key:pem_entry_decode(Entry2, "4567efgh"),
450			     'RSAPrivateKey').
451
452%%--------------------------------------------------------------------
453
454dh_pem() ->
455    [{doc, "DH parametrs PEM-file decode/encode"}].
456dh_pem(Config) when is_list(Config) ->
457    Datadir = proplists:get_value(data_dir, Config),
458    [{'DHParameter', _DerDH, not_encrypted} = Entry] =
459	erl_make_certs:pem_to_der(filename:join(Datadir, "dh.pem")),
460    asn1_encode_decode(Entry).
461
462%%--------------------------------------------------------------------
463
464pkcs10_pem() ->
465   [{doc, "PKCS-10 PEM-file decode/encode"}].
466pkcs10_pem(Config) when is_list(Config) ->
467    Datadir = proplists:get_value(data_dir, Config),
468    [{'CertificationRequest', _DerPKCS10, not_encrypted} = Entry] =
469	erl_make_certs:pem_to_der(filename:join(Datadir, "req.pem")),
470    asn1_encode_decode(Entry).
471%%--------------------------------------------------------------------
472pkcs7_pem() ->
473    [{doc, "PKCS-7 PEM-file decode/encode"}].
474pkcs7_pem(Config) when is_list(Config) ->
475    Datadir = proplists:get_value(data_dir, Config),
476    [{'ContentInfo', _, not_encrypted} = Entry0] =
477	erl_make_certs:pem_to_der(filename:join(Datadir, "pkcs7_cert.pem")),
478    [{'ContentInfo', _, not_encrypted} = Entry1] =
479	erl_make_certs:pem_to_der(filename:join(Datadir, "pkcs7_ext.pem")),
480    asn1_encode_decode(Entry0),
481    asn1_encode_decode(Entry1).
482
483%%--------------------------------------------------------------------
484cert_pem() ->
485    [{doc, "Certificate PEM-file decode/encode"}].
486cert_pem(Config) when is_list(Config) ->
487    Datadir = proplists:get_value(data_dir, Config),
488
489    [{'Certificate', _, not_encrypted} = Entry0] =
490	erl_make_certs:pem_to_der(filename:join(Datadir, "client_cert.pem")),
491
492    asn1_encode_decode(Entry0),
493
494    [{'Certificate', _, not_encrypted} = Entry1,
495     {'Certificate', _, not_encrypted} = Entry2] =
496        erl_make_certs:pem_to_der(filename:join(Datadir, "cacerts.pem")),
497
498    asn1_encode_decode(Entry1),
499    asn1_encode_decode(Entry2).
500
501%%--------------------------------------------------------------------
502encrypt_decrypt() ->
503    [{doc, "Test public_key:encrypt_private and public_key:decrypt_public"}].
504encrypt_decrypt(Config) when is_list(Config) ->
505    {PrivateKey, _DerKey} = erl_make_certs:gen_rsa(64),
506    #'RSAPrivateKey'{modulus=Mod, publicExponent=Exp} = PrivateKey,
507    PublicKey = #'RSAPublicKey'{modulus=Mod, publicExponent=Exp},
508    Msg = list_to_binary(lists:duplicate(5, "Foo bar 100")),
509    RsaEncrypted = public_key:encrypt_private(Msg, PrivateKey),
510    Msg = public_key:decrypt_public(RsaEncrypted, PublicKey),
511    RsaEncrypted2 = public_key:encrypt_public(Msg, PublicKey),
512    Msg = public_key:decrypt_private(RsaEncrypted2, PrivateKey),
513    ok.
514
515%%--------------------------------------------------------------------
516rsa_sign_verify() ->
517    [{doc, "Checks that we can sign and verify rsa signatures."}].
518rsa_sign_verify(Config) when is_list(Config) ->
519    Ca = {_, CaKey} = erl_make_certs:make_cert([]),
520    {Cert1, _} = erl_make_certs:make_cert([{key, dsa}, {issuer, Ca}]),
521    PrivateRSA = #'RSAPrivateKey'{modulus=Mod, publicExponent=Exp} =
522	public_key:pem_entry_decode(CaKey),
523    PublicRSA = #'RSAPublicKey'{modulus=Mod, publicExponent=Exp},
524    true = public_key:pkix_verify(Cert1, PublicRSA),
525
526    Msg = list_to_binary(lists:duplicate(5, "Foo bar 100")),
527    RSASign = public_key:sign(Msg, sha, PrivateRSA),
528    true = public_key:verify(Msg, sha, RSASign, PublicRSA),
529    false = public_key:verify(<<1:8, Msg/binary>>, sha, RSASign, PublicRSA),
530    false = public_key:verify(Msg, sha, <<1:8, RSASign/binary>>, PublicRSA),
531
532    RSASign1 = public_key:sign(Msg, md5, PrivateRSA),
533    true = public_key:verify(Msg, md5, RSASign1, PublicRSA).
534
535%%--------------------------------------------------------------------
536rsa_pss_sign_verify() ->
537    [{doc, "Checks that we can sign and verify rsa pss signatures."}].
538rsa_pss_sign_verify(Config) when is_list(Config) ->
539    CertChainConf  = #{server_chain =>
540                           #{root => [{digest, sha256}, {hardcode_rsa_key(1), pss_params(sha256)}],
541                             intermediates => [[]],
542                             peer => [{digest, sha256}, {hardcode_rsa_key(2), pss_params(sha256)}]},
543                       client_chain =>
544                           #{root => [{digest, sha256}, {hardcode_rsa_key(3), pss_params(sha256)}],
545                             intermediates => [[]],
546                             peer => [{digest, sha256}, {hardcode_rsa_key(4), pss_params(sha256)}]}},
547    #{client_config := ClientConf} = public_key:pkix_test_data(CertChainConf),
548    Cert = proplists:get_value(cert, ClientConf),
549    {#'RSAPrivateKey'{modulus=Mod, publicExponent=Exp}, Parms} = {hardcode_rsa_key(4), pss_params(sha256)},
550
551    public_key:pkix_verify(Cert, {#'RSAPublicKey'{modulus=Mod, publicExponent=Exp}, Parms}).
552
553%%--------------------------------------------------------------------
554
555dsa_sign_verify() ->
556    [{doc, "Checks that we can sign and verify dsa signatures."}].
557dsa_sign_verify(Config) when is_list(Config) ->
558    Ca = erl_make_certs:make_cert([]),
559    CertInfo = {_,CertKey1} = erl_make_certs:make_cert([{key, dsa}, {issuer, Ca}]),
560    {Cert2,_CertKey} = erl_make_certs:make_cert([{issuer, CertInfo}]),
561
562    #'DSAPrivateKey'{p=P, q=Q, g=G, y=Y, x=_X} =
563	public_key:pem_entry_decode(CertKey1),
564    true = public_key:pkix_verify(Cert2, {Y, #'Dss-Parms'{p=P, q=Q, g=G}}),
565
566    Datadir = proplists:get_value(data_dir, Config),
567    [DsaKey = {'DSAPrivateKey', _, _}] =
568	erl_make_certs:pem_to_der(filename:join(Datadir, "dsa.pem")),
569    DSAPrivateKey = public_key:pem_entry_decode(DsaKey),
570    #'DSAPrivateKey'{p=P1, q=Q1, g=G1, y=Y1, x=_X1} = DSAPrivateKey,
571
572    Msg = list_to_binary(lists:duplicate(5, "Foo bar 100")),
573    DSASign = public_key:sign(Msg, sha, DSAPrivateKey),
574    DSAPublicKey = Y1,
575    DSAParams = #'Dss-Parms'{p=P1, q=Q1, g=G1},
576    true = public_key:verify(Msg, sha, DSASign, {DSAPublicKey, DSAParams}),
577    false = public_key:verify(<<1:8, Msg/binary>>, sha, DSASign,
578			      {DSAPublicKey, DSAParams}),
579    false = public_key:verify(Msg, sha, <<1:8, DSASign/binary>>,
580			      {DSAPublicKey, DSAParams}),
581
582    Digest = crypto:hash(sha,Msg),
583    DigestSign = public_key:sign(Digest, none, DSAPrivateKey),
584    true = public_key:verify(Digest, none, DigestSign, {DSAPublicKey, DSAParams}),
585    <<_:8, RestDigest/binary>> = Digest,
586    false = public_key:verify(<<1:8, RestDigest/binary>>, none, DigestSign,
587			      {DSAPublicKey, DSAParams}),
588    false = public_key:verify(Digest, none, <<1:8, DigestSign/binary>>,
589			      {DSAPublicKey, DSAParams}).
590
591%%--------------------------------------------------------------------
592pkix() ->
593    [{doc, "Misc pkix tests not covered elsewhere"}].
594pkix(Config) when is_list(Config) ->
595    Datadir = proplists:get_value(data_dir, Config),
596    Certs0 = erl_make_certs:pem_to_der(filename:join(Datadir, "cacerts.pem")),
597    Certs1 = erl_make_certs:pem_to_der(filename:join(Datadir, "client_cert.pem")),
598    TestTransform = fun({'Certificate', CertDer, not_encrypted}) ->
599			    PlainCert = public_key:pkix_decode_cert(CertDer, plain),
600			    OtpCert = public_key:pkix_decode_cert(CertDer, otp),
601			    CertDer =
602				public_key:pkix_encode('OTPCertificate', OtpCert, otp),
603			    CertDer =
604				public_key:pkix_encode('Certificate', PlainCert, plain),
605			    OTPTBS = OtpCert#'OTPCertificate'.tbsCertificate,
606			    OTPSubj = OTPTBS#'OTPTBSCertificate'.subject,
607			    DNEncoded = public_key:pkix_encode('Name', OTPSubj, otp),
608			    PlainTBS = PlainCert#'Certificate'.tbsCertificate,
609			    Subj2 = PlainTBS#'TBSCertificate'.subject,
610			    DNEncoded = public_key:pkix_encode('Name', Subj2, plain),
611
612			    false = public_key:pkix_is_fixed_dh_cert(CertDer)
613		    end,
614    [TestTransform(Cert) || Cert <- Certs0 ++ Certs1],
615
616    Root = element(2, hd(Certs0)),
617    Peer = element(2, hd(Certs1)),
618
619    true = public_key:pkix_is_self_signed(Root),
620    false = public_key:pkix_is_self_signed(Peer),
621
622    CaIds = [element(2, public_key:pkix_issuer_id(Cert, self)) ||
623		{'Certificate', Cert, _} <- Certs0],
624    {ok, IssuerId} =
625	public_key:pkix_issuer_id(Peer, other),
626
627    {ok, Id} = public_key:pkix_issuer_id(Root, self),
628    Id = public_key:pkix_subject_id(Root),
629
630    true = lists:member(IssuerId, CaIds),
631
632    %% Should be normalized allready
633    TestStr   = {rdnSequence,
634		 [[{'AttributeTypeAndValue', {2,5,4,3},{printableString,"ERLANGCA"}}],
635		  [{'AttributeTypeAndValue', {2,5,4,3},{printableString," erlang  ca "}}]]},
636    VerifyStr = {rdnSequence,
637		 [[{'AttributeTypeAndValue', {2,5,4,3},{printableString,"erlangca"}}],
638		  [{'AttributeTypeAndValue', {2,5,4,3},{printableString,"erlang ca"}}]]},
639    VerifyStr = public_key:pkix_normalize_name(TestStr).
640
641
642%%--------------------------------------------------------------------
643pkix_countryname() ->
644    [{doc, "Test workaround for certs that code x509countryname as utf8"}].
645pkix_countryname(Config) when is_list(Config) ->
646    Cert = incorrect_countryname_pkix_cert(),
647    OTPCert = public_key:pkix_decode_cert(Cert, otp),
648    TBSCert = OTPCert#'OTPCertificate'.tbsCertificate,
649    Issuer = TBSCert#'OTPTBSCertificate'.issuer,
650    Subj   = TBSCert#'OTPTBSCertificate'.subject,
651    check_countryname(Issuer),
652    check_countryname(Subj).
653
654%%--------------------------------------------------------------------
655pkix_emailaddress() ->
656    [{doc, "Test workaround for certs that code emailAddress as utf8"}].
657pkix_emailaddress(Config) when is_list(Config) ->
658    Cert = incorrect_emailaddress_pkix_cert(),
659    OTPCert = public_key:pkix_decode_cert(Cert, otp),
660    TBSCert = OTPCert#'OTPCertificate'.tbsCertificate,
661    Issuer = TBSCert#'OTPTBSCertificate'.issuer,
662    Subj   = TBSCert#'OTPTBSCertificate'.subject,
663    check_emailaddress(Issuer),
664    check_emailaddress(Subj).
665
666%%--------------------------------------------------------------------
667pkix_path_validation() ->
668    [{doc, "Test PKIX path validation"}].
669pkix_path_validation(Config) when is_list(Config) ->
670    CaK = {Trusted,_} =
671	erl_make_certs:make_cert([{key, dsa},
672			     {subject, [
673					{name, "Public Key"},
674					{?'id-at-name', {printableString, "public_key"}},
675					{?'id-at-pseudonym', {printableString, "pubkey"}},
676					{city, "Stockholm"},
677					{country, "SE"},
678					{org, "erlang"},
679					{org_unit, "testing dep"}
680				       ]}
681			    ]),
682    ok = erl_make_certs:write_pem("./", "public_key_cacert", CaK),
683
684    CertK1 = {Cert1, _} = erl_make_certs:make_cert([{issuer, CaK}]),
685    CertK2 = {Cert2,_} = erl_make_certs:make_cert([{issuer, CertK1},
686					      {digest, md5}, {extensions, false}]),
687    ok = erl_make_certs:write_pem("./", "public_key_cert", CertK2),
688
689    {ok, _} = public_key:pkix_path_validation(Trusted, [Cert1], []),
690
691    {error, {bad_cert,invalid_issuer}} =
692	public_key:pkix_path_validation(Trusted, [Cert2], []),
693
694    {ok, _} = public_key:pkix_path_validation(Trusted, [Cert1, Cert2], []),
695    {error, issuer_not_found} = public_key:pkix_issuer_id(Cert2, other),
696
697    CertK3 = {Cert3,_}  = erl_make_certs:make_cert([{issuer, CertK1},
698					       {extensions, [{basic_constraints, false}]}]),
699    {Cert4,_}  = erl_make_certs:make_cert([{issuer, CertK3}, {extensions, [{key_usage, undefined}]}]),
700
701    {error, {bad_cert,missing_basic_constraint}} =
702	public_key:pkix_path_validation(Trusted, [Cert1, Cert3,Cert4], []),
703
704    VerifyFunAndState0  = {fun(_,{bad_cert, missing_basic_constraint}, UserState) ->
705				   {valid, UserState};
706			      (_,{bad_cert, _} = Reason, _) ->
707				   {fail, Reason};
708			      (_,{extension, _}, UserState) ->
709				   {unknown, UserState};
710			      (_, valid, UserState) ->
711				   {valid, UserState};
712			      (_, valid_peer, UserState) ->
713				   {valid, UserState}
714			   end, []},
715    {ok, _} =
716	public_key:pkix_path_validation(Trusted, [Cert1, Cert3,Cert4],
717					[{verify_fun, VerifyFunAndState0}]),
718
719    {error, {bad_cert, unknown_ca}} =
720	public_key:pkix_path_validation(unknown_ca, [Cert1, Cert3, Cert4], []),
721
722    VerifyFunAndState1 =
723	{fun(_,{bad_cert, unknown_ca}, UserState) ->
724		 {valid, UserState};
725	    (_,{bad_cert, _} = Reason, _) ->
726		 {fail, Reason};
727	    (_,{extension, _}, UserState) ->
728		 {unknown, UserState};
729	    (_, valid, UserState) ->
730		 {valid, UserState}
731	 end, []},
732
733    {ok, _} =
734	public_key:pkix_path_validation(unknown_ca, [Cert1], [{verify_fun,
735							      VerifyFunAndState1}]),
736
737    VerifyFunAndState2 =
738        {fun(_, {bad_cert, selfsigned_peer}, _UserState) ->
739                  {fail, custom_reason};
740            (_,{extension, _}, UserState) ->
741		          {unknown, UserState};
742	        (_, valid, UserState) ->
743		          {valid, UserState}
744        end, []},
745
746    {error, custom_reason} =
747        public_key:pkix_path_validation(selfsigned_peer, [Trusted], [{verify_fun,
748                                                                      VerifyFunAndState2}]).
749pkix_path_validation_root_expired() ->
750    [{doc, "Test root expiration so that it does not fall between chairs"}].
751pkix_path_validation_root_expired(Config) when is_list(Config) ->
752    {Year, Month, Day} = date(),
753    SRoot = public_key:pkix_test_root_cert("OTP test server ROOT", [{validity, {{Year-2, Month, Day},
754                                                                                {Year-1, Month, Day}}}]),
755    #{server_config := Conf} = public_key:pkix_test_data(#{server_chain => #{root => SRoot,
756                                                                             intermediates => [],
757                                                                             peer => []},
758                                                           client_chain => #{root => [],
759                                                                             intermediates => [],
760                                                                             peer => []}}),
761    [ICA, Root] = proplists:get_value(cacerts, Conf),
762    true = public_key:pkix_is_self_signed(Root),
763    Peer = proplists:get_value(cert, Conf),
764    {error, {bad_cert, cert_expired}} = public_key:pkix_path_validation(Root, [ICA, Peer], []).
765
766%%--------------------------------------------------------------------
767%% To generate the PEM file contents:
768%%
769%% openssl req -x509 -nodes -newkey rsa:1024 -keyout /dev/null -subj '/C=SE/CN=example.com/CN=*.foo.example.com/CN=a*b.bar.example.com/O=erlang.org' > public_key_SUITE_data/pkix_verify_hostname_cn.pem
770%%
771%% Note that the same pem-file is used in pkix_verify_hostname_options/1
772%%
773%% Subject: C=SE, CN=example.com, CN=*.foo.example.com, CN=a*b.bar.example.com, O=erlang.org
774%% extensions = no subjAltName
775
776pkix_verify_hostname_cn(Config) ->
777    DataDir = proplists:get_value(data_dir, Config),
778    {ok,Bin} = file:read_file(filename:join(DataDir,"pkix_verify_hostname_cn.pem")),
779    Cert = public_key:pkix_decode_cert(element(2,hd(public_key:pem_decode(Bin))), otp),
780
781    %% Check that 1) only CNs are checked,
782    %%            2) an empty label does not match a wildcard and
783    %%            3) a wildcard does not match more than one label
784    false = public_key:pkix_verify_hostname(Cert, [{dns_id,"erlang.org"},
785						   {dns_id,"foo.EXAMPLE.com"},
786						   {dns_id,"b.a.foo.EXAMPLE.com"}]),
787
788    %% Check that a hostname is extracted from a https-uri and used for checking:
789    true =  public_key:pkix_verify_hostname(Cert, [{uri_id,"HTTPS://EXAMPLE.com"}]),
790
791    %% Check wildcard matching one label:
792    true =  public_key:pkix_verify_hostname(Cert, [{dns_id,"a.foo.EXAMPLE.com"}]),
793
794    %% Check wildcard with surrounding chars matches one label:
795    true =  public_key:pkix_verify_hostname(Cert, [{dns_id,"accb.bar.EXAMPLE.com"}]),
796
797    %% Check that a wildcard with surrounding chars matches an empty string:
798    true =  public_key:pkix_verify_hostname(Cert, [{uri_id,"https://ab.bar.EXAMPLE.com"}]).
799
800%%--------------------------------------------------------------------
801%% To generate the PEM file contents:
802%%
803%% openssl req -x509 -nodes -newkey rsa:1024 -keyout /dev/null -extensions SAN -config  public_key_SUITE_data/verify_hostname.conf 2>/dev/null > public_key_SUITE_data/pkix_verify_hostname_subjAltName.pem
804%%
805%% Subject: C=SE, CN=example.com
806%% Subject Alternative Name: DNS:kb.example.org, DNS:*.example.org, URI:http://www.example.org, URI:https://wws.example.org
807
808pkix_verify_hostname_subjAltName(Config) ->
809    DataDir = proplists:get_value(data_dir, Config),
810    {ok,Bin} = file:read_file(filename:join(DataDir,"pkix_verify_hostname_subjAltName.pem")),
811    Cert = public_key:pkix_decode_cert(element(2,hd(public_key:pem_decode(Bin))), otp),
812
813    %% Check that neither a uri nor dns hostname matches a CN if subjAltName is present:
814    false = public_key:pkix_verify_hostname(Cert, [{uri_id,"https://example.com"},
815						   {dns_id,"example.com"}]),
816
817    %% Check that a uri_id matches a URI subjAltName:
818    true =  public_key:pkix_verify_hostname(Cert, [{uri_id,"https://wws.example.org"}]),
819
820    %% Check that a dns_id does not match a URI subjAltName:
821    false = public_key:pkix_verify_hostname(Cert, [{dns_id,"www.example.org"},
822						   {dns_id,"wws.example.org"}]),
823
824    %% Check that a dns_id matches a DNS subjAltName:
825    true =  public_key:pkix_verify_hostname(Cert, [{dns_id,"kb.example.org"}]),
826    true =  public_key:pkix_verify_hostname(Cert, [{dns_id,"KB.EXAMPLE.ORG"}]),
827
828    %% Check that a dns_id does not match a DNS subjAltName wiht wildcard
829    false =  public_key:pkix_verify_hostname(Cert, [{dns_id,"other.example.org"}]),
830
831    %% Check that a dns_id does match a DNS subjAltName wiht wildcard with matchfun
832    MatchFun = {match_fun, public_key:pkix_verify_hostname_match_fun(https)},
833    true =  public_key:pkix_verify_hostname(Cert, [{dns_id,"other.example.org"}], [MatchFun]),
834    true =  public_key:pkix_verify_hostname(Cert, [{dns_id,"OTHER.EXAMPLE.ORG"}], [MatchFun]),
835
836    %% Check that a uri_id does not match a DNS subjAltName wiht wildcard
837    false =  public_key:pkix_verify_hostname(Cert, [{uri_id,"https://other.example.org"}]),
838    false =  public_key:pkix_verify_hostname(Cert, [{uri_id,"https://OTHER.EXAMPLE.ORG"}]),
839
840    %% Check that a dns_id does match a DNS subjAltName wiht wildcard with matchfun
841    true =  public_key:pkix_verify_hostname(Cert, [{uri_id,"https://other.example.org"}], [MatchFun]),
842    true =  public_key:pkix_verify_hostname(Cert, [{uri_id,"https://OTHER.EXAMPLE.ORG"}], [MatchFun]),
843    true =  public_key:pkix_verify_hostname(Cert, [{uri_id,"https://OTHER.example.org"}], [MatchFun]),
844
845    ok.
846
847%%--------------------------------------------------------------------
848%% Uses the pem-file for pkix_verify_hostname_cn
849%% Subject: C=SE, CN=example.com, CN=*.foo.example.com, CN=a*b.bar.example.com, O=erlang.org
850pkix_verify_hostname_options(Config) ->
851    DataDir = proplists:get_value(data_dir, Config),
852    {ok,Bin} = file:read_file(filename:join(DataDir,"pkix_verify_hostname_cn.pem")),
853    Cert = public_key:pkix_decode_cert(element(2,hd(public_key:pem_decode(Bin))), otp),
854
855    %% Check that the fail_callback is called and is presented the correct certificate:
856    true = public_key:pkix_verify_hostname(Cert, [{dns_id,"erlang.org"}],
857					   [{fail_callback,
858					     fun(#'OTPCertificate'{}=C) when C==Cert ->
859						     true; % To test the return value matters
860						(#'OTPCertificate'{}=C) ->
861						     ct:log("~p:~p: Wrong cert:~n~p~nExpect~n~p",
862							    [?MODULE, ?LINE, C, Cert]),
863						     ct:fail("Wrong cert, see log");
864						(C) ->
865						     ct:log("~p:~p: Bad cert: ~p",[?MODULE,?LINE,C]),
866						     ct:fail("Bad cert, see log")
867					     end}]),
868
869    %% Check the callback for user-provided match functions:
870    true =  public_key:pkix_verify_hostname(Cert, [{dns_id,"very.wrong.domain"}],
871					    [{match_fun,
872					      fun("very.wrong.domain", {cn,"example.com"}) ->
873						      true;
874						 (_, _) ->
875						      false
876					      end}]),
877    false = public_key:pkix_verify_hostname(Cert, [{dns_id,"not.example.com"}],
878					    [{match_fun, fun(_, _) -> default end}]),
879    true =  public_key:pkix_verify_hostname(Cert, [{dns_id,"example.com"}],
880					    [{match_fun, fun(_, _) -> default end}]),
881
882    %% Check the callback for user-provided fqdn extraction:
883    true =  public_key:pkix_verify_hostname(Cert, [{uri_id,"some://very.wrong.domain"}],
884					    [{fqdn_fun,
885					      fun({uri_id, "some://very.wrong.domain"}) ->
886						      "example.com";
887						 (_) ->
888						      ""
889					      end}]),
890    true =  public_key:pkix_verify_hostname(Cert, [{uri_id,"https://example.com"}],
891					    [{fqdn_fun, fun(_) -> default end}]),
892    false =  public_key:pkix_verify_hostname(Cert, [{uri_id,"some://very.wrong.domain"}]),
893
894    true = public_key:pkix_verify_hostname(Cert, [{dns_id,"example.com"}]),
895    true = public_key:pkix_verify_hostname(Cert, [{dns_id,"abb.bar.example.com"}]),
896    false = public_key:pkix_verify_hostname(Cert, [{dns_id,"example.com"},
897                                                   {dns_id,"abb.bar.example.com"}],
898                                            [{fqdn_fun,fun(_)->undefined end}]).
899
900
901%%--------------------------------------------------------------------
902%% To generate the PEM file contents:
903%%
904%% openssl req -x509 -nodes -newkey rsa:1024 -keyout /dev/null -extensions SAN -config  public_key_SUITE_data/verify_hostname_ip.conf 2>/dev/null > public_key_SUITE_data/pkix_verify_hostname_subjAltName_IP.pem
905%%
906%% Subject: C=SE, CN=example.com
907%% Subject Alternative Name: DNS:1.2.3.4, DNS: abcd:ef::1, IP:10.67.16.75, URI:https://10.11.12.13
908
909pkix_verify_hostname_subjAltName_IP(Config) ->
910    DataDir = proplists:get_value(data_dir, Config),
911    {ok,Bin} = file:read_file(filename:join(DataDir,"pkix_verify_hostname_subjAltName_IP.pem")),
912    Cert = public_key:pkix_decode_cert(element(2,hd(public_key:pem_decode(Bin))), otp),
913
914    %% Print the tests that a matchfun has to handle
915    catch public_key:pkix_verify_hostname(Cert, [{some_tag,"some.domain"},
916                                                 {ip, {10,67,16,75}}
917                                                ],
918                                          [{match_fun,
919                                            fun(Ref,Pres) ->
920                                                    ct:pal("~p:~p:~nRef : ~p~nPres: ~p",[?MODULE,?LINE,Ref,Pres]),
921                                                    false
922                                            end}]),
923
924    false =  public_key:pkix_verify_hostname(Cert, [{uri_id,"https://1.2.3.4"}]),
925    true  =  public_key:pkix_verify_hostname(Cert, [{uri_id,"https://10.11.12.13"}]),
926    true  =  public_key:pkix_verify_hostname(Cert, [{dns_id,"1.2.3.4"}]),
927    true  =  public_key:pkix_verify_hostname(Cert, [{dns_id,<<"1.2.3.4">>}]),
928    false =  public_key:pkix_verify_hostname(Cert, [{dns_id,"10.67.16.75"}]),
929    true  =  public_key:pkix_verify_hostname(Cert, [{ip, "aBcD:ef:0::0:1"}]),
930    true  =  public_key:pkix_verify_hostname(Cert, [{ip, {16#abcd,16#ef,0,0,0,0,0,1}}]),
931    true  =  public_key:pkix_verify_hostname(Cert, [{ip, "10.67.16.75"}]),
932    true  =  public_key:pkix_verify_hostname(Cert, [{ip, <<"10.67.16.75">>}]),
933    true  =  public_key:pkix_verify_hostname(Cert, [{ip, {10,67,16,75}}]),
934    false =  public_key:pkix_verify_hostname(Cert, [{ip, {1,2,3,4}}]),
935    false =  public_key:pkix_verify_hostname(Cert, [{ip, {10,11,12,13}}]).
936%%--------------------------------------------------------------------
937pkix_iso_rsa_oid() ->
938 [{doc, "Test workaround for supporting certs that use ISO oids"
939   " 1.3.14.3.2.29 instead of PKIX/PKCS oid"}].
940pkix_iso_rsa_oid(Config) when is_list(Config) ->
941    Datadir = proplists:get_value(data_dir, Config),
942    {ok, PemCert} = file:read_file(filename:join(Datadir, "rsa_ISO.pem")),
943    [{_, Cert, _}] = public_key:pem_decode(PemCert),
944    OTPCert = public_key:pkix_decode_cert(Cert, otp),
945    SigAlg = OTPCert#'OTPCertificate'.signatureAlgorithm,
946    {_, rsa} = public_key:pkix_sign_types(SigAlg#'SignatureAlgorithm'.algorithm).
947
948%%--------------------------------------------------------------------
949pkix_iso_dsa_oid() ->
950 [{doc, "Test workaround for supporting certs that use ISO oids"
951   "1.3.14.3.2.27 instead of PKIX/PKCS oid"}].
952pkix_iso_dsa_oid(Config) when is_list(Config) ->
953    Datadir = proplists:get_value(data_dir, Config),
954    {ok, PemCert} = file:read_file(filename:join(Datadir, "dsa_ISO.pem")),
955    [{_, Cert, _}] = public_key:pem_decode(PemCert),
956    OTPCert = public_key:pkix_decode_cert(Cert, otp),
957    SigAlg = OTPCert#'OTPCertificate'.signatureAlgorithm,
958    {_, dsa} = public_key:pkix_sign_types(SigAlg#'SignatureAlgorithm'.algorithm).
959
960%%--------------------------------------------------------------------
961pkix_dsa_sha2_oid() ->
962 [{doc, "Test support dsa_sha2 oid"}].
963pkix_dsa_sha2_oid(Config) when is_list(Config) ->
964    {sha224, dsa} = public_key:pkix_sign_types(?'id-dsa-with-sha224'),
965    {sha256, dsa} = public_key:pkix_sign_types(?'id-dsa-with-sha256').
966
967%%--------------------------------------------------------------------
968
969pkix_crl() ->
970    [{doc, "test pkix_crl_* functions"}].
971
972pkix_crl(Config) when is_list(Config) ->
973    Datadir = proplists:get_value(data_dir, Config),
974    {ok, PemCRL} = file:read_file(filename:join(Datadir, "idp_crl.pem")),
975    [{_, CRL, _}] = public_key:pem_decode(PemCRL),
976
977    {ok, IDPPemCert} = file:read_file(filename:join(Datadir, "idp_cert.pem")),
978    [{_, IDPCert, _}] = public_key:pem_decode(IDPPemCert),
979
980    {ok, SignPemCert} = file:read_file(filename:join(Datadir, "crl_signer.pem")),
981    [{_, SignCert, _}] = public_key:pem_decode(SignPemCert),
982
983    OTPIDPCert = public_key:pkix_decode_cert(IDPCert, otp),
984    OTPSignCert = public_key:pkix_decode_cert(SignCert, otp),
985    ERLCRL = public_key:der_decode('CertificateList',CRL),
986
987    {rdnSequence,_} = public_key:pkix_crl_issuer(CRL),
988    {rdnSequence,_} = public_key:pkix_crl_issuer(ERLCRL),
989
990    true = public_key:pkix_crl_verify(CRL, SignCert),
991    true = public_key:pkix_crl_verify(ERLCRL, OTPSignCert),
992
993    [#'DistributionPoint'{}|_] = public_key:pkix_dist_points(IDPCert),
994    [#'DistributionPoint'{}|_] = public_key:pkix_dist_points(OTPIDPCert),
995
996    #'DistributionPoint'{cRLIssuer = asn1_NOVALUE,
997     			 reasons = asn1_NOVALUE,
998			 distributionPoint =  Point} = public_key:pkix_dist_point(IDPCert),
999    #'DistributionPoint'{cRLIssuer = asn1_NOVALUE,
1000			 reasons = asn1_NOVALUE,
1001			 distributionPoint =  Point} = public_key:pkix_dist_point(OTPIDPCert).
1002
1003general_name() ->
1004    [{doc, "Test that decoding of general name filed may have other values"
1005      " than {rdnSequence,  Seq}"}].
1006
1007general_name(Config) when is_list(Config) ->
1008    DummyRfc822Name = "CN=CNDummy, OU=OUDummy, O=ODummy, C=SE",
1009    {ok, {1,  DummyRfc822Name}} =
1010	pubkey_cert:cert_auth_key_id(
1011	  #'AuthorityKeyIdentifier'{authorityCertIssuer =
1012					[{rfc822Name, DummyRfc822Name}],
1013				    authorityCertSerialNumber =
1014					1}).
1015
1016%%--------------------------------------------------------------------
1017
1018pkix_hash_type() ->
1019     [{doc, "Test API function pkix_hash_type/1"}].
1020
1021pkix_hash_type(Config) when is_list(Config) ->
1022    sha = public_key:pkix_hash_type(?'id-sha1'),
1023    sha512 = public_key:pkix_hash_type(?'id-sha512'),
1024    sha384 = public_key:pkix_hash_type(?'id-sha384'),
1025    sha256 = public_key:pkix_hash_type(?'id-sha256'),
1026    sha224 = public_key:pkix_hash_type('id-sha224'),
1027    md5 = public_key:pkix_hash_type('id-md5').
1028
1029
1030%%--------------------------------------------------------------------
1031
1032pkix_test_data_all_default() ->
1033    [{doc, "Test API function pkix_test_data/1"}].
1034
1035pkix_test_data_all_default(Config) when is_list(Config) ->
1036    #{server_config := ServerConf0,
1037      client_config := ClientConf0} = public_key:pkix_test_data(#{server_chain =>
1038                                                                     #{root => [],
1039                                                                       intermediates => [[]],
1040                                                                       peer => []},
1041                                                                 client_chain =>
1042                                                                     #{root => [],
1043                                                                       intermediates => [[]],
1044                                                                       peer => []}}),
1045    check_conf_member(ServerConf0, [key, cert, cacerts]),
1046    check_conf_member(ClientConf0, [key, cert, cacerts]),
1047
1048    3 = length(proplists:get_value(cacerts, ServerConf0)),
1049    3 = length(proplists:get_value(cacerts, ServerConf0)),
1050
1051    #{server_config := ServerConf1,
1052      client_config := ClientConf1} = public_key:pkix_test_data(#{server_chain =>
1053                                                                     #{root => [],
1054                                                                       peer => []},
1055                                                                 client_chain =>
1056                                                                     #{root => [],
1057                                                                       peer => []}}),
1058    2 = length(proplists:get_value(cacerts, ServerConf1)),
1059    2 = length(proplists:get_value(cacerts, ServerConf1)),
1060
1061    check_conf_member(ServerConf1, [key, cert, cacerts]),
1062    check_conf_member(ClientConf1, [key, cert, cacerts]).
1063
1064
1065pkix_test_data() ->
1066    [{doc, "Test API function pkix_test_data/1"}].
1067
1068pkix_test_data(Config) when is_list(Config) ->
1069    {Year, Month, Day} = date(),
1070    Keygen =
1071        case crypto:ec_curves() of
1072        [] ->
1073            {rsa, 2048, 17};
1074        [Curve |_] ->
1075            Oid = pubkey_cert_records:namedCurves(Curve),
1076            {namedCurve, Oid}
1077        end,
1078    #{server_config := ServerConf0,
1079      client_config := ClientConf0} =
1080        public_key:pkix_test_data(#{server_chain =>
1081                                        #{root => [],
1082                                          intermediates => [],
1083                                          peer => [{key, hardcode_rsa_key(1)}]},
1084                                    client_chain =>
1085                                        #{root => [{validity, {{Year-2, Month, Day},
1086                                                               {Year-1, Month, Day}}}],
1087                                          intermediates =>
1088                                              [[{extensions, [#'Extension'{extnID = ?'id-ce-basicConstraints',
1089                                                                           extnValue = #'BasicConstraints'{cA=true,
1090                                                                                             pathLenConstraint = 1},
1091                                                                           critical = true}]}]],
1092                                               peer => [{key, Keygen}, {digest, sha1}]}}),
1093    check_conf_member(ServerConf0, [key, cert, cacerts]),
1094    check_conf_member(ClientConf0, [key, cert, cacerts]).
1095
1096
1097
1098check_conf_member(_, []) ->
1099    true;
1100check_conf_member(Conf, [Member | Rest]) ->
1101    case lists:keymember(Member, 1, Conf) of
1102        true ->
1103            check_conf_member(Conf, Rest);
1104        false ->
1105            ct:fail({misssing_conf, Member})
1106    end.
1107
1108%%--------------------------------------------------------------------
1109short_cert_issuer_hash() ->
1110    [{doc, "Test OpenSSL-style hash for certificate issuer"}].
1111
1112short_cert_issuer_hash(Config) when is_list(Config) ->
1113    Datadir = ?config(data_dir, Config),
1114    [{'Certificate', CertDER, _}] =
1115	erl_make_certs:pem_to_der(filename:join(Datadir, "client_cert.pem")),
1116
1117    %% This hash value was obtained by running:
1118    %% openssl x509 -in client_cert.pem -issuer_hash -noout
1119    CertIssuerHash = "d4c8d7e5",
1120
1121    #'OTPCertificate'{tbsCertificate = #'OTPTBSCertificate'{issuer = Issuer}} =
1122	public_key:pkix_decode_cert(CertDER, otp),
1123
1124    CertIssuerHash = public_key:short_name_hash(Issuer).
1125
1126%%--------------------------------------------------------------------
1127short_crl_issuer_hash() ->
1128    [{doc, "Test OpenSSL-style hash for CRL issuer"}].
1129
1130short_crl_issuer_hash(Config) when is_list(Config) ->
1131    Datadir = ?config(data_dir, Config),
1132    [{'CertificateList', CrlDER, _}] =
1133	erl_make_certs:pem_to_der(filename:join(Datadir, "idp_crl.pem")),
1134
1135    %% This hash value was obtained by running:
1136    %% openssl crl -in idp_crl.pem -hash -noout
1137    CrlIssuerHash = "d6134ed3",
1138
1139    Issuer = public_key:pkix_crl_issuer(CrlDER),
1140
1141    CrlIssuerHash = public_key:short_name_hash(Issuer).
1142
1143%%--------------------------------------------------------------------
1144gen_ec_param_prime_field() ->
1145    [{doc, "Generate key with EC prime_field parameters"}].
1146gen_ec_param_prime_field(Config) when is_list(Config) ->
1147    Datadir = proplists:get_value(data_dir, Config),
1148    do_gen_ec_param(filename:join(Datadir, "ec_key_param0.pem")).
1149
1150%%--------------------------------------------------------------------
1151gen_ec_param_char_2_field() ->
1152    [{doc, "Generate key with EC characteristic_two_field parameters"}].
1153gen_ec_param_char_2_field(Config) when is_list(Config) ->
1154    Datadir = proplists:get_value(data_dir, Config),
1155    do_gen_ec_param(filename:join(Datadir, "ec_key_param1.pem")).
1156
1157%%--------------------------------------------------------------------
1158%% Internal functions ------------------------------------------------
1159%%--------------------------------------------------------------------
1160asn1_encode_decode({Asn1Type, Der, not_encrypted} = Entry) ->
1161    Decoded = public_key:der_decode(Asn1Type, Der),
1162    Decoded = public_key:pem_entry_decode(Entry),
1163    Entry = public_key:pem_entry_encode(Asn1Type, Decoded),
1164    ok.
1165
1166check_countryname({rdnSequence,DirName}) ->
1167    do_check_countryname(DirName).
1168do_check_countryname([]) ->
1169    ok;
1170do_check_countryname([#'AttributeTypeAndValue'{type = ?'id-at-countryName',
1171					       value = "US"}|_]) ->
1172    ok;
1173do_check_countryname([#'AttributeTypeAndValue'{type = ?'id-at-countryName',
1174					       value =  Value}|_]) ->
1175    ct:fail({incorrect_country_name, Value});
1176do_check_countryname([_| Rest]) ->
1177    do_check_countryname(Rest).
1178
1179check_emailaddress({rdnSequence,DirName}) ->
1180    do_check_emailaddress(DirName).
1181do_check_emailaddress([]) ->
1182    ok;
1183do_check_emailaddress([#'AttributeTypeAndValue'{type = ?'id-emailAddress',
1184					       value = "invalid@email.com"}|_]) ->
1185    ok;
1186do_check_emailaddress([#'AttributeTypeAndValue'{type = ?'id-emailAddress',
1187					       value =  Value}|_]) ->
1188    ct:fail({incorrect_email_address, Value});
1189do_check_emailaddress([_| Rest]) ->
1190    do_check_emailaddress(Rest).
1191
1192check_entry_type(#'DSAPrivateKey'{}, 'DSAPrivateKey') ->
1193    true;
1194check_entry_type(#'RSAPrivateKey'{}, 'RSAPrivateKey') ->
1195    true;
1196check_entry_type(#'RSAPublicKey'{}, 'RSAPublicKey') ->
1197    true;
1198check_entry_type({_Int, #'Dss-Parms'{}}, 'DSAPublicKey') when is_integer(_Int) ->
1199    true;
1200check_entry_type(#'DHParameter'{}, 'DHParameter') ->
1201    true;
1202check_entry_type(#'Certificate'{}, 'Certificate') ->
1203    true;
1204check_entry_type({#'ECPoint'{}, _}, 'ECPoint') ->
1205    true;
1206check_entry_type(#'ECPrivateKey'{}, 'ECPrivateKey') ->
1207    true;
1208check_entry_type({namedCurve, _}, 'EcpkParameters') ->
1209    true;
1210check_entry_type({ecParameters, #'ECParameters'{}}, 'EcpkParameters') ->
1211    true;
1212check_entry_type(_,_) ->
1213    false.
1214
1215check_encapsulated_header(Pem) when is_binary(Pem)->
1216    check_encapsulated_header( binary:split(Pem, <<"\n">>, [global]));
1217check_encapsulated_header([<<"DEK-Info: DES-CBC,FB7577791A9056A1">>, <<>> | _]) ->
1218    true;
1219check_encapsulated_header([ _ | Rest]) ->
1220    check_encapsulated_header(Rest);
1221check_encapsulated_header([]) ->
1222    false.
1223
1224strip_superfluous_newlines(Bin) ->
1225    Str = string:strip(binary_to_list(Bin), right, 10),
1226    re:replace(Str,"\n\n","\n", [{return,list}, global]).
1227
1228do_gen_ec_param(File) ->
1229    {ok, KeyPem} = file:read_file(File),
1230    Entries = public_key:pem_decode(KeyPem),
1231    [ParamInfo] = [Entry || Entry={'EcpkParameters', _, not_encrypted} <- Entries],
1232    {ecParameters, Params} = public_key:pem_entry_decode(ParamInfo),
1233    Key = public_key:generate_key(Params),
1234    case check_entry_type(Key, 'ECPrivateKey') of
1235        true ->
1236            ok;
1237        false ->
1238            ct:fail({key_gen_fail, File})
1239    end.
1240
1241init_per_testcase_gen_ec_param(_TC, Curve, Config) ->
1242    case crypto:ec_curves() of
1243        [] ->
1244            {skip, missing_ec_support};
1245        Curves ->
1246            case lists:member(Curve, Curves)
1247                andalso crypto_supported_curve(Curve, Curves)
1248            of
1249                true ->
1250                    init_common_per_testcase(Config);
1251                false ->
1252                    {skip, {missing_ec_support, Curve}}
1253            end
1254    end.
1255
1256
1257crypto_supported_curve(Curve, _Curves) ->
1258    try crypto:generate_key(ecdh, Curve) of
1259        {error,_} -> false; % Just in case crypto is changed in the future...
1260        _-> true
1261    catch
1262        _:_-> false
1263    end.
1264
1265incorrect_countryname_pkix_cert() ->
1266    <<48,130,5,186,48,130,4,162,160,3,2,1,2,2,7,7,250,61,63,6,140,137,48,13,6,9,42, 134,72,134,247,13,1,1,5,5,0,48,129,220,49,11,48,9,6,3,85,4,6,19,2,85,83,49, 16,48,14,6,3,85,4,8,19,7,65,114,105,122,111,110,97,49,19,48,17,6,3,85,4,7,19, 10,83,99,111,116,116,115,100,97,108,101,49,37,48,35,6,3,85,4,10,19,28,83,116, 97,114,102,105,101,108,100,32,84,101,99,104,110,111,108,111,103,105,101,115, 44,32,73,110,99,46,49,57,48,55,6,3,85,4,11,19,48,104,116,116,112,58,47,47,99, 101,114,116,105,102,105,99,97,116,101,115,46,115,116,97,114,102,105,101,108, 100,116,101,99,104,46,99,111,109,47,114,101,112,111,115,105,116,111,114,121, 49,49,48,47,6,3,85,4,3,19,40,83,116,97,114,102,105,101,108,100,32,83,101,99, 117,114,101,32,67,101,114,116,105,102,105,99,97,116,105,111,110,32,65,117, 116,104,111,114,105,116,121,49,17,48,15,6,3,85,4,5,19,8,49,48,54,56,56,52,51, 53,48,30,23,13,49,48,49,48,50,51,48,49,51,50,48,53,90,23,13,49,50,49,48,50, 51,48,49,51,50,48,53,90,48,122,49,11,48,9,6,3,85,4,6,12,2,85,83,49,11,48,9,6, 3,85,4,8,12,2,65,90,49,19,48,17,6,3,85,4,7,12,10,83,99,111,116,116,115,100, 97,108,101,49,38,48,36,6,3,85,4,10,12,29,83,112,101,99,105,97,108,32,68,111, 109,97,105,110,32,83,101,114,118,105,99,101,115,44,32,73,110,99,46,49,33,48, 31,6,3,85,4,3,12,24,42,46,108,111,103,105,110,46,115,101,99,117,114,101,115, 101,114,118,101,114,46,110,101,116,48,130,1,34,48,13,6,9,42,134,72,134,247, 13,1,1,1,5,0,3,130,1,15,0,48,130,1,10,2,130,1,1,0,185,136,240,80,141,36,124, 245,182,130,73,19,188,74,166,117,72,228,185,209,43,129,244,40,44,193,231,11, 209,12,234,88,43,142,1,162,48,122,17,95,230,105,171,131,12,147,46,204,36,80, 250,171,33,253,35,62,83,22,71,212,186,141,14,198,89,89,121,204,224,122,246, 127,110,188,229,162,67,95,6,74,231,127,99,131,7,240,85,102,203,251,50,58,58, 104,245,103,181,183,134,32,203,121,232,54,32,188,139,136,112,166,126,14,91, 223,153,172,164,14,61,38,163,208,215,186,210,136,213,143,70,147,173,109,217, 250,169,108,31,211,104,238,103,93,182,59,165,43,196,189,218,241,30,148,240, 109,90,69,176,194,52,116,173,151,135,239,10,209,179,129,192,102,75,11,25,168, 223,32,174,84,223,134,70,167,55,172,143,27,130,123,226,226,7,34,142,166,39, 48,246,96,231,150,84,220,106,133,193,55,95,159,227,24,249,64,36,1,142,171,16, 202,55,126,7,156,15,194,22,116,53,113,174,104,239,203,120,45,131,57,87,84, 163,184,27,83,57,199,91,200,34,43,98,61,180,144,76,65,170,177,2,3,1,0,1,163, 130,1,224,48,130,1,220,48,15,6,3,85,29,19,1,1,255,4,5,48,3,1,1,0,48,29,6,3, 85,29,37,4,22,48,20,6,8,43,6,1,5,5,7,3,1,6,8,43,6,1,5,5,7,3,2,48,14,6,3,85, 29,15,1,1,255,4,4,3,2,5,160,48,56,6,3,85,29,31,4,49,48,47,48,45,160,43,160, 41,134,39,104,116,116,112,58,47,47,99,114,108,46,115,116,97,114,102,105,101, 108,100,116,101,99,104,46,99,111,109,47,115,102,115,50,45,48,46,99,114,108, 48,83,6,3,85,29,32,4,76,48,74,48,72,6,11,96,134,72,1,134,253,110,1,7,23,2,48, 57,48,55,6,8,43,6,1,5,5,7,2,1,22,43,104,116,116,112,115,58,47,47,99,101,114, 116,115,46,115,116,97,114,102,105,101,108,100,116,101,99,104,46,99,111,109, 47,114,101,112,111,115,105,116,111,114,121,47,48,129,141,6,8,43,6,1,5,5,7,1, 1,4,129,128,48,126,48,42,6,8,43,6,1,5,5,7,48,1,134,30,104,116,116,112,58,47, 47,111,99,115,112,46,115,116,97,114,102,105,101,108,100,116,101,99,104,46,99, 111,109,47,48,80,6,8,43,6,1,5,5,7,48,2,134,68,104,116,116,112,58,47,47,99, 101,114,116,105,102,105,99,97,116,101,115,46,115,116,97,114,102,105,101,108, 100,116,101,99,104,46,99,111,109,47,114,101,112,111,115,105,116,111,114,121, 47,115,102,95,105,110,116,101,114,109,101,100,105,97,116,101,46,99,114,116, 48,31,6,3,85,29,35,4,24,48,22,128,20,73,75,82,39,209,27,188,242,161,33,106, 98,123,81,66,122,138,215,213,86,48,59,6,3,85,29,17,4,52,48,50,130,24,42,46, 108,111,103,105,110,46,115,101,99,117,114,101,115,101,114,118,101,114,46,110, 101,116,130,22,108,111,103,105,110,46,115,101,99,117,114,101,115,101,114,118, 101,114,46,110,101,116,48,29,6,3,85,29,14,4,22,4,20,138,233,191,208,157,203, 249,85,242,239,20,195,48,10,148,49,144,101,255,116,48,13,6,9,42,134,72,134, 247,13,1,1,5,5,0,3,130,1,1,0,82,31,121,162,49,50,143,26,167,202,143,61,71, 189,201,199,57,81,122,116,90,192,88,24,102,194,174,48,157,74,27,87,210,223, 253,93,3,91,150,109,120,1,110,27,11,200,198,141,222,246,14,200,71,105,41,138, 13,114,122,106,63,17,197,181,234,121,61,89,74,65,41,231,248,219,129,83,176, 219,55,107,55,211,112,98,38,49,69,77,96,221,108,123,152,12,210,159,157,141, 43,226,55,187,129,3,82,49,136,66,81,196,91,234,196,10,82,48,6,80,163,83,71, 127,102,177,93,209,129,26,104,2,84,24,255,248,161,3,244,169,234,92,122,110, 43,4,17,113,185,235,108,219,210,236,132,216,177,227,17,169,58,162,159,182, 162,93,160,229,200,9,163,229,110,121,240,168,232,14,91,214,188,196,109,210, 164,222,0,109,139,132,113,91,16,118,173,178,176,80,132,34,41,199,51,206,250, 224,132,60,115,192,94,107,163,219,212,226,225,65,169,148,108,213,46,174,173, 103,110,189,229,166,149,254,31,51,44,144,108,187,182,11,251,201,206,86,138, 208,59,51,86,132,235,81,225,88,34,190,8,184>>.
1267
1268incorrect_emailaddress_pkix_cert() ->
1269    <<48,130,3,74,48,130,2,50,2,9,0,133,49,203,25,198,156,252,230,48,13,6,9,42,134, 72,134,247,13,1,1,5,5,0,48,103,49,11,48,9,6,3,85,4,6,19,2,65,85,49,19,48,17, 6,3,85,4,8,12,10,83,111,109,101,45,83,116,97,116,101,49,33,48,31,6,3,85,4,10, 12,24,73,110,116,101,114,110,101,116,32,87,105,100,103,105,116,115,32,80,116, 121,32,76,116,100,49,32,48,30,6,9,42,134,72,134,247,13,1,9,1,12,17,105,110, 118,97,108,105,100,64,101,109,97,105,108,46,99,111,109,48,30,23,13,49,51,49, 49,48,55,50,48,53,54,49,56,90,23,13,49,52,49,49,48,55,50,48,53,54,49,56,90, 48,103,49,11,48,9,6,3,85,4,6,19,2,65,85,49,19,48,17,6,3,85,4,8,12,10,83,111, 109,101,45,83,116,97,116,101,49,33,48,31,6,3,85,4,10,12,24,73,110,116,101, 114,110,101,116,32,87,105,100,103,105,116,115,32,80,116,121,32,76,116,100,49, 32,48,30,6,9,42,134,72,134,247,13,1,9,1,12,17,105,110,118,97,108,105,100,64, 101,109,97,105,108,46,99,111,109,48,130,1,34,48,13,6,9,42,134,72,134,247,13, 1,1,1,5,0,3,130,1,15,0,48,130,1,10,2,130,1,1,0,190,243,49,213,219,60,232,105, 1,127,126,9,130,15,60,190,78,100,148,235,246,223,21,91,238,200,251,84,55,212, 78,32,120,61,85,172,0,144,248,5,165,29,143,79,64,178,51,153,203,76,115,238, 192,49,173,37,121,203,89,62,157,13,181,166,30,112,154,40,202,140,104,211,157, 73,244,9,78,236,70,153,195,158,233,141,42,238,2,143,160,225,249,27,30,140, 151,176,43,211,87,114,164,108,69,47,39,195,123,185,179,219,28,218,122,53,83, 77,48,81,184,14,91,243,12,62,146,86,210,248,228,171,146,225,87,51,146,155, 116,112,238,212,36,111,58,41,67,27,6,61,61,3,84,150,126,214,121,57,38,12,87, 121,67,244,37,45,145,234,131,115,134,58,194,5,36,166,52,59,229,32,47,152,80, 237,190,58,182,248,98,7,165,198,211,5,31,231,152,116,31,108,71,218,64,188, 178,143,27,167,79,15,112,196,103,116,212,65,197,94,37,4,132,103,91,217,73, 223,207,185,7,153,221,240,232,31,44,102,108,82,83,56,242,210,214,74,71,246, 177,217,148,227,220,230,4,176,226,74,194,37,2,3,1,0,1,48,13,6,9,42,134,72, 134,247,13,1,1,5,5,0,3,130,1,1,0,89,247,141,154,173,123,123,203,143,85,28,79, 73,37,164,6,17,89,171,224,149,22,134,17,198,146,158,192,241,41,253,58,230, 133,71,189,43,66,123,88,15,242,119,227,249,99,137,61,200,54,161,0,177,167, 169,114,80,148,90,22,97,78,162,181,75,93,209,116,245,46,81,232,64,157,93,136, 52,57,229,113,197,218,113,93,42,161,213,104,205,137,30,144,183,58,10,98,47, 227,177,96,40,233,98,150,209,217,68,22,221,133,27,161,152,237,46,36,179,59, 172,97,134,194,205,101,137,71,192,57,153,20,114,27,173,233,166,45,56,0,61, 205,45,202,139,7,132,103,248,193,157,184,123,43,62,172,236,110,49,62,209,78, 249,83,219,133,1,213,143,73,174,16,113,143,189,41,84,60,128,222,30,177,104, 134,220,52,239,171,76,59,176,36,113,176,214,118,16,44,235,21,167,199,216,200, 76,219,142,248,13,70,145,205,216,230,226,148,97,223,216,179,68,209,222,63, 140,137,24,164,192,149,194,79,119,247,75,159,49,116,70,241,70,116,11,40,119, 176,157,36,160,102,140,255,34,248,25,231,136,59>>.
1270
1271hardcode_rsa_key(1) ->
1272    #'RSAPrivateKey'{
1273       version = 'two-prime',
1274       modulus = 23995666614853919027835084074500048897452890537492185072956789802729257783422306095699263934587064480357348855732149402060270996295002843755712064937715826848741191927820899197493902093529581182351132392364214171173881547273475904587683433713767834856230531387991145055273426806331200574039205571401702219159773947658558490957010003143162250693492642996408861265758000254664396313741422909188635443907373976005987612936763564996605457102336549804831742940035613780926178523017685712710473543251580072875247250504243621640157403744718833162626193206685233710319205099867303242759099560438381385658382486042995679707669,
1275       publicExponent = 17,
1276       privateExponent = 11292078406990079542510627799764728892919007311761028269626724613049062486316379339152594792746853873109340637991599718616598115903530750002688030558925094987642913848386305504703012749896273497577003478759630198199473669305165131570674557041773098755873191241407597673069847908861741446606684974777271632545629600685952292605647052193819136445675100211504432575554351515262198132231537860917084269870590492135731720141577986787033006338680118008484613510063003323516659048210893001173583018220214626635609151105287049126443102976056146630518124476470236027123782297108342869049542023328584384300970694412006494684657,
1277       prime1 = 169371138592582642967021557955633494538845517070305333860805485424261447791289944610138334410987654265476540480228705481960508520379619587635662291973699651583489223555422528867090299996446070521801757353675026048850480903160224210802452555900007597342687137394192939372218903554801584969667104937092080815197,
1278       prime2 = 141675062317286527042995673340952251894209529891636708844197799307963834958115010129693036021381525952081167155681637592199810112261679449166276939178032066869788822014115556349519329537177920752776047051833616197615329017439297361972726138285974555338480581117881706656603857310337984049152655480389797687577,
1279       exponent1 = 119556097830058336212015217380447172615655659108450823901745048534772786676204666783627059584226579481512852103690850928442711896738555003036938088452023283470698275450886490965004917644550167427154181661417665446247398284583687678213495921811770068712485038160606780733330990744565824684470897602653233516609,
1280       exponent2 = 41669135975672507953822256864985956439473391144599032012999352737636422046504414744027363535700448809435637398729893409470532385959317485048904982111185902020526124121798693043976273393287623750816484427009887116945685005129205106462566511260580751570141347387612266663707016855981760014456663376585234613993,
1281       coefficient = 76837684977089699359024365285678488693966186052769523357232308621548155587515525857011429902602352279058920284048929101483304120686557782043616693940283344235057989514310975192908256494992960578961614059245280827077951132083993754797053182279229469590276271658395444955906108899267024101096069475145863928441,
1282       otherPrimeInfos = asn1_NOVALUE};
1283
1284hardcode_rsa_key(2) ->
1285    #'RSAPrivateKey'{
1286       version = 'two-prime',
1287       modulus = 21343679768589700771839799834197557895311746244621307033143551583788179817796325695589283169969489517156931770973490560582341832744966317712674900833543896521418422508485833901274928542544381247956820115082240721897193055368570146764204557110415281995205343662628196075590438954399631753508888358737971039058298703003743872818150364935790613286541190842600031570570099801682794056444451081563070538409720109449780410837763602317050353477918147758267825417201591905091231778937606362076129350476690460157227101296599527319242747999737801698427160817755293383890373574621116766934110792127739174475029121017282777887777,
1288       publicExponent = 17,
1289       privateExponent = 18832658619343853622211588088997845201745658451136447382185486691577805721584993260814073385267196632785528033211903435807948675951440868570007265441362261636545666919252206383477878125774454042314841278013741813438699754736973658909592256273895837054592950290554290654932740253882028017801960316533503857992358685308186680144968293076156011747178275038098868263178095174694099811498968993700538293188879611375604635940554394589807673542938082281934965292051746326331046224291377703201248790910007232374006151098976879987912446997911775904329728563222485791845480864283470332826504617837402078265424772379987120023773,
1290       prime1 = 146807662748886761089048448970170315054939768171908279335181627815919052012991509112344782731265837727551849787333310044397991034789843793140419387740928103541736452627413492093463231242466386868459637115999163097726153692593711599245170083315894262154838974616739452594203727376460632750934355508361223110419,
1291       prime2 = 145385325050081892763917667176962991350872697916072592966410309213561884732628046256782356731057378829876640317801978404203665761131810712267778698468684631707642938779964806354584156202882543264893826268426566901882487709510744074274965029453915224310656287149777603803201831202222853023280023478269485417083,
1292       exponent1 = 51814469205489445090252393754177758254684624060673510353593515699736136004585238510239335081623236845018299924941168250963996835808180162284853901555621683602965806809675350150634081614988136541809283687999704622726877773856604093851236499993845033701707873394143336209718962603456693912094478414715725803677,
1293       exponent2 = 51312467664734785681382706062457526359131540440966797517556579722433606376221663384746714140373192528191755406283051201483646739222992016094510128871300458249756331334105225772206172777487956446433115153562317730076172132768497908567634716277852432109643395464627389577600646306666889302334125933506877206029,
1294       coefficient = 30504662229874176232343608562807118278893368758027179776313787938167236952567905398252901545019583024374163153775359371298239336609182249464886717948407152570850677549297935773605431024166978281486607154204888016179709037883348099374995148481968169438302456074511782717758301581202874062062542434218011141540,
1295       otherPrimeInfos = asn1_NOVALUE};
1296hardcode_rsa_key(3) ->
1297    #'RSAPrivateKey'{
1298       version = 'two-prime',
1299       modulus = 25089040456112869869472694987833070928503703615633809313972554887193090845137746668197820419383804666271752525807484521370419854590682661809972833718476098189250708650325307850184923546875260207894844301992963978994451844985784504212035958130279304082438876764367292331581532569155681984449177635856426023931875082020262146075451989132180409962870105455517050416234175675478291534563995772675388370042873175344937421148321291640477650173765084699931690748536036544188863178325887393475703801759010864779559318631816411493486934507417755306337476945299570726975433250753415110141783026008347194577506976486290259135429,
1300       publicExponent = 17,
1301       privateExponent = 8854955455098659953931539407470495621824836570223697404931489960185796768872145882893348383311931058684147950284994536954265831032005645344696294253579799360912014817761873358888796545955974191021709753644575521998041827642041589721895044045980930852625485916835514940558187965584358347452650930302268008446431977397918214293502821599497633970075862760001650736520566952260001423171553461362588848929781360590057040212831994258783694027013289053834376791974167294527043946669963760259975273650548116897900664646809242902841107022557239712438496384819445301703021164043324282687280801738470244471443835900160721870265,
1302       prime1 = 171641816401041100605063917111691927706183918906535463031548413586331728772311589438043965564336865070070922328258143588739626712299625805650832695450270566547004154065267940032684307994238248203186986569945677705100224518137694769557564475390859269797990555863306972197736879644001860925483629009305104925823,
1303       prime2 =146170909759497809922264016492088453282310383272504533061020897155289106805616042710009332510822455269704884883705830985184223718261139908416790475825625309815234508695722132706422885088219618698987115562577878897003573425367881351537506046253616435685549396767356003663417208105346307649599145759863108910523,
1304       exponent1 = 60579464612132153154728441333538327425711971378777222246428851853999433684345266860486105493295364142377972586444050678378691780811632637288529186629507258781295583787741625893888579292084087601124818789392592131211843947578009918667375697196773859928702549128225990187436545756706539150170692591519448797349,
1305       exponent2 = 137572620950115585809189662580789132500998007785886619351549079675566218169991569609420548245479957900898715184664311515467504676010484619686391036071176762179044243478326713135456833024206699951987873470661533079532774988581535389682358631768109586527575902839864474036157372334443583670210960715165278974609,
1306       coefficient = 15068630434698373319269196003209754243798959461311186548759287649485250508074064775263867418602372588394608558985183294561315208336731894947137343239541687540387209051236354318837334154993136528453613256169847839789803932725339395739618592522865156272771578671216082079933457043120923342632744996962853951612,
1307       otherPrimeInfos = asn1_NOVALUE};
1308hardcode_rsa_key(4) ->
1309    #'RSAPrivateKey'{
1310       version ='two-prime',
1311       modulus = 28617237755030755643854803617273584643843067580642149032833640135949799721163782522787597288521902619948688786051081993247908700824196122780349730169173433743054172191054872553484065655968335396052034378669869864779940355219732200954630251223541048434478476115391643898092650304645086338265930608997389611376417609043761464100338332976874588396803891301015812818307951159858145399281035705713082131199940309445719678087542976246147777388465712394062188801177717719764254900022006288880246925156931391594131839991579403409541227225173269459173129377291869028712271737734702830877034334838181789916127814298794576266389,
1312       publicExponent = 17,
1313       privateExponent = 26933870828264240605980991639786903194205240075898493207372837775011576208154148256741268036255908348187001210401018346586267012540419880263858569570986761169933338532757527109161473558558433313931326474042230460969355628442100895016122589386862163232450330461545076609969553227901257730132640573174013751883368376011370428995523268034111482031427024082719896108094847702954695363285832195666458915142143884210891427766607838346722974883433132513540317964796373298134261669479023445911856492129270184781873446960437310543998533283339488055776892320162032014809906169940882070478200435536171854883284366514852906334641,
1314       prime1 = 177342190816702392178883147766999616783253285436834252111702533617098994535049411784501174309695427674025956656849179054202187436663487378682303508229883753383891163725167367039879190685255046547908384208614573353917213168937832054054779266431207529839577747601879940934691505396807977946728204814969824442867,
1315       prime2 = 161367340863680900415977542864139121629424927689088951345472941851682581254789586032968359551717004797621579428672968948552429138154521719743297455351687337112710712475376510559020211584326773715482918387500187602625572442687231345855402020688502483137168684570635690059254866684191216155909970061793538842967,
1316       exponent1 = 62591361464718491357252875682470452982324688977706206627659717747211409835899792394529826226951327414362102349476180842659595565881230839534930649963488383547255704844176717778780890830090016428673547367746320007264898765507470136725216211681602657590439205035957626212244060728285168687080542875871702744541,
1317       exponent2 = 28476589564178982426348978152495139111074987239250991413906989738532220221433456358759122273832412611344984605059935696803369847909621479954699550944415412431654831613301737157474154985469430655673456186029444871051571607533040825739188591886206320553618003159523945304574388238386685203984112363845918619347,
1318       coefficient = 34340318160575773065401929915821192439103777558577109939078671096408836197675640654693301707202885840826672396546056002756167635035389371579540325327619480512374920136684787633921441576901246290213545161954865184290700344352088099063404416346968182170720521708773285279884132629954461545103181082503707725012,
1319       otherPrimeInfos = asn1_NOVALUE}.
1320
1321pss_params(sha256) ->
1322    #'RSASSA-PSS-params'{
1323       hashAlgorithm = #'HashAlgorithm'{algorithm = ?'id-sha256'},
1324       maskGenAlgorithm = #'MaskGenAlgorithm'{algorithm = ?'id-mgf1',
1325                                              parameters = #'HashAlgorithm'{algorithm = ?'id-sha256'}
1326                                             },
1327       saltLength = 32,
1328       trailerField = 1}.
1329
1330