summaryrefslogtreecommitdiff
path: root/lib/libcrypto/man/PEM_read_bio_PrivateKey.3
blob: 7dcea6dbe126a93e12562cd4167df2813ba4dd22 (plain)
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
.Dd $Mdocdate: November 3 2016 $
.Dt PEM_READ_BIO_PRIVATEKEY 3
.Os
.Sh NAME
.Nm PEM ,
.Nm PEM_read_bio_PrivateKey ,
.Nm PEM_read_PrivateKey ,
.Nm PEM_write_bio_PrivateKey ,
.Nm PEM_write_PrivateKey ,
.Nm PEM_write_bio_PKCS8PrivateKey ,
.Nm PEM_write_PKCS8PrivateKey ,
.Nm PEM_write_bio_PKCS8PrivateKey_nid ,
.Nm PEM_write_PKCS8PrivateKey_nid ,
.Nm PEM_read_bio_PUBKEY ,
.Nm PEM_read_PUBKEY ,
.Nm PEM_write_bio_PUBKEY ,
.Nm PEM_write_PUBKEY ,
.Nm PEM_read_bio_RSAPrivateKey ,
.Nm PEM_read_RSAPrivateKey ,
.Nm PEM_write_bio_RSAPrivateKey ,
.Nm PEM_write_RSAPrivateKey ,
.Nm PEM_read_bio_RSAPublicKey ,
.Nm PEM_read_RSAPublicKey ,
.Nm PEM_write_bio_RSAPublicKey ,
.Nm PEM_write_RSAPublicKey ,
.Nm PEM_read_bio_RSA_PUBKEY ,
.Nm PEM_read_RSA_PUBKEY ,
.Nm PEM_write_bio_RSA_PUBKEY ,
.Nm PEM_write_RSA_PUBKEY ,
.Nm PEM_read_bio_DSAPrivateKey ,
.Nm PEM_read_DSAPrivateKey ,
.Nm PEM_write_bio_DSAPrivateKey ,
.Nm PEM_write_DSAPrivateKey ,
.Nm PEM_read_bio_DSA_PUBKEY ,
.Nm PEM_read_DSA_PUBKEY ,
.Nm PEM_write_bio_DSA_PUBKEY ,
.Nm PEM_write_DSA_PUBKEY ,
.Nm PEM_read_bio_DSAparams ,
.Nm PEM_read_DSAparams ,
.Nm PEM_write_bio_DSAparams ,
.Nm PEM_write_DSAparams ,
.Nm PEM_read_bio_DHparams ,
.Nm PEM_read_DHparams ,
.Nm PEM_write_bio_DHparams ,
.Nm PEM_write_DHparams ,
.Nm PEM_read_bio_X509 ,
.Nm PEM_read_X509 ,
.Nm PEM_write_bio_X509 ,
.Nm PEM_write_X509 ,
.Nm PEM_read_bio_X509_AUX ,
.Nm PEM_read_X509_AUX ,
.Nm PEM_write_bio_X509_AUX ,
.Nm PEM_write_X509_AUX ,
.Nm PEM_read_bio_X509_REQ ,
.Nm PEM_read_X509_REQ ,
.Nm PEM_write_bio_X509_REQ ,
.Nm PEM_write_X509_REQ ,
.Nm PEM_write_bio_X509_REQ_NEW ,
.Nm PEM_write_X509_REQ_NEW ,
.Nm PEM_read_bio_X509_CRL ,
.Nm PEM_read_X509_CRL ,
.Nm PEM_write_bio_X509_CRL ,
.Nm PEM_write_X509_CRL ,
.Nm PEM_read_bio_PKCS7 ,
.Nm PEM_read_PKCS7 ,
.Nm PEM_write_bio_PKCS7 ,
.Nm PEM_write_PKCS7 ,
.Nm PEM_read_bio_NETSCAPE_CERT_SEQUENCE ,
.Nm PEM_read_NETSCAPE_CERT_SEQUENCE ,
.Nm PEM_write_bio_NETSCAPE_CERT_SEQUENCE ,
.Nm PEM_write_NETSCAPE_CERT_SEQUENCE
.Nd PEM routines
.Sh SYNOPSIS
.In openssl/pem.h
.Ft EVP_PKEY *
.Fo PEM_read_bio_PrivateKey
.Fa "BIO *bp"
.Fa "EVP_PKEY **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft EVP_PKEY *
.Fo PEM_read_PrivateKey
.Fa "FILE *fp"
.Fa "EVP_PKEY **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_PrivateKey
.Fa "BIO *bp"
.Fa "EVP_PKEY *x"
.Fa "const EVP_CIPHER *enc"
.Fa "unsigned char *kstr"
.Fa "int klen"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_PrivateKey
.Fa "FILE *fp"
.Fa "EVP_PKEY *x"
.Fa "const EVP_CIPHER *enc"
.Fa "unsigned char *kstr"
.Fa "int klen"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_PKCS8PrivateKey
.Fa "BIO *bp"
.Fa "EVP_PKEY *x"
.Fa "const EVP_CIPHER *enc"
.Fa "char *kstr"
.Fa "int klen"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_PKCS8PrivateKey
.Fa "FILE *fp"
.Fa "EVP_PKEY *x"
.Fa "const EVP_CIPHER *enc"
.Fa "char *kstr"
.Fa "int klen"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_PKCS8PrivateKey_nid
.Fa "BIO *bp"
.Fa "EVP_PKEY *x"
.Fa "int nid"
.Fa "char *kstr"
.Fa "int klen"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_PKCS8PrivateKey_nid
.Fa "FILE *fp"
.Fa "EVP_PKEY *x"
.Fa "int nid"
.Fa "char *kstr"
.Fa "int klen"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft EVP_PKEY *
.Fo PEM_read_bio_PUBKEY
.Fa "BIO *bp"
.Fa "EVP_PKEY **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft EVP_PKEY *
.Fo PEM_read_PUBKEY
.Fa "FILE *fp"
.Fa "EVP_PKEY **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_PUBKEY
.Fa "BIO *bp"
.Fa "EVP_PKEY *x"
.Fc
.Ft int
.Fo PEM_write_PUBKEY
.Fa "FILE *fp"
.Fa "EVP_PKEY *x"
.Fc
.Ft RSA *
.Fo PEM_read_bio_RSAPrivateKey
.Fa "BIO *bp"
.Fa "RSA **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft RSA *
.Fo PEM_read_RSAPrivateKey
.Fa "FILE *fp"
.Fa "RSA **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_RSAPrivateKey
.Fa "BIO *bp"
.Fa "RSA *x"
.Fa "const EVP_CIPHER *enc"
.Fa "unsigned char *kstr"
.Fa "int klen"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_RSAPrivateKey
.Fa "FILE *fp"
.Fa "RSA *x"
.Fa "const EVP_CIPHER *enc"
.Fa "unsigned char *kstr"
.Fa "int klen"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft RSA *
.Fo PEM_read_bio_RSAPublicKey
.Fa "BIO *bp"
.Fa "RSA **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft RSA *
.Fo PEM_read_RSAPublicKey
.Fa "FILE *fp"
.Fa "RSA **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_RSAPublicKey
.Fa "BIO *bp"
.Fa "RSA *x"
.Fc
.Ft int
.Fo PEM_write_RSAPublicKey
.Fa "FILE *fp"
.Fa "RSA *x"
.Fc
.Ft RSA *
.Fo PEM_read_bio_RSA_PUBKEY
.Fa "BIO *bp"
.Fa "RSA **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft RSA *
.Fo PEM_read_RSA_PUBKEY
.Fa "FILE *fp"
.Fa "RSA **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_RSA_PUBKEY
.Fa "BIO *bp"
.Fa "RSA *x"
.Fc
.Ft int
.Fo PEM_write_RSA_PUBKEY
.Fa "FILE *fp"
.Fa "RSA *x"
.Fc
.Ft DSA *
.Fo PEM_read_bio_DSAPrivateKey
.Fa "BIO *bp"
.Fa "DSA **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft DSA *
.Fo PEM_read_DSAPrivateKey
.Fa "FILE *fp"
.Fa "DSA **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_DSAPrivateKey
.Fa "BIO *bp"
.Fa "DSA *x"
.Fa "const EVP_CIPHER *enc"
.Fa "unsigned char *kstr"
.Fa "int klen"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_DSAPrivateKey
.Fa "FILE *fp"
.Fa "DSA *x"
.Fa "const EVP_CIPHER *enc"
.Fa "unsigned char *kstr"
.Fa "int klen"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft DSA *
.Fo PEM_read_bio_DSA_PUBKEY
.Fa "BIO *bp"
.Fa "DSA **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft DSA *
.Fo PEM_read_DSA_PUBKEY
.Fa "FILE *fp"
.Fa "DSA **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_DSA_PUBKEY
.Fa "BIO *bp"
.Fa "DSA *x"
.Fc
.Ft int
.Fo PEM_write_DSA_PUBKEY
.Fa "FILE *fp"
.Fa "DSA *x"
.Fc
.Ft DSA *
.Fo PEM_read_bio_DSAparams
.Fa "BIO *bp"
.Fa "DSA **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft DSA *
.Fo PEM_read_DSAparams
.Fa "FILE *fp"
.Fa "DSA **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_DSAparams
.Fa "BIO *bp"
.Fa "DSA *x"
.Fc
.Ft int
.Fo PEM_write_DSAparams
.Fa "FILE *fp"
.Fa "DSA *x"
.Fc
.Ft DH *
.Fo PEM_read_bio_DHparams
.Fa "BIO *bp"
.Fa "DH **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft DH *
.Fo PEM_read_DHparams
.Fa "FILE *fp"
.Fa "DH **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_DHparams
.Fa "BIO *bp"
.Fa "DH *x"
.Fc
.Ft int
.Fo PEM_write_DHparams
.Fa "FILE *fp"
.Fa "DH *x"
.Fc
.Ft X509 *
.Fo PEM_read_bio_X509
.Fa "BIO *bp"
.Fa "X509 **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft X509 *
.Fo PEM_read_X509
.Fa "FILE *fp"
.Fa "X509 **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_X509
.Fa "BIO *bp"
.Fa "X509 *x"
.Fc
.Ft int
.Fo PEM_write_X509
.Fa "FILE *fp"
.Fa "X509 *x"
.Fc
.Ft X509 *
.Fo PEM_read_bio_X509_AUX
.Fa "BIO *bp"
.Fa "X509 **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft X509 *
.Fo PEM_read_X509_AUX
.Fa "FILE *fp"
.Fa "X509 **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_X509_AUX
.Fa "BIO *bp"
.Fa "X509 *x"
.Fc
.Ft int
.Fo PEM_write_X509_AUX
.Fa "FILE *fp"
.Fa "X509 *x"
.Fc
.Ft X509_REQ *
.Fo PEM_read_bio_X509_REQ
.Fa "BIO *bp"
.Fa "X509_REQ **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft X509_REQ *
.Fo PEM_read_X509_REQ
.Fa "FILE *fp"
.Fa "X509_REQ **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_X509_REQ
.Fa "BIO *bp"
.Fa "X509_REQ *x"
.Fc
.Ft int
.Fo PEM_write_X509_REQ
.Fa "FILE *fp"
.Fa "X509_REQ *x"
.Fc
.Ft int
.Fo PEM_write_bio_X509_REQ_NEW
.Fa "BIO *bp"
.Fa "X509_REQ *x"
.Fc
.Ft int
.Fo PEM_write_X509_REQ_NEW
.Fa "FILE *fp"
.Fa "X509_REQ *x"
.Fc
.Ft X509_CRL *
.Fo PEM_read_bio_X509_CRL
.Fa "BIO *bp"
.Fa "X509_CRL **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft X509_CRL *
.Fo PEM_read_X509_CRL
.Fa "FILE *fp"
.Fa "X509_CRL **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_X509_CRL
.Fa "BIO *bp"
.Fa "X509_CRL *x"
.Fc
.Ft int
.Fo PEM_write_X509_CRL
.Fa "FILE *fp"
.Fa "X509_CRL *x"
.Fc
.Ft PKCS7 *
.Fo PEM_read_bio_PKCS7
.Fa "BIO *bp"
.Fa "PKCS7 **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft PKCS7 *
.Fo PEM_read_PKCS7
.Fa "FILE *fp"
.Fa "PKCS7 **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_PKCS7
.Fa "BIO *bp"
.Fa "PKCS7 *x"
.Fc
.Ft int
.Fo PEM_write_PKCS7
.Fa "FILE *fp"
.Fa "PKCS7 *x"
.Fc
.Ft NETSCAPE_CERT_SEQUENCE *
.Fo PEM_read_bio_NETSCAPE_CERT_SEQUENCE
.Fa "BIO *bp"
.Fa "NETSCAPE_CERT_SEQUENCE **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft NETSCAPE_CERT_SEQUENCE *
.Fo PEM_read_NETSCAPE_CERT_SEQUENCE
.Fa "FILE *fp"
.Fa "NETSCAPE_CERT_SEQUENCE **x"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Ft int
.Fo PEM_write_bio_NETSCAPE_CERT_SEQUENCE
.Fa "BIO *bp"
.Fa "NETSCAPE_CERT_SEQUENCE *x"
.Fc
.Ft int
.Fo PEM_write_NETSCAPE_CERT_SEQUENCE
.Fa "FILE *fp"
.Fa "NETSCAPE_CERT_SEQUENCE *x"
.Fc
.Sh DESCRIPTION
The PEM functions read or write structures in PEM format.
In this sense PEM format is simply base64 encoded data surrounded by
header lines.
.Pp
For more details about the meaning of arguments see the
.Sx PEM function arguments
section.
.Pp
Each operation has four functions associated with it.
For clarity the term
.Dq Sy foobar No functions
will be used to collectively refer to the
.Fn PEM_read_bio_foobar ,
.Fn PEM_read_foobar ,
.Fn PEM_write_bio_foobar ,
and
.Fn PEM_write_foobar
functions.
.Pp
The
.Sy PrivateKey
functions read or write a private key in PEM format using an
.Vt EVP_PKEY
structure.
The write routines use "traditional" private key format and can handle
both RSA and DSA private keys.
The read functions can additionally transparently handle PKCS#8 format
encrypted and unencrypted keys, too.
.Pp
.Fn PEM_write_bio_PKCS8PrivateKey
and
.Fn PEM_write_PKCS8PrivateKey
write a private key in an
.Vt EVP_PKEY
structure in PKCS#8 EncryptedPrivateKeyInfo format using PKCS#5
v2.0 password based encryption algorithms.
The
.Fa enc
argument specifies the encryption algorithm to use: unlike all other PEM
routines, the encryption is applied at the PKCS#8 level and not in the
PEM headers.
If
.Fa enc
is
.Dv NULL ,
then no encryption is used and a PKCS#8 PrivateKeyInfo structure
is used instead.
.Pp
.Fn PEM_write_bio_PKCS8PrivateKey_nid
and
.Fn PEM_write_PKCS8PrivateKey_nid
also write out a private key as a PKCS#8 EncryptedPrivateKeyInfo.
However they use PKCS#5 v1.5 or PKCS#12 encryption algorithms instead.
The algorithm to use is specified in the
.Fa nid
parameter and should be the NID of the corresponding OBJECT IDENTIFIER
(see NOTES section).
.Pp
The
.Sy PUBKEY
functions process a public key using an
.Vt EVP_PKEY
structure.
The public key is encoded as a SubjectPublicKeyInfo structure.
.Pp
The
.Sy RSAPrivateKey
functions process an RSA private key using an
.Vt RSA
structure.
They handle the same formats as the
.Sy PrivateKey
functions, but an error occurs if the private key is not RSA.
.Pp
The
.Sy RSAPublicKey
functions process an RSA public key using an
.Vt RSA
structure.
The public key is encoded using a PKCS#1 RSAPublicKey structure.
.Pp
The
.Sy RSA_PUBKEY
functions also process an RSA public key using an
.Vt RSA
structure.
However the public key is encoded using a SubjectPublicKeyInfo structure
and an error occurs if the public key is not RSA.
.Pp
The
.Sy DSAPrivateKey
functions process a DSA private key using a
.Vt DSA
structure.
They handle the same formats as the
.Sy PrivateKey
functions but an error occurs if the private key is not DSA.
.Pp
The
.Sy DSA_PUBKEY
functions process a DSA public key using a
.Vt DSA
structure.
The public key is encoded using a SubjectPublicKeyInfo structure and an
error occurs if the public key is not DSA.
.Pp
The
.Sy DSAparams
functions process DSA parameters using a
.Vt DSA
structure.
The parameters are encoded using a Dss-Parms structure as defined in RFC 2459.
.Pp
The
.Sy DHparams
functions process DH parameters using a
.Vt DH
structure.
The parameters are encoded using a PKCS#3 DHparameter structure.
.Pp
The
.Sy X509
functions process an X509 certificate using an
.Vt X509
structure.
They will also process a trusted X509 certificate but any trust settings
are discarded.
.Pp
The
.Sy X509_AUX
functions process a trusted X509 certificate using an
.Vt X509
structure.
.Pp
The
.Sy X509_REQ
and
.Sy X509_REQ_NEW
functions process a PKCS#10 certificate request using an
.Vt X509_REQ
structure.
The
.Sy X509_REQ
write functions use CERTIFICATE REQUEST in the header whereas the
.Sy X509_REQ_NEW
functions use NEW CERTIFICATE REQUEST (as required by some CAs).
The
.Sy X509_REQ
read functions will handle either form so there are no
.Sy X509_REQ_NEW
read functions.
.Pp
The
.Sy X509_CRL
functions process an X509 CRL using an
.Vt X509_CRL
structure.
.Pp
The
.Sy PKCS7
functions process a PKCS#7 ContentInfo using a
.Vt PKCS7
structure.
.Pp
The
.Sy NETSCAPE_CERT_SEQUENCE
functions process a Netscape Certificate Sequence using a
.Vt NETSCAPE_CERT_SEQUENCE
structure.
.Pp
The old
.Sy PrivateKey
write routines are retained for compatibility.
New applications should write private keys using the
.Fn PEM_write_bio_PKCS8PrivateKey
or
.Fn PEM_write_PKCS8PrivateKey
routines because they are more secure (they use an iteration count of
2048 whereas the traditional routines use a count of 1) unless
compatibility with older versions of OpenSSL is important.
.Pp
The
.Sy PrivateKey
read routines can be used in all applications because they handle all
formats transparently.
.Ss PEM function arguments
The PEM functions have many common arguments.
.Pp
The
.Fa bp
parameter specifies the
.Vt BIO
to read from or write to.
.Pp
The
.Fa fp
parameter specifies the
.Vt FILE
pointer to read from or write to.
.Pp
The PEM read functions all take a pointer to pointer argument
.Fa x
and return a pointer of the same type.
If
.Fa x
is
.Dv NULL ,
then the parameter is ignored.
If
.Fa x
is not
.Dv NULL
but
.Pf * Fa x
is
.Dv NULL ,
then the structure returned will be written to
.Pf * Fa x .
If neither
.Fa x
nor
.Pf * Fa x
are
.Dv NULL ,
then an attempt is made to reuse the structure at
.Pf * Fa x ,
but see the
.Sx BUGS
and
.Sx EXAMPLES
sections.
Irrespective of the value of
.Fa x ,
a pointer to the structure is always returned, or
.Dv NULL
if an error occurred.
.Pp
The PEM functions which write private keys take an
.Fa enc
parameter which specifies the encryption algorithm to use.
Encryption is done at the PEM level.
If this parameter is set to
.Dv NULL ,
then the private key is written in unencrypted form.
.Pp
The
.Fa cb
argument is the callback to use when querying for the passphrase used
for encrypted PEM structures (normally only private keys).
.Pp
For the PEM write routines, if the
.Fa kstr
parameter is not
.Dv NULL ,
then
.Fa klen
bytes at
.Fa kstr
are used as the passphrase and
.Fa cb
is ignored.
.Pp
If the
.Fa cb
parameters is set to
.Dv NULL
and the
.Fa u
parameter is not
.Dv NULL ,
then the
.Fa u
parameter is interpreted as a null terminated string to use as the
passphrase.
If both
.Fa cb
and
.Fa u
are
.Dv NULL ,
then the default callback routine is used which will typically
prompt for the passphrase on the current terminal with echoing
turned off.
.Pp
The default passphrase callback is sometimes inappropriate (for example
in a GUI application) so an alternative can be supplied.
The callback routine has the following form:
.Bd -filled -offset inset
.Ft int
.Fo cb
.Fa "char *buf"
.Fa "int size"
.Fa "int rwflag"
.Fa "void *u"
.Fc
.Ed
.Pp
.Fa buf
is the buffer to write the passphrase to.
.Fa size
is the maximum length of the passphrase, i.e. the size of
.Fa buf .
.Fa rwflag
is a flag which is set to 0 when reading and 1 when writing.
A typical routine will ask the user to verify the passphrase (for
example by prompting for it twice) if
.Fa rwflag
is 1.
The
.Fa u
parameter has the same value as the
.Fa u
parameter passed to the PEM routine.
It allows arbitrary data to be passed to the callback by the application
(for example a window handle in a GUI application).
The callback must return the number of characters in the passphrase
or 0 if an error occurred.
.Ss PEM encryption format
This old
.Sy PrivateKey
routines use a non standard technique for encryption.
.Pp
The private key (or other data) takes the following form:
.Bd -literal -offset indent
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,3F17F5316E2BAC89

