aboutsummaryrefslogtreecommitdiffstats
path: root/lib/public_key/doc/src/public_key.xml
blob: 3040f2db0d64ec827de7c0824343819da30b0e6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>2008</year>
      <year>2017</year>
      <holder>Ericsson AB, All Rights Reserved</holder>
    </copyright>
    <legalnotice>
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  The Initial Developer of the Original Code is Ericsson AB.
    </legalnotice>

    <title>public_key</title>
    <prepared>Ingela Anderton Andin</prepared>
    <responsible></responsible>
    <docno></docno>
    <date></date>
    <rev></rev>
  </header>
  <module>public_key</module>
  <modulesummary>API module for public-key infrastructure.</modulesummary>
  <description>
    <p>Provides functions to handle public-key infrastructure,
    for details see
    <seealso marker="public_key_app">public_key(6)</seealso>.
    </p>
  </description>

  <section>
    <title>DATA TYPES</title> 
    
    <note><p>All records used in this Reference Manual 
    <!--     except #policy_tree_node{}  -->
    are generated from ASN.1 specifications
    and are documented in the User's Guide. See <seealso 
    marker="public_key_records">Public-key Records</seealso>.
    </p></note>
    
    <p>Use the following include directive to get access to the 
    records and constant macros described here and in the User's Guide:</p>
    
    <code> -include_lib("public_key/include/public_key.hrl").</code>

    <p>The following data types are used in the functions for <c>public_key</c>:</p>

    <taglist>
      <tag><c>oid()</c></tag>
      <item><p>Object identifier, a tuple of integers as generated by the <c>ASN.1</c> compiler.</p></item>

      <tag><c>boolean() =</c></tag>
      <item><p><c>true | false</c></p></item>
      
      <tag><c>string() =</c></tag>
      <item><p><c>[bytes()]</c></p></item>

      <tag><c>der_encoded() =</c></tag>
      <item><p><c>binary()</c></p></item>

      <tag><c>pki_asn1_type() =</c></tag>
      <item>
	<p><c>'Certificate'</c></p>
	<p><c>| 'RSAPrivateKey'</c></p>
	<p><c>| 'RSAPublicKey'</c></p>
	<p><c>| 'DSAPrivateKey'</c></p>
	<p><c>| 'DSAPublicKey'</c></p>
	<p><c>| 'DHParameter'</c></p>
	<p><c>| 'SubjectPublicKeyInfo'</c></p>
	<p><c>| 'PrivateKeyInfo'</c></p>
	<p><c>| 'CertificationRequest'</c></p>
	<p><c>| 'CertificateList'</c></p>
	<p><c>| 'ECPrivateKey'</c></p>
	<p><c>| 'EcpkParameters'</c></p>
      </item>

      <tag><c>pem_entry () =</c></tag>
      <item><p><c>{pki_asn1_type(), binary(), %% DER or encrypted DER</c></p>
      <p><c> not_encrypted | cipher_info()}</c></p></item>
      
      <tag><c>cipher_info() = </c></tag>
      <item><p><c>{"RC2-CBC" | "DES-CBC" | "DES-EDE3-CBC", crypto:strong_rand_bytes(8)</c></p>
      <p><c>| {#'PBEParameter{}, digest_type()} | #'PBES2-params'{}}</c></p>
      </item>
      
      <tag><c>public_key() =</c></tag>
      <item><p><c>rsa_public_key() | dsa_public_key() | ec_public_key()</c></p></item>
      
      <tag><c>private_key() =</c></tag>
      <item><p><c>rsa_private_key() | dsa_private_key() | ec_private_key()</c></p></item>

      <tag><c>rsa_public_key() =</c></tag>
      <item><p><c>#'RSAPublicKey'{}</c></p></item>

      <tag><c>rsa_private_key() =</c></tag>
      <item><p><c>#'RSAPrivateKey'{}</c></p></item>

      <tag><c>dsa_public_key() =</c></tag>
      <item><p><c>{integer(),  #'Dss-Parms'{}}</c></p></item>

      <tag><c>dsa_private_key() =</c></tag>
      <item><p><c>#'DSAPrivateKey'{}</c></p></item>

      <tag><c>ec_public_key()</c></tag>
      <item><p>= <c>{#'ECPoint'{}, #'ECParameters'{} | {namedCurve, oid()}}</c></p></item>

      <tag><c>ec_private_key() =</c></tag>
      <item><p><c>#'ECPrivateKey'{}</c></p></item>

      <tag><c>key_params() =</c></tag>
      <item><p> #'DHParameter'{} |  {namedCurve, oid()} |  #'ECParameters'{} 
      | {rsa, Size::integer(), PubExp::integer()} </p></item>      

      <tag><c>public_crypt_options() =</c></tag>
      <item><p><c>[{rsa_pad, rsa_padding()}]</c></p></item>

      <tag><c>rsa_padding() =</c></tag>
      <item>
	<p><c>'rsa_pkcs1_padding'</c></p>
	<p><c>| 'rsa_pkcs1_oaep_padding'</c></p>
	<p><c>| 'rsa_no_padding'</c></p>
      </item>

      <tag><c>public_sign_options() =</c></tag>
      <item><p><c>[{rsa_pad, rsa_sign_padding()} | {rsa_pss_saltlen, integer()}]</c></p></item>

      <tag><c>rsa_sign_padding() =</c></tag>
      <item>
	<p><c>'rsa_pkcs1_padding'</c></p>
	<p><c>| 'rsa_pkcs1_pss_padding'</c></p>
      </item>

      <tag><c>digest_type() = </c></tag>
      <item><p>Union of <c>rsa_digest_type()</c>, <c>dss_digest_type()</c>, 
      and <c>ecdsa_digest_type()</c>.</p></item>

      <tag><c>rsa_digest_type() = </c></tag>
      <item><p><c>'md5' | 'ripemd160' | 'sha' | 'sha224' | 'sha256' | 'sha384' | 'sha512'</c></p></item>

      <tag><c>dss_digest_type() = </c></tag>
      <item><p><c>'sha' | 'sha224' | 'sha256' | 'sha384' | 'sha512'</c></p>
      <p>Note that the actual supported dss_digest_type depends on the underlying crypto library.
      In OpenSSL version >= 1.0.1 the listed digest are supported, while in 1.0.0 only
      sha, sha224 and sha256 are supported. In version 0.9.8 only sha is supported.</p>
      </item>

      <tag><c>ecdsa_digest_type() = </c></tag>
      <item><p><c>'sha' | 'sha224' | 'sha256' | 'sha384' | 'sha512'</c></p></item>
      
      <tag><c>crl_reason() = </c></tag>
      <item>
	<p><c>unspecified</c></p>
	<p><c>| keyCompromise</c></p>
	<p><c>| cACompromise</c></p>
	<p><c>| affiliationChanged</c></p>
	<p><c>| superseded</c></p>
	<p><c>| cessationOfOperation</c></p>
	<p><c>| certificateHold</c></p>
	<p><c>| privilegeWithdrawn</c></p>
	<p><c>| aACompromise</c></p>
      </item>

      <tag><c>issuer_name() =</c></tag>
      <item><p><c>{rdnSequence,[#'AttributeTypeAndValue'{}]}</c></p>  
      </item>
      
      <tag><c>ssh_file() =</c></tag>
      <item>
	<p><c>openssh_public_key</c></p>
	<p><c>| rfc4716_public_key</c></p>
	<p><c>| known_hosts</c></p>
	<p><c>| auth_keys</c></p>
      </item>
    </taglist>
    
    
<!--     <p><code>policy_tree() = [Root, Children]</code></p> -->
    
<!--     <p><code>Root = #policy_tree_node{}</code></p>    -->

<!--     <p><code>Children = [] | policy_tree()</code></p> -->
	
<!--     <p>The <c>policy_tree_node</c> record has the following fields:</p> -->
    
<!--     <taglist> -->
      
<!--       <tag>valid_policy</tag> -->
<!--       <item>A single policy OID representing a -->
<!--          valid policy for the path of length x.</item> -->
      
<!--       <tag>qualifier_set</tag> -->
<!--       <item>A set of policy qualifiers associated -->
<!--          with the valid policy in certificate x.</item> -->
      
<!--       <tag>critically_indicator</tag> -->
<!--       <item>Indicates whether the -->
<!--          certificate policy extension in certificate x was marked as -->
<!--          critical.</item> -->
      
<!--       <tag>expected_policy_set</tag> -->
<!--       <item>Contains one or more policy OIDs -->
<!--          that would satisfy this policy in the certificate x+1.</item>         -->
<!--     </taglist> -->
  </section> 

<funcs>    

  <func>
    <name>compute_key(OthersKey, MyKey)-></name>
    <name>compute_key(OthersKey, MyKey, Params)-></name>
    <fsummary>Computes shared secret.</fsummary>
    <type>
      <v>OthersKey = #'ECPoint'{} | binary(), MyKey = #'ECPrivateKey'{} | binary()</v>
      <v>Params =  #'DHParameter'{}</v>
    </type>
  <desc>
    <p>Computes shared secret.</p>
  </desc>
  </func>

  <func>
    <name>decrypt_private(CipherText, Key) -> binary()</name>
    <name>decrypt_private(CipherText, Key, Options) -> binary()</name>
    <fsummary>Public-key decryption.</fsummary>
    <type>
      <v>CipherText = binary()</v>
      <v>Key = rsa_private_key()</v>
      <v>Options = public_crypt_options()</v>
  </type> 
  <desc> 
    <p>Public-key decryption using the private key. See also <seealso
	marker="crypto:crypto#private_decrypt/4">crypto:private_decrypt/4</seealso></p> 
  </desc> 
  </func>

  <func>
    <name>decrypt_public(CipherText, Key) - > binary()</name>
    <name>decrypt_public(CipherText, Key, Options) - > binary()</name>
    <fsummary>Public-key decryption.</fsummary>
    <type>
      <v>CipherText = binary()</v>
      <v>Key = rsa_public_key()</v>
      <v>Options = public_crypt_options()</v>
  </type> 
  <desc> 
    <p>Public-key decryption using the public key. See also <seealso
	marker="crypto:crypto#public_decrypt/4">crypto:public_decrypt/4</seealso></p> 
  </desc> 
  </func> 

  <func>
    <name>der_decode(Asn1type, Der) -> term()</name>
    <fsummary>Decodes a public-key ASN.1 DER encoded entity.</fsummary>
    <type>
      <v>Asn1Type = atom()</v>
      <d>ASN.1 type present in the Public Key applications
      ASN.1 specifications.</d>
      <v>Der = der_encoded()</v>
    </type> 
    <desc> 
      <p>Decodes a public-key ASN.1 DER encoded entity.</p>
    </desc> 
  </func>
    
  <func>
    <name>der_encode(Asn1Type, Entity) -> der_encoded()</name>
    <fsummary>Encodes a public-key entity with ASN.1 DER encoding.</fsummary>
    <type>
      <v>Asn1Type = atom()</v>
      <d>ASN.1 type present in the Public Key applications
	 ASN.1 specifications.</d>
      <v>Entity = term()</v>
      <d>Erlang representation of <c>Asn1Type</c></d>
  </type> 
  <desc> 
    <p>Encodes a public-key entity with ASN.1 DER encoding.</p>
  </desc> 
  </func>

  <func>
    <name>dh_gex_group(MinSize, SuggestedSize, MaxSize, Groups) -> {ok, {Size,Group}} | {error,Error}</name>
    <fsummary>Selects a group for Diffie-Hellman key exchange</fsummary>
    <type>
      <v>MinSize = positive_integer()</v>
      <v>SuggestedSize = positive_integer()</v>
      <v>MaxSize = positive_integer()</v>
      <v>Groups = undefined | [{Size,[{G,P}]}]</v>
      <v>Size = positive_integer()</v>
      <v>Group = {G,P}</v>
      <v>G = positive_integer()</v>
      <v>P = positive_integer()</v>
    </type>
    <desc>
      <p>Selects a group for Diffie-Hellman key exchange with the key size in the range <c>MinSize...MaxSize</c>
      and as close to <c>SuggestedSize</c> as possible. If <c>Groups == undefined</c> a default set will be
      used, otherwise the group is selected from <c>Groups</c>.</p>
      <p>First a size, as close as possible to SuggestedSize, is selected. Then one group with that key size
      is randomly selected from the specified set of groups. If no size within the limits of <c>MinSize</c>
      and <c>MaxSize</c> is available, <c>{error,no_group_found}</c> is returned.</p>
      <p>The default set of groups is listed in <c>lib/public_key/priv/moduli</c>. This file may be regenerated like this:</p>
      <pre>
	$> cd $ERL_TOP/lib/public_key/priv/
	$> generate
         ---- wait until all background jobs has finished. It may take several days !
	$> cat moduli-* > moduli
	$> cd ..; make 
      </pre>
    </desc>
  </func>

    <func>
    <name>encrypt_private(PlainText, Key) -> binary()</name>
    <fsummary>Public-key encryption using the private key.</fsummary>
    <type>
      <v>PlainText = binary()</v>
      <v>Key = rsa_private_key()</v> 
  </type> 
  <desc> 
    <p>Public-key encryption using the private key.
     See also <seealso
	marker="crypto:crypto#private_encrypt/4">crypto:private_encrypt/4</seealso>.</p> 
  </desc> 
  </func>   

  <func>
    <name>encrypt_public(PlainText, Key) -> binary()</name>
    <fsummary>Public-key encryption using the public key.</fsummary>
    <type>
      <v>PlainText = binary()</v>
      <v>Key = rsa_public_key()</v> 
  </type> 
  <desc> 
    <p>Public-key encryption using the public key. See also <seealso
	marker="crypto:crypto#public_encrypt/4">crypto:public_encrypt/4</seealso>.</p> 
  </desc> 
  </func>   
  
  <func>
    <name>generate_key(Params) -> {Public::binary(), Private::binary()}  | #'ECPrivateKey'{} | #'RSAPrivateKey'{}</name>
    <fsummary>Generates a new keypair.</fsummary>
    <type>
      <v>Params = key_params()</v>
    </type>
  <desc>
    <p>Generates a new keypair. Note that except for Diffie-Hellman
    the public key is included in the private key structure. See also
    <seealso marker="crypto:crypto#generate_key/2">crypto:generate_key/2</seealso>
    </p>
  </desc>
  </func>

  <func>
    <name>pem_decode(PemBin) -> [pem_entry()]</name>
    <fsummary>Decodes PEM binary data and returns
    entries as ASN.1 DER encoded entities.</fsummary>
    <type>
      <v>PemBin = binary()</v>
      <d>Example {ok, PemBin} = file:read_file("cert.pem").</d>
    </type> 
  <desc> 
    <p>Decodes PEM binary data and returns
    entries as ASN.1 DER encoded entities.</p>
  </desc> 
  </func> 
    
   <func>
    <name>pem_encode(PemEntries) -> binary()</name>
    <fsummary>Creates a PEM binary.</fsummary>
    <type>
      <v> PemEntries = [pem_entry()] </v> 
  </type> 
  <desc> 
    <p>Creates a PEM binary.</p> 
  </desc> 
  </func>

   <func>
    <name>pem_entry_decode(PemEntry) -> term()</name>
    <name>pem_entry_decode(PemEntry, Password) -> term()</name>
    <fsummary>Decodes a PEM entry.</fsummary>
    <type>
      <v>PemEntry = pem_entry()</v> 
      <v>Password = string()</v> 
  </type> 
  <desc> 
    <p>Decodes a PEM entry. <c>pem_decode/1</c> returns a list of PEM
    entries. Notice that if the PEM entry is of type
    'SubjectPublickeyInfo', it is further decoded to an
    <c>rsa_public_key()</c> or <c>dsa_public_key()</c>.</p>
  </desc> 
  </func>

   <func>
    <name>pem_entry_encode(Asn1Type, Entity) -> pem_entry()</name>
    <name>pem_entry_encode(Asn1Type, Entity, {CipherInfo, Password}) -> pem_entry()</name>
    <fsummary>Creates a PEM entry that can be fed to <c>pem_encode/1</c>.</fsummary>
    <type>
      <v>Asn1Type = pki_asn1_type()</v>
      <v>Entity = term()</v>
      <d>Erlang representation of
      <c>Asn1Type</c>.  If <c>Asn1Type</c> is 'SubjectPublicKeyInfo',
      <c>Entity</c> must be either an <c>rsa_public_key()</c>, 
      <c>dsa_public_key()</c> or an <c>ec_public_key()</c>
      and this function creates the appropriate
      'SubjectPublicKeyInfo' entry.
      </d>
      <v>CipherInfo = cipher_info()</v>
      <v>Password = string()</v> 
    </type> 
  <desc> 
    <p>Creates a PEM entry that can be feed to <c>pem_encode/1</c>.</p>
  </desc> 
  </func>
  
  <func>
    <name>pkix_decode_cert(Cert, otp|plain) ->  #'Certificate'{} | #'OTPCertificate'{}</name>
    <fsummary>Decodes an ASN.1 DER-encoded PKIX x509 certificate.</fsummary>
    <type>
      <v>Cert = der_encoded()</v> 
  </type> 
  <desc> 
    <p>Decodes an ASN.1 DER-encoded PKIX certificate. Option <c>otp</c>
    uses the customized ASN.1 specification OTP-PKIX.asn1 for
    decoding and also recursively decode most of the standard
    parts.</p>
  </desc> 
  </func>

  <func>
    <name>pkix_encode(Asn1Type, Entity, otp | plain) -> der_encoded()</name>
    <fsummary>DER encodes a PKIX x509 certificate or part of such a
    certificate.</fsummary>
    <type>
      <v>Asn1Type = atom()</v>
      <d>The ASN.1 type can be 'Certificate', 'OTPCertificate' or a subtype of either.</d>
      <v>Entity = #'Certificate'{} | #'OTPCertificate'{} | a valid subtype</v>
  </type> 
  <desc> 
    <p>DER encodes a PKIX x509 certificate or part of such a
    certificate. This function must be used for encoding certificates or parts of certificates
    that are decoded/created in the <c>otp</c> format, whereas for the plain format this
    function directly calls <c>der_encode/2</c>.</p> 
  </desc> 
  </func>

 <func>
    <name>pkix_is_issuer(Cert, IssuerCert) -> boolean()</name>
    <fsummary>Checks if <c>IssuerCert</c> issued <c>Cert</c>.</fsummary>
    <type>
      <v>Cert = der_encoded() | #'OTPCertificate'{} | #'CertificateList'{}</v>
      <v>IssuerCert = der_encoded() | #'OTPCertificate'{}</v>
  </type> 
  <desc> 
    <p>Checks if <c>IssuerCert</c> issued <c>Cert</c>.</p> 
  </desc> 
  </func>
  
  <func>
    <name>pkix_is_fixed_dh_cert(Cert) -> boolean()</name>
    <fsummary>Checks if a certificate is a fixed Diffie-Hellman certificate.</fsummary>
    <type>
        <v>Cert = der_encoded() | #'OTPCertificate'{}</v>
  </type> 
  <desc> 
    <p>Checks if a certificate is a fixed Diffie-Hellman certificate.</p> 
  </desc> 
  </func>  
  
  <func>
    <name>pkix_is_self_signed(Cert) -> boolean()</name>
    <fsummary>Checks if a certificate is self-signed.</fsummary>
    <type>
       <v>Cert = der_encoded() | #'OTPCertificate'{}</v>
  </type> 
  <desc> 
    <p>Checks if a certificate is self-signed.</p> 
  </desc> 
  </func>

  <func>
    <name>pkix_issuer_id(Cert, IssuedBy) -> {ok, IssuerID} | {error, Reason}</name>
    <fsummary>Returns the issuer id.</fsummary>
    <type>
        <v>Cert = der_encoded() | #'OTPCertificate'{}</v>
	<v>IssuedBy = self | other</v>
	<v>IssuerID = {integer(), issuer_name()}</v>
	<d>The issuer id consists of the serial number and the issuers name.</d>
	<v>Reason = term()</v>
    </type> 
    <desc> 
    <p>Returns the issuer id.</p> 
    </desc> 
  </func>
  
 
  <func>
    <name>pkix_normalize_name(Issuer) -> Normalized</name>
    <fsummary>Normalizes an issuer name so that it can be easily
    compared to another issuer name.</fsummary>
    <type>
      <v>Issuer = issuer_name()</v>
      <v>Normalized = issuer_name()</v>
  </type> 
  <desc> 
    <p>Normalizes an issuer name so that it can be easily
    compared to another issuer name.</p> 
  </desc> 
  </func>
   
  <func>
    <name>pkix_path_validation(TrustedCert, CertChain, Options) -> {ok, {PublicKeyInfo, PolicyTree}} | {error, {bad_cert, Reason}} </name>
    <fsummary>Performs a basic path validation according to RFC 5280.</fsummary>
     <type>
       <v>TrustedCert =  #'OTPCertificate'{} | der_encoded() | atom()</v>
       <d>Normally a trusted certificate, but it can also be a path-validation
       error that can be discovered while
       constructing the input to this function and that is to be run through the <c>verify_fun</c>.
       Examples are <c>unknown_ca</c> and <c>selfsigned_peer.</c>
       </d>
       <v>CertChain = [der_encoded()]</v>
       <d>A list of DER-encoded certificates in trust order ending with the peer certificate.</d>
       <v>Options = proplists:proplist()</v>
       <v>PublicKeyInfo = {?'rsaEncryption' | ?'id-dsa',
       rsa_public_key() | integer(), 'NULL' | 'Dss-Parms'{}}</v>
       <v>PolicyTree = term()</v>
       <d>At the moment this is always an empty list as policies are not currently supported.</d>
       <v>Reason = cert_expired | invalid_issuer | invalid_signature | name_not_permitted |
       missing_basic_constraint | invalid_key_usage | {revoked, crl_reason()} | atom()
       </v>
     </type>
     <desc>
       <p>
	 Performs a basic path validation according to
	 <url href="http://www.ietf.org/rfc/rfc5280.txt">RFC 5280.</url>
	 However, CRL validation is done separately by <seealso
	 marker="#pkix_crls_validate-3">pkix_crls_validate/3 </seealso> and is to be called
	 from the supplied <c>verify_fun</c>.
       </p>

       <p>Available options:</p>

       <taglist>
	<tag>{verify_fun, fun()}</tag>
	<item>
	  <p>The fun must be defined as:</p>

	  <code>
fun(OtpCert :: #'OTPCertificate'{},
    Event :: {bad_cert, Reason :: atom() | {revoked, atom()}} |
             {extension, #'Extension'{}},
    InitialUserState :: term()) ->
	{valid, UserState :: term()} |
	{valid_peer, UserState :: term()} |
	{fail, Reason :: term()} |
	{unknown, UserState :: term()}.
	  </code>

	<p>If the verify callback fun returns <c>{fail, Reason}</c>, the
	verification process is immediately stopped. If the verify
	callback fun returns <c>{valid, UserState}</c>, the verification
	process is continued. This can be used to accept specific path
	validation errors, such as <c>selfsigned_peer</c>, as well as
	verifying application-specific extensions. If called with an
	extension unknown to the user application, the return value
	<c>{unknown, UserState}</c> is to be used.</p>

	</item>
	<tag>{max_path_length, integer()}</tag>
	<item>
	  The <c>max_path_length</c> is the maximum number of non-self-issued
	  intermediate certificates that can follow the peer certificate
	  in a valid certification path. So, if <c>max_path_length</c> is 0, the PEER must
	  be signed by the trusted ROOT-CA directly, if it is 1, the path can
	  be PEER, CA, ROOT-CA, if it is 2, the path can
	  be PEER, CA, CA, ROOT-CA, and so on.
	</item>
      </taglist>

      <p>Possible reasons for a bad certificate: </p>
      <taglist>
	<tag>cert_expired</tag>
	<item><p>Certificate is no longer valid as its expiration date has passed.</p></item>

	<tag>invalid_issuer</tag>
	<item><p>Certificate issuer name does not match the name of the issuer certificate in the chain.</p></item>

	<tag>invalid_signature</tag>
	<item><p>Certificate was not signed by its issuer certificate in the chain.</p></item>

	<tag>name_not_permitted</tag>
	<item><p>Invalid Subject Alternative Name extension.</p></item>

	<tag>missing_basic_constraint</tag>
	<item><p>Certificate, required to have the basic constraints extension, does not have
	a basic constraints extension.</p></item>

	<tag>invalid_key_usage</tag>
	<item><p>Certificate key is used in an invalid way according to the key-usage extension.</p></item>

	<tag>{revoked, crl_reason()}</tag>
	<item><p>Certificate has been revoked.</p></item>

	<tag>atom()</tag>
	<item><p>Application-specific error reason that is to be checked by the <c>verify_fun</c>.</p></item>
      </taglist>

    </desc>
   </func>

    <func>  
      <name>pkix_crl_issuer(CRL) -> issuer_name()</name>
      <fsummary>Returns the issuer of the <c>CRL</c>.</fsummary>
      <type>
	<v>CRL = der_encoded() | #'CertificateList'{} </v> 
      </type> 
      <desc> 
	<p>Returns the issuer of the <c>CRL</c>.</p>
      </desc> 
    </func> 
   
   <func>
     <name>pkix_crls_validate(OTPCertificate, DPAndCRLs, Options) -> CRLStatus()</name>
     <fsummary>Performs CRL validation.</fsummary>
     <type>
       <v>OTPCertificate =  #'OTPCertificate'{}</v>
       <v>DPAndCRLs  = [{DP::#'DistributionPoint'{}, {DerCRL::der_encoded(), CRL::#'CertificateList'{}}}] </v>
       <v>Options = proplists:proplist()</v>
       <v>CRLStatus() =  valid | {bad_cert, revocation_status_undetermined} |  {bad_cert, {revocation_status_undetermined,
       {bad_crls, Details::term()}}} | {bad_cert, {revoked, crl_reason()}}</v>
     </type>
     <desc>
      <p>Performs CRL validation. It is intended to be called from
      the verify fun of  <seealso marker="#pkix_path_validation-3"> pkix_path_validation/3
       </seealso>.</p>

       <p>Available options:</p>

      <taglist>
	
	<tag>{update_crl, fun()}</tag>
	<item>
	  <p>The fun has the following type specification:</p>

	  <code> fun(#'DistributionPoint'{}, #'CertificateList'{}) ->
        #'CertificateList'{}</code>

	  <p>The fun uses the information in the distribution point to access
	  the latest possible version of the CRL. If this fun is not specified,
	  Public Key uses the default implementation:
	  </p>
	  <code> fun(_DP, CRL) -> CRL end</code>
	</item>

	<tag>{issuer_fun, fun()}</tag>
	<item>
	  <p>The fun has the following type specification:</p>
	  
	  <code>
fun(#'DistributionPoint'{}, #'CertificateList'{},
    {rdnSequence,[#'AttributeTypeAndValue'{}]}, term()) ->
	{ok, #'OTPCertificate'{}, [der_encoded]}</code>

	  <p>The fun returns the root certificate and certificate chain
	  that has signed the CRL. 
	  </p>
	  <code> fun(DP, CRL, Issuer, UserState) -> {ok, RootCert, CertChain}</code>
	</item>

	<tag>{undetermined_details, boolean()}</tag>
	<item>
	  <p>Defaults to false. When revocation status can not be
	  determined, and this option is set to true, details of why no
	  CRLs where accepted are included in the return value.</p>
	</item>

      </taglist>
    </desc>
   </func>
   
   <func>  
     <name>pkix_crl_verify(CRL, Cert) -> boolean()</name>
     <fsummary> Verify that  <c>Cert</c>  is the <c> CRL</c>  signer. </fsummary>
     <type>
       <v>CRL = der_encoded() | #'CertificateList'{} </v> 
       <v>Cert = der_encoded() | #'OTPCertificate'{} </v> 
     </type> 
     <desc> 
       <p>Verify that <c>Cert</c> is the <c>CRL</c> signer.</p>
     </desc> 
   </func>

   <func>  
     <name>pkix_dist_point(Cert) -> DistPoint</name>
     <fsummary>Creates a distribution point for CRLs issued by the same issuer as <c>Cert</c>.</fsummary>
     <type>
       <v> Cert  = der_encoded() | #'OTPCertificate'{} </v> 
       <v> DistPoint =  #'DistributionPoint'{}</v> 
     </type> 
     <desc> 
       <p>Creates a distribution point for CRLs issued by the same issuer as <c>Cert</c>.
       Can be used as input to <seealso
       marker="#pkix_crls_validate-3">pkix_crls_validate/3 </seealso>
       </p>
     </desc> 
   </func>
   
   <func>  
     <name>pkix_dist_points(Cert) -> DistPoints</name>
     <fsummary> Extracts distribution points from the certificates extensions.</fsummary>
     <type>
       <v> Cert  = der_encoded() | #'OTPCertificate'{} </v> 
       <v> DistPoints =  [#'DistributionPoint'{}]</v> 
     </type> 
     <desc> 
       <p> Extracts distribution points from the certificates extensions.</p>
     </desc> 
 </func>
   
  <func>
    <name>pkix_match_dist_point(CRL, DistPoint) -> boolean()</name>
    <fsummary>Checks whether the given distribution point matches the
    Issuing Distribution Point of the CRL.</fsummary>

    <type>
      <v>CRL = der_encoded() | #'CertificateList'{} </v>
      <v>DistPoint = #'DistributionPoint'{}</v>
    </type>
    <desc>
      <p>Checks whether the given distribution point matches the
      Issuing Distribution Point of the CRL, as described in RFC 5280.
      If the CRL doesn't have an Issuing Distribution Point extension,
      the distribution point always matches.</p>
    </desc>
  </func>

  <func>
    <name>pkix_sign(#'OTPTBSCertificate'{}, Key) -> der_encoded()</name>
    <fsummary>Signs certificate.</fsummary>
    <type>
      <v>Key = rsa_private_key() | dsa_private_key()</v> 
    </type> 
    <desc> 
      <p>Signs an 'OTPTBSCertificate'. Returns the corresponding
      DER-encoded certificate.</p> 
    </desc> 
  </func> 

  <func>
    <name>pkix_sign_types(AlgorithmId) -> {DigestType, SignatureType}</name>
    <fsummary>Translates signature algorithm OID to Erlang digest and signature algorithm types.</fsummary>
    <type>
      <v>AlgorithmId = oid()</v>
      <d>Signature OID from a certificate or a certificate revocation list.</d>
      <v>DigestType = rsa_digest_type() | dss_digest_type()</v>
      <v>SignatureType = rsa | dsa | ecdsa</v>
    </type>
    <desc>
      <p>Translates signature algorithm OID to Erlang digest and signature types.
      </p>
    </desc>
  </func>

  <func>
    <name>pkix_test_data(Options) -> Config </name>
    <fsummary>Creates certificate test data.</fsummary>
    <type>
      <v>Options = #{chain_type() := chain_opts()} </v>
      <d>Options for ROOT, Intermediate and Peer certs</d>
      
      <v>chain_type() = server_chain | client_chain </v>

      <v>chain_opts() = #{chain_end() := [cert_opt()],
          intermediates => [[cert_opt()]]}</v>
      <d>A valid chain must have at least a ROOT and a peer cert</d>

      <v>chain_end() = root | peer </v>

      <v>cert_opt() = {Key, Value}</v>
      <d>For available options see <seealso marker="#cert_opt"> cert_opt()</seealso> below.</d>

      <v>Config = #{server_config := [conf_opt()],
      client_config := [conf_opt()]}</v>

      <v>conf_opt() = {cert, der_encoded()} | {key, der_encoded()} |{cacerts, [der_encoded()]}</v>
      <d>This is a subset of the type <seealso marker="ssl:ssl#type-ssloption"> ssl:ssl_option()</seealso> </d>
    </type>
    
    <desc>
      <p>Creates certificate test data to facilitate automated testing
      of applications using X509-certificates often through
      SSL/TLS. The test data can be used when you have control
      over both the client and the server in a test scenario.
      </p>
      
      <p> The <marker id="cert_opt"/> cert_opt() type consists of the following options: </p>
      <taglist>
	<tag> {digest, digest_type()}</tag>
	<item><p>Hash algorithm to be used for
	signing the certificate together with the key option. Defaults to sha that is sha1.
	</p></item>
	<tag> {key, key_params() | private_key()}</tag>
	<item><p>Parameters to be used to call public_key:generate_key/1, to generate a key, or an existing
	key. Defaults to generating an ECDSA key. Note this could fail if Erlang/OTP is compiled with a very old
	cryptolib.</p></item>
	<tag> {validity, {From::erlang:timestamp(), To::erlang:timestamp()}} </tag>
	<item><p>The validity period of the certificate.</p></item>
	<tag> {extensions, [#'Extension'{}]}</tag>
	<item><p> Extensions to include in the certificate.</p>
	      
	  <p>Default extensions included in CA certificates if not
	  otherwise specified are: </p>
	  <code>[#'Extension'{extnID = ?'id-ce-keyUsage',
              extnValue = [keyCertSign, cRLSign],
              critical = false},
#'Extension'{extnID = ?'id-ce-basicConstraints',
             extnValue = #'BasicConstraints'{cA = true},
             critical = true}]
	  </code>

	  <p>Default extensions included in the server peer cert if not
	  otherwise specified are: </p>
	  <code>[#'Extension'{extnID = ?'id-ce-keyUsage',
              extnValue = [digitalSignature, keyAgreement],
              critical = false},
#'Extension'{extnID = ?'id-ce-subjectAltName',
             extnValue = [{dNSName, Hostname}],
             critical = false}]
	  </code>
	  <p>Hostname is the result of calling net_adm:localhost() in the Erlang node
	  where this funcion is called.
	  </p></item>

	</taglist>
	  
	<note><p>
	Note that the generated certificates and keys does not provide a formally correct PKIX-trust-chain 
	and they can not be used to achieve real security. This function is provided for testing purposes only.
</p></note>
    </desc>
  </func>
  
  <func>  
    <name>pkix_verify(Cert, Key) -> boolean()</name>
    <fsummary>Verifies PKIX x.509 certificate signature.</fsummary>
    <type>
      <v>Cert = der_encoded()</v>
      <v>Key = rsa_public_key() | dsa_public_key() | ec_public_key()</v> 
    </type> 
  <desc> 
    <p>Verifies PKIX x.509 certificate signature.</p>
  </desc> 
  </func> 

  <func>
    <name>pkix_verify_hostname(Cert, ReferenceIDs) -> boolean()</name>
    <name>pkix_verify_hostname(Cert, ReferenceIDs, Opts) -> boolean()</name>
    <fsummary>Verifies that a PKIX x.509 certificate <i>presented identifier</i> (e.g hostname) is
    an expected one.</fsummary>
    <type>
      <v>Cert = der_encoded() | #'OTPCertificate'{} </v>
      <v>ReferenceIDs = [ RefID ]</v>
      <v>RefID = {dns_id,string()} | {srv_id,string()} | {uri_id,string()} | {ip,inet:ip_address()|string()} | {OtherRefID,term()}}</v>
      <v>OtherRefID = atom()</v>
      <v>Opts = [ PvhOpt() ]</v>
      <v>PvhOpt = [MatchOpt | FailCallBackOpt | FqdnExtractOpt]</v>
      <v>MatchOpt = {match_fun, fun(RefId | FQDN::string(), PresentedID) -> boolean() | default}</v>
      <v>PresentedID = {dNSName,string()} | {uniformResourceIdentifier,string() | {iPAddress,list(byte())} | {OtherPresId,term()}}</v>
      <v>OtherPresID = atom()</v>
      <v>FailCallBackOpt = {fail_callback, fun(#'OTPCertificate'{}) -> boolean()}</v>
      <v>FqdnExtractOpt = {fqdn_fun, fun(RefID) -> FQDN::string() | default | undefined}</v>
    </type>
    <desc>
      <p>This function checks that the <i>Presented Identifier</i> (e.g hostname) in a peer certificate
      conforms with the Expected Identifier that the client wants to connect to.
      This functions is intended to be added as an extra client check to the peer certificate when performing
      <seealso marker="public_key:public_key#pkix_path_validation-3">public_key:pkix_path_validation/3</seealso>
      </p>
      <p>See <url href="https://tools.ietf.org/html/rfc6125">RFC 6125</url>
      for detailed information about hostname verification.
      The <seealso marker="using_public_key#verify_hostname">User's Manual</seealso>
      and
      <seealso marker="using_public_key#verify_hostname_examples">code examples</seealso>
      describes this function more detailed.
      </p>
      <p>The <c>{OtherRefId,term()}</c> is defined by the user and is passed to the <c>match_fun</c>, if defined.
      If that term is a binary, it will be converted to a string.
      </p>
      <p>The <c>ip</c> takes a 4-tuple or a 
      </p>
    </desc>
  </func>

  <func>
    <name>sign(Msg, DigestType, Key) -> binary()</name>
    <name>sign(Msg, DigestType, Key, Options) -> binary()</name>
    <fsummary>Creates a digital signature.</fsummary>
    <type>
       <v>Msg = binary() | {digest,binary()}</v>
       <d>The <c>Msg</c> is either the binary "plain text" data to be
       signed or it is the hashed value of "plain text", that is, the
       digest.</d>
       <v>DigestType = rsa_digest_type() | dss_digest_type() | ecdsa_digest_type()</v>
       <v>Key = rsa_private_key() | dsa_private_key() | ec_private_key()</v>
       <v>Options = public_sign_options()</v>
  </type>
  <desc>
    <p>Creates a digital signature.</p> 
  </desc> 
  </func>   

  <func>
    <name>ssh_decode(SshBin,  Type) -> [{public_key(), Attributes::list()}]</name>
    <fsummary>Decodes an SSH file-binary.</fsummary>
    <type>
      <v>SshBin = binary()</v>
      <d>Example <c>{ok, SshBin} = file:read_file("known_hosts")</c>.</d>
      <v>Type = public_key | ssh_file()</v>
      <d>If <c>Type</c> is <c>public_key</c> the binary can be either
      an RFC4716 public key or an OpenSSH public key.</d>
    </type>
  <desc>
    <p>Decodes an SSH file-binary. In the case of <c>known_hosts</c> or
    <c>auth_keys</c>, the binary can include one or more lines of the
    file. Returns a list of public keys and their attributes, possible
    attribute values depends on the file type represented by the
    binary.
    </p>

    <taglist>
      <tag>RFC4716 attributes - see RFC 4716.</tag>
      <item><p>{headers, [{string(), utf8_string()}]}</p></item>
      <tag>auth_key attributes - see manual page for sshd.</tag>
      <item>{comment, string()}</item>
      <item>{options, [string()]}</item>
      <item><p>{bits, integer()} - In SSH version 1 files.</p></item>
      <tag>known_host attributes - see manual page for sshd.</tag>
      <item>{hostnames, [string()]}</item>
      <item>{comment, string()}</item>
      <item><p>{bits, integer()} - In SSH version 1 files.</p></item>
    </taglist>

  </desc>
  </func>

  <func>
    <name>ssh_encode([{Key, Attributes}], Type) -> binary()</name>
    <fsummary>Encodes a list of SSH file entries to a binary.</fsummary>
    <type>
      <v>Key = public_key()</v>
      <v>Attributes = list()</v>
      <v>Type = ssh_file()</v>
    </type>
  <desc>
    <p>Encodes a list of SSH file entries (public keys and attributes) to a binary. Possible
    attributes depend on the file type, see <seealso
    marker="#ssh_decode-2"> ssh_decode/2 </seealso>.</p>
  </desc>
  </func>

  <func>
    <name>ssh_hostkey_fingerprint(HostKey) -> string()</name>
    <name>ssh_hostkey_fingerprint(DigestType, HostKey) -> string()</name>
    <name>ssh_hostkey_fingerprint([DigestType], HostKey) -> [string()]</name>
    <fsummary>Calculates a ssh fingerprint for a hostkey.</fsummary>
    <type>
      <v>Key = public_key()</v>
      <v>DigestType = digest_type()</v>
    </type>
  <desc>
    <p>Calculates a ssh fingerprint from a public host key as openssh does.</p>
    <p>The algorithm in <c>ssh_hostkey_fingerprint/1</c> is md5 to be compatible with older
    ssh-keygen commands. The string from the second variant is prepended by the algorithm name
    in uppercase as in newer ssh-keygen commands.</p>
    <p>Examples:</p>
    <code>
 2> public_key:ssh_hostkey_fingerprint(Key).    
 "f5:64:a6:c1:5a:cb:9f:0a:10:46:a2:5c:3e:2f:57:84"

 3> public_key:ssh_hostkey_fingerprint(md5,Key).
 "MD5:f5:64:a6:c1:5a:cb:9f:0a:10:46:a2:5c:3e:2f:57:84"

 4> public_key:ssh_hostkey_fingerprint(sha,Key).
 "SHA1:bSLY/C4QXLDL/Iwmhyg0PGW9UbY"

 5> public_key:ssh_hostkey_fingerprint(sha256,Key).
 "SHA256:aZGXhabfbf4oxglxltItWeHU7ub3Dc31NcNw2cMJePQ"

 6> public_key:ssh_hostkey_fingerprint([sha,sha256],Key).
 ["SHA1:bSLY/C4QXLDL/Iwmhyg0PGW9UbY",
  "SHA256:aZGXhabfbf4oxglxltItWeHU7ub3Dc31NcNw2cMJePQ"]
    </code>
  </desc>
  </func>

  <func>
    <name>verify(Msg, DigestType, Signature, Key) -> boolean()</name>
    <name>verify(Msg, DigestType, Signature, Key, Options) -> boolean()</name>
    <fsummary>Verifies a digital signature.</fsummary>
    <type>
      <v>Msg = binary() | {digest,binary()}</v>
       <d>The <c>Msg</c> is either the binary "plain text" data 
        or it is the hashed value of "plain text", that is, the digest.</d>
      <v>DigestType = rsa_digest_type() | dss_digest_type() | ecdsa_digest_type()</v>
      <v>Signature = binary()</v>
      <v>Key = rsa_public_key() | dsa_public_key() | ec_public_key()</v>
      <v>Options = public_sign_options()</v>
  </type>
  <desc>
    <p>Verifies a digital signature.</p>
  </desc> 
  </func>

  <func>
    <name>short_name_hash(Name) -> string()</name>
    <fsummary>Generates a short hash of an issuer name.</fsummary>
    <type>
      <v>Name = issuer_name()</v>
    </type>
    <desc>
      <p>Generates a short hash of an issuer name.  The hash is
      returned as a string containing eight hexadecimal digits.</p>

      <p>The return value of this function is the same as the result
      of the commands <c>openssl crl -hash</c> and
      <c>openssl x509 -issuer_hash</c>, when passed the issuer name of
      a CRL or a certificate, respectively.  This hash is used by the
      <c>c_rehash</c> tool to maintain a directory of symlinks to CRL
      files, in order to facilitate looking up a CRL by its issuer
      name.</p>
    </desc>
  </func>

</funcs>

</erlref>