aboutsummaryrefslogtreecommitdiff
path: root/vendor/certificate_authority/lib/certificate_authority/extensions.rb
blob: 89de0321749ae4cbdb5617b4dd0603e722e289fb (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
module CertificateAuthority
  module Extensions
    module ExtensionAPI
      def to_s
        raise "Implementation required"
      end

      def self.parse(value, critical)
        raise "Implementation required"
      end

      def config_extensions
        {}
      end

      def openssl_identifier
        raise "Implementation required"
      end

      def ==(value)
        raise "Implementation required"
      end
    end

    # Specifies whether an X.509v3 certificate can act as a CA, signing other
    # certificates to be verified. If set, a path length constraint can also be
    # specified.
    # Reference: Section 4.2.1.10 of RFC3280
    # http://tools.ietf.org/html/rfc3280#section-4.2.1.10
    class BasicConstraints
      OPENSSL_IDENTIFIER = "basicConstraints"

      include ExtensionAPI
      include ActiveModel::Validations

      attr_accessor :critical
      attr_accessor :ca
      attr_accessor :path_len
      validates :critical, :inclusion => [true,false]
      validates :ca, :inclusion => [true,false]

      def initialize
        @critical = false
        @ca = false
      end

      def openssl_identifier
        OPENSSL_IDENTIFIER
      end

      def is_ca?
        @ca
      end

      def path_len=(value)
        raise "path_len must be a non-negative integer" if value < 0 or !value.is_a?(Fixnum)
        @path_len = value
      end

      def to_s
        res = []
        res << "CA:#{@ca}"
        res << "pathlen:#{@path_len}" unless @path_len.nil?
        res.join(',')
      end

      def ==(o)
        o.class == self.class && o.state == state
      end

      def self.parse(value, critical)
        obj = self.new
        return obj if value.nil?
        obj.critical = critical
        value.split(/,\s*/).each do |v|
          c = v.split(':', 2)
          obj.ca = (c.last.upcase == "TRUE") if c.first == "CA"
          obj.path_len = c.last.to_i if c.first == "pathlen"
        end
        obj
      end

      protected
      def state
        [@critical,@ca,@path_len]
      end
    end

    # Specifies where CRL information be be retrieved. This extension isn't
    # critical, but is recommended for proper CAs.
    # Reference: Section 4.2.1.14 of RFC3280
    # http://tools.ietf.org/html/rfc3280#section-4.2.1.14
    class CrlDistributionPoints
      OPENSSL_IDENTIFIER = "crlDistributionPoints"

      include ExtensionAPI

      attr_accessor :critical
      attr_accessor :uris

      def initialize
        @critical = false
        @uris = []
      end

      def openssl_identifier
        OPENSSL_IDENTIFIER
      end

      ## NB: At this time it seems OpenSSL's extension handlers don't support
      ## any of the config options the docs claim to support... everything comes back
      ## "missing value" on GENERAL NAME. Even if copied verbatim
      def config_extensions
        {
          # "custom_crl_fields" => {"fullname" => "URI:#{fullname}"},
          # "issuer_sect" => {"CN" => "crlissuer.com", "C" => "US", "O" => "shudder"}
        }
      end

      # This is for legacy support. Technically it can (and probably should)
      # be an array. But if someone is calling the old accessor we shouldn't
      # necessarily break it.
      def uri=(value)
        @uris << value
      end

      def to_s
        res = []
        @uris.each do |uri|
          res << "URI:#{uri}"
        end
        res.join(',')
      end

      def ==(o)
        o.class == self.class && o.state == state
      end

      def self.parse(value, critical)
        obj = self.new
        return obj if value.nil?
        obj.critical = critical
        value.split(/,\s*/).each do |v|
          c = v.split(':', 2)
          obj.uris << c.last if c.first == "URI"
        end
        obj
      end

      protected
      def state
        [@critical,@uri]
      end
    end

    # Identifies the public key associated with a given certificate.
    # Should be required for "CA" certificates.
    # Reference: Section 4.2.1.2 of RFC3280
    # http://tools.ietf.org/html/rfc3280#section-4.2.1.2
    class SubjectKeyIdentifier
      OPENSSL_IDENTIFIER = "subjectKeyIdentifier"

      include ExtensionAPI

      attr_accessor :critical
      attr_accessor :identifier

      def initialize
        @critical = false
        @identifier = "hash"
      end

      def openssl_identifier
        OPENSSL_IDENTIFIER
      end

      def to_s
        res = []
        res << @identifier
        res.join(',')
      end

      def ==(o)
        o.class == self.class && o.state == state
      end

      def self.parse(value, critical)
        obj = self.new
        return obj if value.nil?
        obj.critical = critical
        obj.identifier = value
        obj
      end

      protected
      def state
        [@critical,@identifier]
      end
    end

    # Identifies the public key associated with a given private key.
    # Reference: Section 4.2.1.1 of RFC3280
    # http://tools.ietf.org/html/rfc3280#section-4.2.1.1
    class AuthorityKeyIdentifier
      OPENSSL_IDENTIFIER = "authorityKeyIdentifier"

      include ExtensionAPI

      attr_accessor :critical
      attr_accessor :identifier

      def initialize
        @critical = false
        @identifier = ["keyid", "issuer"]
      end

      def openssl_identifier
        OPENSSL_IDENTIFIER
      end

      def to_s
        res = []
        res += @identifier
        res.join(',')
      end

      def ==(o)
        o.class == self.class && o.state == state
      end

      def self.parse(value, critical)
        obj = self.new
        return obj if value.nil?
        obj.critical = critical
        obj.identifier = value.split(/,\s*/).last.chomp
        obj
      end

      protected
      def state
        [@critical,@identifier]
      end
    end

    # Specifies how to access CA information and services for the CA that
    # issued this certificate.
    # Generally used to specify OCSP servers.
    # Reference: Section 4.2.2.1 of RFC3280
    # http://tools.ietf.org/html/rfc3280#section-4.2.2.1
    class AuthorityInfoAccess
      OPENSSL_IDENTIFIER = "authorityInfoAccess"

      include ExtensionAPI

      attr_accessor :critical
      attr_accessor :ocsp
      attr_accessor :ca_issuers

      def initialize
        @critical = false
        @ocsp = []
        @ca_issuers = []
      end

      def openssl_identifier
        OPENSSL_IDENTIFIER
      end

      def to_s
        res = []
        res += @ocsp.map {|o| "OCSP;URI:#{o}" }
        res += @ca_issuers.map {|c| "caIssuers;URI:#{c}" }
        res.join(',')
      end

      def ==(o)
        o.class == self.class && o.state == state
      end

      def self.parse(value, critical)
        obj = self.new
        return obj if value.nil?
        obj.critical = critical
        value.split("\n").each do |v|
          if v.starts_with?("OCSP")
            obj.ocsp << v.split.last
          end

          if v.starts_with?("CA Issuers")
            obj.ca_issuers << v.split.last
          end
        end
        obj
      end

      protected
      def state
        [@critical,@ocsp,@ca_issuers]
      end
    end

    # Specifies the allowed usage purposes of the keypair specified in this certificate.
    # Reference: Section 4.2.1.3 of RFC3280
    # http://tools.ietf.org/html/rfc3280#section-4.2.1.3
    #
    # Note: OpenSSL when parsing an extension will return results in the form
    # 'Digital Signature', but on signing you have to set it to 'digitalSignature'.
    # So copying an extension from an imported cert isn't going to work yet.
    class KeyUsage
      OPENSSL_IDENTIFIER = "keyUsage"

      include ExtensionAPI

      attr_accessor :critical
      attr_accessor :usage

      def initialize
        @critical = false
        @usage = ["digitalSignature", "nonRepudiation"]
      end

      def openssl_identifier
        OPENSSL_IDENTIFIER
      end

      def to_s
        res = []
        res += @usage
        res.join(',')
      end

      def ==(o)
        o.class == self.class && o.state == state
      end

      def self.parse(value, critical)
        obj = self.new
        return obj if value.nil?
        obj.critical = critical
        obj.usage = value.split(/,\s*/)
        obj
      end

      protected
      def state
        [@critical,@usage]
      end
    end

    # Specifies even more allowed usages in addition to what is specified in
    # the Key Usage extension.
    # Reference: Section 4.2.1.13 of RFC3280
    # http://tools.ietf.org/html/rfc3280#section-4.2.1.13
    class ExtendedKeyUsage
      OPENSSL_IDENTIFIER = "extendedKeyUsage"

      include ExtensionAPI

      attr_accessor :critical
      attr_accessor :usage

      def initialize
        @critical = false
        @usage = ["serverAuth"]
      end

      def openssl_identifier
        OPENSSL_IDENTIFIER
      end

      def to_s
        res = []
        res += @usage
        res.join(',')
      end

      def ==(o)
        o.class == self.class && o.state == state
      end

      def self.parse(value, critical)
        obj = self.new
        return obj if value.nil?
        obj.critical = critical
        obj.usage = value.split(/,\s*/)
        obj
      end

      protected
      def state
        [@critical,@usage]
      end
    end

    # Specifies additional "names" for which this certificate is valid.
    # Reference: Section 4.2.1.7 of RFC3280
    # http://tools.ietf.org/html/rfc3280#section-4.2.1.7
    class SubjectAlternativeName
      OPENSSL_IDENTIFIER = "subjectAltName"

      include ExtensionAPI

      attr_accessor :critical
      attr_accessor :uris, :dns_names, :ips, :emails

      def initialize
        @critical = false
        @uris = []
        @dns_names = []
        @ips = []
        @emails = []
      end

      def openssl_identifier
        OPENSSL_IDENTIFIER
      end

      def uris=(value)
        raise "URIs must be an array" unless value.is_a?(Array)
        @uris = value
      end

      def dns_names=(value)
        raise "DNS names must be an array" unless value.is_a?(Array)
        @dns_names = value
      end

      def ips=(value)
        raise "IPs must be an array" unless value.is_a?(Array)
        @ips = value
      end

      def emails=(value)
        raise "Emails must be an array" unless value.is_a?(Array)
        @emails = value
      end

      def to_s
        res = []
        res += @uris.map {|u| "URI:#{u}" }
        res += @dns_names.map {|d| "DNS:#{d}" }
        res += @ips.map {|i| "IP:#{i}" }
        res += @emails.map {|i| "email:#{i}" }
        res.join(',')
      end

      def ==(o)
        o.class == self.class && o.state == state
      end

      def self.parse(value, critical)
        obj = self.new
        return obj if value.nil?
        obj.critical = critical
        value.split(/,\s*/).each do |v|
          c = v.split(':', 2)
          obj.uris << c.last if c.first == "URI"
          obj.dns_names << c.last if c.first == "DNS"
          obj.ips << c.last if c.first == "IP"
          obj.emails << c.last if c.first == "EMAIL"
        end
        obj
      end

      protected
      def state
        [@critical,@uris,@dns_names,@ips,@emails]
      end
    end

    class CertificatePolicies
      OPENSSL_IDENTIFIER = "certificatePolicies"

      include ExtensionAPI

      attr_accessor :critical
      attr_accessor :policy_identifier
      attr_accessor :cps_uris
      ##User notice
      attr_accessor :explicit_text
      attr_accessor :organization
      attr_accessor :notice_numbers

      def initialize
        self.critical = false
        @contains_data = false
      end

      def openssl_identifier
        OPENSSL_IDENTIFIER
      end

      def user_notice=(value={})
        value.keys.each do |key|
          self.send("#{key}=".to_sym, value[key])
        end
      end

      def config_extensions
        config_extension = {}
        custom_policies = {}
        notice = {}
        unless self.policy_identifier.nil?
          custom_policies["policyIdentifier"] = self.policy_identifier
        end

        if !self.cps_uris.nil? and self.cps_uris.is_a?(Array)
          self.cps_uris.each_with_index do |cps_uri,i|
            custom_policies["CPS.#{i}"] = cps_uri
          end
        end

        unless self.explicit_text.nil?
          notice["explicitText"] = self.explicit_text
        end

        unless self.organization.nil?
          notice["organization"] = self.organization
        end

        unless self.notice_numbers.nil?
          notice["noticeNumbers"] = self.notice_numbers
        end

        if notice.keys.size > 0
          custom_policies["userNotice.1"] = "@notice"
          config_extension["notice"] = notice
        end

        if custom_policies.keys.size > 0
          config_extension["custom_policies"] = custom_policies
          @contains_data = true
        end

        config_extension
      end

      def to_s
        return "" unless @contains_data
        res = []
        res << "ia5org"
        res += @config_extensions["custom_policies"] unless @config_extensions.nil?
        res.join(',')
      end

      def self.parse(value, critical)
        obj = self.new
        return obj if value.nil?
        obj.critical = critical
        value.split(/,\s*/).each do |v|
          c = v.split(':', 2)
          obj.policy_identifier = c.last if c.first == "policyIdentifier"
          obj.cps_uris << c.last if c.first =~ %r{CPS.\d+}
          # TODO: explicit_text, organization, notice_numbers
        end
        obj
      end
    end

    # DEPRECATED
    # Specifics the purposes for which a certificate can be used.
    # The basicConstraints, keyUsage, and extendedKeyUsage extensions are now used instead.
    # https://www.openssl.org/docs/apps/x509v3_config.html#Netscape_Certificate_Type
    class NetscapeCertificateType
      OPENSSL_IDENTIFIER = "nsCertType"

      include ExtensionAPI

      attr_accessor :critical
      attr_accessor :flags

      def initialize
        self.critical = false
        self.flags = []
      end

      def openssl_identifier
        OPENSSL_IDENTIFIER
      end

      def to_s
        res = []
        res += self.flags
        res.join(',')
      end

      def self.parse(value, critical)
        obj = self.new
        return obj if value.nil?
        obj.critical = critical
        obj.flags = value.split(/,\s*/)
        obj
      end
    end

    # DEPRECATED
    # Contains a comment which will be displayed when the certificate is viewed in some browsers.
    # https://www.openssl.org/docs/apps/x509v3_config.html#Netscape_String_extensions_
    class NetscapeComment
      OPENSSL_IDENTIFIER = "nsComment"

      include ExtensionAPI

      attr_accessor :critical
      attr_accessor :comment

      def initialize
        self.critical = false
      end

      def openssl_identifier
        OPENSSL_IDENTIFIER
      end

      def to_s
        res = []
        res << self.comment if self.comment
        res.join(',')
      end

      def self.parse(value, critical)
        obj = self.new
        return obj if value.nil?
        obj.critical = critical
        obj.comment = value
        obj
      end
    end

  end
end