\&...base64 encoded data...
-----END RSA PRIVATE KEY-----
.Ed
.Pp
The line beginning with
.Dq DEK-Info
contains two comma separated pieces of information:
the encryption algorithm name as used by
.Xr EVP_get_cipherbyname 3
and an 8 byte salt encoded as a set of hexadecimal digits.
.Pp
After this is the base64 encoded encrypted data.
.Pp
The encryption key is determined using
.Xr EVP_BytesToKey 3 ,
using the salt and an iteration count of 1.
The IV used is the value of the salt and *not* the IV returned by
.Xr EVP_BytesToKey 3 .
.Sh RETURN VALUES
The read routines return either a pointer to the structure read or
.Dv NULL
if an error occurred.
.Pp
The write routines return 1 for success or 0 for failure.
.Sh EXAMPLES
Although the PEM routines take several arguments, in almost all
applications most of them are set to 0 or
.Dv NULL .
.Pp
Read a certificate in PEM format from a
.Vt BIO :
.Bd -literal
X509 *x;
x = PEM_read_bio_X509(bp, NULL, 0, NULL);
if (x == NULL) {
	/* Error */
}
.Ed
.Pp
Alternative method:
.Bd -literal
X509 *x = NULL;
if (!PEM_read_bio_X509(bp, &x, 0, NULL)) {
	/* Error */
}
.Ed
.Pp
Write a certificate to a
.Vt BIO :
.Bd -literal
if (!PEM_write_bio_X509(bp, x)) {
	/* Error */
}
.Ed
.Pp
Write an unencrypted private key to a
.Vt FILE :
.Bd -literal
if (!PEM_write_PrivateKey(fp, key, NULL, NULL, 0, 0, NULL)) {
	/* Error */
}
.Ed
.Pp
Write a private key (using traditional format) to a
.Vt BIO
using triple DES encryption, the pass phrase is prompted for:
.Bd -literal
if (!PEM_write_bio_PrivateKey(bp, key, EVP_des_ede3_cbc(),
    NULL, 0, 0, NULL)) {
	/* Error */
}
.Ed
.Pp
Write a private key (using PKCS#8 format) to a
.Vt BIO
using triple DES encryption, using the pass phrase "hello":
.Bd -literal
if (!PEM_write_bio_PKCS8PrivateKey(bp, key, EVP_des_ede3_cbc(),
    NULL, 0, 0, "hello")) {
	/* Error */
}
.Ed
.Pp
Read a private key from a
.Vt BIO
using the pass phrase "hello":
.Bd -literal
key = PEM_read_bio_PrivateKey(bp, NULL, 0, "hello");
if (key == NULL) {
	/* Error */
}
.Ed
.Pp
Read a private key from a
.Vt BIO
using a pass phrase callback:
.Bd -literal
key = PEM_read_bio_PrivateKey(bp, NULL, pass_cb, "My Private Key");
if (key == NULL) {
	/* Error */
}
.Ed
.Pp
Skeleton pass phrase callback:
.Bd -literal
int
pass_cb(char *buf, int size, int rwflag, void *u)
{
	int len;
	char *tmp;

	/* We'd probably do something else if 'rwflag' is 1 */
	printf("Enter pass phrase for \e"%s\e"\en", u);

	/* get pass phrase, length 'len' into 'tmp' */
	tmp = "hello";
	len = strlen(tmp);

	if (len == 0)
		return 0;
	/* if too long, truncate */
	if (len > size)
		len = size;
	memcpy(buf, tmp, len);
	return len;
}
.Ed
.Sh CAVEATS
A frequent cause of problems is attempting to use the PEM routines like
this:
.Bd -literal
X509 *x;
PEM_read_bio_X509(bp, &x, 0, NULL);
.Ed
.Pp
This is a bug because an attempt will be made to reuse the data at
.Fa x
which is an uninitialised pointer.
.Sh BUGS
The PEM read routines in some versions of OpenSSL will not correctly
reuse an existing structure.
Therefore
.Pp
.Dl PEM_read_bio_X509(bp, &x, 0, NULL);
.Pp
where
.Fa x
already contains a valid certificate may not work, whereas
.Bd -literal -offset indent
X509_free(x);
x = PEM_read_bio_X509(bp, NULL, 0, NULL);
.Ed
.Pp
is guaranteed to work.